package com.ruoyi.zhushi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.zhushi.entity.*;
import com.ruoyi.zhushi.mapper.DkAttendanceGroupAndUserMapper;
import com.ruoyi.zhushi.mapper.DkAttendanceGroupMapper;
import com.ruoyi.zhushi.service.DkAttendanceGroupService;
import com.ruoyi.zhushi.util.Constans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DkAttendanceGroupServiceImpl implements DkAttendanceGroupService {


    @Autowired
    private DkAttendanceGroupMapper dkAttendanceGroupMapper;

    @Autowired
    private DkAttendanceGroupAndUserMapper dkAttendanceGroupAndUserMapper;
    @Autowired
    private ISysDeptService iSysDeptService;

    // 查询考勤组
    @Override
    public TableDataInfo<DkAttendanceGroupDTO> queryPageList(DkAttendanceGroupDTO dkAttendanceGroupDTO) {
        List<DkAttendanceGroupDTO> dkAttendanceGroupDTOS = new ArrayList<>();
        List<DkAttendanceGroup> dkAttendanceGroups = dkAttendanceGroupMapper.selectList();
        for (DkAttendanceGroup dkAttendanceGroup : dkAttendanceGroups) {
            DkAttendanceGroupDTO dkAttendanceGroupDTO1 = BeanUtil.toBean(dkAttendanceGroup, DkAttendanceGroupDTO.class);
            LambdaQueryWrapper<DkAttendanceGroupAndUser> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DkAttendanceGroupAndUser::getAttendanceTeamId, dkAttendanceGroup.getId());

            List<DkAttendanceGroupAndUser> dkUsers = dkAttendanceGroupAndUserMapper.selectList(queryWrapper);
            if (dkUsers!=null&&dkUsers.size()>0) {
                for (DkAttendanceGroupAndUser dkAttendanceGroupAndUser:dkUsers) {
                    long deptId = dkAttendanceGroupAndUser.getDeptId();
                    dkAttendanceGroupAndUser.setDept(iSysDeptService.selectDeptById(deptId));
                }
            }
            dkAttendanceGroupDTO1.setMembers(dkUsers);
            dkAttendanceGroupDTOS.add(dkAttendanceGroupDTO1);
        }
        return TableDataInfo.build(dkAttendanceGroupDTOS);
    }

    // 添加或者更新考勤组
    @Override
    public String addOrUpdate(DkAttendanceGroupDTO dkAttendanceGroupDTO) {
        //1.新增或者更新考勤组信息
        DkAttendanceGroup dkAttendanceGroup = BeanUtil.toBean(dkAttendanceGroupDTO, DkAttendanceGroup.class);
        dkAttendanceGroupMapper.insertOrUpdate(dkAttendanceGroup);
        //2.删除考勤组关联的用户信息
        QueryWrapper<DkAttendanceGroupAndUser> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("attendance_team_id",dkAttendanceGroupDTO.getId());
        dkAttendanceGroupAndUserMapper.delete(deleteWrapper);
        //3.重新插入本次关联的用户信息
        List<DkAttendanceGroupAndUser> members = dkAttendanceGroupDTO.getMembers();
        if (members!=null&&members.size()>0) {
            for (DkAttendanceGroupAndUser member: members) {
                QueryWrapper<DkAttendanceGroupAndUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id",member.getUserId());
                List<DkAttendanceGroupAndUser> dkAttendanceGroupAndUsers = dkAttendanceGroupAndUserMapper.selectList(queryWrapper);
                if(dkAttendanceGroupAndUsers!=null&&dkAttendanceGroupAndUsers.size()>0) {
                    throw new GlobalException(member.getNickName()+"已经被其他考勤组规则选择！");
                }
                member.setAttendanceTeamId(dkAttendanceGroup.getId());
                member.setAttendanceGroupName(dkAttendanceGroup.getName());
            }
        }
        if(members.size() > 0){
            dkAttendanceGroupAndUserMapper.insertOrUpdateBatch(members);
        }
        return Constans.SUCCESS;
    }

    @Override
    public int del(long id) {
        //1.删除考勤组配置信息
        int i = dkAttendanceGroupMapper.deleteById(id);
        //2.删除考勤组关联的用户信息
        QueryWrapper<DkAttendanceGroupAndUser> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("attendance_team_id",id);
        dkAttendanceGroupAndUserMapper.delete(deleteWrapper);
        return i;
    }

    // 查询用户所在的考勤组
    public AppDTO queryAttendanceGroupBYUserId(long userId){
        AppDTO appDTO;
        appDTO = dkAttendanceGroupMapper.queryAttendanceGroupBYUserId(userId);
        return appDTO;
    }

    // 删除用户考勤组
    @Override
    public int deleteConfig(long id) {
        return dkAttendanceGroupAndUserMapper.deleteById(id);

    }

    // 查询所有考勤组
    @Override
    public List<DkAttendanceGroupDTO> queryAttendanceGroup() {
        LambdaQueryWrapper<DkAttendanceGroup> queryWrapper = Wrappers.lambdaQuery();
        return dkAttendanceGroupMapper.selectVoList(queryWrapper);
    }
}
