package com.yida.data.attendance.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.attendance.service.EduAttendanceConfigService;
import com.yida.data.attendance.service.EduAttendanceRuleService;
import com.yida.data.attendance.vo.ConflictRuleVO;
import com.yida.data.attendance.vo.RuleListVO;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.attendance.EduAttendanceConfig;
import com.yida.data.common.core.entity.attendance.EduAttendanceRule;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.common.core.utils.FebsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "考勤-学生-规则")
@RestController
@RequestMapping("/rule")
public class EduAttendanceRuleController {

    @Resource
    private EduAttendanceRuleService eduAttendanceRuleService;
    @Resource
    private EduAttendanceConfigService eduAttendanceConfigService;

    @ApiOperation("查询规则列表")
    @GetMapping("/listRule")
    @ControllerLog(operation = "查询规则列表")
    public ResultBean<IPage<RuleListVO>> listRule(
        @ApiParam("学校id") Long schoolId,
        @ApiParam("校区id") Long campusId,
        @ApiParam("规则名称") String ruleName,
        @ApiParam("规则类型,0-上学，1-归寝") Integer type,
        @ApiParam("运行状态，0-运行中，1-停止，2-失效，3-待运行") Integer status,
        @ApiParam("运行日期段开始") String startDate,
        @ApiParam("运行日期段结束") String endDate,
        @ApiParam("当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
        @ApiParam("页面大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return ResultBean.buildSuccess(eduAttendanceRuleService.listRulePage(new Page(pageNum, pageSize),
            FebsUtil.getCurrentUser(),
            schoolId, campusId, ruleName, type, status, startDate, endDate));
    }

    @ApiOperation("新增、修改规则")
    @PostMapping("/saveRule")
    @ControllerLog(operation = "新增、修改考勤规则")
    public ResultBean saveRule(@RequestBody EduAttendanceRule rule) {
        List<ConflictRuleVO> conflict = eduAttendanceRuleService.checkConflict(rule);
        if (CollUtil.isNotEmpty(conflict)) {
            StringBuilder builder = new StringBuilder();
            for (ConflictRuleVO vo : conflict) {
                builder.append(vo.getDeptName())
                    .append("已存在")
                    .append(vo.getRuleName())
                    .append("规则")
                    .append(",");
            }
            builder.append("上述规则的运行时间与本规则运行时间有重叠，请调整设置。");
            throw new FebsException(builder.toString());
        }
        eduAttendanceRuleService.saveRule(rule);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("校验规则是否冲突")
    @PostMapping("/checkConflict")
    @ControllerLog(operation = "校验考勤规则是否冲突")
    public ResultBean checkConflict(@RequestBody EduAttendanceRule rule) {
        return ResultBean.buildSuccess(eduAttendanceRuleService.checkConflict(rule));
    }

    @ApiOperation("删除规则")
    @PostMapping("/delRule")
    @ControllerLog(operation = "删除考勤规则")
    public ResultBean delRule(@RequestBody List<Long> ruleIds) {
        eduAttendanceRuleService.deleteRule(ruleIds);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("改变规则运行状态")
    @GetMapping("/changeRuleRun")
    @ControllerLog(operation = "改变考勤规则状态")
    public ResultBean changeRuleRun(@ApiParam("规则id") @RequestParam Long id) {
        eduAttendanceRuleService.changeRuleRun(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询考勤设置")
    @GetMapping("/getConfig")
    public ResultBean<EduAttendanceConfig> getConfig() {
        Long schoolId = null;
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            schoolId = currentUser.getDeptId();
        }
        EduAttendanceConfig config = eduAttendanceConfigService.getConfig(schoolId);
        return ResultBean.buildSuccess(config);
    }

    @ApiOperation("更改考勤设置")
    @PostMapping("/saveConfig")
    public ResultBean saveConfig(@RequestBody EduAttendanceConfig config) {
        config.setSchoolId(FebsUtil.getCurrentUser().getDeptId());
        eduAttendanceConfigService.saveConfig(config);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询当前时间能否查寝")
    @GetMapping("/getDormAttendanceStatus")
    public ResultBean<Boolean> getDormAttendanceStatus(@ApiParam("学校id") Long schoolId,
        @ApiParam("1-中午，2-晚上") Integer time) {
        return ResultBean.buildSuccess(eduAttendanceConfigService.getDormAttendanceStatus(schoolId, time));
    }
}
