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

import com.project.zcustom.domain.employee.PlatEmployeeHealthChart;
import com.project.zcustom.mapper.employee.PlatEmployeeHealthChartMapper;
import com.project.zcustom.service.service.employee.IPlatEmployeeHealthChartService;
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工健康图数据Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatEmployeeHealthChartServiceImpl extends ServiceImpl<PlatEmployeeHealthChartMapper, PlatEmployeeHealthChart> implements IPlatEmployeeHealthChartService {

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

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

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

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

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

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

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

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 员工健康图数据
     */
    @Override
    public List<PlatEmployeeHealthChart> selectPlatEmployeeHealthChartListByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeHealthChart> lqw = new QueryWrapper<PlatEmployeeHealthChart>();
        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<PlatEmployeeHealthChart> lqw = new QueryWrapper<PlatEmployeeHealthChart>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 0健康指标趋势 and 1甲状腺结节发现及时率趋势
     * @param appOrg
     * @return
     */
    @Override
    public Map<String, Object> getHealthBrokenData(String appOrg, int type){
        QueryWrapper<PlatEmployeeHealthChart> lqw = new QueryWrapper<PlatEmployeeHealthChart>();
        lqw.eq("app_org", appOrg);
        lqw.eq("type", type);
        lqw.last("limit 1");
        PlatEmployeeHealthChart flowInfo = this.getOne(lqw);
        List<String> monthsList = Arrays.asList("01月", "02月", "03月", "04月", "05月", "06月", "07月", "08月", "09月", "10月", "11月", "12月");
        List<Long> valueList = StringUtils.getDefaultListLong(12);
        if(StringUtils.isNotNull(flowInfo)){
            valueList = Arrays.asList(flowInfo.getJanuaryNum(), flowInfo.getFebruaryNum(), flowInfo.getMarchNum(), flowInfo.getAprilNum(), flowInfo.getMayNum(), flowInfo.getJuneNum(), flowInfo.getJulyNum(), flowInfo.getAugustNum(), flowInfo.getSeptemberNum(), flowInfo.getOctoberNum(), flowInfo.getNovemberNum(), flowInfo.getDecemberNum());
        }
        Map<String, Object> tempMap = new HashMap<>();
        StringUtils.createdTableData(monthsList,valueList,"#04e0e1",tempMap);
        return tempMap;
    }
}
