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.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.zhushi.entity.*;
import com.ruoyi.zhushi.mapper.NjRuleConfigAndUserMapper;
import com.ruoyi.zhushi.mapper.NjRuleConfigMapper;
import com.ruoyi.zhushi.service.NjBalanceManageService;
import com.ruoyi.zhushi.service.NjRuleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class NjRuleConfigServiceImpl implements NjRuleConfigService {

    @Autowired
    private NjRuleConfigMapper njRuleConfigMapper;

    @Autowired
    private NjRuleConfigAndUserMapper njRuleConfigAndUserMapper;

    @Autowired
    private NjBalanceManageService njBalanceManageService;

    @Override
    public TableDataInfo<NjRuleConfigDTO> queryRuleConfig(NjRuleConfigDTO njRuleConfigDTO, PageQuery pageQuery) {

        // 创建查询条件
        LambdaQueryWrapper<NjRuleConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(null != njRuleConfigDTO.getRuleName(), NjRuleConfig::getRuleName, njRuleConfigDTO.getRuleName());
        // 查询
        IPage<NjRuleConfigDTO> njRuleConfigDTOIPage = njRuleConfigMapper.selectVoPage(pageQuery.build(), queryWrapper, NjRuleConfigDTO.class);
        // 获取记录
        List<NjRuleConfigDTO> njRuleConfigDTOS = njRuleConfigDTOIPage.getRecords();

        njRuleConfigDTOS.forEach(nj -> {
            if(!nj.getApplicableRange()){
                List<DkUsers> users = new ArrayList<>();
                LambdaQueryWrapper<NjRuleConfigAndUser> njRuleConfigAndUserLambdaQueryWrapper = Wrappers.lambdaQuery();
                njRuleConfigAndUserLambdaQueryWrapper.eq(NjRuleConfigAndUser::getNjRuleConfigId, nj.getId());
                List<NjRuleConfigAndUserDTO> njRuleConfigAndUserDTOS = njRuleConfigAndUserMapper.selectVoList(njRuleConfigAndUserLambdaQueryWrapper, NjRuleConfigAndUserDTO.class);
                njRuleConfigAndUserDTOS.forEach(njRuleConfigAndUser -> {
                    DkUsers dkUsers = new DkUsers();
                    BeanUtil.copyProperties(njRuleConfigAndUser, dkUsers);
                    users.add(dkUsers);
                });
                nj.setMembers(users);
            }
        });

        return TableDataInfo.build(njRuleConfigDTOS);
    }

    @Override
    public Boolean  addRule(NjRuleConfigDTO njRuleConfigDTO) {
        // 定义实体
        NjRuleConfig njRuleConfig = new NjRuleConfig();
        // 拷贝属性
        BeanUtil.copyProperties(njRuleConfigDTO, njRuleConfig);
        // 插入规则
        njRuleConfigMapper.insertOrUpdate(njRuleConfig);
        // 根据规则更新年假信息
        if(BeanUtil.isNotEmpty(njRuleConfig.getId())){
            njBalanceManageService.generateYearBalance("0", njRuleConfigDTO);
        }
        // 定义集合
        List<NjRuleConfigAndUser> njRuleConfigAndUsers = new ArrayList<>();
        // 判断集合是否为空
        if(null != njRuleConfigDTO.getMembers() && njRuleConfigDTO.getMembers().size() > 0){
            // 遍历集合
            for (DkUsers member : njRuleConfigDTO.getMembers()){
                NjRuleConfigAndUser njRuleConfigAndUser = new NjRuleConfigAndUser();
                // 根据userId 查询用户信息
                LambdaQueryWrapper<NjRuleConfigAndUser> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(NjRuleConfigAndUser::getUserId, member.getUserId());
                List<NjRuleConfigAndUserDTO> njRuleConfigAndUserDTOS = njRuleConfigAndUserMapper.selectVoList(queryWrapper, NjRuleConfigAndUserDTO.class);
                if(null != njRuleConfigAndUserDTOS && njRuleConfigAndUserDTOS.size() > 0){
                    continue;
                }

                njRuleConfigAndUser.setNjRuleConfigId(njRuleConfig.getId());
                njRuleConfigAndUser.setRuleName(njRuleConfig.getRuleName());
                njRuleConfigAndUser.setUserId(member.getUserId());
                njRuleConfigAndUser.setUserName(member.getUserName());
                njRuleConfigAndUser.setNickName(member.getNickName());
                njRuleConfigAndUser.setJoinedDate(member.getJoinedDate());
                njRuleConfigAndUser.setDeptName(member.getDeptName());
                njRuleConfigAndUsers.add(njRuleConfigAndUser);
            }
        }
        // 批量插入
        return njRuleConfigAndUserMapper.insertOrUpdateBatch(njRuleConfigAndUsers);

    }

    @Override
    public int delRule(long id) {
        return njRuleConfigMapper.deleteById(id);
    }

    @Override
    public int delUser(long id) {
        return njRuleConfigAndUserMapper.deleteById(id);
    }

    @Override
    public List<NjRuleConfigDTO> queryAll() {
        LambdaQueryWrapper<NjRuleConfig> queryWrapper = Wrappers.lambdaQuery();
        List<NjRuleConfigDTO> njRuleConfigDTOS = njRuleConfigMapper.selectVoList(queryWrapper, NjRuleConfigDTO.class);
        return njRuleConfigDTOS;
    }

    @Override
    public List<NjRuleConfigAndUserDTO> queryUserByRuleConfigId(Long id) {
        LambdaQueryWrapper<NjRuleConfigAndUser> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(NjRuleConfigAndUser::getNjRuleConfigId, id);
        List<NjRuleConfigAndUserDTO> njRuleConfigAndUserDTOS = njRuleConfigAndUserMapper.selectVoList(queryWrapper);
        return njRuleConfigAndUserDTOS;
    }
}
