package com.fs.hisStore.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fs.common.config.FSSysConfig;
import com.fs.common.core.domain.R;
import com.fs.common.utils.sign.Md5Utils;
import com.fs.hisStore.param.PrescribeParam;
import com.fs.hisStore.service.IPrescribeScrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Locale;

/**
 * 参数配置 服务层实现
 * 
 
 */
@Service
public class PrescribeScrmServiceImpl implements IPrescribeScrmService
{

    @Autowired
    FSSysConfig fsSysConfig;

    @Override
    public R doPrescribe(PrescribeParam prescribeParam) {
        prescribeParam.setAct_id(fsSysConfig.getActId());
        prescribeParam.setManu_id(fsSysConfig.getManuId());
        prescribeParam.setApp_id(fsSysConfig.getAppId());
        prescribeParam.setTimestamp(String.valueOf(new Date().getTime()));
        //计算签名，计算法为 md5(act_id&manu_id&app_id&timestamp)，把⼊参act_id、
        //manu_id、app_id、timestamp四个参数通过&拼接起来，返回MD5加密⼤写字符串
        prescribeParam.setSig(Md5Utils.hash(prescribeParam.getAct_id()+"&"+prescribeParam.getManu_id()+"&"+prescribeParam.getApp_id()+"&"+prescribeParam.getTimestamp()).toUpperCase(Locale.ROOT));
        String jsons=JSONUtil.toJsonStr(prescribeParam);
        String post = HttpRequest.post(fsSysConfig.getPrescribeUrl())
                .body(JSONUtil.toJsonStr(prescribeParam))
                .execute().body();
        com.alibaba.fastjson.JSONObject json = JSON.parseObject(post);
        String code=json.getString("code");
        String msg=json.getString("msg");
        if(code.equals("1000")){
            return R.ok("提交成功");
        }
        else{
            return R.error(msg);
        }
    }
}
