package com.gz.service.borrow.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.core.exception.BusinessException;
import com.gz.core.exception.CustomExceptionEnum;
import com.gz.dto.borrow.ArchiveBorrowDTO;
import com.gz.mapper.archive.ArchiveMapper;
import com.gz.mapper.borrow.ArchiveBorrowMapper;
import com.gz.rvo.borrow.ArchiveBorrowHistoryRVO;
import com.gz.rvo.borrow.AuditInfoRVO;
import com.gz.service.archive.ArchiveService;
import com.gz.service.borrow.ArchiveBorrowService;
import com.gz.utils.JwtUtils;
import com.gz.vo.PageVO;
import lombok.Data;
import org.springframework.stereotype.Service;

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

/**
 * @author LiuchangLan
 * @date 2021/2/27 14:52
 */
@Service
public class ArchiveBorrowServiceImpl implements ArchiveBorrowService {

    @Resource
    private ArchiveBorrowMapper mapper;

    @Override
    public Integer insert(ArchiveBorrowDTO dto) throws Exception {
        dto.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.insertSelective(dto);
    }

    @Override
    public ArchiveBorrowDTO selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer borrow(ArchiveBorrowDTO dto) throws Exception {
        ArchiveBorrowDTO selectBorrowParam = new ArchiveBorrowDTO();
        selectBorrowParam.setArchiveId(dto.getArchiveId());
        // 原件借阅需要判断是否已经被借阅
        if (dto.getBorrowType().equals(1)) {
            // 该文档的所有借阅记录
            List<ArchiveBorrowDTO> borrowHistory = mapper.select(selectBorrowParam);
            for (ArchiveBorrowDTO archiveBorrowDTO : borrowHistory) {
                // 该文档借阅方式为原件借阅 && 借阅出去未归还
                if (archiveBorrowDTO.getBorrowType().equals(1) && new Integer(0).equals(archiveBorrowDTO.getReturnStatus()) && archiveBorrowDTO.getAuditStatus().equals(2)) {
                    throw new BusinessException(CustomExceptionEnum.ORIGINAL_SCRIPT_NO_EXISTS);
                }
            }
        }
        // 查询当前用户 该文档的借阅记录
        selectBorrowParam.setAdminId(JwtUtils.getCurrentUserJwtPayload().getId());
        List<ArchiveBorrowDTO> currAdminborrowHistory = mapper.select(selectBorrowParam);
        for (ArchiveBorrowDTO archiveBorrowDTO : currAdminborrowHistory) {
            // 如果记录中包含该文档并且未审核完成 || 借阅未归 不给予借阅
            if (archiveBorrowDTO.getAuditStatus().equals(0) || archiveBorrowDTO.equals(2)) {
                throw new BusinessException(CustomExceptionEnum.REPEAT_BORROW);
            }
        }
        dto.setReturnStatus(0);
        // 插入借阅记录
        dto.setAdminId(JwtUtils.getCurrentUserJwtPayload().getId());
        return this.insert(dto);
    }

    @Override
    public PageInfo<ArchiveBorrowHistoryRVO> selectCurrAdminArchiveBorrowHistory(PageVO vo) {
        Integer id = JwtUtils.getCurrentUserJwtPayload().getId();
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        return new PageInfo<>(mapper.selectArchiveBorrowHistory(id));
    }

    @Override
    public PageInfo<ArchiveBorrowHistoryRVO> selectArchiveBorrowHistory(PageVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        return new PageInfo<>(mapper.selectArchiveBorrowHistory(null));
    }

    @Override
    public void audit(ArchiveBorrowDTO dto) {
        mapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public Integer delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public AuditInfoRVO selectBorrowInfo(Integer id) {
        return mapper.selectBorrowInfo(id);
    }

    @Override
    public void returnBorrow(Integer id) {
        ArchiveBorrowDTO archiveBorrowDTO = mapper.selectByPrimaryKey(id);
        archiveBorrowDTO.setReturnTime(DateUtil.now());
        archiveBorrowDTO.setReturnStatus(1);
        mapper.updateByPrimaryKeySelective(archiveBorrowDTO);

    }
}
