package com.gz.service.borrow.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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.rvo.system.AdminRVO;
import com.gz.service.archive.ArchiveService;
import com.gz.service.borrow.ArchiveBorrowService;
import com.gz.service.system.AdminService;
import com.gz.utils.JwtUtils;
import com.gz.vo.PageVO;
import com.gz.vo.borrow.BorrowVO;
import com.gz.vo.borrow.SearchBorrowVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;

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

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

    @Resource
    private ArchiveBorrowMapper mapper;

    @Resource
    private AdminService adminService;

    @Override
    public Integer insert(ArchiveBorrowDTO dto) throws Exception {
        if ("".equals(dto.getEstimateReturnTime())) {
            dto.setEstimateReturnTime(null);
        }
        dto.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.insertSelective(dto);
    }

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

    @Override
    public Integer borrow(BorrowVO dto) throws Exception {
        ArchiveBorrowDTO selectBorrowParam = new ArchiveBorrowDTO();
        selectBorrowParam.setArchiveId(dto.getArchiveId());

        // 未选择用户 默认当前登录用户
        if (dto.getRecordType() == 0){
            // 登记
            dto.setAuditStatus(2);
        }else {
            // 借阅
            dto.setAdminId(JwtUtils.getCurrentUserJwtPayload().getId());
        }
        // 原件借阅需要判断是否已经被借阅
        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(dto.getAdminId());
        List<ArchiveBorrowDTO> currAdminborrowHistory = mapper.select(selectBorrowParam);
        for (ArchiveBorrowDTO archiveBorrowDTO : currAdminborrowHistory) {
            // 如果记录中包含该文档并且未审核完成 || 借阅未归 不给予借阅
            if (archiveBorrowDTO.getAuditStatus().equals(0) || archiveBorrowDTO.equals(2)) {
                throw new BusinessException(CustomExceptionEnum.REPEAT_BORROW);
            }
        }
//        if (StrUtil.isNotEmpty(dto.getAdminId())) {
//            AdminRVO adminRVO = adminService.selectByPrimaryKeyDept(dto.getAdminId());
//            dto.setName(adminRVO.getAdminName());
//            dto.setDept(adminRVO.getDeptName());
//            dto.setPhone(adminRVO.getPhone());
//            dto.setIdCard(adminRVO.getIdCard());
//            log.info("局内人员借阅，姓名：{}，部门：{}，手机号：{}，身份证号码：{}", dto.getName(), dto.getDept(), dto.getPhone(), dto.getIdCard());
//        }
        dto.setReturnStatus(0);
        return this.insert(dto);
    }

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

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

    @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);

    }


}
