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

import com.project.zcustom.domain.employee.PlatEmployeeHealthIllness;
import com.project.zcustom.mapper.employee.PlatEmployeeHealthIllnessMapper;
import com.project.zcustom.service.service.employee.IPlatEmployeeHealthIllnessService;
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 org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 多发疾病排行Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatEmployeeHealthIllnessServiceImpl extends ServiceImpl<PlatEmployeeHealthIllnessMapper, PlatEmployeeHealthIllness> implements IPlatEmployeeHealthIllnessService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatEmployeeHealthIllness> getLambdaQueryWrapper(PlatEmployeeHealthIllness platEmployeeHealthIllness) {
        LambdaQueryWrapper<PlatEmployeeHealthIllness> lqw = new LambdaQueryWrapper<PlatEmployeeHealthIllness>();
        if (StringUtils.isNotBlank(platEmployeeHealthIllness.getAppOrg())) {
            lqw.eq(PlatEmployeeHealthIllness::getAppOrg, platEmployeeHealthIllness.getAppOrg());
        }
        if (StringUtils.isNotBlank(platEmployeeHealthIllness.getParamsName())) {
            lqw.like(PlatEmployeeHealthIllness::getParamsName, platEmployeeHealthIllness.getParamsName());
        }
        if (StringUtils.isNotNull(platEmployeeHealthIllness.getParamsValue())) {
            lqw.eq(PlatEmployeeHealthIllness::getParamsValue, platEmployeeHealthIllness.getParamsValue());
        }
        return lqw;
    }

    /**
     * 分页查询多发疾病排行列表
     *
     * @param platEmployeeHealthIllness 多发疾病排行
     * @return 多发疾病排行集合
     */
    @Override
    public IPage<PlatEmployeeHealthIllness> pagingPlatEmployeeTypeIllnessList(PlatEmployeeHealthIllness platEmployeeHealthIllness) {
        IPage<PlatEmployeeHealthIllness> page = new Page<>(platEmployeeHealthIllness.getPageNum(), platEmployeeHealthIllness.getPageSize());
        LambdaQueryWrapper<PlatEmployeeHealthIllness> lqw = getLambdaQueryWrapper(platEmployeeHealthIllness);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealthIllness::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询多发疾病排行列表
     *
     * @param lqw
     * @return 多发疾病排行集合
     */
    @Override
    public IPage<PlatEmployeeHealthIllness> pagingPlatEmployeeTypeIllnessList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatEmployeeHealthIllness> lqw) {
        IPage<PlatEmployeeHealthIllness> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealthIllness::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询多发疾病排行列表
     *
     * @param platEmployeeHealthIllness 多发疾病排行
     * @return 多发疾病排行
     */
    @Override
    public List<PlatEmployeeHealthIllness> selectPlatEmployeeTypeIllnessList(PlatEmployeeHealthIllness platEmployeeHealthIllness) {
        LambdaQueryWrapper<PlatEmployeeHealthIllness> lqw = getLambdaQueryWrapper(platEmployeeHealthIllness);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealthIllness::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询多发疾病排行列表
     *
     * @param platEmployeeHealthIllness 多发疾病排行
     * @return 多发疾病排行
     */
    @Override
    public List<PlatEmployeeHealthIllness> selectPlatEmployeeTypeIllnessList(PlatEmployeeHealthIllness platEmployeeHealthIllness, Long limitNum) {
        LambdaQueryWrapper<PlatEmployeeHealthIllness> lqw = getLambdaQueryWrapper(platEmployeeHealthIllness);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeHealthIllness::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 多发疾病排行
     */
    @Override
    public PlatEmployeeHealthIllness getPlatEmployeeTypeIllnessByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeHealthIllness> lqw = new QueryWrapper<PlatEmployeeHealthIllness>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 多发疾病排行
     */
    @Override
    public List<PlatEmployeeHealthIllness> selectPlatEmployeeTypeIllnessListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatEmployeeHealthIllness> lqw = new QueryWrapper<PlatEmployeeHealthIllness>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 多发疾病排行
     */
    @Override
    public List<PlatEmployeeHealthIllness> selectPlatEmployeeTypeIllnessListByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeHealthIllness> lqw = new QueryWrapper<PlatEmployeeHealthIllness>();
        lqw.eq(field, filedValue);
        lqw.orderByDesc("params_value");
        return this.list(lqw);
    }

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

    /**
     * 添加数据
     *
     * @param appOrg
     * @param mapField
     */
    @Override
    @Transactional
    public void addEmployeeHealthIllness(String appOrg, Map<String, Object> mapField) {
        // 先删除数据
        System.out.printf("==========================:%s",mapField);
        this.reallyDeletePlatEmployeeHealthIllness(new LambdaQueryWrapper<PlatEmployeeHealthIllness>().eq(PlatEmployeeHealthIllness::getAppOrg, appOrg));
        List<String> keyList = Arrays.asList("hyperlipidemia", "hypertension", "hyperglycemia");
        List<String> nameList = Arrays.asList("高血脂", "高血压", "高血糖");
        List<PlatEmployeeHealthIllness> addList = new ArrayList<>();
        for (int i = 0; i < keyList.size(); i++) {
            PlatEmployeeHealthIllness platEmployeeHealthIllness = new PlatEmployeeHealthIllness();
            platEmployeeHealthIllness.setAppOrg(appOrg);
            platEmployeeHealthIllness.setParamsName(nameList.get(i));
            StringUtils.setFieldValue(platEmployeeHealthIllness, mapField, keyList, i, PlatEmployeeHealthIllness::setParamsValue, Long.class);
            addList.add(platEmployeeHealthIllness);
        }
        this.saveBatch(addList);
    }


    /**
     * 多发疾病排行
     */
    @Override
    public Map<String, Object> getIllnessChartData(String appOrg){
        QueryWrapper<PlatEmployeeHealthIllness> lqw = new QueryWrapper<PlatEmployeeHealthIllness>();
        if(!Objects.equals(appOrg,"0000")){
            lqw.eq("app_org", appOrg);
            lqw.select("params_name,params_value");
        }else{
            lqw.groupBy("params_name");
            lqw.select("params_name,sum(params_value) as params_value");
        }
        lqw.orderByDesc("params_value");
        List<PlatEmployeeHealthIllness> list = this.list(lqw);
        Map<String, Object> tempMap = new HashMap<>();
        List<String> nameList;
        List<String> colorList;
        List<Long> valueList;
        if (list.isEmpty()){
            nameList = Arrays.asList("高血压","高血脂","高血糖");
            valueList = StringUtils.getDefaultListLong(3);
            colorList = Arrays.asList("#26E0DE","#13F9B9","#EEB164");
        }else{
            nameList = list.stream().map(PlatEmployeeHealthIllness::getParamsName).collect(Collectors.toList());
            valueList = list.stream().map(PlatEmployeeHealthIllness::getParamsValue).collect(Collectors.toList());
            colorList = Arrays.asList("#26E0DE","#13F9B9","#EEB164");
        }
        StringUtils.createdTableData(nameList,valueList,colorList,tempMap);
        return tempMap;
    }
}
