package com.project.zcustom.service.service.smart.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.smart.PlatGreenSmartRank;

import com.project.zcustom.mapper.smart.PlatGreenSmartRankMapper;
import com.project.zcustom.service.service.smart.IPlatGreenSmartRankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 绿智食堂 排名Service业务层处理
 *
 * @author change
 * @date 2024-10-31
 */
@Service
public class PlatGreenSmartRankServiceImpl extends ServiceImpl<PlatGreenSmartRankMapper, PlatGreenSmartRank> implements IPlatGreenSmartRankService {

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

    /**
     * 分页查询绿智食堂 排名列表
     *
     * @param platGreenSmartRank 绿智食堂 排名
     * @return 绿智食堂 排名集合
     */
    @Override
    public IPage<PlatGreenSmartRank> pagingPlatGreenSmartRankList(PlatGreenSmartRank platGreenSmartRank) {
        IPage<PlatGreenSmartRank> page = new Page<>(platGreenSmartRank.getPageNum(), platGreenSmartRank.getPageSize());
        LambdaQueryWrapper<PlatGreenSmartRank> lqw = getLambdaQueryWrapper(platGreenSmartRank);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartRank::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

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

    /**
     * 查询绿智食堂 排名列表
     *
     * @param platGreenSmartRank 绿智食堂 排名
     * @return 绿智食堂 排名
     */
    @Override
    public List<PlatGreenSmartRank> selectPlatGreenSmartRankList(PlatGreenSmartRank platGreenSmartRank) {
        LambdaQueryWrapper<PlatGreenSmartRank> lqw = getLambdaQueryWrapper(platGreenSmartRank);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartRank::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询绿智食堂 排名列表
     *
     * @param platGreenSmartRank 绿智食堂 排名
     * @return 绿智食堂 排名
     */
    @Override
    public List<PlatGreenSmartRank> selectPlatGreenSmartRankList(PlatGreenSmartRank platGreenSmartRank, Long limitNum) {
        LambdaQueryWrapper<PlatGreenSmartRank> lqw = getLambdaQueryWrapper(platGreenSmartRank);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartRank::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 绿智食堂 排名
     */
    @Override
    public PlatGreenSmartRank getPlatGreenSmartRankByField(String field, String filedValue) {
        QueryWrapper<PlatGreenSmartRank> lqw = new QueryWrapper<PlatGreenSmartRank>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 绿智食堂 排名
     */
    @Override
    public List<PlatGreenSmartRank> selectPlatGreenSmartRankListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatGreenSmartRank> lqw = new QueryWrapper<PlatGreenSmartRank>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

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

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

    /**
     *  添加热销菜品数据
     */
    @Override
    @Transactional
    public void addPlatGreenSmartRank(String appOrg, Map<String, Object> mapField){
        // 先删除数据
        this.reallyDeletePlatGreenSmartRank(new LambdaQueryWrapper<PlatGreenSmartRank>().eq(PlatGreenSmartRank::getAppOrg, appOrg));
        List<String> nameList = Arrays.asList("FirstDish","secondtDish","thirdDish","fourDish","fiveDish");
        List<String> keyList = Arrays.asList("FirstNum","secondNum","thirdNum","fourNum","fiveNum");
        List<PlatGreenSmartRank> addList = new ArrayList<>();
        for (int i = 0; i < nameList.size(); i++) {
            PlatGreenSmartRank platGreenSmartRank = new PlatGreenSmartRank();
            platGreenSmartRank.setAppOrg(appOrg);
            StringUtils.setFieldValue(platGreenSmartRank, mapField, keyList, i, PlatGreenSmartRank::setParamsValue, Long.class);
            StringUtils.setFieldValue(platGreenSmartRank, mapField, nameList, i, PlatGreenSmartRank::setParamsName, String.class);
            addList.add(platGreenSmartRank);
        }
        this.saveBatch(addList);
    }

    /**
     * 热销菜品排行
     * @param appOrg
     * @return
     */
    public Map<String,Object> getRankData(String appOrg){
        QueryWrapper<PlatGreenSmartRank> lqw = new QueryWrapper<PlatGreenSmartRank>();
        if(!Objects.equals(appOrg,"0000")){
            lqw.eq("app_org", appOrg);
        }
        lqw.groupBy("params_name");
        lqw.select("params_name,(sum(params_value) / count(id)) as params_value");
        lqw.orderByDesc("params_value");
        lqw.last("limit 5");
        List<PlatGreenSmartRank> list = this.list(lqw);
        if(list.isEmpty()){
            return new HashMap<>();
        }
        Map<String, Object> tempMap = new HashMap<>();
        List<String> nameList = list.stream().map(PlatGreenSmartRank::getParamsName).collect(Collectors.toList());
        List<Long> valueList = list.stream().map(PlatGreenSmartRank::getParamsValue).collect(Collectors.toList());
        StringUtils.createdTableData(nameList,valueList,"#efb164",tempMap);
        return tempMap;
    }
}
