package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysRecipeFoodIngredientBo;
import org.dromara.system.domain.vo.SysRecipeFoodIngredientVo;
import org.dromara.system.domain.SysRecipeFoodIngredient;
import org.dromara.system.mapper.SysRecipeFoodIngredientMapper;
import org.dromara.system.service.ISysRecipeFoodIngredientService;

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

/**
 * 食谱与食材关联Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-01
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysRecipeFoodIngredientServiceImpl implements ISysRecipeFoodIngredientService {

    private final SysRecipeFoodIngredientMapper baseMapper;

    /**
     * 查询食谱与食材关联
     *
     * @param id 主键
     * @return 食谱与食材关联
     */
    @Override
    public SysRecipeFoodIngredientVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询食谱与食材关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 食谱与食材关联分页列表
     */
    @Override
    public TableDataInfo<SysRecipeFoodIngredientVo> queryPageList(SysRecipeFoodIngredientBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysRecipeFoodIngredient> lqw = buildQueryWrapper(bo);
        Page<SysRecipeFoodIngredientVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的食谱与食材关联列表
     *
     * @param bo 查询条件
     * @return 食谱与食材关联列表
     */
    @Override
    public List<SysRecipeFoodIngredientVo> queryList(SysRecipeFoodIngredientBo bo) {
        LambdaQueryWrapper<SysRecipeFoodIngredient> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysRecipeFoodIngredient> buildQueryWrapper(SysRecipeFoodIngredientBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysRecipeFoodIngredient> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SysRecipeFoodIngredient::getId);
        lqw.eq(bo.getRecipeId() != null, SysRecipeFoodIngredient::getRecipeId, bo.getRecipeId());
        lqw.eq(bo.getFoodIngredientId() != null, SysRecipeFoodIngredient::getFoodIngredientId, bo.getFoodIngredientId());
        lqw.eq(bo.getQuantity() != null, SysRecipeFoodIngredient::getQuantity, bo.getQuantity());
        lqw.eq(bo.getCalories() != null, SysRecipeFoodIngredient::getCalories, bo.getCalories());
        return lqw;
    }

    /**
     * 新增食谱与食材关联
     *
     * @param bo 食谱与食材关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysRecipeFoodIngredientBo bo) {
        SysRecipeFoodIngredient add = MapstructUtils.convert(bo, SysRecipeFoodIngredient.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改食谱与食材关联
     *
     * @param bo 食谱与食材关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysRecipeFoodIngredientBo bo) {
        SysRecipeFoodIngredient update = MapstructUtils.convert(bo, SysRecipeFoodIngredient.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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