package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.constants.BizTypeEnum;
import com.cusc.nirvana.user.rnr.fp.dto.FpRnrSmsInfoDTO;
import com.cusc.nirvana.user.rnr.fp.dto.MessageBizTypeEnum;
import com.cusc.nirvana.user.rnr.fp.dto.SmsRequestDTO;
import com.cusc.nirvana.user.rnr.fp.dto.SmsResponseDTO;
import com.cusc.nirvana.user.rnr.fp.service.EnterpriseUnBindService;
import com.cusc.nirvana.user.rnr.fp.service.IFpRnrSmsInfoService;
import com.cusc.nirvana.user.rnr.fp.service.ShortMessageCheckService;
import com.cusc.nirvana.user.rnr.mg.client.ChangeOrUnbindIccidsClient;
import com.cusc.nirvana.user.rnr.mg.client.MgRnrRelationClient;
import com.cusc.nirvana.user.rnr.mg.dto.RnrOrderDTO;
import com.cusc.nirvana.user.rnr.mg.dto.RnrRelationDTO;
import com.cusc.nirvana.user.util.CuscStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: EnterpriseUnBindServiceImpl
 * @description: 企业解绑
 * @author: jk
 * @date: 2022/6/17 14:46
 * @version: 1.0
 **/
@Service
@Slf4j
public class EnterpriseUnBindServiceImpl implements EnterpriseUnBindService {

    @Resource
    private MgRnrRelationClient mgRnrRelationClient;

    @Resource
    private ShortMessageCheckService shortMessageCheckService;

    @Autowired
    private IFpRnrSmsInfoService smsInfoService;

    @Autowired
    private ChangeOrUnbindIccidsClient client;

    @Override
    public Response enterpriseUnBind(RnrRelationDTO dto) {
        Response response = mgRnrRelationClient.saveRnrRelation(dto);
        if(!response.isSuccess()){
            throw new CuscUserException(response.getCode(),response.getMsg());
        }
        SmsRequestDTO smsRequestDTO = new SmsRequestDTO();
        smsRequestDTO.setBizType(BizTypeEnum.ENTERPRISE_UNBIND.getCode());
        smsRequestDTO.setTenantNo(dto.getInfo().getTenantNo());
        smsRequestDTO.setPhone(dto.getInfo().getPhone());
        //短信参数
        List<String> params = new ArrayList<>();
        params.add(dto.getInfo().getFullName());
        params.add(String.valueOf(dto.getCarNumber()));
        smsRequestDTO.setParams(params);

        log.warn("企业解绑发送入参{}",JSONObject.toJSONString(smsRequestDTO));
        Response<SmsResponseDTO> smsResponseDTOResponse = shortMessageCheckService.sendSms(smsRequestDTO);
        log.warn("企业解绑发送回参{}",JSONObject.toJSONString(smsResponseDTOResponse));

        if(!smsResponseDTOResponse.isSuccess()){
            throw new CuscUserException(smsResponseDTOResponse.getCode(),smsResponseDTOResponse.getMsg());
        }

        //短信入库
        FpRnrSmsInfoDTO fpRnrSmsInfoDTO = new FpRnrSmsInfoDTO();
        fpRnrSmsInfoDTO.setIsDelete(0);
        fpRnrSmsInfoDTO.setUuid(CuscStringUtils.generateUuid());
        fpRnrSmsInfoDTO.setSendPhone(smsRequestDTO.getPhone());
        fpRnrSmsInfoDTO.setSendSmsId(smsResponseDTOResponse.getData().getMessageId());
        fpRnrSmsInfoDTO.setRnrId(dto.getInfo().getUuid());
        fpRnrSmsInfoDTO.setOrderId(dto.getOrder().getUuid());
//        fpRnrSmsInfoDTO.setIccids(JSONObject.toJSONString(dto.getCardList().stream().map(c -> c.getIccid())
//                .collect(Collectors.toList())));
        fpRnrSmsInfoDTO.setSendContent(smsRequestDTO.getParams().toString());
        fpRnrSmsInfoDTO.setBizType(MessageBizTypeEnum.UNBIND.getCode());
        fpRnrSmsInfoDTO.setCreator(dto.getInfo().getCreator());
        fpRnrSmsInfoDTO.setOperator(dto.getInfo().getCreator());

        smsInfoService.insert(fpRnrSmsInfoDTO);
        //发送延迟队列
        RnrOrderDTO rnrOrderDTO = new RnrOrderDTO();
        rnrOrderDTO.setUuid(dto.getOrder().getUuid());
        client.sendMSMToMQ(rnrOrderDTO);

        return smsResponseDTOResponse;
    }
}
