package com.chy.borrow.service.impl;

import com.chy.borrow.entity.BorrowApply;
import com.chy.borrow.entity.BorrowApplyZjl0754;
import com.chy.borrow.entity.BorrowExamine;
import com.chy.borrow.entity.BorrowExamineZjl0754;
import com.chy.borrow.eum.BorrowStatusEnum;
import com.chy.borrow.eum.ExamineStatusEnum;
import com.chy.borrow.mapper.BorrowExamineMapper;
import com.chy.borrow.mapper.BorrowExamineZjl0754Mapper;
import com.chy.borrow.service.BorrowApplyService;
import com.chy.borrow.service.BorrowApplyZjl0754Service;
import com.chy.borrow.service.BorrowExamineZjl0754Service;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-03-25
 */
@Service
public class BorrowExamineZjl0754ServiceImpl extends ServiceImpl<BorrowExamineZjl0754Mapper, BorrowExamineZjl0754> implements BorrowExamineZjl0754Service {

    @Autowired
    BorrowExamineZjl0754Mapper borrowExamineZjl0754Mapper;

    @Autowired
    BorrowApplyZjl0754Service borrowApplyZjl0754Service;

    @Override
    public String check(BorrowExamineZjl0754 borrowExamineZjl0754, String userId) {
        try {
            if (borrowExamineZjl0754Mapper.hasChecked(borrowExamineZjl0754.getBorrowApplyId(),borrowExamineZjl0754.getExamineType())>0){
                return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("已经审核过了").toJSON();
            }
            borrowExamineZjl0754.setExamineTime(new Date());
            Integer borrowCode = ExamineStatusEnum.get(Integer.valueOf(borrowExamineZjl0754.getExamineStatus())).getStatusCode();
            Integer examineCode = Integer.valueOf(borrowExamineZjl0754.getExamineType());
            String borrowStatus = BorrowStatusEnum.get(2*(examineCode-1)+1+borrowCode)==null?null:BorrowStatusEnum.get(2*(examineCode-1)+1+borrowCode).getStatusCodeString();
            BorrowApplyZjl0754 borrowApply = new BorrowApplyZjl0754();
            borrowApply.setBorrowApplyId(borrowExamineZjl0754.getBorrowApplyId());
            borrowApply.setBorrowStatus(borrowStatus);
            borrowApplyZjl0754Service.updateById(borrowApply);
            borrowExamineZjl0754.setBorrowExamineId(new SnowflakeConfig().snowflakeStringId());
            save(borrowExamineZjl0754);
            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(BorrowExamineZjl0754 borrowExamineZjl0754, String userId) {
        List<Map> map = borrowExamineZjl0754Mapper.getCheckById(borrowExamineZjl0754.getBorrowApplyId(),borrowExamineZjl0754.getExamineType());
        List result = new ArrayList();
        if (map==null) {
            map = result;
        }
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(map).toJSON();
    }
}
