package com.reptileclient.jlifeapp.service;

import com.alibaba.fastjson.JSONObject;
import com.reptileclient.jlifeapp.constant.JlifeConstant;
import com.reptileclient.jlifeapp.dao.hm.HmTokenMapper;
import com.reptileclient.jlifeapp.dto.HmDecryptDTO;
import com.reptileclient.jlifeapp.entity.hm.HmToken;
import com.reptileclient.jlifeapp.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class HmPhoneDecryptService {

    @Resource
    private RetryService retryService;

    @Resource
    private HmTokenMapper hmTokenMapper;



    /**
     * 根据工单号获取客户手机
     * 技师作业单 fieldName：userPhone orderType:BOOK_TRADE：
     * 服务单 fieldName：phone    orderType:BOOK_ORDER
     * 付款单：fieldName：buyerPhone  orderType: "SERVICE_PAYMENT_ORDER"
     */
    public String getPhoneByTradeNo(String filedName, String orderNo, String orderType) {
        //如果是付款单 不返回手机号 20230926
//        if(orderType.equals("SERVICE_PAYMENT_ORDER")) return null;

        log.info("getPhoneByTradeNo,filedName:{},orderNo:{},orderType:{}", filedName, orderNo, orderType);
        HmToken hmToken = hmTokenMapper.selectAll();
        String token = hmToken.getToken();

        HmDecryptDTO hmDecryptDTO = new HmDecryptDTO();
        hmDecryptDTO.setOrderNo(orderNo);
        hmDecryptDTO.setFieldName(filedName);
        hmDecryptDTO.setOrderType(orderType);
        String jsonString = JSONObject.toJSONString(hmDecryptDTO);
        try {
            //随机停几秒
            int waitTime = new Random().nextInt(10);
            new Thread().sleep(waitTime * 1000);

            Map<String, Object> request  = retryService.requestPost(token, jsonString, JlifeConstant.HM_PHONE_DECRYPT);
            if ("true".equals(request.get("success").toString())) {
                return request.get("data").toString();
            }

        } catch (Exception ex) {
            log.error("HmPhoneDecryptService:" + orderNo, ex);
        }

        log.info("失败getPhoneByTradeNo,filedName:{},orderNo:{},orderType:{}", filedName, orderNo, orderType);
        return null;
    }

}
