package com.inkasso.factoring.creditmanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.exception.ServiceException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.factoring.common.entity.ResponseJson;
import com.inkasso.factoring.common.service.CommonService;
import com.inkasso.factoring.creditmanagement.dao.DueDiligenceAppointmentMapper;
import com.inkasso.factoring.creditmanagement.entity.DueDiligenceAppointment;
import com.inkasso.factoring.creditmanagement.entity.Enterprise;
import com.inkasso.factoring.creditmanagement.entity.SupplierContact;
import com.inkasso.factoring.creditmanagement.entity.SupplierInfo;
import com.inkasso.factoring.creditmanagement.enums.SupplierStatusEnums;
import com.inkasso.factoring.creditmanagement.service.IDueDiligenceAppointmentService;
import com.inkasso.factoring.creditmanagement.service.IEnterpriseService;
import com.inkasso.factoring.creditmanagement.service.ISupplierContactService;
import com.inkasso.factoring.creditmanagement.service.ISupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 线下尽调预约 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-11-25
 */
@Service
public class DueDiligenceAppointmentServiceImpl extends ServiceImpl<DueDiligenceAppointmentMapper, DueDiligenceAppointment> implements IDueDiligenceAppointmentService {

    @Autowired
    private ISupplierInfoService supplierInfoService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private ISupplierContactService supplierContactService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${trade.supplierStartProcessUrl}")
    private String supplierStartProcess;


    @Value("${trade.completeVideoDueDiligenceAppointmentUrl}")
    private String completeVideoDueDiligenceAppointment;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void offlineDueDiligenceAppointment(DueDiligenceAppointment dueDiligenceAppointment) {
        SupplierContact supplierContact = supplierContactService.lambdaQuery().eq(SupplierContact::getSupplierInfoId, dueDiligenceAppointment.getSupplierInfoId()).one();
        if(!supplierContact.getMobile().equals(dueDiligenceAppointment.getMobile())){
            boolean verifySMSCode = commonService.verifySMSCode(dueDiligenceAppointment.getMobile(), dueDiligenceAppointment.getCode());
            supplierContact.setMobile(dueDiligenceAppointment.getMobile());
            supplierContact.setContactName(dueDiligenceAppointment.getContactName());
            supplierContactService.updateById(supplierContact);
            if(!verifySMSCode){
                throw new ServiceException(CodeUtil.FAIL,"短信验证码有误，请确认!");
            }
        }
        save(dueDiligenceAppointment);
        //完成流程
        String completeVideoDueDiligenceAppointmentUrl = completeVideoDueDiligenceAppointment + "?id=" + dueDiligenceAppointment.getSupplierInfoId() + "&historyId=" + dueDiligenceAppointment.getId();
        restTemplate.getForObject(completeVideoDueDiligenceAppointmentUrl, ResponseJson.class);
        supplierInfoService.lambdaUpdate()
                .eq(SupplierInfo::getId,dueDiligenceAppointment.getSupplierInfoId())
                .set(SupplierInfo::getStatus, SupplierStatusEnums.CONFIRM_DUE_DILIGENCE_TIME.getCode()).update();
    }

    @Override
    public DueDiligenceAppointment getOfflineDueDiligenceAppointment(Long id) {
        DueDiligenceAppointment dueDiligenceAppointment = new DueDiligenceAppointment();
        SupplierInfo supplierInfo = supplierInfoService.getById(id);
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        SupplierContact supplierContact = supplierContactService.lambdaQuery().eq(SupplierContact::getSupplierInfoId, id).one();
        dueDiligenceAppointment.setContactName(supplierContact.getContactName());
        dueDiligenceAppointment.setCorporateName(enterprise.getCorporateName());
        dueDiligenceAppointment.setSupplierInfoId(id);
        dueDiligenceAppointment.setMobile(supplierContact.getMobile());
        dueDiligenceAppointment.setSocialCreditCode(enterprise.getSocialCreditCode());
        return dueDiligenceAppointment;
    }
}
