package com.quantgroup.asset.distribution.service.funding.impl;

import com.quantgroup.asset.distribution.enums.funding.FundingResult;
import com.quantgroup.asset.distribution.service.funding.IAidFundRouteRecordService;
import com.quantgroup.asset.distribution.service.jpa.entity.AidLoanFundRouteRecord;
import com.quantgroup.asset.distribution.service.jpa.repository.IAidLoanFundRouteRecordRepository;
import com.quantgroup.asset.distribution.service.redis.IRedisService;
import com.quantgroup.asset.distribution.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.quantgroup.asset.distribution.constant.RedisKeyConstants.AID_LOAN_COUNT_LIMIT_KEY;
import static com.quantgroup.asset.distribution.constant.RedisKeyConstants.FINISH_ROUTE_AID_FUND_KEY;

/**
 * Created by renfeng on 2019/7/19.
 */
@Service
@Slf4j
public class AidFundRouteRecordServiceImpl implements IAidFundRouteRecordService {




    @Autowired
    private IAidLoanFundRouteRecordRepository iAidLoanFundRouteRecordRepository;
    @Autowired
    private IRedisService<AidLoanFundRouteRecord> redisService;

    /**
     * 资金分配结果通知
     *
     * @param bizNo
     * @param fundingResult
     */
    @Override
    public void fundingResultNotity(String bizNo, FundingResult fundingResult) {

        //查询未完成的助贷资金路由 且唯一
        AidLoanFundRouteRecord aidLoanFundRouteRecord = iAidLoanFundRouteRecordRepository.findByBizNoAndAidFundRouteStatus(bizNo, 1);
        //更新助贷资金路由状态 2-成功 3-失败 1-未完成
        if(aidLoanFundRouteRecord!=null){
            aidLoanFundRouteRecord.setAidFundRouteStatus(fundingResult==FundingResult.FUAD_ASSIGN_SUCC?2:3);
            iAidLoanFundRouteRecordRepository.save(aidLoanFundRouteRecord);

            //助贷资金分配成功 累加一次  限制每天分配次数
            if(aidLoanFundRouteRecord.getAidFundRouteStatus()==2)
                redisService.setIncr(AID_LOAN_COUNT_LIMIT_KEY + DateUtil.getDay(), 1, 3, TimeUnit.DAYS);
        }
        log.info("更新助贷资金路由状态结束，bizNo : {} , fundingResult : {} ,是否命中 : {} ",bizNo,fundingResult,aidLoanFundRouteRecord!=null);
    }

    /**
     * 保存主贷资金路由记录
     *
     * @param aidLoanFundRouteRecord
     */
    @Override
    public AidLoanFundRouteRecord saveAidLoanFundRouteRecord(AidLoanFundRouteRecord aidLoanFundRouteRecord) {
        AidLoanFundRouteRecord record = iAidLoanFundRouteRecordRepository.save(aidLoanFundRouteRecord);
        redisService.setEntityEx(FINISH_ROUTE_AID_FUND_KEY + record.getBizNo() + "_" + record.getFundId(),
                record, 30, TimeUnit.MINUTES);
        return record;
    }

    @Override
    public AidLoanFundRouteRecord findByBizNoAndFundId(String bizNo, String fundId) {
        String key = FINISH_ROUTE_AID_FUND_KEY + bizNo + "_" + fundId;
        AidLoanFundRouteRecord record = redisService.getEntity(key);
        if (record == null) {
            record = iAidLoanFundRouteRecordRepository.findByBizNoAndFundId(bizNo, fundId);
            if (record != null) {
                redisService.setEntityEx(key, record, 30, TimeUnit.MINUTES);
            }
        }
        return record;
    }

    @Override
    public AidLoanFundRouteRecord updateAidLoanFundRouteRecordStatus(AidLoanFundRouteRecord aidLoanFundRouteRecord, Integer status) {
        aidLoanFundRouteRecord.setAidFundRouteStatus(status);
        return saveAidLoanFundRouteRecord(aidLoanFundRouteRecord);
    }
}
