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

import com.project.zcustom.domain.employee.PlatEmployeeTypeIllness;
import com.project.zcustom.mapper.employee.PlatEmployeeTypeIllnessMapper;
import com.project.zcustom.service.service.employee.IPlatEmployeeTypeIllnessService;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 多发疾病排行Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatEmployeeTypeIllnessServiceImpl extends ServiceImpl<PlatEmployeeTypeIllnessMapper, PlatEmployeeTypeIllness> implements IPlatEmployeeTypeIllnessService {

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

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

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

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

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

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

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

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

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

    /**
     * 多发疾病排行
     */
    @Override
    public Map<String, Object> getIllnessChartData(String appOrg){
        List<PlatEmployeeTypeIllness> list = this.selectPlatEmployeeTypeIllnessListByField("app_org", appOrg);
        Map<String, Object> tempMap = new HashMap<>();
        if (list.isEmpty()){
            StringUtils.getEmptyTableData(tempMap);
        }else{
            List<String> nameList = list.stream().map(PlatEmployeeTypeIllness::getParamsName).collect(Collectors.toList());
            List<Long> valueList = list.stream().map(PlatEmployeeTypeIllness::getParamsValue).collect(Collectors.toList());
            StringUtils.createdTableData(nameList,valueList,"#2EC7C9",tempMap);
        }
        return tempMap;
    }
}
