package com.jeethink.using.service.impl;

import com.jeethink.bean.domain.ArchivesMaster;
import com.jeethink.common.constant.Constants;
import com.jeethink.common.exception.CustomException;
import com.jeethink.common.utils.SecurityUtils;
import com.jeethink.common.utils.uuid.IdUtils;
import com.jeethink.flow.domain.FlowRecord;
import com.jeethink.flow.service.IFlowService;
import com.jeethink.using.domain.ArchivesBorrow;
import com.jeethink.using.mapper.BorrowMapper;
import com.jeethink.using.service.IBorrowService;
import com.jeethink.using.vo.BorrowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class BorrowServiceImpl implements IBorrowService {

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

    @Override
    public List<ArchivesMaster> selectArchivesList(ArchivesMaster archives) {

        archives.setCreateBy(SecurityUtils.getUsername());
        return mapper.selectArchivesList(archives);
    }

    @Override
    public void applyBorrow(ArchivesBorrow borrow) {
        borrow.setCreateBy(SecurityUtils.getUsername());
        List<ArchivesBorrow> list = mapper.selectBorrowingList(borrow);
        if (list.size() > 0) {
            throw new CustomException("你已借阅该档案");
        }
        String borrowId = IdUtils.simpleUUID();
        borrow.setBorrowId(borrowId);
        borrow.setBorrowStatus(Constants.BORROW_STATUS_APPLY);
        mapper.insertBorrow(borrow);
        flowService.insertFlow(borrowId, Constants.FLOW_TYPE_BORROW);
    }

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

        String userName = SecurityUtils.getUsername();
        borrow.setCreateBy(userName);
        return mapper.selectMyBorrowList(borrow);
    }

    @Override
    public void revocationBorrow(String borrowId) {

        ArchivesBorrow borrow = mapper.selectById(borrowId);
        if (!Constants.BORROW_STATUS_APPLY.equals(borrow.getBorrowStatus())) {
            throw new CustomException("该借阅申请已无法撤销");
        }
        mapper.removeBorrow(borrowId);
        flowService.destroyFlow(borrowId, Constants.FLOW_TYPE_BORROW);
    }

    @Override
    public BorrowVO selectById(String borrowId) {

        ArchivesBorrow borrow = mapper.selectById(borrowId);
        List<FlowRecord> records = new ArrayList<>();
        if (Integer.parseInt(Constants.BORROW_STATUS_VIEW) >= Integer.parseInt(borrow.getBorrowStatus())) {
            records = flowService.selectRecord(borrowId, Constants.FLOW_TYPE_BORROW);
        }
        if (Integer.parseInt(Constants.BORROW_STATUS_VIEW) < Integer.parseInt(borrow.getBorrowStatus())) {
            records = flowService.selectRecord(borrowId, Constants.FLOW_TYPE_BACK);
        }
        BorrowVO vo = new BorrowVO(borrow);
        vo.setFlowRecords(records);
        return vo;
    }

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

        return mapper.selectBorrowHistory(borrow);
    }

    @Override
    public void reback(String borrowId) {

        ArchivesBorrow borrow = mapper.selectById(borrowId);
        if (!Constants.BORROW_STATUS_VIEW.equals(borrow.getBorrowStatus())
                && !Constants.BORROW_STATUS_BACK_REJECT.equals(borrow.getBorrowStatus())) {
            throw new CustomException("该档案当前状态无法归还");
        }
        if (Constants.BORROW_STATUS_BACK_REJECT.equals(borrow.getBorrowStatus())) {
            flowService.destroyFlow(borrowId, Constants.FLOW_TYPE_BACK);
        }
        mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_BACK);
        flowService.insertFlow(borrowId, Constants.FLOW_TYPE_BACK);
    }

    @Override
    public void cancelReback(String borrowId) {

        ArchivesBorrow borrow = mapper.selectById(borrowId);
        if (!Constants.BORROW_STATUS_BACK_REJECT.equals(borrow.getBorrowStatus())) {
            throw new CustomException("当前状态无法撤销归还");
        }
        mapper.changeBorrowStatus(borrowId, Constants.BORROW_STATUS_VIEW);
        flowService.destroyFlow(borrowId, Constants.FLOW_TYPE_BACK);
    }
}
