package com.yida.data.attendance.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.attendance.dto.StaffRuleDTO;
import com.yida.data.attendance.service.EduAttendanceStaffRuleAdminService;
import com.yida.data.attendance.service.EduAttendanceStaffRuleService;
import com.yida.data.attendance.vo.FindStaffRuleVO;
import com.yida.data.attendance.vo.StaffRuleVO;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.attendance.EduAttendanceStaffRuleAdmin;
import com.yida.data.common.core.utils.FebsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 教职工考勤规则 Controller
 *
 * @author ccl
 * @date 2021-09-27 18:56:21
 */
@Api(tags = "考勤-教职工-规则")
@Slf4j
@Validated
@RestController
@RequestMapping("/staffRule")
@RequiredArgsConstructor
public class EduAttendanceStaffRuleController {

    private final EduAttendanceStaffRuleService eduAttendanceStaffRuleService;
    private final EduAttendanceStaffRuleAdminService eduAttendanceStaffRuleAdminService;

    @ApiOperation("查询规则列表")
    @GetMapping("findStaffRuleList")
    public ResultBean<IPage<StaffRuleVO>> findStaffRuleList(@ApiParam("规则名称") @RequestParam(required = false) String ruleName,
        @ApiParam("管理员名称") @RequestParam(required = false) String staffName,
        @RequestParam(defaultValue = "10") Long pageSize,
        @RequestParam(defaultValue = "1") Long pageNum) {

        IPage iPage = new Page();
        iPage.setSize(pageSize);
        iPage.setCurrent(pageNum);
        Long schoolId = null;
        if (FebsUtil.getCurrentUser().getDeptType() != 5) {
            schoolId = FebsUtil.getCurrentUser().getDeptId();
        }

        return ResultBean.buildSuccess(eduAttendanceStaffRuleService.findStaffRuleList(iPage, ruleName, staffName, schoolId));
    }

    @ApiOperation("查询规则详情")
    @GetMapping("findStaffRule")
    public ResultBean<FindStaffRuleVO> findStaffRule(@ApiParam("规则id") @RequestParam Long ruleId) {

        return ResultBean.buildSuccess(eduAttendanceStaffRuleService.findStaffRule(ruleId));
    }

    @ApiOperation("新增或修改规则详情")
    @PostMapping("saveOrUpStaffRule")
    public ResultBean saveOrUpStaffRule(@RequestBody StaffRuleDTO staffRuleDTO) {

        eduAttendanceStaffRuleService.saveOrUpStaffRule(staffRuleDTO);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除规则")
    @PostMapping("removeStaffRule")
    public ResultBean removeStaffRule(@ApiParam("规则id") @RequestParam Long ruleId) {
        eduAttendanceStaffRuleService.removeStaffRule(ruleId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询当前用户是否是规则管理员")
    @GetMapping("/checkStaffAdmin")
    public ResultBean<Boolean> checkStaffAdmin(@ApiParam("职工id") Long staffId) {
        return ResultBean
            .buildSuccess(eduAttendanceStaffRuleAdminService.count(Wrappers.<EduAttendanceStaffRuleAdmin>lambdaQuery()
                .eq(EduAttendanceStaffRuleAdmin::getStaffId, staffId)) > 0);
    }
}
