package com.qingyun.service.service.impl;

import com.qingyun.common.exception.ServiceException;
import com.qingyun.common.utils.StringUtils;
import com.qingyun.service.compare.service.ReviewTaskService;
import com.qingyun.service.mapper.ContractRuleTemplateMapper;
import lombok.extern.slf4j.Slf4j;

import com.qingyun.common.utils.MapstructUtils;
import com.qingyun.mybatisplus.page.TableDataInfo;
import com.qingyun.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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.qingyun.service.domain.bo.ContractReviewTaskBo;
import com.qingyun.service.domain.vo.ContractReviewTaskVo;
import com.qingyun.service.domain.ContractReviewTask;
import com.qingyun.service.mapper.ContractReviewTaskMapper;
import com.qingyun.service.service.IContractReviewTaskService;

import com.qingyun.mybatisplus.core.ServicePlusImpl;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 审查任务Service业务层处理
 *
 * @author jianlu
 * @date 2025-07-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
@SuppressWarnings("all")
public class ContractReviewTaskServiceImpl extends ServicePlusImpl<ContractReviewTaskMapper, ContractReviewTask, ContractReviewTaskVo> implements IContractReviewTaskService {

    private final ContractRuleTemplateMapper contractRuleTemplateMapper;

    private final ReviewTaskService contractReviewService;


    /**
     * 查询审查任务
     */
    @Override
    public ContractReviewTaskVo queryById(Long taskId){
        ContractReviewTaskVo vo = baseMapper.selectVoById(taskId);
        if (vo == null) {
            throw new ServiceException("审查任务不存在");
        }
        return vo;
    }

    /**
     * 查询审查任务列表
     */
    @Override
    public TableDataInfo<ContractReviewTaskVo> queryPageList(ContractReviewTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ContractReviewTask> lqw = buildQueryWrapper(bo);
        Page<ContractReviewTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询审查任务列表
     */
    @Override
    public List<ContractReviewTaskVo> queryList(ContractReviewTaskBo bo) {
        LambdaQueryWrapper<ContractReviewTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ContractReviewTask> buildQueryWrapper(ContractReviewTaskBo bo) {
        LambdaQueryWrapper<ContractReviewTask> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getContractName()), ContractReviewTask::getContractName, bo.getContractName());
        lqw.orderByDesc(ContractReviewTask::getCreateTime);
        return lqw;
    }

    /**
     * 新增审查任务
     */
    @Override
    public Boolean insertByBo(ContractReviewTaskBo bo) {
        ContractReviewTask add = MapstructUtils.convert(bo, ContractReviewTask.class);
        add.setUserId(1L);
        add.setDeptId(1L);
        add.setStatus(0);
        add.setCreateBy("admin");
        add.setCreateTime(new Date());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTaskId(add.getTaskId());
        }
        return flag;
    }

    /**
     * 修改审查任务
     */
    @Override
    public Boolean updateByBo(ContractReviewTaskBo bo) {
        ContractReviewTask update = MapstructUtils.convert(bo, ContractReviewTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Async
    @Override
    public void execute(ContractReviewTask contractReviewTask, Long modelId) {
        contractReviewService.execute(contractReviewTask, modelId);
    }
}
