package com.yc.ship.module.trade.service.notify;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.yc.ship.framework.common.util.date.DateUtils;
import com.yc.ship.module.trade.controller.admin.notify.vo.NotifyInsuranceReqVO;
import com.yc.ship.module.trade.dal.dataobject.insurance.InsuranceDO;
import com.yc.ship.module.trade.dal.mysql.insurance.InsuranceMapper;
import com.yc.ship.module.trade.enums.InsuranceStatusEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 第三方调用通知的具体处理业务
 */
@Service
public class NotifyServiceImpl implements NotifyService {

    @Resource
    private InsuranceMapper insuranceMapper;
    @Override
    public Boolean notifyInsurance(NotifyInsuranceReqVO reqVO) {
        String status = reqVO.getStatus();
        String externalPolicyNumber = reqVO.getExternalPolicyNumber();
        String policyNo = reqVO.getPolicyNo();
        String downloadUrl = reqVO.getDownloadUrl();
        String successDate = reqVO.getSuccessDate();
        Long orderId = Long.valueOf(externalPolicyNumber);
        String service = reqVO.getService();
        if("SUCCESS".equals(status)) {
            InsuranceDO insuranceDO = insuranceMapper.selectByOrderId(orderId);
            insuranceDO.setPolicyNo(policyNo);
            insuranceDO.setElectronicPolicy(downloadUrl);
            if("applyTeam".equals(service)) {
                insuranceDO.setInsuranceStatus(InsuranceStatusEnum.SUCCESS.getValue());
            }else if ("cancel".equals(service)) {
                insuranceDO.setInsuranceStatus(InsuranceStatusEnum.CANCELLED.getValue());
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            insuranceDO.setInsuranceEffectDate(LocalDateTime.parse(successDate,formatter));
            insuranceMapper.updateById(insuranceDO);
        }
        return true;
    }
}
