package org.dromara.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.web.domain.DailyMealRecipe;
import org.dromara.web.domain.bo.DailyMealRecipeBo;
import org.dromara.web.domain.vo.DailyMealRecipeVo;
import org.dromara.web.mapper.DailyMealRecipeMapper;
import org.dromara.web.service.IDailyMealRecipeService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 日常膳食食谱明细Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DailyMealRecipeServiceImpl implements IDailyMealRecipeService {

    private final DailyMealRecipeMapper baseMapper;

    /**
     * 查询日常膳食食谱明细
     *
     * @param id 主键
     * @return 日常膳食食谱明细
     */
    @Override
    public DailyMealRecipeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询日常膳食食谱明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 日常膳食食谱明细分页列表
     */
    @Override
    public TableDataInfo<DailyMealRecipeVo> queryPageList(DailyMealRecipeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DailyMealRecipe> lqw = buildQueryWrapper(bo);
        Page<DailyMealRecipeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的日常膳食食谱明细列表
     *
     * @param bo 查询条件
     * @return 日常膳食食谱明细列表
     */
    @Override
    public List<DailyMealRecipeVo> queryList(DailyMealRecipeBo bo) {
        LambdaQueryWrapper<DailyMealRecipe> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DailyMealRecipe> buildQueryWrapper(DailyMealRecipeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DailyMealRecipe> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(DailyMealRecipe::getId);
        lqw.eq(bo.getPlanId() != null, DailyMealRecipe::getPlanId, bo.getPlanId());
        lqw.eq(bo.getRecipeNo() != null, DailyMealRecipe::getRecipeNo, bo.getRecipeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMealTime()), DailyMealRecipe::getMealTime, bo.getMealTime());
        lqw.eq(bo.getEatTime() != null, DailyMealRecipe::getEatTime, bo.getEatTime());
        lqw.like(StringUtils.isNotBlank(bo.getFoodCategoryName()), DailyMealRecipe::getFoodCategoryName, bo.getFoodCategoryName());
        lqw.eq(bo.getFoodWeight() != null, DailyMealRecipe::getFoodWeight, bo.getFoodWeight());
        lqw.eq(bo.getCalorie() != null, DailyMealRecipe::getCalorie, bo.getCalorie());
        lqw.eq(bo.getAmount() != null, DailyMealRecipe::getAmount, bo.getAmount());
        lqw.eq(bo.getPrice() != null, DailyMealRecipe::getPrice, bo.getPrice());
        return lqw;
    }

    /**
     * 新增日常膳食食谱明细
     *
     * @param bo 日常膳食食谱明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(DailyMealRecipeBo bo) {
        DailyMealRecipe add = MapstructUtils.convert(bo, DailyMealRecipe.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改日常膳食食谱明细
     *
     * @param bo 日常膳食食谱明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(DailyMealRecipeBo bo) {
        DailyMealRecipe update = MapstructUtils.convert(bo, DailyMealRecipe.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DailyMealRecipe entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除日常膳食食谱明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
