package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.Utils.HttpUtil;
import cn.hnthyy.thmz.entity.thmz.PayInfo;
import cn.hnthyy.thmz.service.thmz.TsmzService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class TsmzServiceImpl implements TsmzService {
    @Value("${tsmzServiceUrl}")
    private String tsmzServiceUrl;
    @Override
    public Map<String, Object> getPersonInfo(String idCard, String bizType, String opId) {
        String realUrl =tsmzServiceUrl+"/getPersonInfo";
        return null;
    }

    @Override
    public PayInfo uploadFees(String staffId, String patientId, Integer times, Integer receiptNo) {
        String realUrl =tsmzServiceUrl+"/uploadFees";
        Map<String,Object> map=new HashMap<>();
        //Map 对象存入 用户名,密码,电话号码
        map.put("staffId", staffId);
        map.put("patientId", patientId);
        map.put("times", times);
        map.put("receiptNo", receiptNo);
        //Map 转成  JSONObject 字符串
        JSONObject jsonObj=new JSONObject(map);
        try {
            String result= HttpUtil.sendHttpPost(realUrl, jsonObj.toString());
            if(StringUtils.isBlank(result)){
                return null;
            }
            JSONObject resultJSONO =  new JSONObject(result);
            if(resultJSONO==null){
                return null;
            }
            if((int)resultJSONO.get("code")==0){
                return getPayInfo(resultJSONO);
            }else if((int)resultJSONO.get("code")==99){
                //已经上传过
                realUrl =tsmzServiceUrl+"/calculateCost";
                result= HttpUtil.sendHttpPost(realUrl, jsonObj.toString());
                if(StringUtils.isBlank(result)){
                    return null;
                }
                resultJSONO =  new JSONObject(result);
                if(resultJSONO==null){
                    return null;
                }
                if((int)resultJSONO.get("code")==0){
                    return getPayInfo(resultJSONO);
                }else if((int)resultJSONO.get("code")==-1){
                    PayInfo payInfo= new PayInfo();
                    payInfo.setCode(-1);
                    payInfo.setErrorMessage("该患者暂未读卡,请到医保卡进行读卡操作!");
                }
            }else if((int)resultJSONO.get("code")==-1){
                PayInfo payInfo= new PayInfo();
                payInfo.setCode(-1);
                payInfo.setErrorMessage("该患者暂未读卡,请到医保卡进行读卡操作!");
            }
            return null;
        } catch (Exception e) {
            log.error("特殊门诊病人费用上传失败，病人id={},就诊次数times={},缴费次数receiptNo={}",patientId,times,receiptNo);
            e.printStackTrace();
        }
        return null;
    }

    private PayInfo getPayInfo(JSONObject resultJSONO) {
        JSONObject tempJSONO=(JSONObject)resultJSONO.get("payInfo");
        PayInfo payInfo= new PayInfo();
        payInfo.setCode(0);
        String  fundPay= (String) tempJSONO.get("fundPay");
        String  selfPay= (String) tempJSONO.get("selfPay");
        String  totalCost= (String) tempJSONO.get("totalCost");
        payInfo.setFundPay(StringUtils.isBlank(fundPay)?BigDecimal.ZERO:new BigDecimal(fundPay).setScale(1, BigDecimal.ROUND_HALF_UP));
        payInfo.setSelfPay(StringUtils.isBlank(selfPay)?BigDecimal.ZERO:new BigDecimal(selfPay).setScale(1, BigDecimal.ROUND_HALF_UP));
        payInfo.setTotalCost(StringUtils.isBlank(totalCost)?BigDecimal.ZERO:new BigDecimal(totalCost).setScale(1, BigDecimal.ROUND_HALF_UP));
        return payInfo;
    }

    @Override
    public PayInfo retractFees(String staffId, String patientId, Integer times, Integer receiptNo) {
        String realUrl =tsmzServiceUrl+"/retractFees";
        Map<String,Object> map=new HashMap<>();
        //Map 对象存入 用户名,密码,电话号码
        map.put("staffId", staffId);
        map.put("patientId", patientId);
        map.put("times", times);
        map.put("receiptNo", receiptNo);
        //Map 转成  JSONObject 字符串
        JSONObject jsonObj=new JSONObject(map);
        try {
            String result= HttpUtil.sendHttpPost(realUrl, jsonObj.toString());
            if(StringUtils.isBlank(result)){
                return null;
            }
            JSONObject resultJSONO =  new JSONObject(result);
            if(resultJSONO==null){
                return null;
            }
            if((int)resultJSONO.get("code")==0){
                return getPayInfo(resultJSONO);
            }else if((int)resultJSONO.get("code")==98){
                //已经取消过
                realUrl =tsmzServiceUrl+"/calculateCost";
                result= HttpUtil.sendHttpPost(realUrl, jsonObj.toString());
                if(StringUtils.isBlank(result)){
                    return null;
                }
                resultJSONO =  new JSONObject(result);
                if(resultJSONO==null){
                    return null;
                }
                if((int)resultJSONO.get("code")==0){
                    return getPayInfo(resultJSONO);
                }else if((int)resultJSONO.get("code")==-1){
                    PayInfo payInfo= new PayInfo();
                    payInfo.setCode(-1);
                    payInfo.setErrorMessage("该患者暂未读卡,请到医保卡进行读卡操作!");
                }
            }else if((int)resultJSONO.get("code")==-1){
                PayInfo payInfo= new PayInfo();
                payInfo.setCode(-1);
                payInfo.setErrorMessage("该患者暂未读卡,请到医保卡进行读卡操作!");
            }
            return null;
        } catch (Exception e) {
            log.error("特殊门诊病人取消费用上传失败，病人id={},就诊次数times={},缴费次数receiptNo={}",patientId,times,receiptNo);
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String calculateCost(String staffId, String patientId, Integer times, Integer receiptNo) {
        String realUrl =tsmzServiceUrl+"/calculateCost";
        return null;
    }

}
