package com.brilliance.isc.common.api;

import com.brilliance.isc.common.api.lim.service.LimApiService;
import com.brilliance.isc.common.api.lim.vo.LimRequest;
import com.brilliance.isc.common.api.lim.vo.LimResponse;
import com.brilliance.isc.common.api.lim.vo.YapinRequest;
import com.brilliance.isc.common.api.lim.vo.YapinResponse;
import com.brilliance.isc.common.converter.DtoMapper;
import com.brilliance.isc.common.limmod.service.LimOptionService;
import com.brilliance.isc.common.limmod.service.LimmodService;
import com.brilliance.isc.common.limmod.service.YpnmodService;
import com.brilliance.isc.common.limmod.vo.LimmodVo;
import com.brilliance.isc.common.limmod.vo.LimptsVo;
import com.brilliance.isc.common.limmod.vo.YpnmodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LimApiServiceImpl implements LimApiService {


    @Resource(name = "limquery")
    private LimOptionService limqueryOptionService;
    @Resource(name = "limprecal")
    private LimOptionService limprecalOptionService;
    @Resource(name = "limZsq")
    private LimOptionService limZsqOptionService;
    @Autowired
    private LimmodService limmodService;
    @Autowired
    private YpnmodService ypnmodService;

    /**
     * 额度查询
     *
     * @param limRequest
     * @return
     */
    @Override
    public LimResponse limQuery(LimRequest limRequest) {
        limRequest.reset();
        LimmodVo limmodVo = limRequest.getLimmodVo();
        limqueryOptionService.assertLim(limmodVo);
        return LimResponse.builder()
                .limgrdVoList(limmodVo.getLimQueryVo().getLimgrdVoList())
                .lmegrdVos(limmodVo.getLimQueryVo().getLmegrdVos())
                .limpts(limmodVo.getLimpts())
                .ownref(limmodVo.getOwnref())
                .ccvamt(limmodVo.getCcvamt())
                .comamt(limmodVo.getComamt())
                .rw02ConfirmInfos(limmodVo.getRw02ConfirmInfos())
                .rw02Rmatch(limmodVo.getRw02Rmatch())
                .build();
    }

    /**
     * 额度试算
     *
     * @param limRequest
     * @return
     */
    @Override
    public LimResponse limPreCal(LimRequest limRequest) {
        limRequest.reset();
        LimmodVo limmodVo = limRequest.getLimmodVo();
        limprecalOptionService.assertLim(limmodVo);


        return LimResponse.builder().
                limgrdVoList(limmodVo.getLimQueryVo().getLimgrdVoList()).
                lmegrdVos(limmodVo.getLimQueryVo().getLmegrdVos())
                .clmcbbVos(limmodVo.getLimPreCalVo().getClmcbbVos())
                .clmcbeVos(limmodVo.getLimPreCalVo().getClmcbeVos())
                .clmptyVos(limmodVo.getLimPreCalVo().getClmptyVos())
                .tygrd(limmodVo.getLimPreCalVo().getTygrd())
                .dggrd(limmodVo.getLimPreCalVo().getDggrd())
                .limpts(limmodVo.getLimpts())
                .ownref(limmodVo.getOwnref())
                .comamt(limmodVo.getComamt())
                .ccvamt(limmodVo.getCcvamt()).build();
    }

    /**
     * 额度界面初始化
     *
     * @param limRequest
     * @return
     */
    @Override
    public LimResponse limInit(LimRequest limRequest) {
        limRequest.reset();
        LimmodVo limmodVo = limRequest.getLimmodVo();
        LimptsVo limptsVo = limmodVo.getLimpts();
        limmodService.LimRegParstm(limmodVo);
        limmodService.clearLimgrd(limmodVo);
        return LimResponse.builder().limgrdVoList(limmodVo.getLimQueryVo().getLimgrdVoList()).lmegrdVos(limmodVo.getLimQueryVo().getLmegrdVos()).limpts(limptsVo).ownref(limmodVo.getOwnref()).comamt(limmodVo.getComamt()).ccvamt(limmodVo.getCcvamt()).build();
    }

    /**
     * 转授权查询
     *
     * @param limRequest
     * @return
     */
    @Override
    public LimResponse limZsqQuery(LimRequest limRequest) {
        limRequest.reset();
        LimmodVo limmodVo = limRequest.getLimmodVo();
        limZsqOptionService.assertLim(limmodVo);
        return LimResponse.builder().zsqInfos(limmodVo.getZsqInfos()).build();
    }

    /**
     * 押品查询
     *
     * @param yapinRequest
     * @return
     */
    @Override
    public YapinResponse yapinInit(YapinRequest yapinRequest) {
        yapinRequest.reset();
        YpnmodVo ypnmodVo = new YpnmodVo();
        DtoMapper.INSTANCE.updateRequestToYpnmod(yapinRequest, ypnmodVo);
        ypnmodVo.reset();
        ypnmodService.assertYpn(ypnmodVo);
        YapinResponse yapinResponse = YapinResponse.builder().ypInfoVo(ypnmodVo.getYpInfoVo()).ypInfoVos(ypnmodVo.getYpInfoVos()).mac(ypnmodVo.getMac()).build();
        return yapinResponse;
    }
}
