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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.property.PlatPropertyTools;

import com.project.zcustom.mapper.property.PlatPropertyToolsMapper;
import com.project.zcustom.service.service.property.IPlatPropertyToolsService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 工具列Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatPropertyToolsServiceImpl extends ServiceImpl<PlatPropertyToolsMapper, PlatPropertyTools> implements IPlatPropertyToolsService {

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

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

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

    /**
     * 查询工具列列表
     *
     * @param platPropertyTools 工具列
     * @return 工具列
     */
    @Override
    public List<PlatPropertyTools> selectPlatPropertyToolsList(PlatPropertyTools platPropertyTools) {
        LambdaQueryWrapper<PlatPropertyTools> lqw = getLambdaQueryWrapper(platPropertyTools);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPropertyTools::getCreateTime);
        return baseMapper.selectList(lqw);
    }

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

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

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 工具列
     */
    @Override
    public PlatPropertyTools getPlatPropertyToolsByField(String field, String filedValue,String fieldString) {
        QueryWrapper<PlatPropertyTools> lqw = new QueryWrapper<PlatPropertyTools>();
        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<PlatPropertyTools> selectPlatPropertyToolsListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatPropertyTools> lqw = new QueryWrapper<PlatPropertyTools>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 工具列
     */
    @Override
    public List<PlatPropertyTools> selectPlatPropertyToolsListByField(String field, String filedValue) {
        QueryWrapper<PlatPropertyTools> lqw = new QueryWrapper<PlatPropertyTools>();
        lqw.eq(field, filedValue);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

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

    /**
     *  真删接口
     */
    @Override
    public void reallyDeletePlatPropertyTools(LambdaQueryWrapper<PlatPropertyTools> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    /**
     * 工具箱二具数量 服务内容
     */
    @Override
    public List<Map<String, Object>> getToolsChartsList(String appOrg){
        String fieldString = "self_test,consultation";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(self_test) as self_test,sum(consultation) as consultation";
        }
        PlatPropertyTools toolsInfo = this.getPlatPropertyToolsByField("app_org", appOrg,fieldString);
        List<String> nameList = Arrays.asList("健康自测","远程问诊");
        List<String> colorList = Arrays.asList("#00ffff","#2f82fe");
        List<Long> valueList = StringUtils.getDefaultListLong(2);
        if (StringUtils.isNotNull(toolsInfo)) {
            valueList = Arrays.asList(toolsInfo.getSelfTest(), toolsInfo.getConsultation());
        }
        List<Map<String, Object>> tempList = new ArrayList<>();
        StringUtils.createdPieData(nameList, colorList, valueList, tempList);
        return tempList;
    }
}
