package com.jeethink.receive.service.impl;

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.bean.domain.ArchivesMaster;
import com.jeethink.receive.service.IArchivesApprovalService;
import com.jeethink.bean.vo.Approval;
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 java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * 人事档案接收Service业务层处理
 * 
 * @author sh
 * @date 2022-11-08
 */
@Service
public class ArchivesApprovalServiceImpl implements IArchivesApprovalService
{
    @Autowired
    private IArchivesService archivesService;
    @Autowired
    private IFlowService flowService;

    /**
     * 查询人事档案接收列表
     * 
     * @param archives 人事档案接收
     * @return 人事档案接收
     */
    @Override
    public List<ArchivesMaster> selectApprovalList(ArchivesMaster archives)
    {
        String userType = SecurityUtils.getLoginUser().getUser().getUserType();
        List<ArchivesMaster> list = new ArrayList<>();
        if (Constants.USER_TYPE_ZG.equals(userType)) {
            archives.setNowStatus(Constants.ARCHIVES_STATUS_RECEIVE_SUBMITED);
            list = archivesService.selectArchivesList(archives);
        } else if (Constants.USER_TYPE_BZ.equals(userType)) {
            archives.setNowStatus(Constants.ARCHIVES_STATUS_RECEIVE_APPROVE);
            list = archivesService.selectArchivesList(archives);
        }

        return list;
    }

    @Transient
    @Override
    public void approvalArchive(Approval approval) {
        String userType = SecurityUtils.getLoginUser().getUser().getUserType();
        if (!Constants.USER_TYPE_ZG.equals(userType) && !Constants.USER_TYPE_BZ.equals(userType)) {
            throw new CustomException("您没有审核权限");
        }
        flowService.updateFlow(approval.getObjectId(), Constants.FLOW_TYPE_RECEIVE, approval.getStatus(), approval.getRemark());
        if (Constants.APPROVE_PASS.equals(approval.getStatus())) {
            if (Constants.USER_TYPE_BZ.equals(userType)) {
                archivesService.changeNowStatus(approval.getObjectId(), Constants.ARCHIVES_STATUS_RECEIVE_WAIT);
            }else if (Constants.USER_TYPE_ZG.equals(userType)) {
                archivesService.changeNowStatus(approval.getObjectId(), Constants.ARCHIVES_STATUS_RECEIVE_APPROVE);
            }
        } else {
            archivesService.changeNowStatus(approval.getObjectId(), Constants.ARCHIVES_STATUS_RECEIVE_REJECT);
        }
    }

    @Override
    public ArchivesVO getArchives(String archivesId) {

        return archivesService.selectArchivesById(archivesId);
    }
}
