package cn.iocoder.yudao.module.system.service.holiday.holidayuser;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.Constants;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidayuser.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaybalancesetting.HolidayBalanceSettingDO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaysetting.HolidaySettingDO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidayuser.HolidayUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.holiday.holidayuser.HolidayUserMapper;
import cn.iocoder.yudao.module.system.service.holiday.holidaybalancesetting.HolidayBalanceSettingService;
import cn.iocoder.yudao.module.system.service.holiday.holidaysetting.HolidaySettingService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 员工假期 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class HolidayUserServiceImpl implements HolidayUserService {

    @Resource
    private HolidayUserMapper holidayUserMapper;
    @Resource
    @Lazy
    private HolidaySettingService holidaySettingService;
    @Resource
    @Lazy
    private HolidayBalanceSettingService holidayBalanceSettingService;
    @Resource
    @Lazy
    private AdminUserService adminUserService;

    @Override
    public Long createHolidayUser(HolidayUserSaveReqVO createReqVO) {
        // 插入
        HolidayUserDO holidayUser = BeanUtils.toBean(createReqVO, HolidayUserDO.class);
        holidayUserMapper.insert(holidayUser);
        // 返回
        return holidayUser.getId();
    }

    @Override
    public void updateHolidayUser(HolidayUserSaveReqVO updateReqVO) {
        // 更新
        HolidayUserDO updateObj = BeanUtils.toBean(updateReqVO, HolidayUserDO.class);
        holidayUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteHolidayUser(Long id) {
        // 删除
        holidayUserMapper.deleteById(id);
    }

    @Override
    public HolidayUserDO getHolidayUser(Long id) {
        return holidayUserMapper.selectById(id);
    }

    @Override
    public PageResult<HolidayUserDO> getHolidayUserPage(HolidayUserPageReqVO pageReqVO) {
        return holidayUserMapper.selectPage(pageReqVO);
    }

    @Override
    public HolidayUserVO getHolidayUserDynamic(UserPageReqVO userPageReqVO) {
        HolidayUserVO holidayUser = new HolidayUserVO();
        //获取用户列表 -
        Map<Integer, String> headers = new HashMap<>();
        headers.put(1, "姓名");
        headers.put(2, "部门");
        headers.put(3, "入职时间");
        // -- 查询假期的类型
        List<HolidaySettingDO> holidaySettings = holidaySettingService.getAll();
        // 根据假期设置获取到假期余额设置
        Map<Long, HolidayBalanceSettingDO> holidayBalanceSettingMap = holidayBalanceSettingService.selectBySettingIds(holidaySettings.stream().map(HolidaySettingDO::getId).collect(Collectors.toList()));
        int index = 4;
        for (HolidaySettingDO holidaySetting : holidaySettings) {
            headers.put(index, holidaySetting.getName());
            index++;
        }
        holidayUser.setHeaders(headers);
        // -- 获取用户列表
        PageResult<AdminUserDO> pageResult = adminUserService.getUserBringDeptPage(userPageReqVO);
        List<HolidayUserValuePageVO> list = new ArrayList<>();
        // -- 获取这些用户所有的假期余额
        List<Long> userIds = pageResult.getList().stream().map(AdminUserDO::getId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(userIds)) {
            List<HolidayUserDO> holidayUserDOS = holidayUserMapper.selectList(new LambdaQueryWrapper<HolidayUserDO>().in(HolidayUserDO::getUserId, userIds));
            Map<Long, List<HolidayUserDO>> holidayUserMaps = holidayUserDOS.stream().collect(Collectors.groupingBy(HolidayUserDO::getUserId));
            for (AdminUserDO adminUserDO : pageResult.getList()) {
                HolidayUserValuePageVO holidayUserValuePageVO = new HolidayUserValuePageVO();
                holidayUserValuePageVO.setUserId(adminUserDO.getId());
                holidayUserValuePageVO.getListOfHolidayTypes().put(1, new HolidayUserMapVO().setValue(adminUserDO.getNickname()));
                holidayUserValuePageVO.getListOfHolidayTypes().put(2, new HolidayUserMapVO().setValue(adminUserDO.getDeptName()));
                holidayUserValuePageVO.getListOfHolidayTypes().put(3, new HolidayUserMapVO().setValue(adminUserDO.getEntryDate() == null ? "未设置" : adminUserDO.getEntryDate().format(Constants.REPO_DATE_FORMAT)));

                List<HolidayUserDO> items = holidayUserMaps.get(adminUserDO.getId());
                Map<Long, HolidayUserDO> HolidayUserMap = MapUtil.empty();
                if (CollUtil.isNotEmpty(items)) {
                    HolidayUserMap = items.stream().collect(Collectors.toMap(HolidayUserDO::getHolidayBalanceSettingId, holidayUserDO -> holidayUserDO));
                }
                // ------- 还有每个用户的假期余额
                index = 4;
                for (HolidaySettingDO holidaySetting : holidaySettings) {
                    HolidayBalanceSettingDO holidayBalanceSettingDO = holidayBalanceSettingMap.get(holidaySetting.getId());
                    String value = "";
                    if (holidayBalanceSettingDO.getStatus() == 0) {
                        value = "不限制余额";
                    } else {
                        HolidayUserDO holidayUserDO = HolidayUserMap.get(holidaySetting.getId());
                        if (holidayUserDO == null) {
                            value = "不适用";
                        } else {
                            value = holidayUserDO.getHolidayBalance().toString();
                        }
                    }
                    holidayUserValuePageVO.getListOfHolidayTypes().put(index, new HolidayUserMapVO().setId(holidaySetting.getId()).setValue(value));
                    index++;
                }
                list.add(holidayUserValuePageVO);
            }
        }
        // --
        PageResult<HolidayUserValuePageVO> result = new PageResult<>(list, pageResult.getTotal());
        holidayUser.setPage(result);
        return holidayUser;
    }
}
