package com.jeethink.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.jeethink.bean.domain.ArchivesMaster;
import com.jeethink.bean.vo.Approval;
import com.jeethink.receive.mapper.ArchivesMapper;
import com.jeethink.common.constant.Constants;
import com.jeethink.common.exception.CustomException;
import com.jeethink.common.utils.SecurityUtils;
import com.jeethink.flow.service.IFlowService;
import com.jeethink.receive.service.IArchivesService;
import com.jeethink.receive.vo.ArchivesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.manage.service.IDestroyService;

/**
 * 销毁清册Service业务层处理
 * 
 * @author zjt
 * @date 2022-11-10
 */
@Service
public class DestroyServiceImpl implements IDestroyService
{
    @Autowired
    private IArchivesService archivesService;
    
    @Autowired
    private IFlowService flowService;


    /**
     * 查询销毁清册列表
     * 
     * @param archive 销毁清册
     * @return 销毁清册
     */
    @Override
    public List<ArchivesMaster> selectDestroyList(ArchivesMaster archive)
    {
        String userType = SecurityUtils.getLoginUser().getUser().getUserType();
        List<ArchivesMaster> list = new ArrayList<>();
        if (Constants.USER_TYPE_ZG.equals(userType)) {
            archive.setNowStatus(Constants.ARCHIVES_STATUS_DESTROY_APPLY);
            list = archivesService.selectArchivesList(archive);
        } else if (Constants.USER_TYPE_BZ.equals(userType)) {
            archive.setNowStatus(Constants.ARCHIVES_STATUS_DESTROY_APPROVE);
            list = archivesService.selectArchivesList(archive);
        } else {
            archive.setStage(Constants.ARCHIVES_STAGE_DESTROY);
            list = archivesService.selectArchivesList(archive);
        }
        return list;
    }

    @Override
    public void applyDestroy(String archivesId) {

        ArchivesMaster archives = archivesService.selectById(archivesId);
        if (!Constants.ARCHIVES_STATUS_RK.equals(archives.getNowStatus())
                && !Constants.ARCHIVES_STATUS_DESTROY_REJECT.equals(archives.getNowStatus())) {
            throw new CustomException("该档案当前不能申请销毁");
        }
        if (Constants.ARCHIVES_STATUS_DESTROY_REJECT.equals(archives.getNowStatus())) {
            //审批未通过，再次提交申请时，需要将上次的审批详情废弃
            flowService.destroyFlow(archivesId, Constants.FLOW_TYPE_DESTROY);
        }
        archivesService.changeNowStatus(archivesId, Constants.ARCHIVES_STATUS_DESTROY_APPLY);
        flowService.insertFlow(archivesId, Constants.FLOW_TYPE_DESTROY);
    }

    @Override
    public void approvalDestroy(Approval approval) {
        String userType = SecurityUtils.getLoginUser().getUser().getUserType();
        if (!Constants.USER_TYPE_ZG.equals(userType) && !Constants.USER_TYPE_BZ.equals(userType)) {
            throw new CustomException("您没有审核权限");
        }
        String archivesId = approval.getObjectId();
        if (Constants.APPROVE_PASS.equals(approval.getStatus())) {
            String nowStatus = Constants.USER_TYPE_BZ.equals(userType)? Constants.ARCHIVES_STATUS_DESTROY_WAIT: Constants.ARCHIVES_STATUS_DESTROY_APPROVE;
            archivesService.changeNowStatus(archivesId, nowStatus);
        } else {
            archivesService.changeNowStatus(archivesId, Constants.ARCHIVES_STATUS_DESTROY_REJECT);
        }
        flowService.updateFlow(archivesId, Constants.FLOW_TYPE_DESTROY, approval.getStatus(), approval.getRemark());
    }

    @Override
    public ArchivesVO seelectArchivesById(String archivesId) {

        return archivesService.selectArchivesById(archivesId);
    }

    @Override
    public void revocationDestroy(String archivesId) {

        ArchivesMaster master = archivesService.selectById(archivesId);
        if (!Constants.ARCHIVES_STATUS_DESTROY_APPLY.equals(master.getNowStatus())
            && !Constants.ARCHIVES_STATUS_DESTROY_REJECT.equals(master.getNowStatus())) {
            throw new CustomException("该档案当前不能撤销注销申请");
        }
        archivesService.changeNowStatus(archivesId, Constants.ARCHIVES_STATUS_RK);
        flowService.destroyFlow(archivesId, Constants.FLOW_TYPE_DESTROY);
    }
}
