package com.chy.borrow.service.impl;

import com.chy.borrow.entity.BorrowApply;
import com.chy.borrow.entity.BorrowDownload;
import com.chy.borrow.entity.BorrowExamine;
import com.chy.borrow.eum.BorrowStatusEnum;
import com.chy.borrow.eum.ExamineStatusEnum;
import com.chy.borrow.mapper.BorrowExamineMapper;
import com.chy.borrow.service.BorrowApplyService;
import com.chy.borrow.service.BorrowDownloadService;
import com.chy.borrow.service.BorrowExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanghaigeography.Util.SnowflakeConfig;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-03-25
 */
@Service
public class BorrowExamineServiceImpl extends ServiceImpl<BorrowExamineMapper, BorrowExamine> implements BorrowExamineService {

    @Autowired
    BorrowExamineMapper borrowExamineMapper;

    @Autowired
    BorrowApplyService borrowApplyService;

    @Autowired
    BorrowDownloadService borrowDownloadService;

    @Override
    public String check(BorrowExamine borrowExamine,String userId) {
        try {
            if (borrowExamineMapper.hasChecked(borrowExamine.getBorrowApplyId(),borrowExamine.getExamineType())>0){
                return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("已经审核过了").toJSON();
            }
            Integer borrowCode = ExamineStatusEnum.get(Integer.valueOf(borrowExamine.getExamineStatus())).getStatusCode();
            Integer examineCode = Integer.valueOf(borrowExamine.getExamineType());
            String borrowStatus = BorrowStatusEnum.get(2*(examineCode-1)+1+borrowCode)==null?null:BorrowStatusEnum.get(2*(examineCode-1)+1+borrowCode).getStatusCodeString();
            BorrowApply borrowApply = new BorrowApply();
            borrowApply.setBorrowApplyId(borrowExamine.getBorrowApplyId());
            borrowApply.setBorrowStatus(borrowStatus);
            borrowApplyService.updateById(borrowApply);
            borrowExamine.setBorrowExamineId(new SnowflakeConfig().snowflakeStringId());
            save(borrowExamine);
            if((Integer.valueOf(borrowExamine.getExamineType())).equals(3)&&(Integer.valueOf(borrowExamine.getExamineStatus())).equals(1)){
                download(borrowExamine);
            }
            return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("审核成功").toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("审核失败").toJSON();
        }
    }

    @Override
    public String getCheckById(BorrowExamine borrowExamine, String userId) {
        List<Map> map = borrowExamineMapper.getCheckById(borrowExamine.getBorrowApplyId(),borrowExamine.getExamineType());
        List result = new ArrayList();
        if (map==null) {
            map = result;
        }
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(map).toJSON();
    }

    public void download(BorrowExamine borrowExamine){
        BorrowDownload borrowDownload = new BorrowDownload();
        borrowDownload.setBorrowApplyId(borrowExamine.getBorrowApplyId());
        borrowDownload.setDownloadType("1");
        borrowDownload.setDownloadFileType("zip");
        borrowDownload.setDownloadName("测试下载");
        borrowDownload.setDownloadStaus("1");
        borrowDownload.setDownloadUrl("http://47.101.207.123:8080/file/pkq.zip");
        borrowDownload.setId(new SnowflakeConfig().snowflakeStringId()+"2");
        borrowDownloadService.save(borrowDownload);
        BorrowDownload borrowDownload1 = new BorrowDownload();
        borrowDownload1.setBorrowApplyId(borrowExamine.getBorrowApplyId());
        borrowDownload1.setDownloadType("2");
        borrowDownload1.setDownloadName("测试获取");
        borrowDownload1.setDownloadStaus("1");
        borrowDownload1.setId(new SnowflakeConfig().snowflakeStringId()+"1");
        borrowDownloadService.save(borrowDownload1);
    }
}
