package com.jeethink.using.service.impl;

import com.jeethink.bean.vo.Approval;
import com.jeethink.common.constant.Constants;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.exception.CustomException;
import com.jeethink.common.utils.SecurityUtils;
import com.jeethink.flow.service.IFlowService;
import com.jeethink.using.domain.ArchivesBorrow;
import com.jeethink.using.mapper.BorrowMapper;
import com.jeethink.using.service.IBorrowApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class BorrowApprovalServiceImpl implements IBorrowApprovalService {

    @Autowired
    private BorrowMapper mapper;
    @Autowired
    private IFlowService flowService;

    @Override
    public List<ArchivesBorrow> selectApprovalList(ArchivesBorrow borrow) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        borrow.setCreateBy(user.getUserName());
        List<ArchivesBorrow> list = new ArrayList<>();
        List statusList = new ArrayList();
        if (Constants.USER_TYPE_ZG.equals(user.getUserType())) {
            statusList.add(Constants.BORROW_STATUS_APPLY);
            statusList.add(Constants.BORROW_STATUS_BACK);
            list = mapper.selectStatusBorrowList(borrow, statusList);
        }else if (Constants.USER_TYPE_BZ.equals(user.getUserType())) {
            statusList.add(Constants.BORROW_STATUS_APPROVE);
            list = mapper.selectStatusBorrowList(borrow, statusList);
        }
        return list;
    }

    @Override
    public void checkApproval(Approval approval) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!Constants.USER_TYPE_ZG.equals(user.getUserType())
            && !Constants.USER_TYPE_BZ.equals(user.getUserType())) {
            throw new CustomException("您没有审批权限");
        }

        String borrowId = approval.getObjectId();
        ArchivesBorrow borrow = mapper.selectById(borrowId);
        if (Constants.USER_TYPE_ZG.equals(user.getUserType())) {//主管：借阅审批1与归还审批
            if (Constants.BORROW_STATUS_APPLY.equals(borrow.getBorrowStatus())) {
                if(Constants.APPROVE_PASS.equals(approval.getStatus())) {
                    mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_APPROVE);
                }else {
                    mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_REJECT);
                }
                flowService.updateFlow(borrowId, Constants.FLOW_TYPE_BORROW, approval.getStatus(), approval.getRemark());
            }else if (Constants.BORROW_STATUS_BACK.equals(borrow.getBorrowStatus())) {
                if(Constants.APPROVE_PASS.equals(approval.getStatus())) {
                    mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_FINISH);
                }else {
                    mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_BACK_REJECT);
                }
                flowService.updateFlow(borrowId, Constants.FLOW_TYPE_BACK, approval.getStatus(), approval.getRemark());
            } else {
                throw new CustomException("该档案目前无需审批");
            }
        }else if (Constants.USER_TYPE_BZ.equals(user.getUserType())) {//部长：借阅审批2
            if (Constants.BORROW_STATUS_APPROVE.equals(borrow.getBorrowStatus())) {
                if(Constants.APPROVE_PASS.equals(approval.getStatus())) {
                    mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_VIEW);
                }else {
                    mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_REJECT);
                }
                flowService.updateFlow(borrowId, Constants.FLOW_TYPE_BORROW, approval.getStatus(), approval.getRemark());
            }else {
                throw new CustomException("该档案目前无需审批");
            }
        }
    }
}
