package org.dromara.web.service.impl;

import cn.hutool.core.util.ObjUtil;
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.dromara.web.domain.EnteralNutritionConsumable;
import org.dromara.web.domain.bo.EnteralNutritionConsumableBo;
import org.dromara.web.domain.vo.EnteralNutritionConsumableVo;
import org.dromara.web.mapper.EnteralNutritionConsumableMapper;
import org.dromara.web.service.IEnteralNutritionConsumableService;
import org.springframework.stereotype.Service;

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

/**
 * 肠内营养耗材Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EnteralNutritionConsumableServiceImpl implements IEnteralNutritionConsumableService {

    private final EnteralNutritionConsumableMapper baseMapper;

    /**
     * 查询肠内营养耗材
     *
     * @param id 主键
     * @return 肠内营养耗材
     */
    @Override
    public EnteralNutritionConsumableVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询肠内营养耗材列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 肠内营养耗材分页列表
     */
    @Override
    public TableDataInfo<EnteralNutritionConsumableVo> queryPageList(EnteralNutritionConsumableBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EnteralNutritionConsumable> lqw = buildQueryWrapper(bo);
        Page<EnteralNutritionConsumableVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的肠内营养耗材列表
     *
     * @param bo 查询条件
     * @return 肠内营养耗材列表
     */
    @Override
    public List<EnteralNutritionConsumableVo> queryList(EnteralNutritionConsumableBo bo) {
        LambdaQueryWrapper<EnteralNutritionConsumable> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EnteralNutritionConsumable> buildQueryWrapper(EnteralNutritionConsumableBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EnteralNutritionConsumable> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(EnteralNutritionConsumable::getId);
        lqw.eq(bo.getSettlementId() != null, EnteralNutritionConsumable::getSettlementId, bo.getSettlementId());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupNo()), EnteralNutritionConsumable::getGroupNo, bo.getGroupNo());
        lqw.eq(StringUtils.isNotBlank(bo.getConsumable()), EnteralNutritionConsumable::getConsumable, bo.getConsumable());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecification()), EnteralNutritionConsumable::getSpecification, bo.getSpecification());
        lqw.eq(ObjUtil.isNotNull(bo.getDosePerTime()), EnteralNutritionConsumable::getDosePerTime, bo.getDosePerTime());
        lqw.eq(ObjUtil.isNotNull(bo.getFrequency()), EnteralNutritionConsumable::getFrequency, bo.getFrequency());
        lqw.eq(ObjUtil.isNotNull(bo.getDosePerDay()), EnteralNutritionConsumable::getDosePerDay, bo.getDosePerDay());
        lqw.eq(bo.getUsageDays() != null, EnteralNutritionConsumable::getUsageDays, bo.getUsageDays());
        lqw.eq(ObjUtil.isNotNull(bo.getFirstDay()), EnteralNutritionConsumable::getFirstDay, bo.getFirstDay());
        lqw.eq(bo.getQuantity() != null, EnteralNutritionConsumable::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getPrescriptionRemark()), EnteralNutritionConsumable::getPrescriptionRemark, bo.getPrescriptionRemark());
        lqw.eq(bo.getAmount() != null, EnteralNutritionConsumable::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EnteralNutritionConsumable::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增肠内营养耗材
     *
     * @param bo 肠内营养耗材
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EnteralNutritionConsumableBo bo) {
        EnteralNutritionConsumable add = MapstructUtils.convert(bo, EnteralNutritionConsumable.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改肠内营养耗材
     *
     * @param bo 肠内营养耗材
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EnteralNutritionConsumableBo bo) {
        EnteralNutritionConsumable update = MapstructUtils.convert(bo, EnteralNutritionConsumable.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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