package com.xjrsoft.module.attendance.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.attendance.dto.AddAttendanceMessageSetDto;
import com.xjrsoft.module.attendance.dto.AttendanceMessageSetDto;
import com.xjrsoft.module.attendance.dto.UpdateAttendanceMessageSetDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.attendance.dto.AttendanceMessageSetPageDto;
import com.xjrsoft.module.attendance.entity.AttendanceMessageSet;
import com.xjrsoft.module.attendance.service.IAttendanceMessageSetService;
import com.xjrsoft.module.attendance.vo.AttendanceMessageSetPageVo;

import com.xjrsoft.module.attendance.vo.AttendanceMessageSetVo;
import com.xjrsoft.module.attendance.vo.AttendanceMessageUserRelationVo;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.service.IBaseClassService;
import com.xjrsoft.module.concat.service.IXjrUserService;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.service.IDepartmentService;
import com.xjrsoft.module.teacher.entity.XjrUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 考勤消息设置
* @Author dzx
* @Date: 2024-05-21
* @Version 1.0
*/
@RestController
@RequestMapping("/attendance" + "/attendanceMessageSet")
@Api(value = "/attendance"  + "/attendanceMessageSet",tags = "考勤消息设置代码")
@AllArgsConstructor
public class AttendanceMessageSetController {


    private final IAttendanceMessageSetService attendanceMessageSetService;
    private final IBaseClassService classService;
    private final IDepartmentService departmentService;
    private final IXjrUserService xjrUserService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考勤消息设置列表(分页)")
    @SaCheckPermission("attendancemessageset:detail")
    public RT<PageOutput<AttendanceMessageSetPageVo>> page(@Valid AttendanceMessageSetPageDto dto){

        LambdaQueryWrapper<AttendanceMessageSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(AttendanceMessageSet::getId)
                .select(AttendanceMessageSet.class,x -> VoToColumnUtil.fieldsToColumns(AttendanceMessageSetPageVo.class).contains(x.getProperty()));
        IPage<AttendanceMessageSet> page = attendanceMessageSetService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<AttendanceMessageSetPageVo> pageOutput = ConventPage.getPageOutput(page, AttendanceMessageSetPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/list")
    @ApiOperation(value="考勤消息设置列表")
    @SaCheckPermission("attendancemessageset:detail")
    public RT<List<AttendanceMessageSetVo>> list(@Valid AttendanceMessageSetDto dto) {
        List<AttendanceMessageSet> list = attendanceMessageSetService.list(MPJWrappers.<AttendanceMessageSet>lambdaJoin().eq(ObjectUtil.isNotNull(dto.getRoleType()) && dto.getRoleType() != 0, AttendanceMessageSet::getRoleType, dto.getRoleType()));
        List<AttendanceMessageSetVo> res = new ArrayList<>();
        for (AttendanceMessageSet item : list) {
            AttendanceMessageSet attendanceMessageSet = attendanceMessageSetService.getByIdDeep(item.getId());
            if (attendanceMessageSet != null) {

                AttendanceMessageSetVo attendanceMessageSetVo=BeanUtil.toBean(attendanceMessageSet, AttendanceMessageSetVo.class);

                for (AttendanceMessageUserRelationVo attendanceMessageUserRelationVo:attendanceMessageSetVo.getAttendanceMessageUserRelationList()){
                    if(attendanceMessageUserRelationVo.getUserId() != null){
                        XjrUser xjrUser = xjrUserService.getById(attendanceMessageUserRelationVo.getUserId());
                        if(xjrUser != null){
                            attendanceMessageUserRelationVo.setName(xjrUser.getName());
                        }
                    }else if(attendanceMessageUserRelationVo.getDeptId() != null){
                        Department department = departmentService.getById(attendanceMessageUserRelationVo.getDeptId());
                        if(department != null){
                            attendanceMessageUserRelationVo.setName(department.getName());
                        }

                    }else if(attendanceMessageUserRelationVo.getClassId() != null){
                        BaseClass aClass = classService.getById(attendanceMessageUserRelationVo.getClassId());
                        if(aClass != null){
                            attendanceMessageUserRelationVo.setName(aClass.getName());
                        }
                    }
                }
                res.add(attendanceMessageSetVo);
            }
        }
        return RT.ok(res);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考勤消息设置信息")
    @SaCheckPermission("attendancemessageset:detail")
    public RT<AttendanceMessageSetVo> info(@RequestParam Long id){
        AttendanceMessageSet attendanceMessageSet = attendanceMessageSetService.getByIdDeep(id);
        if (attendanceMessageSet == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(attendanceMessageSet, AttendanceMessageSetVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增考勤消息设置")
    @SaCheckPermission("attendancemessageset:add")
    public RT<Boolean> add(@Valid @RequestBody AddAttendanceMessageSetDto dto){
        AttendanceMessageSet attendanceMessageSet = BeanUtil.toBean(dto, AttendanceMessageSet.class);
        boolean isSuccess = attendanceMessageSetService.add(attendanceMessageSet);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考勤消息设置")
    @SaCheckPermission("attendancemessageset:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateAttendanceMessageSetDto dto){

        AttendanceMessageSet attendanceMessageSet = BeanUtil.toBean(dto, AttendanceMessageSet.class);
        return RT.ok(attendanceMessageSetService.update(attendanceMessageSet));

    }

    @PostMapping("edit")
    @ApiOperation(value = "新增考勤消息设置")
    @SaCheckPermission("attendancemessageset:add")
    public RT<Boolean> edit(@Valid @RequestBody List<UpdateAttendanceMessageSetDto> dtos) {
        boolean isSuccess = attendanceMessageSetService.edit(dtos);
        return RT.ok(isSuccess);
    }

    @DeleteMapping
    @ApiOperation(value = "删除考勤消息设置")
    @SaCheckPermission("attendancemessageset:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(attendanceMessageSetService.delete(ids));

    }

}