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.PayWorkOrderTuzhiBo;
import com.ruoyi.pay.domain.vo.PayWorkOrderTuzhiVo;
import com.ruoyi.pay.domain.PayWorkOrderTuzhi;
import com.ruoyi.pay.mapper.PayWorkOrderTuzhiMapper;
import com.ruoyi.pay.service.IPayWorkOrderTuzhiService;

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

/**
 * 图纸流程卡工单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayWorkOrderTuzhiServiceImpl implements IPayWorkOrderTuzhiService {

    private final PayWorkOrderTuzhiMapper baseMapper;

    /**
     * 查询图纸流程卡工单
     */
    @Override
    public PayWorkOrderTuzhiVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询图纸流程卡工单列表
     */
    @Override
    public TableDataInfo<PayWorkOrderTuzhiVo> queryPageList(PayWorkOrderTuzhiBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayWorkOrderTuzhi> lqw = buildQueryWrapper(bo);
        Page<PayWorkOrderTuzhiVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询图纸流程卡工单列表
     */
    @Override
    public List<PayWorkOrderTuzhiVo> queryList(PayWorkOrderTuzhiBo bo) {
        LambdaQueryWrapper<PayWorkOrderTuzhi> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayWorkOrderTuzhi> buildQueryWrapper(PayWorkOrderTuzhiBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayWorkOrderTuzhi> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayWorkOrderTuzhi::getTenantId, bo.getTenantId());
        lqw.eq(StringUtils.isNotBlank(bo.getArchiveId()), PayWorkOrderTuzhi::getArchiveId, bo.getArchiveId());
        lqw.eq(bo.getProductId() != null, PayWorkOrderTuzhi::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getMachineId()), PayWorkOrderTuzhi::getMachineId, bo.getMachineId());
        lqw.eq(bo.getDeptId() != null, PayWorkOrderTuzhi::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkshop()), PayWorkOrderTuzhi::getWorkshop, bo.getWorkshop());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PayWorkOrderTuzhi::getName, bo.getName());
        lqw.eq(bo.getProductNum() != null, PayWorkOrderTuzhi::getProductNum, bo.getProductNum());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMaterial()), PayWorkOrderTuzhi::getProductMaterial, bo.getProductMaterial());
        lqw.eq(StringUtils.isNotBlank(bo.getProductSpecification()), PayWorkOrderTuzhi::getProductSpecification, bo.getProductSpecification());
        lqw.eq(bo.getMaterialNum() != null, PayWorkOrderTuzhi::getMaterialNum, bo.getMaterialNum());
        lqw.eq(StringUtils.isNotBlank(bo.getXialiao()), PayWorkOrderTuzhi::getXialiao, bo.getXialiao());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessingType()), PayWorkOrderTuzhi::getProcessingType, bo.getProcessingType());
        lqw.eq(bo.getPrice() != null, PayWorkOrderTuzhi::getPrice, bo.getPrice());
        lqw.eq(bo.getBeginTime() != null, PayWorkOrderTuzhi::getBeginTime, bo.getBeginTime());
        lqw.eq(bo.getEndTime() != null, PayWorkOrderTuzhi::getEndTime, bo.getEndTime());
        lqw.like(StringUtils.isNotBlank(bo.getQualityName()), PayWorkOrderTuzhi::getQualityName, bo.getQualityName());
        lqw.eq(bo.getInstorageTime() != null, PayWorkOrderTuzhi::getInstorageTime, bo.getInstorageTime());
        lqw.like(StringUtils.isNotBlank(bo.getInstorageName()), PayWorkOrderTuzhi::getInstorageName, bo.getInstorageName());
        lqw.eq(bo.getInstorageNum() != null, PayWorkOrderTuzhi::getInstorageNum, bo.getInstorageNum());
        lqw.eq(bo.getDeleted() != null, PayWorkOrderTuzhi::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增图纸流程卡工单
     */
    @Override
    public Boolean insertByBo(PayWorkOrderTuzhiBo bo) {
        PayWorkOrderTuzhi add = BeanUtil.toBean(bo, PayWorkOrderTuzhi.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改图纸流程卡工单
     */
    @Override
    public Boolean updateByBo(PayWorkOrderTuzhiBo bo) {
        PayWorkOrderTuzhi update = BeanUtil.toBean(bo, PayWorkOrderTuzhi.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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