package com.gz.service.archive.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.common.DataGlobalVariable;
import com.gz.core.exception.BusinessException;
import com.gz.core.exception.CustomExceptionEnum;
import com.gz.dto.archive.ArchiveDTO;
import com.gz.dto.system.ArchivesTreeDTO;
import com.gz.mapper.archive.ArchiveMapper;
import com.gz.rvo.archive.ArchiveRVO;
import com.gz.rvo.borrow.ArchiveBorrowListRVO;
import com.gz.service.archive.ArchiveService;
import com.gz.service.system.ArchivesTreeService;
import com.gz.utils.JwtUtils;
import com.gz.vo.PageVO;
import com.gz.vo.archive.InsertArchiveVO;
import com.gz.vo.archive.SearchArchiveVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author LiuchangLan
 * @date 2021/2/23 18:12
 */
@Slf4j
@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Resource
    private ArchiveMapper mapper;

    @Resource
    private ArchivesTreeService archivesTreeService;

    @Override
    public Integer insert(InsertArchiveVO vo) throws Exception {
        vo.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        this.generateDh(vo);
        return mapper.insertSelective(vo);
    }

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

    @Override
    public Integer update(ArchiveDTO dto) {
        dto.setUpdated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public List<ArchiveDTO> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public PageInfo<ArchiveRVO> selectByPage(SearchArchiveVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        return new PageInfo<>(mapper.searchList(vo));
    }

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

    /**
     * @description 生成档号
     * @author LiuChangLan
     * @since 2021/2/24 17:11
     */
    private void generateDh(InsertArchiveVO vo) throws Exception {
        ArchivesTreeDTO archivesTreeDTO = archivesTreeService.selectParentByCode(vo.getMl());
        // 查询件号
        String jh = String.format("%05d", mapper.selectCurrAutoIncrement());
        switch (archivesTreeDTO.getCode()) {
            case "WS":
                vo.setDh(String.format(DataGlobalVariable.WS_DH_FOMAT, vo.getQzh(), vo.getGdnd(), vo.getBgqx(), jh));
                break;
//                return String.format(DataGlobalVariable.WS_DH_FOMAT, vo.getQzh(), vo.getGdnd(), vo.getBgqx(), jh);
            case "KU":
                vo.setDh(String.format(DataGlobalVariable.KU_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getGdnd(), vo.getHsdwdh(), vo.getBgqx(), jh));
                break;
//                return String.format(DataGlobalVariable.KU_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getGdnd(), vo.getHsdwdh(), vo.getBgqx(), jh);
            case "KJ":
                vo.setDh(String.format(DataGlobalVariable.KJ_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), vo.getXmdh(), jh));
                break;
//                return String.format(DataGlobalVariable.KJ_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), vo.getXmdh(), jh);
            case "ZY":
                vo.setDh(String.format(DataGlobalVariable.ZY_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), jh));
                break;
//                return String.format(DataGlobalVariable.ZY_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), jh);
            case "YX":
                // 段号
                String dh = String.format("%02d", 0);
                vo.setDh(String.format(DataGlobalVariable.YX_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), jh, dh));
                break;
//                return String.format(DataGlobalVariable.YX_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), jh, dh);
            case "SJ":
                vo.setDh(String.format(DataGlobalVariable.SJ_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getBgqx(), jh));
                break;
//                return String.format(DataGlobalVariable.SJ_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getBgqx(), jh);
            case "SW":
                vo.setDh(String.format(DataGlobalVariable.SW_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), jh));
                break;
//                return String.format(DataGlobalVariable.SW_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getMl(), vo.getBgqx(), jh);
            case "MT":
                vo.setDh(String.format(DataGlobalVariable.MT_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getBgqx(), jh));
                break;
//                return String.format(DataGlobalVariable.MT_DH_FOMAT, vo.getQzh(), vo.getMlh(), vo.getBgqx(), jh);
            default:
                throw new BusinessException(CustomExceptionEnum.GENERATE_DH_ERROR);
        }
    }


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