package cn.ezhizao.project.business.order.service.impl;

import cn.ezhizao.common.exception.ServiceException;
import cn.ezhizao.common.utils.uuid.SnowflakeIdWorker;
import cn.ezhizao.project.business.order.domain.BizArchiveInput;
import cn.ezhizao.project.business.order.domain.BizArchiveInputDetail;
import cn.ezhizao.project.business.order.domain.BizArchiveInputDetailProcess;
import cn.ezhizao.project.business.order.mapper.BizArchiveInputMapper;
import cn.ezhizao.project.business.order.service.IBizArchiveInputDetailProcessService;
import cn.ezhizao.project.business.order.service.IBizArchiveInputDetailService;
import cn.ezhizao.project.business.order.service.IBizArchiveInputService;
import cn.ezhizao.project.system.service.ISysFileStorageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 档案入库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-16
 */
@Service
public class BizArchiveInputServiceImpl extends ServiceImpl<BizArchiveInputMapper, BizArchiveInput> implements IBizArchiveInputService {
    @Resource
    private BizArchiveInputMapper bizArchiveInputMapper;
    @Resource
    private IBizArchiveInputDetailService archiveInputDetailService;
    @Resource
    private IBizArchiveInputDetailProcessService archiveInputDetailProcessService;
    @Resource
    private ISysFileStorageService fileStorageService;
    @Resource
    SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 查询档案入库列表
     *
     * @param bizArchiveInput 档案入库
     * @return 档案入库
     */
    @Override
    public List<BizArchiveInput> getList(BizArchiveInput bizArchiveInput) {
        return bizArchiveInputMapper.getList(bizArchiveInput);
    }

    /**
     * 物理删除
     *
     * @param bizArchiveInput
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizArchiveInput bizArchiveInput) {
        return bizArchiveInputMapper.physicalDelete(bizArchiveInput);
    }
    @Override
    public boolean removeBatchByIds(List<Long> ids) {
        // 判断是否已审核
        if (this.query().in("id", ids).eq("verify_status", 1).count() > 0) {
            throw new ServiceException("已审核合同不能删除。");
        } else {
            // 未审核的删掉 合同信息和对应明细信息
            super.removeBatchByIds(ids);
            QueryWrapper<BizArchiveInputDetail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.in("contract_id", ids);
            archiveInputDetailService.remove(detailQueryWrapper);
            QueryWrapper<BizArchiveInputDetailProcess> processQueryWrapper = new QueryWrapper<>();
            processQueryWrapper.in("contract_id", ids);
            archiveInputDetailProcessService.remove(processQueryWrapper);
        }
        return true;
    }





}
