package com.yida.data.attendance.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.attendance.dto.AddAttendanceRuleDTO;
import com.yida.data.attendance.dto.AttendanceRuleAdminUserAddDTO;
import com.yida.data.attendance.dto.AttendanceRuleAdminUserPageDTO;
import com.yida.data.attendance.dto.AttendanceRuleUserAddDTO;
import com.yida.data.attendance.dto.AttendanceRuleUserPageDTO;
import com.yida.data.attendance.dto.PageAttendanceRuleDTO;
import com.yida.data.attendance.dto.UpdateAttendanceRuleStatusDTO;
import com.yida.data.attendance.service.AttendanceRuleService;
import com.yida.data.attendance.service.AttendanceRuleWifiService;
import com.yida.data.attendance.vo.AttendanceRuleAdminUserVO;
import com.yida.data.attendance.vo.AttendanceRuleUserVO;
import com.yida.data.attendance.vo.AttendanceRuleVO;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.common.ResultMsgType;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.constant.FebsConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.FileUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.vo.smart.ImportRosterDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
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;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教职工考勤规则 Controller
 *
 * @date 2023-08-09 11:37:25
 */
@Slf4j
@RestController
@RequestMapping("attendanceRule")
@RequiredArgsConstructor
@Api(tags = "考勤-规则", value = "考勤规则")
public class AttendanceRuleController {

    private final AttendanceRuleService attendanceRuleService;

    private final AttendanceRuleWifiService attendanceRuleWifiService;

    @ApiOperation("查询规则列表")
    @PostMapping("/listRule")
    public ResultBean<IPage<AttendanceRuleVO>> listRule(@RequestBody PageAttendanceRuleDTO dto) {
        dto.setSchoolId(FebsUtil.getTopDeptId());
        return ResultBean
            .buildSuccess(attendanceRuleService.listRulePage(dto));
    }

    @ApiOperation("新增、修改规则")
    @PostMapping("/saveRule")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "新增、修改考勤规则", type = OperationLogTypeEnum.SAVE)
    public ResultBean saveRule(@RequestBody @Validated AddAttendanceRuleDTO dto) {
        attendanceRuleService.addAttendanceRule(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("获取规则")
    @GetMapping("/getRule")
    public ResultBean<AttendanceRuleVO> getRule(Long ruleId) {
        return ResultBean
            .buildSuccess(attendanceRuleService.getRule(ruleId));
    }

    @ApiOperation("删除规则")
    @PostMapping("/delRule")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "删除考勤规则", type = OperationLogTypeEnum.DELETE)
    public ResultBean delRule(@RequestBody List<Long> ruleIds) {
        attendanceRuleService.deleteRule(ruleIds);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("更改规则状态")
    @PostMapping("/updateRuleStatus")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "更改规则状态", type = OperationLogTypeEnum.UPDATE)
    public ResultBean updateRuleStatus(@RequestBody UpdateAttendanceRuleStatusDTO dto) {
        attendanceRuleService.updateRuleStatus(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("导入BSSID")
    @PostMapping(value = "/importBssid", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultBean<ImportRosterDataVO> importBssid(
        @ApiParam(value = "导入的excel文件", required = true) @RequestParam("file") MultipartFile file,
        @ApiParam(value = "规则ID", required = true) @RequestParam("ruleId") Long ruleId) {
        // 验证文件类型
        if (!FileUtil.checkMultipartFile(file, FebsConstant.EXCEL_FILE_TYPE)) {
            return ResultBean.buildError(ResultMsgType.FILE_TYPE_ERROR.getValue());
        }
        String uuid = StrUtil.uuid();
        // 当前登录用户信息
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        String fileUrl = FileUtil.uploadMultipartFile(file);
        // 导入花名册数据
        attendanceRuleWifiService.ImportBssid(fileUrl, ruleId);
        return ResultBean
            .buildSuccess(ImportRosterDataVO.builder().percent(0).finish(0).key(uuid).build());
    }


    @ApiOperation("查询打卡人员列表")
    @PostMapping("/listRuleAttendanceUser")
    public ResultBean<IPage<AttendanceRuleUserVO>> listRuleAttendanceUser(@RequestBody AttendanceRuleUserPageDTO dto) {
        dto.setSchoolId(FebsUtil.getTopDeptId());
        return ResultBean
            .buildSuccess(attendanceRuleService.listRuleAttendanceUser(dto));
    }


    @ApiOperation("批量删除打卡人员")
    @PostMapping("/deleteRuleAttendanceUser")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "批量删除打卡人员", type = OperationLogTypeEnum.DELETE)
    public ResultBean deleteRuleAttendanceUser(@RequestBody List<Long> ids) {
        attendanceRuleService.deleteRuleAttendanceUser(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("增加打卡人员")
    @PostMapping("/addRuleAttendanceUser")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "增加打卡人员", type = OperationLogTypeEnum.INSERT)
    public ResultBean addRuleAttendanceUser(@RequestBody AttendanceRuleUserAddDTO dto) {
        attendanceRuleService.addRuleAttendanceUser(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询规则管理员列表")
    @PostMapping("/listRuleAttendanceAdminUser")
    public ResultBean<IPage<AttendanceRuleAdminUserVO>> listRuleAttendanceUser(@RequestBody AttendanceRuleAdminUserPageDTO dto) {
        return ResultBean.buildSuccess(attendanceRuleService.listRuleAttendanceAdminUser(dto));
    }

    @ApiOperation("批量删除管理员人员")
    @PostMapping("/deleteRuleAttendanceAdminUser")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "批量删除管理员人员", type = OperationLogTypeEnum.DELETE)
    public ResultBean deleteRuleAttendanceAdminUser(@RequestBody List<Long> ids) {
        attendanceRuleService.deleteRuleAttendanceAdminUser(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("增加管理员人员")
    @PostMapping("/addRuleAttendanceAdminUser")
    public ResultBean addRuleAttendanceAdminUser(@RequestBody AttendanceRuleAdminUserAddDTO dto) {
        attendanceRuleService.addAttendanceAdminUser(dto);
        return ResultBean.buildSuccess();
    }
}
