package com.project.zcustom.service.service.employee.impl;

import com.project.zcustom.domain.employee.PlatEmployeeHealth;
import com.project.zcustom.domain.employee.PlatEmployeeHealthChart;
import com.project.zcustom.mapper.employee.PlatEmployeeHealthMapper;
import com.project.zcustom.service.service.employee.IPlatEmployeeHealthChartService;
import com.project.zcustom.service.service.employee.IPlatEmployeeHealthIllnessService;
import com.project.zcustom.service.service.employee.IPlatEmployeeHealthService;
import com.project.common.core.domain.search.PageParamVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 员工健康Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatEmployeeHealthServiceImpl extends ServiceImpl<PlatEmployeeHealthMapper, PlatEmployeeHealth> implements IPlatEmployeeHealthService {
    @Resource
    private IPlatEmployeeHealthChartService platEmployeeHealthChartService;
    @Resource
    private IPlatEmployeeHealthIllnessService iPlatEmployeeHealthIllnessService;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatEmployeeHealth> getLambdaQueryWrapper(PlatEmployeeHealth platEmployeeHealth) {
        LambdaQueryWrapper<PlatEmployeeHealth> lqw = new LambdaQueryWrapper<PlatEmployeeHealth>();
        if (StringUtils.isNotBlank(platEmployeeHealth.getAppOrg())) {
            lqw.eq(PlatEmployeeHealth::getAppOrg, platEmployeeHealth.getAppOrg());
        }
        return lqw;
    }

    /**
     * 分页查询员工健康列表
     *
     * @param platEmployeeHealth 员工健康
     * @return 员工健康集合
     */
    @Override
    public IPage<PlatEmployeeHealth> pagingPlatEmployeeHealthList(PlatEmployeeHealth platEmployeeHealth) {
        IPage<PlatEmployeeHealth> page = new Page<>(platEmployeeHealth.getPageNum(), platEmployeeHealth.getPageSize());
        LambdaQueryWrapper<PlatEmployeeHealth> lqw = getLambdaQueryWrapper(platEmployeeHealth);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealth::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询员工健康列表
     *
     * @param lqw
     * @return 员工健康集合
     */
    @Override
    public IPage<PlatEmployeeHealth> pagingPlatEmployeeHealthList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatEmployeeHealth> lqw) {
        IPage<PlatEmployeeHealth> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealth::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询员工健康列表
     *
     * @param platEmployeeHealth 员工健康
     * @return 员工健康
     */
    @Override
    public List<PlatEmployeeHealth> selectPlatEmployeeHealthList(PlatEmployeeHealth platEmployeeHealth) {
        LambdaQueryWrapper<PlatEmployeeHealth> lqw = getLambdaQueryWrapper(platEmployeeHealth);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealth::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询员工健康列表
     *
     * @param platEmployeeHealth 员工健康
     * @return 员工健康
     */
    @Override
    public List<PlatEmployeeHealth> selectPlatEmployeeHealthList(PlatEmployeeHealth platEmployeeHealth, Long limitNum) {
        LambdaQueryWrapper<PlatEmployeeHealth> lqw = getLambdaQueryWrapper(platEmployeeHealth);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealth::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 员工健康
     */
    @Override
    public PlatEmployeeHealth getPlatEmployeeHealthByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeHealth> lqw = new QueryWrapper<PlatEmployeeHealth>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 员工健康
     */
    @Override
    public PlatEmployeeHealth getPlatEmployeeHealthByField(String field, String filedValue,String fieldString) {
        QueryWrapper<PlatEmployeeHealth> lqw = new QueryWrapper<PlatEmployeeHealth>();
        if(!(Objects.equals(field,"app_org") && Objects.equals(filedValue,"0000"))){
            lqw.eq(field, filedValue);
        }
        lqw.select(fieldString);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 员工健康
     */
    @Override
    public List<PlatEmployeeHealth> selectPlatEmployeeHealthListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatEmployeeHealth> lqw = new QueryWrapper<PlatEmployeeHealth>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 员工健康
     */
    @Override
    public List<PlatEmployeeHealth> selectPlatEmployeeHealthListByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeHealth> lqw = new QueryWrapper<PlatEmployeeHealth>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 添加员工健康信息
     */
    @Override
    public void addPlatEmployeeHealth(String appOrg,Map<String, Object> mapField){
        PlatEmployeeHealth platEmployeeHealth = this.getPlatEmployeeHealthByField("app_org", appOrg);
        try {
            if(StringUtils.isNotNull(platEmployeeHealth)){
                this.setPlatEmployeeHealth(mapField,platEmployeeHealth);
                this.updateById(platEmployeeHealth);
            }else{
                platEmployeeHealth = new PlatEmployeeHealth();
                this.setPlatEmployeeHealth(mapField,platEmployeeHealth);
                platEmployeeHealth.setAppOrg(appOrg);
                this.save(platEmployeeHealth);
            }
            iPlatEmployeeHealthIllnessService.addEmployeeHealthIllness(appOrg,mapField);
//            this.handleHealthChartData(appOrg,mapField);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 设置数据
     */
    private void setPlatEmployeeHealth(Map<String, Object> mapField,PlatEmployeeHealth platEmployeeHealth){
//        platEmployeeHealth.setHealthyNum((Long) mapField.get("healthy"));
//        platEmployeeHealth.setSubHealthyNum((Long) mapField.get("subHealth"));
        List<String> keyList = Arrays.asList("participate","actualParticipate","participateProportion");
        StringUtils.setFieldValue(platEmployeeHealth,mapField,keyList,0, PlatEmployeeHealth::setParticipateNum,Long.class);
        StringUtils.setFieldValue(platEmployeeHealth,mapField,keyList,1, PlatEmployeeHealth::setActualParticipateNum,Long.class);
        StringUtils.setFieldValue(platEmployeeHealth,mapField,keyList,2, PlatEmployeeHealth::setParticipateProportionRate,BigDecimal.class);
    }

    /**
     * 近12月大病发现数量
     */
    @Deprecated
    private void handleHealthChartData(String appOrg,Map<String, Object> mapField) {
        QueryWrapper<PlatEmployeeHealthChart> lqw0 = new QueryWrapper<PlatEmployeeHealthChart>();
        lqw0.eq("app_org", appOrg);
        lqw0.eq("type", 0);
        PlatEmployeeHealthChart healthChart0 = platEmployeeHealthChartService.getOne(lqw0);
        if(StringUtils.isNotNull(healthChart0)){
            healthChart0.setJanuaryNum((Long) mapField.get("oneIllness"));
            healthChart0.setFebruaryNum((Long) mapField.get("twoIllness"));
            healthChart0.setMarchNum((Long) mapField.get("threeIllness"));
            healthChart0.setAprilNum((Long) mapField.get("fourIllness"));
            healthChart0.setMayNum((Long) mapField.get("fiveIllness"));
            healthChart0.setJuneNum((Long) mapField.get("sixIllness"));
            healthChart0.setJulyNum((Long) mapField.get("sevenIllness"));
            healthChart0.setAugustNum((Long) mapField.get("eightIllness"));
            healthChart0.setSeptemberNum((Long) mapField.get("nineIllness"));
            healthChart0.setOctoberNum((Long) mapField.get("tenIllness"));
            healthChart0.setNovemberNum((Long) mapField.get("elevenllness"));
            healthChart0.setDecemberNum((Long) mapField.get("twelvellness"));
            platEmployeeHealthChartService.updateById(healthChart0);
        }else{
            healthChart0.setJanuaryNum((Long) mapField.get("oneIllness"));
            healthChart0.setFebruaryNum((Long) mapField.get("twoIllness"));
            healthChart0.setMarchNum((Long) mapField.get("threeIllness"));
            healthChart0.setAprilNum((Long) mapField.get("fourIllness"));
            healthChart0.setMayNum((Long) mapField.get("fiveIllness"));
            healthChart0.setJuneNum((Long) mapField.get("sixIllness"));
            healthChart0.setJulyNum((Long) mapField.get("sevenIllness"));
            healthChart0.setAugustNum((Long) mapField.get("eightIllness"));
            healthChart0.setSeptemberNum((Long) mapField.get("nineIllness"));
            healthChart0.setOctoberNum((Long) mapField.get("tenIllness"));
            healthChart0.setNovemberNum((Long) mapField.get("elevenllness"));
            healthChart0.setDecemberNum((Long) mapField.get("twelvellness"));
            healthChart0.setAppOrg(appOrg);
            healthChart0.setType(0);
            platEmployeeHealthChartService.save(healthChart0);
        }
    }


    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatEmployeeHealth> lqw = new QueryWrapper<PlatEmployeeHealth>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 健康服务
     */
    @Override
    public Map<String, Object> getHealthData(String appOrg){
        String fieldString = "participate_num,actual_participate_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(participate_num) as participate_num,sum(actual_participate_num) as actual_participate_num";
        }
        PlatEmployeeHealth healthInfo = this.getPlatEmployeeHealthByField("app_org", appOrg,fieldString);
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(healthInfo)) {
            StringUtils.getMapData("应参检人数", "participateNum", 0, tempMap);
            StringUtils.getMapData("实际参检人数", "actualParticipateNum", 0, tempMap);
            StringUtils.getMapData("实际检查人数占比", "participateProportionRate", 0, tempMap);
        } else {
            StringUtils.getMapData("应参检人数", "participateNum", healthInfo.getParticipateNum(), tempMap);
            StringUtils.getMapData("实际参检人数", "actualParticipateNum", healthInfo.getActualParticipateNum(), tempMap);
            StringUtils.getMapData("实际检查人数占比", "participateProportionRate", StringUtils.sumPercentage(healthInfo.getActualParticipateNum(), healthInfo.getParticipateNum()), tempMap);
        }
        return tempMap;
    }


    /**
     * 体检情况 柱形图
     */
    @Override
    public Map<String, Object> getPhysicalChartData(String appOrg){
        PlatEmployeeHealth employeeHealth = this.getPlatEmployeeHealthByField("app_org", appOrg);
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(employeeHealth)){
            StringUtils.createdTableData(Arrays.asList("在册人数","体检人数"),Arrays.asList(0L,0L),"#d67c15",tempMap);
        }else{
            StringUtils.createdTableData(Arrays.asList("在册人数","体检人数"),Arrays.asList(employeeHealth.getParticipateNum(),employeeHealth.getActualParticipateNum()),"#d67c15",tempMap);
        }
        return tempMap;
    }

    /**
     * 体检情况 扇形图
     */
    @Override
    public List<Map<String, Object>> getPhysicalPieData(String appOrg){
        PlatEmployeeHealth employeeHealth = this.getPlatEmployeeHealthByField("app_org", appOrg);
        List<Long> valueList = StringUtils.getDefaultListLong(2);
        if (StringUtils.isNotNull(employeeHealth)) {
            valueList = Arrays.asList(employeeHealth.getParticipateNum(),employeeHealth.getActualParticipateNum());
        }
        List<String> xList = Arrays.asList("应参检人数", "实际参检人数");
        List<String> colorList = Arrays.asList("#2f82fe", "#00ffff");
        List<Map<String, Object>> tempList = new ArrayList<>();
        StringUtils.createdPieData(xList, colorList, valueList, tempList);
        return tempList;
    }

    /**
     * 员工健康分布
     */
    @Override
    public List<Map<String, Object>> getHealthPieData(String appOrg) {
        PlatEmployeeHealth employeeHealth = this.getPlatEmployeeHealthByField("app_org", appOrg);
        List<Long> valueList = StringUtils.getDefaultListLong(2);
        if (StringUtils.isNotNull(employeeHealth)) {
            valueList = Arrays.asList(employeeHealth.getHealthyNum(),employeeHealth.getSubHealthyNum());
        }
        List<String> xList = Arrays.asList("健康", "亚健康");
        List<String> colorList = Arrays.asList("#2f82fe", "#00ffff");
        List<Map<String, Object>> tempList = new ArrayList<>();
        StringUtils.createdPieData(xList, colorList, valueList, tempList);
        return tempList;
    }

}
