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.PayProductsBo;
import com.ruoyi.pay.domain.vo.PayProductsVo;
import com.ruoyi.pay.domain.PayProducts;
import com.ruoyi.pay.mapper.PayProductsMapper;
import com.ruoyi.pay.service.IPayProductsService;

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

/**
 * 产品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayProductsServiceImpl implements IPayProductsService {

    private final PayProductsMapper baseMapper;

    /**
     * 查询产品
     */
    @Override
    public PayProductsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品列表
     */
    @Override
    public TableDataInfo<PayProductsVo> queryPageList(PayProductsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayProducts> lqw = buildQueryWrapper(bo);
        Page<PayProductsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品列表
     */
    @Override
    public List<PayProductsVo> queryList(PayProductsBo bo) {
        LambdaQueryWrapper<PayProducts> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayProducts> buildQueryWrapper(PayProductsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayProducts> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayProducts::getTenantId, bo.getTenantId());
        lqw.eq(bo.getMaterialId() != null, PayProducts::getMaterialId, bo.getMaterialId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), PayProducts::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getNetMachineNo()), PayProducts::getNetMachineNo, bo.getNetMachineNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDrawingNum()), PayProducts::getDrawingNum, bo.getDrawingNum());
        lqw.eq(StringUtils.isNotBlank(bo.getProcess()), PayProducts::getProcess, bo.getProcess());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), PayProducts::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getDanwei()), PayProducts::getDanwei, bo.getDanwei());
        lqw.like(StringUtils.isNotBlank(bo.getOriginName()), PayProducts::getOriginName, bo.getOriginName());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginModel()), PayProducts::getOriginModel, bo.getOriginModel());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginSize()), PayProducts::getOriginSize, bo.getOriginSize());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginDanwei()), PayProducts::getOriginDanwei, bo.getOriginDanwei());
        lqw.eq(bo.getStorageTime() != null, PayProducts::getStorageTime, bo.getStorageTime());
        lqw.eq(bo.getMinPrice() != null, PayProducts::getMinPrice, bo.getMinPrice());
        lqw.eq(bo.getMaxPrice() != null, PayProducts::getMaxPrice, bo.getMaxPrice());
        lqw.eq(bo.getDeleted() != null, PayProducts::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增产品
     */
    @Override
    public Boolean insertByBo(PayProductsBo bo) {
        PayProducts add = BeanUtil.toBean(bo, PayProducts.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品
     */
    @Override
    public Boolean updateByBo(PayProductsBo bo) {
        PayProducts update = BeanUtil.toBean(bo, PayProducts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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