package org.dromara.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.web.domain.HospitalMealRecipe;
import org.dromara.web.domain.bo.HospitalMealRecipeBo;
import org.dromara.web.domain.vo.HospitalMealRecipeVo;
import org.dromara.web.mapper.HospitalMealRecipeMapper;
import org.dromara.web.service.IHospitalMealRecipeService;
import org.springframework.stereotype.Service;

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

/**
 * 院内膳食食谱明细Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HospitalMealRecipeServiceImpl implements IHospitalMealRecipeService {

    private final HospitalMealRecipeMapper baseMapper;

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

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

    /**
     * 查询符合条件的院内膳食食谱明细列表
     *
     * @param bo 查询条件
     * @return 院内膳食食谱明细列表
     */
    @Override
    public List<HospitalMealRecipeVo> queryList(HospitalMealRecipeBo bo) {
        LambdaQueryWrapper<HospitalMealRecipe> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

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

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

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

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