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

import com.yc.ship.framework.tenant.core.util.TenantUtils;
import com.yc.ship.module.system.api.notify.NotifyMessageSendApi;
import com.yc.ship.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
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.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.mysql.insurance.InsuranceMapper;
import com.yc.ship.module.trade.dal.mysql.order.TradeVisitorMapper;
import com.yc.ship.module.trade.enums.InsuranceStatusEnum;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

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

    @Resource
    private InsuranceMapper insuranceMapper;
    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;
    @Resource
    private NotifyMessageSendApi notifyMessageSendApi;

    @Resource
    private TradeVisitorMapper tradeVisitorMapper;
    @Override
    public Boolean notifyInsurance(NotifyInsuranceReqVO reqVO) {
        TenantUtils.execute(1L, () -> {
            String status = reqVO.getStatus();
            String externalPolicyNumber = reqVO.getExternalPolicyNumber();
            String externalOrderNo = reqVO.getExternalOrderNo();
            String policyNo = reqVO.getPolicyNo();
            String downloadUrl = reqVO.getDownloadUrl();
            String msg = reqVO.getMsg();
//        String successDate = reqVO.getSuccessDate();
            Long id = Long.valueOf(externalOrderNo);
            String service = reqVO.getService();
            InsuranceDO insuranceDO = insuranceMapper.selectById(id);
            if("SUCCESS".equals(status)) {
                insuranceDO.setPolicyNo(policyNo);
                insuranceDO.setElectronicPolicy(downloadUrl);
                if("apply".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));

            }else if ("FAIL".equals(status)) {
                insuranceDO.setInsuranceStatus(InsuranceStatusEnum.FAIL.getValue());
                // 发送站内信
                TradeOrderDO orderDO = tradeOrderRepositoryService.getById(insuranceDO.getOrderId());
                String insuranceNo = insuranceDO.getInsuranceNo();

                Map<String,Object> templateData = new HashMap<>();
                templateData.put("orderNo",insuranceNo);
                templateData.put("msg",msg);
                NotifySendSingleToUserReqDTO reqDTO = new NotifySendSingleToUserReqDTO();
                reqDTO.setUserId(Long.parseLong(orderDO.getCreator()));
                reqDTO.setTemplateParams(templateData);
                reqDTO.setTemplateCode("send_insurance_fail");
                notifyMessageSendApi.sendSingleMessageToMember(reqDTO);
                insuranceDO.setResMsg(msg);
            }
            insuranceMapper.updateById(insuranceDO);
//            tradeOrderRepositoryService.updateOrderInsureStatus(insuranceDO.getOrderId(),insuranceDO.getInsuranceStatus());
            tradeVisitorMapper.updateInsuranceById(insuranceDO.getVisitorId(), insuranceDO.getInsuranceStatus(), insuranceDO.getResMsg());
        });
        return true;
    }
}
