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.PlatGreenFoods;

import com.project.zcustom.mapper.smart.PlatGreenFoodsMapper;
import com.project.zcustom.service.service.smart.IPlatGreenFoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 菜谱数据Service业务层处理
 *
 * @author change
 * @date 2024-11-01
 */
@Service
public class PlatGreenFoodsServiceImpl extends ServiceImpl<PlatGreenFoodsMapper, PlatGreenFoods> implements IPlatGreenFoodsService {
    @Autowired
    private PlatGreenFoodsMapper platGreenFoodsMapper;

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

    /**
     * 分页查询菜谱数据列表
     *
     * @param platGreenFoods 菜谱数据
     * @return 菜谱数据集合
     */
    @Override
    public IPage<PlatGreenFoods> pagingPlatGreenFoodsList(PlatGreenFoods platGreenFoods) {
        IPage<PlatGreenFoods> page = new Page<>(platGreenFoods.getPageNum(), platGreenFoods.getPageSize());
        LambdaQueryWrapper<PlatGreenFoods> lqw = getLambdaQueryWrapper(platGreenFoods);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenFoods::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

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

    /**
     * 查询菜谱数据列表
     *
     * @param platGreenFoods 菜谱数据
     * @return 菜谱数据
     */
    @Override
    public List<PlatGreenFoods> selectPlatGreenFoodsList(PlatGreenFoods platGreenFoods) {
        LambdaQueryWrapper<PlatGreenFoods> lqw = getLambdaQueryWrapper(platGreenFoods);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenFoods::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询菜谱数据列表
     *
     * @param platGreenFoods 菜谱数据
     * @return 菜谱数据
     */
    @Override
    public List<PlatGreenFoods> selectPlatGreenFoodsList(PlatGreenFoods platGreenFoods, Long limitNum) {
        LambdaQueryWrapper<PlatGreenFoods> lqw = getLambdaQueryWrapper(platGreenFoods);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenFoods::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

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

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

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

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

    @Override
    public void addPlatGreenFoods(String appOrg, Map<String, Object> mapField) {
        // 先删除所有数据
        LambdaQueryWrapper<PlatGreenFoods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PlatGreenFoods::getAppOrg, appOrg);
        this.reallyDeletePlatGreenFoods(lqw);
        if(mapField.containsKey("week")){
            PlatGreenFoods foods = new PlatGreenFoods();
            foods.setAppOrg(appOrg);
            foods.setFoodInfo(mapField.get("week").toString());
            this.save(foods);
        }
    }

    public int getFoodsTypeNum(String appOrg){
        String str = platGreenFoodsMapper.getFoodsTypeNum(appOrg);
        Pattern pattern = Pattern.compile("\\bname\\b", Pattern.CASE_INSENSITIVE);  // 单词边界，忽略大小写
        Matcher matcher = pattern.matcher(str);
        int count = 0;
        // 遍历匹配到的结果
        while (matcher.find()) {
            count++;
        }
        return count;
    }
}
