package com.brilliance.isc.common.api.cpd.service.impl;

import com.brilliance.isc.bo.*;
import com.brilliance.isc.common.api.cpd.service.CpdRelrowService;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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


@Service
public class CpdRelrowServiceImpl implements CpdRelrowService {

    private final Logger logger = LoggerFactory.getLogger(CpdRelrowServiceImpl.class);
    @Resource
    private SptMapper sptMapper;
    @Resource
    private QueMapper queMapper;
    @Resource
    private CpdMapper cpdMapper;

    @Resource
    private EcmMapper ecmMapper;

    @Resource
    private GpiMapper gpiMapper;

    @Resource
    private SmhMapper smhMapper;

    @Resource
    private EjscpdMapper ejscpdMapper;
    @Resource
    private CounterService counterService;
    @Resource
    private SysmodService sysmodService;

    @ApiOperation("汇出汇款行内划转复核")
    @Override
    public void cpdRelrow(Trn trn) {
        if ("CPD".equalsIgnoreCase(trn.getObjtyp())) {
            Gpi gpi=gpiMapper.selectByTrninr(trn.getInr());
            if(gpi!=null){
                //gpi更新复核时间
                gpi.setComdat(new Date());
                gpi.setComdattim(new Date());
                gpiMapper.updateByPrimaryKey(gpi);
            }
            Cpd cpdReq = new Cpd();
            cpdReq.setInr(trn.getObjinr());
            Cpd cpd = cpdMapper.selectByPrimaryKey(cpdReq);
            if("-CPTECI-CPTECO-".indexOf(cpd.getEhtyp())>0){
                //更新电商汇出，电商汇入状态
                ecmMapper.updateStaByObjtypAndObjinr("R",trn.getObjtyp(),trn.getObjinr());
            }
            if (cpd != null && "HNHZ".equalsIgnoreCase(cpd.getCptrou()) && "O".equalsIgnoreCase(cpd.getPaytyp())) {
                Spt sptReq = new Spt();
                sptReq.setObjtyp("CPD");
                sptReq.setObjinr(trn.getObjinr());
                sptReq.setDelflg("X");
                sptReq.setHldflg("Z");
                Spt sptRecord = sptMapper.getSptByCpdAndSpt(sptReq);
                sptRecord.setDelflg("");
                // SMHINR
                Smh smh = smhMapper.selectByCortypAndTrninr("FMT", trn.getInr());
                if (smh != null) {
                    Smh cpdadvSmh = MdaUtils.deepClone(smh);
                    String smhinr = counterService.dbCounterFormat("SMH");
                    cpdadvSmh.setInr(smhinr);
                    cpdadvSmh.setObjtyp("");
                    cpdadvSmh.setObjinr("");
                    cpdadvSmh.setTrntyp("");
                    cpdadvSmh.setTrninr("");
                    cpdadvSmh.setDir("<");
                    sptRecord.setSmhinr(smhinr);
                    smhMapper.insertSelective(cpdadvSmh);
                }
                sptMapper.updateByPrimaryKey(sptRecord);
                logger.info("汇出汇款复核,更新SPT记录的DELFLG字段为空字符串,SPTINR:{}", sptRecord.getInr());
                Que queReq = new Que();
                queReq.setObjtyp("SPT");
                queReq.setObjinr(sptRecord.getInr());
                queReq.setConobjtyp("CPD");
                queReq.setConobjinr(trn.getObjinr());
                Que queRecord = queMapper.selectByQueAndCpd(queReq);
                queRecord.setHldflg("Z");
                logger.info("汇出汇款复核,更新QUE记录的HLDFLG字段为'Z',QUEINR:{}", queRecord.getInr());
                queMapper.updateByPrimaryKey(queRecord);
            }
            dealBusiness(trn);
        }
    }

    public void dealBusiness(Trn argTRN){
        switch (argTRN.getInifrm()){
            case "CPTADV":
               Smh smh =smhMapper.selectByCortypAndTrninrForCpd("<", argTRN.getInr());
                if( smh!=null && ! MdaUtils.isEmpty(smh.getInr()) ) {
                    ejscpdMapper.updateStaBySmhinr(smh.getInr());
                }
                Smh othsmh =smhMapper.selectByCortypAndDirAndMsgtypForCpd("<",argTRN.getInr());
                if( othsmh!=null && ! MdaUtils.isEmpty(othsmh.getInr()) ) {
                    ejscpdMapper.updateStaBySmhinr(othsmh.getInr());
                }
                break;
        }
    }

    public void cpdRepair(Trn trn){
        Smh smh =smhMapper.selectByCortypAndTrninrForCpd("<", trn.getInr());
        if( smh!=null && ! MdaUtils.isEmpty(smh.getInr()) ) {
            ejscpdMapper.updateStaBySmhinr(smh.getInr());
        }
        if ("CPD".equalsIgnoreCase(trn.getObjtyp())) {
            Spt sptReq = new Spt();
            sptReq.setObjtyp("CPD");
            sptReq.setObjinr(trn.getObjinr());
            sptReq.setDelflg("X");
            sptReq.setHldflg("Z");
            Spt sptRecord = sptMapper.getSptByCpdAndSpt(sptReq);
            if (sptRecord != null && StringUtils.isNotEmpty(sptRecord.getInr())) {
                logger.warn("汇出汇款行内划转打回时,删除给汇入汇款生成的SPT:{}", sptRecord.getInr());
                sysmodService.sptDeleteEntry(sptRecord);
            }
        }
    }

}
