package com.cool.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.StaffExamInfoDTO;
import com.cool.store.dto.ehr.FranchiseeInfoDTO;
import com.cool.store.dto.ehr.StaffBaseInfoDTO;
import com.cool.store.exception.ServiceException;
import com.cool.store.mq.util.HttpRestTemplateService;
import com.cool.store.service.XfsgEhrService;
import com.cool.store.utils.SecureUtil;
import com.cool.store.utils.poi.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * describe: 鲜丰水果api
 *
 * @author wxp
 * @date 2024/04/28
 */
@Service
@Slf4j
public class XfsgEhrServiceImpl implements XfsgEhrService {

    @Value("${xfsg.url}")
    private String xfsgUrl;

    @Resource
    private HttpRestTemplateService httpRestTemplateService;

    @Override
    public StaffBaseInfoDTO getUserInfoByCode(String staffNumber) {
        Map<String, Object> requestMap = new HashMap<>();
        fillSignatureInfo(requestMap);
        requestMap.put("staffNumber", staffNumber);
        String url = xfsgUrl + Constants.GET_USER_INFO_BY_CODE;
        JSONObject result = httpRestTemplateService.getForObject(url, JSONObject.class, requestMap);
        Integer code = result.getInteger("code");
        if (code != 0) {
            log.info("xfsg#getUserInfoByCode,staffNumber:{},请求失败", staffNumber);
            throw new ServiceException("鲜丰水果"+result.getString("msg"));
        }
        JSONObject data = result.getJSONObject("data");
        if (data == null) {
            log.info("xfsg#getUserInfoByCode,staffNumber:{},返回值data为空", staffNumber);
            return null;
        }
        StaffBaseInfoDTO staffBaseInfoDTO = JSON.parseObject(data.toJSONString(), StaffBaseInfoDTO.class);
        return staffBaseInfoDTO;
    }

    @Override
    public FranchiseeInfoDTO getFranchiseeInfoByFrId(String frId) {
        Map<String, Object> requestMap = new HashMap<>();
        fillSignatureInfo(requestMap);
        requestMap.put("frId", frId);
        String url = xfsgUrl + Constants.GET_FRANCHISEE_INFO_BY_CODE;
        JSONObject result = httpRestTemplateService.getForObject(url, JSONObject.class, requestMap);
        Integer code = result.getInteger("code");
        if (code != 0) {
            log.info("xfsg#getFranchiseeInfoByFrId,frId:{},请求失败", frId);
            return null;
        }
        JSONObject data = result.getJSONObject("data");
        if (data == null) {
            log.info("xfsg#getFranchiseeInfoByFrId,frId:{},返回值data为空", frId);
            return null;
        }
        FranchiseeInfoDTO franchiseeInfoDTO = JSON.parseObject(data.toJSONString(), FranchiseeInfoDTO.class);
        return franchiseeInfoDTO;
    }

    private void fillSignatureInfo(Map<String, Object> requestMap) {
        long timestamp = System.currentTimeMillis();
        String signature = SecureUtil.getSignature(timestamp);
        requestMap.put("timestamp", timestamp);
        requestMap.put("signature", signature);
    }

    @Override
    public StaffExamInfoDTO getUserExamInfo(String staffNumber) {
        Map<String, Object> requestMap = new HashMap<>();
        fillSignatureInfo(requestMap);
        requestMap.put("staffNumber", staffNumber);
        String url = xfsgUrl + Constants.GET_USER_EXAM_INFO;
        try {
            JSONObject result = httpRestTemplateService.getForObject(url, JSONObject.class, requestMap);
            Integer code = result.getInteger("code");
            if (code != 0) {
                log.info("xfsg#getUserExamInfo,staffNumber:{},请求失败", staffNumber);
                return null;
            }
            JSONObject data = result.getJSONObject("data");
            if (data == null) {
                log.info("xfsg#getUserExamInfo,staffNumber:{},返回值data为空", staffNumber);
                return null;
            }
            return JSON.parseObject(data.toJSONString(), StaffExamInfoDTO.class);
        } catch (Exception e) {
            log.info("xfsg#getUserExamInfo,staffNumber:{},Exception:", staffNumber, e);
        }
        return null;
    }

}
