package com.ruoyi.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 org.springframework.stereotype.Service;
import com.ruoyi.pay.domain.bo.PayBaohuoProductBo;
import com.ruoyi.pay.domain.vo.PayBaohuoProductVo;
import com.ruoyi.pay.domain.PayBaohuoProduct;
import com.ruoyi.pay.mapper.PayBaohuoProductMapper;
import com.ruoyi.pay.service.IPayBaohuoProductService;

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

/**
 * 包活工单产品关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayBaohuoProductServiceImpl implements IPayBaohuoProductService {

    private final PayBaohuoProductMapper baseMapper;

    /**
     * 查询包活工单产品关联
     */
    @Override
    public PayBaohuoProductVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询包活工单产品关联列表
     */
    @Override
    public TableDataInfo<PayBaohuoProductVo> queryPageList(PayBaohuoProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayBaohuoProduct> lqw = buildQueryWrapper(bo);
        Page<PayBaohuoProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询包活工单产品关联列表
     */
    @Override
    public List<PayBaohuoProductVo> queryList(PayBaohuoProductBo bo) {
        LambdaQueryWrapper<PayBaohuoProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayBaohuoProduct> buildQueryWrapper(PayBaohuoProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayBaohuoProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayBaohuoProduct::getTenantId, bo.getTenantId());
        lqw.eq(bo.getProductId() != null, PayBaohuoProduct::getProductId, bo.getProductId());
        lqw.eq(bo.getWoId() != null, PayBaohuoProduct::getWoId, bo.getWoId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), PayBaohuoProduct::getProductName, bo.getProductName());
        lqw.eq(bo.getProductNum() != null, PayBaohuoProduct::getProductNum, bo.getProductNum());
        lqw.eq(bo.getMinPrice() != null, PayBaohuoProduct::getMinPrice, bo.getMinPrice());
        lqw.eq(bo.getMaxPrice() != null, PayBaohuoProduct::getMaxPrice, bo.getMaxPrice());
        lqw.eq(bo.getAmount() != null, PayBaohuoProduct::getAmount, bo.getAmount());
        lqw.eq(bo.getPrice() != null, PayBaohuoProduct::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), PayBaohuoProduct::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getProcess()), PayBaohuoProduct::getProcess, bo.getProcess());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMaterial()), PayBaohuoProduct::getProductMaterial, bo.getProductMaterial());
        lqw.eq(StringUtils.isNotBlank(bo.getProductSpecification()), PayBaohuoProduct::getProductSpecification, bo.getProductSpecification());
        return lqw;
    }

    /**
     * 新增包活工单产品关联
     */
    @Override
    public Boolean insertByBo(PayBaohuoProductBo bo) {
        PayBaohuoProduct add = BeanUtil.toBean(bo, PayBaohuoProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改包活工单产品关联
     */
    @Override
    public Boolean updateByBo(PayBaohuoProductBo bo) {
        PayBaohuoProduct update = BeanUtil.toBean(bo, PayBaohuoProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除包活工单产品关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
