package com.jkcredit.invoice.service.nocar.impl;

import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyDetailAddRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyDetailAddResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.OperatorEditConfirmRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.OperatorEditRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.OperatorEvidenceRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.waybill.OperatorSendCodeRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.waybill.OperatorValidCodeRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.common.CommonConst;
import com.jkcredit.invoice.mapper.waybill.NoCarCompanyMangerMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.entity.waybill.NoCarCompanyManger;
import com.jkcredit.invoice.model.vo.OperatorEditConfirmVo;
import com.jkcredit.invoice.model.vo.OperatorEditVo;
import com.jkcredit.invoice.model.vo.OperatorEvidenceVo;
import com.jkcredit.invoice.model.vo.OperatorSendCodeVo;
import com.jkcredit.invoice.service.manager.ParamService;
import com.jkcredit.invoice.service.nocar.NoCarCompanyMangerOper;
import com.jkcredit.invoice.service.upservice.NoCarInterface;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class NoCarCompanyMangerOperImpl extends ServiceImpl<NoCarCompanyMangerMapper,NoCarCompanyManger> implements NoCarCompanyMangerOper{
    @Autowired
    private NoCarCompanyMangerMapper noCarCompanyMangerMapper;

    @Autowired
    private NoCarInterface noCarInterface;

    @Autowired
    ParamService paramService;
    @Override
    public RespR companyDetailAdd(NoCarCompanyManger noCarCompanyManger) {
        QueryWrapper<NoCarCompanyManger> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("operator_mobile").eq("company_num", noCarCompanyManger.getCompanyNum());
        NoCarCompanyManger noCarCompanyMangerDb = noCarCompanyMangerMapper.selectOne(queryWrapper);
        if(noCarCompanyMangerDb !=null){
            return new RespR(false,"已经上传企业开票人，无需重复上传");
        }
        CompanyDetailAddRequest companyDetailAddRequest = new CompanyDetailAddRequest();
        BeanUtils.copyProperties(noCarCompanyManger,companyDetailAddRequest);
        RespR<CompanyDetailAddResponse> respR = noCarInterface.companyDetailAdd(companyDetailAddRequest);
        if(respR.getCode() == 1){
            log.error("companyDetailAdd上传失败，失败原因:{}",respR.getMsg());
            return new RespR(false,respR.getMsg());
        }else {
            //开票经办人上传成功（管理端）
            noCarCompanyManger.setStatus("3");
            noCarCompanyMangerMapper.insert(noCarCompanyManger);
            return new RespR("success");
        }
    }

    @Override
    public RespR operatorEdit(OperatorEditVo operatorEditVo) {
        OperatorEditRequest operatorEditRequest = new OperatorEditRequest();
        BeanUtils.copyProperties(operatorEditVo,operatorEditRequest);
        RespR respR = noCarInterface.operatorEdit(operatorEditRequest);
        if(respR.getCode() == 1){
            log.error("operatorEdit失败，失败原因:{}",respR.getMsg());
            return new RespR(false,respR.getMsg());
        }else{
            UpdateWrapper<NoCarCompanyManger> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("operator_name_temp",operatorEditVo.getTargetOperatorName());
            updateWrapper.set("operator_mobile_temp",operatorEditVo.getTargetOperatorMobile());
            updateWrapper.set("identification_id_temp",operatorEditVo.getTargetIdentificationId());
            updateWrapper.set("status",CommonConst.TWO);
            updateWrapper.eq("company_num",operatorEditVo.getCompanyNum());
            NoCarCompanyManger noCarCompanyManger  = new NoCarCompanyManger();
            noCarCompanyManger.setCompanyNum(operatorEditVo.getCompanyNum());
            noCarCompanyMangerMapper.update(noCarCompanyManger,updateWrapper);
            return new RespR("success");
        }

    }

    @Override
    public RespR operatorEditConfirm(OperatorEditConfirmVo operatorEditConfirmVo) {
        OperatorEditConfirmRequest operatorEditConfirmRequest = new OperatorEditConfirmRequest();
        BeanUtils.copyProperties(operatorEditConfirmVo,operatorEditConfirmRequest);
        RespR respR = noCarInterface.operatorEditConfirm(operatorEditConfirmRequest);
        if(respR.getCode() == 1){
            log.error("operatorEditConfirm失败，失败原因:{}",respR.getMsg());
            return new RespR(false,respR.getMsg());
        }else{
            QueryWrapper<NoCarCompanyManger> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("company_num","operator_name_temp","operator_mobile_temp","identification_id_temp").eq("company_num",operatorEditConfirmVo.getCompanyNum());
            NoCarCompanyManger noCarCompanyMangerDb = noCarCompanyMangerMapper.selectOne(queryWrapper);
            UpdateWrapper<NoCarCompanyManger> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("operator_name",noCarCompanyMangerDb.getOperatorNameTemp());
            updateWrapper.set("operator_mobile",noCarCompanyMangerDb.getOperatorMobileTemp());
            updateWrapper.set("identification_id",noCarCompanyMangerDb.getIdentificationIdTemp());
            updateWrapper.set("status",CommonConst.ONE);
            updateWrapper.eq("company_num",operatorEditConfirmVo.getCompanyNum());
            noCarCompanyMangerMapper.update(noCarCompanyMangerDb,updateWrapper);
            return new RespR("success");
        }
    }

    @Override
    public RespR operatorEvidence(OperatorEvidenceVo operatorEvidenceVo) {
        log.info("开始上传证明文件：NoCarCompanyMangerOperImpl.operatorEvidenceAdd{}", operatorEvidenceVo);
        OperatorEvidenceRequest operatorEvidenceRequest = new OperatorEvidenceRequest();
        BeanUtils.copyProperties(operatorEvidenceVo,operatorEvidenceRequest);
        RespR respR = noCarInterface.operatorEvidence(operatorEvidenceRequest);
        if(respR.getCode() == 1){
            log.error("operatorEvidence失败，失败原因:{}",respR.getMsg());
            return new RespR(false,respR.getMsg());
        }else{
            UpdateWrapper<NoCarCompanyManger> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("operator_evidence_contract_file_name",operatorEvidenceVo.getContractFileName());
            updateWrapper.set("operator_evidence_base64_str",operatorEvidenceVo.getBase64Str());
            updateWrapper.set("status", CommonConst.FOR);
            updateWrapper.eq("company_num",operatorEvidenceVo.getCompanyNum());
            NoCarCompanyManger noCarCompanyManger  = new NoCarCompanyManger();
            noCarCompanyManger.setCompanyNum(operatorEvidenceVo.getCompanyNum());
            noCarCompanyMangerMapper.update(noCarCompanyManger,updateWrapper);
            return new RespR("success");
        }
    }

    @Override
    public RespR operatorSendCode(OperatorSendCodeVo operatorSendCodeVo, CustomerRec customerRec) {
        NoCarCompanyManger noCarCompanyManger = new NoCarCompanyManger();
        noCarCompanyManger.setCompanyNum(customerRec.getCompanyNum());
        QueryWrapper<NoCarCompanyManger> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("operator_mobile").eq("company_num", noCarCompanyManger.getCompanyNum());
        NoCarCompanyManger noCarCompanyMangerDb = noCarCompanyMangerMapper.selectOne(queryWrapper);
        if (noCarCompanyMangerDb == null) {
            return new RespR(false, "未上传开票经办人，请联系管理员上传");
        }

        OperatorSendCodeRequest operatorSendCodeRequest = new OperatorSendCodeRequest();
        operatorSendCodeRequest.setBuyerCode(operatorSendCodeVo.getBuyerCode());
        String companyNum = paramService.getParamsByParamName("REQUEST_COMPANY_NUM").getParamValue();
        operatorSendCodeRequest.setCompanyNum(companyNum);
        RespR respR = noCarInterface.operatorSendCode(operatorSendCodeRequest);
        if (respR.getCode() == 1) {
            return new RespR(false, respR.getMsg());
        } else {
            UpdateWrapper<NoCarCompanyManger> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", CommonConst.FIVE);
            updateWrapper.eq("company_num",customerRec.getCompanyNum());
            noCarCompanyMangerMapper.update(noCarCompanyManger,updateWrapper);
            return new RespR("success");
        }
    }

    @Override
    public RespR operatorValidCode(OperatorSendCodeVo operatorSendCodeVo, CustomerRec customerRec) {
        NoCarCompanyManger noCarCompanyManger = new NoCarCompanyManger();
        noCarCompanyManger.setCompanyNum(customerRec.getCompanyNum());
        QueryWrapper<NoCarCompanyManger> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("operator_mobile").eq("company_num", noCarCompanyManger.getCompanyNum());
        NoCarCompanyManger noCarCompanyMangerDb = noCarCompanyMangerMapper.selectOne(queryWrapper);
        if (noCarCompanyMangerDb == null) {
            return new RespR(false, "未上传开票经办人，请联系管理员上传");
        }

        OperatorValidCodeRequest operatorValidCodeRequest = new OperatorValidCodeRequest();
        operatorValidCodeRequest.setBuyerCode(operatorSendCodeVo.getBuyerCode());
        String companyNum = paramService.getParamsByParamName("REQUEST_COMPANY_NUM").getParamValue();
        operatorValidCodeRequest.setCompanyNum(companyNum);
        operatorValidCodeRequest.setValidCode(operatorSendCodeVo.getValidCode());
        RespR respR = noCarInterface.operatorValidCode(operatorValidCodeRequest);
        if (respR.getCode() == 1) {
            return new RespR(false, respR.getMsg());
        } else {
            UpdateWrapper<NoCarCompanyManger> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", CommonConst.SIX);
            updateWrapper.set("last_time", DateUtil.getCurrentDateStr());
            updateWrapper.eq("company_num",customerRec.getCompanyNum());
            noCarCompanyMangerMapper.update(noCarCompanyManger,updateWrapper);
            return new RespR("success");
        }
    }

    @Override
    public IPage getCustomerCompanyManagerDetailPage(Page page, NoCarCompanyManger noCarCompanyManger) {

        return baseMapper.selectAllCustomerCompanyManagerDetailPage(page, noCarCompanyManger);
    }

    @Override
    public RespR operatorEvidenceAdd(NoCarCompanyManger noCarCompanyManger) {
        log.info("开始上传证明文件：NoCarCompanyMangerOperImpl.operatorEvidenceAdd{}", noCarCompanyManger);
        if (noCarCompanyManger.getStatus().equals(CommonConst.FOR)) {
            return new RespR(false, "证明文件已上传，请误重复上传");
        }
        //if (!noCarCompanyManger.getStatus().equals(CommonConst.THREE)) {
        //    return new RespR(false, "无法上传证明文件，开票经办人未上传或者处于经办人调整过程中");
        //}
        OperatorEvidenceRequest operatorEvidenceRequest = new OperatorEvidenceRequest();
        operatorEvidenceRequest.setCompanyNum(noCarCompanyManger.getCompanyNum());
        operatorEvidenceRequest.setOperatorMobile(noCarCompanyManger.getOperatorMobile());
        operatorEvidenceRequest.setBase64Str(noCarCompanyManger.getOperatorEvidenceBase64Str());
        operatorEvidenceRequest.setContractFileName(noCarCompanyManger.getOperatorEvidenceContractFileName());
        RespR respR = noCarInterface.operatorEvidence(operatorEvidenceRequest);
        if (respR.getCode() == 1) {
            log.error("operatorEvidence失败，失败原因:{}", respR.getMsg());
            return new RespR(false, respR.getMsg());
        } else {
            UpdateWrapper<NoCarCompanyManger> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("company_num", noCarCompanyManger.getCompanyNum());
            updateWrapper.set("operator_evidence_base64_str", noCarCompanyManger.getOperatorEvidenceBase64Str());
            updateWrapper.set("operator_evidence_contract_file_name", noCarCompanyManger.getOperatorEvidenceContractFileName());
            updateWrapper.set("status", CommonConst.FOR);
            // 4-证明附件上传成功（管理端）
            noCarCompanyManger.setStatus("4");
            noCarCompanyMangerMapper.update(noCarCompanyManger, updateWrapper);
            return new RespR("success");
        }
    }

    @Override
    public RespR noCarCompanyMangerQuery(NoCarCompanyManger noCarCompanyManger) {
        return new RespR(noCarCompanyMangerMapper.selectBycompanyNumAndOr(noCarCompanyManger));
    }
}
