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

import com.project.common.core.domain.search.PageParamVo;
import com.project.zcustom.domain.smart.PlatGreenSmartOther;
import com.project.zcustom.mapper.service.smart.PlatGreenSmartOtherMapper;
import com.project.zcustom.service.service.smart.*;
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 javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 绿智食堂 其他参数Service业务层处理
 *
 * @author change
 * @date 2024-10-31
 */
@Service
public class PlatGreenSmartOtherServiceImpl extends ServiceImpl<PlatGreenSmartOtherMapper, PlatGreenSmartOther> implements IPlatGreenSmartOtherService {
    @Resource
    private IPlatGreenSmartService iPlatGreenSmartService;
    @Resource
    private IPlatGreenSmartEnergyService iPlatGreenSmartEnergyService;
    @Resource
    private IPlatGreenSmartRankService iPlatGreenSmartRankService;
    @Resource
    private IPlatGreenSmartSafetyService iPlatGreenSmartSafetyService;

    @Resource
    private IPlatGreenFoodsService iPlatGreenFoodsService;

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

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

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

    /**
     * 查询绿智食堂 其他参数列表
     *
     * @param platGreenSmartOther 绿智食堂 其他参数
     * @return 绿智食堂 其他参数
     */
    @Override
    public List<PlatGreenSmartOther> selectPlatGreenSmartOtherList(PlatGreenSmartOther platGreenSmartOther) {
        LambdaQueryWrapper<PlatGreenSmartOther> lqw = getLambdaQueryWrapper(platGreenSmartOther);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartOther::getCreateTime);
        return baseMapper.selectList(lqw);
    }

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

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

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 绿智食堂 其他参数
     */
    @Override
    public PlatGreenSmartOther getPlatGreenSmartOtherByField(String field, String filedValue, String fieldString) {
        QueryWrapper<PlatGreenSmartOther> lqw = new QueryWrapper<PlatGreenSmartOther>();
        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<PlatGreenSmartOther> selectPlatGreenSmartOtherListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatGreenSmartOther> lqw = new QueryWrapper<PlatGreenSmartOther>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

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

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

    /**
     * 添加智慧食堂其他信息
     */
    @Override
    public void addPlatGreenSmartOther(String appOrg, Map<String, Object> mapField){
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg);
        try {
            if (StringUtils.isNotNull(platGreenSmartOther)) {
                this.setPlatGreenSmartOther(mapField, platGreenSmartOther);
                this.updateById(platGreenSmartOther);
            } else {
                platGreenSmartOther = new PlatGreenSmartOther();
                this.setPlatGreenSmartOther(mapField, platGreenSmartOther);
                platGreenSmartOther.setAppOrg(appOrg);
                this.save(platGreenSmartOther);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            // 食堂概览
            iPlatGreenSmartService.addPlatGreenSmart(appOrg, mapField);
        }catch (Exception e){}
        try {
            // 食堂安全管理
            iPlatGreenSmartSafetyService.addPlatGreenSmartSafety(appOrg, mapField);
        }catch (Exception e){}
        try {
            // 热销菜品排行
            iPlatGreenSmartRankService.addPlatGreenSmartRank(appOrg, mapField);
        }catch (Exception e){}
        try {
            // 处理食堂每周菜品信息
            iPlatGreenFoodsService.addPlatGreenFoods(appOrg, mapField);
        }catch (Exception e){}
    }

    /**
     * 设置数据
     */
    private void setPlatGreenSmartOther(Map<String, Object> mapField, PlatGreenSmartOther platGreenSmartOther) {
        //绿智食堂参评食堂情况
        List<String> keyList = Arrays.asList("participate", "completed","breakfast","lunch","dinner","morning","noon","night","foodVoting","clearPlate","culinaryExchange","takeOutFood","selfFood","holidaySpecial","breakService","laundryService","barberService");
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,0, PlatGreenSmartOther::setParticipateNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,1, PlatGreenSmartOther::setCompletedNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,2, PlatGreenSmartOther::setActualDiningNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,3, PlatGreenSmartOther::setLunchNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,4, PlatGreenSmartOther::setDinnerNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,5, PlatGreenSmartOther::setMorningNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,6, PlatGreenSmartOther::setNoonNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,7, PlatGreenSmartOther::setNightNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,8, PlatGreenSmartOther::setFoodVotingNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,9, PlatGreenSmartOther::setClearPlateNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,10, PlatGreenSmartOther::setCulinaryExchangeNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,11, PlatGreenSmartOther::setTakeOutFoodNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,12, PlatGreenSmartOther::setSelfFoodNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,13, PlatGreenSmartOther::setHolidaySpecialNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,14, PlatGreenSmartOther::setBreakServiceNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,15, PlatGreenSmartOther::setLaundryServiceNum,Long.class);
        StringUtils.setFieldValue(platGreenSmartOther,mapField,keyList,16, PlatGreenSmartOther::setBarberServiceNum,Long.class);
        Pattern pattern = Pattern.compile("\\bname\\b", Pattern.CASE_INSENSITIVE);  // 单词边界，忽略大小写
        Matcher matcher = pattern.matcher(mapField.get("week").toString());
        int count = 0;
        // 遍历匹配到的结果
        while (matcher.find()) {
            count++;
        }
        platGreenSmartOther.setWeeklyCuisineNum(Long.valueOf(count));
    }

    /**
     * 绿智食堂参评食堂情况
     * @param appOrg
     * @return
     */
    @Override
    public Map<String, Object> geParticipateData(String appOrg){
        String fieldString = "participate_num,completed_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(participate_num) as participate_num," +
                    "sum(completed_num) as completed_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platGreenSmartOther)) {
            StringUtils.getMapData("参评绿智食堂", "participate", 0, tempMap);
            StringUtils.getMapData("已完成单位", "completed", 0, tempMap);
            StringUtils.getMapData("完成率", "rate", 0, tempMap);
        }else{
            StringUtils.getMapData("参评绿智食堂", "participate", platGreenSmartOther.getParticipateNum(), tempMap);
            StringUtils.getMapData("已完成单位", "completed", platGreenSmartOther.getCompletedNum(), tempMap);
            StringUtils.getMapData("完成率", "rate", StringUtils.sumPercentage(platGreenSmartOther.getCompletedNum(),platGreenSmartOther.getParticipateNum()), tempMap);
        }
        return tempMap;
    }

    /**
     * 就餐情况
     * @param appOrg
     * @return
     */
    public List<Map<String, Object>> getDiningPieData(String appOrg){
        String fieldString = "actual_dining_num,lunch_num,dinner_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(actual_dining_num) as actual_dining_num," +
                    "sum(lunch_num) as lunch_num," +
                    "sum(dinner_num) as dinner_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        List<String> nameList = Arrays.asList("早餐","午餐","晚餐");
        List<String> colorList = Arrays.asList("#fec935","#2f82fe","#7666ee");
        List<Long> valueList = StringUtils.getDefaultListLong(3);
        if (StringUtils.isNotNull(platGreenSmartOther)) {
            valueList = Arrays.asList(platGreenSmartOther.getActualDiningNum(), platGreenSmartOther.getLunchNum(), platGreenSmartOther.getDinnerNum());
        }
        List<Map<String, Object>> tempList = new ArrayList<>();
        StringUtils.createdPieData(nameList, colorList, valueList, tempList);
        return tempList;
    }

    /**
     * 智慧食堂
     * @param appOrg
     * @return
     */
    public List<Map<String, Object>> getSmartCanteenPieData(String appOrg){
        String fieldString = "take_out_food_num,self_food_num,holiday_special_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(take_out_food_num) as take_out_food_num," +
                    "sum(self_food_num) as self_food_num," +
                    "sum(holiday_special_num) as holiday_special_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        List<String> nameList = Arrays.asList("外卖","自选食品","节日特供");
        List<String> colorList = Arrays.asList("#2f82fe","#00ffff","#12db62");
        List<Long> valueList = StringUtils.getDefaultListLong(3);
        if (StringUtils.isNotNull(platGreenSmartOther)) {
            valueList = Arrays.asList(platGreenSmartOther.getTakeOutFoodNum(), platGreenSmartOther.getSelfFoodNum(), platGreenSmartOther.getHolidaySpecialNum());
        }
        List<Map<String, Object>> tempList = new ArrayList<>();
        StringUtils.createdPieData(nameList, colorList, valueList, tempList);
        return tempList;
    }

    /**
     * 员工服务
     * @param appOrg
     * @return
     */
    public List<Map<String, Object>> getEmployeePieData(String appOrg){
        String fieldString = "break_service_num,laundry_service_num,barber_service_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(break_service_num) as break_service_num," +
                    "sum(laundry_service_num) as laundry_service_num," +
                    "sum(barber_service_num) as barber_service_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        List<String> nameList = Arrays.asList("茶歇服务","洗衣服务","理发服务");
        List<String> colorList = Arrays.asList("#12DB62","#00FFFF","#EF6765");
        List<Long> valueList = StringUtils.getDefaultListLong(3);
        if (StringUtils.isNotNull(platGreenSmartOther)) {
            valueList = Arrays.asList(platGreenSmartOther.getBreakServiceNum(), platGreenSmartOther.getLaundryServiceNum(), platGreenSmartOther.getBarberServiceNum());
        }
        List<Map<String, Object>> tempList = new ArrayList<>();
        StringUtils.createdPieData(nameList, colorList, valueList, tempList);
        return tempList;
    }

    /**
     * 获取菜品信息
     * @param appOrg
     * @return
     */
    @Override
    public Map<String, Object> getFoodData(String appOrg){
        String fieldString = "daily_menu_num,weekly_cuisine_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(daily_menu_num) as daily_menu_num," +
                    "sum(weekly_cuisine_num) as weekly_cuisine_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platGreenSmartOther)) {
            //StringUtils.getMapData("每日菜谱", "dailyMenu", 0, tempMap);
            StringUtils.getMapData("周菜食谱", "weeklyCuisine", 0, tempMap);
        }else{
            //StringUtils.getMapData("每日菜谱", "dailyMenu", platGreenSmartOther.getDailyMenuNum(), tempMap);
            StringUtils.getMapData("周菜食谱", "weeklyCuisine", platGreenSmartOther.getWeeklyCuisineNum(), tempMap);
        }
        return tempMap;
    }

    /**
     * 获取其他数据信息
     * @param appOrg
     * @return
     */
    @Override
    public Map<String, Object> getOtherData(String appOrg){
        String fieldString = "food_voting_num,clear_plate_num,culinary_exchange_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(food_voting_num) as food_voting_num," +
                    "sum(clear_plate_num) as clear_plate_num," +
                    "sum(culinary_exchange_num) as culinary_exchange_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platGreenSmartOther)) {
            StringUtils.getMapData("菜品投票参与人数", "foodVoting", 0, tempMap);
            StringUtils.getMapData("光盘行动参与人数", "clearPlate", 0, tempMap);
            StringUtils.getMapData("厨艺交流上传数量", "culinaryExchange", 0, tempMap);
        }else{
            StringUtils.getMapData("菜品投票参与人数", "foodVoting", platGreenSmartOther.getFoodVotingNum(), tempMap);
            StringUtils.getMapData("光盘行动参与人数", "clearPlate", platGreenSmartOther.getClearPlateNum(), tempMap);
            StringUtils.getMapData("厨艺交流上传数量", "culinaryExchange", platGreenSmartOther.getCulinaryExchangeNum(), tempMap);
        }
        return tempMap;
    }

    /**
     * 电子餐券
     * @param appOrg
     * @return
     */
    public Map<String, Object> getElectronicVouchersData(String appOrg){
        String fieldString = "morning_num,noon_num,night_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(morning_num) as morning_num," +
                    "sum(noon_num) as noon_num," +
                    "sum(night_num) as night_num";
        }
        PlatGreenSmartOther platGreenSmartOther = this.getPlatGreenSmartOtherByField("app_org", appOrg,fieldString);
        List<Long> valueList = StringUtils.getDefaultListLong(3);
        if (StringUtils.isNotNull(platGreenSmartOther)) {
            valueList = Arrays.asList(platGreenSmartOther.getMorningNum(), platGreenSmartOther.getNoonNum(), platGreenSmartOther.getNightNum());
        }
        Map<String, Object> tempMap = new HashMap<>();
        List<String> xList = Arrays.asList("早", "中", "晚");
        StringUtils.createdTableData(xList, valueList, "#eaa756",tempMap);
        return tempMap;
    }

}
