package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import net.ferrum.business.domain.QsSpecAttributeTypes;
import net.ferrum.common.utils.DateUtils;
import net.ferrum.common.utils.StringUtils;
import net.ferrum.common.core.page.TableDataInfo;
import net.ferrum.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 net.ferrum.system.service.ISysConfigService;
import org.springframework.stereotype.Service;
import net.ferrum.business.domain.bo.QsBatchBo;
import net.ferrum.business.domain.vo.QsBatchVo;
import net.ferrum.business.domain.QsBatch;
import net.ferrum.business.mapper.QsBatchMapper;
import net.ferrum.business.service.IQsBatchService;

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

/**
 * 批次管理Service业务层处理
 *
 * @author tiezx
 * @date 2025-07-18
 */
@RequiredArgsConstructor
@Service
public class QsBatchServiceImpl implements IQsBatchService {

    private final QsBatchMapper baseMapper;
    private final ISysConfigService configService;

    /**
     * 查询批次管理
     */
    @Override
    public QsBatchVo queryById(Long batchId){
        return baseMapper.selectVoById(batchId);
    }

    /**
     * 查询批次管理列表
     */
    @Override
    public TableDataInfo<QsBatchVo> queryPageList(QsBatchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsBatch> lqw = buildQueryWrapper(bo);
        Page<QsBatchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询批次管理列表
     */
    @Override
    public List<QsBatchVo> queryList(QsBatchBo bo) {
        LambdaQueryWrapper<QsBatch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsBatch> buildQueryWrapper(QsBatchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsBatch> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), QsBatch::getBatchNo, bo.getBatchNo());
        lqw.like(StringUtils.isNotBlank(bo.getBatchName()), QsBatch::getBatchName, bo.getBatchName());
        lqw.between(params.get("beginBatchStartDate") != null && params.get("endBatchStartDate") != null,
            QsBatch::getBatchStartDate ,params.get("beginBatchStartDate"), params.get("endBatchStartDate"));
        lqw.between(params.get("beginBatchPlanEndDate") != null && params.get("endBatchPlanEndDate") != null,
            QsBatch::getBatchPlanEndDate ,params.get("beginBatchPlanEndDate"), params.get("endBatchPlanEndDate"));
        lqw.between(params.get("beginBatchEndDate") != null && params.get("endBatchEndDate") != null,
            QsBatch::getBatchEndDate ,params.get("beginBatchEndDate"), params.get("endBatchEndDate"));
        return lqw;
    }

    /**
     * 新增批次管理
     */
    @Override
    public Boolean insertByBo(QsBatchBo bo) {
        QsBatch add = BeanUtil.toBean(bo, QsBatch.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBatchId(add.getBatchId());
        }
        return flag;
    }

    /**
     * 修改批次管理
     */
    @Override
    public Boolean updateByBo(QsBatchBo bo) {
        QsBatch update = BeanUtil.toBean(bo, QsBatch.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 查询批次
     *
     * @param entity 要查询的规格参数类型批次参数
     * @return 批次信息集合
     */
    @Override
    public List<QsBatch> selectBatchList(QsBatch entity) {
        LambdaQueryWrapper<QsBatch> lqw = new LambdaQueryWrapper<>();
        Map<String, Object> params = entity.getParams();
        lqw.eq(QsBatch::getDelFlag, "0")
            .eq(ObjectUtil.isNotNull(entity.getBatchId()), QsBatch::getBatchId, entity.getBatchId())
            .like(StringUtils.isNotBlank(entity.getBatchName()), QsBatch::getBatchName, entity.getBatchName() );

        if(params.get("CurrentDate") != null){
            String sysConfig = configService.selectConfigByKey("work.create.queryBatchStartDateRange");
            Integer month = -1 * Integer.parseInt(sysConfig);
            Date d = DateUtils.parseDate(params.get("CurrentDate"));
            Date d1 = DateUtils.addMonths(d,month);
            lqw.between(ObjectUtil.isNotNull(entity.getBatchStartDate()), QsBatch::getBatchStartDate ,d1, d);
        }

        return baseMapper.selectBatchList(lqw);
    }
}
