package com.chaozhanggui.system.order.service;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chaozhanggui.common.system.config.MsgException;
import com.chaozhanggui.common.system.config.ResultGenerator;
import com.chaozhanggui.dao.system.dao.TbPlussSubMercCodeYsMapper;
import com.chaozhanggui.dao.system.entity.TbPlussMerchantStore;
import com.chaozhanggui.dao.system.entity.TbPlussSubMercCodeYs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class OrderService {


    @Autowired
    OrderParamCheck orderParamCheck;

    @Autowired
    TbPlussSubMercCodeYsMapper tbPlussSubMercCodeYsMapper;

    public JSONObject pay(String json,String ip){
        Optional.ofNullable(json).orElseThrow(() -> new MsgException("请求参数为空"));
        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(json);
        } catch (JSONException e) {
            throw new MsgException("JSON字符串有误，请检查!");
        }
        String res = orderParamCheck.currencyParamsCheck(jsonObject);
        if(ObjectUtil.isNotEmpty(res)){
            return ResultGenerator.genSuccessJsonResult("处理成功",JSONObject.parseObject(res));
        }
        if(ObjectUtil.isEmpty(ip)){
            ip = jsonObject.getString("ip");
        }
        jsonObject.put("ip",ip);
        String cityName = jsonObject.getString("cityName");
        String provinceName = jsonObject.getString("provinceName");
        if(ObjectUtil.isNotEmpty(cityName)){
            jsonObject.put("ipAddress",provinceName + "-" + cityName);
            TbPlussSubMercCodeYs ysCode = tbPlussSubMercCodeYsMapper.selectByCityName(cityName);
            if(ysCode != null){
                jsonObject.put("pCode",ysCode.getPcode());
                jsonObject.put("cCode",ysCode.getCode());
            }
        }else{
            if(ObjectUtil.isNotEmpty(ip)){
                DescribeIpv4LocationResponse location = ipLocationService.getLocation(ip);
                if(location != null && StringUtil.isNotEmpty(location.getCity())){
                    SubMercCodeYs ysCode = subMercCodeYsMapper.getSubMercCodeYsByCity(location.getCity());
                    if(ysCode != null){
                        jsonObject.put("pCode",ysCode.getPCode());
                        jsonObject.put("cCode",ysCode.getCode());
                    }
                    jsonObject.put("ipAddress",location.getProvince() + "-" + location.getCity());
                }else{
                    String ipAddress = IpUtils.getIpLoactionAddress(location);
                    jsonObject.put("ipAddress",ipAddress);
                }
            }
        }
        switch (jsonObject.getString("method")) {
            case "trans_query":
                return tradeQuery(jsonObject);
            default:
                return tradePay(jsonObject);
        }

    }




}
