package com.bweb.controller;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.*;
import com.hfkj.config.CommonSysConst;

import com.hfkj.model.ResponseData;

import com.hfkj.service.pay.huift.TradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


@Controller
@RequestMapping(value="/test")
@Api(value="测试")
public class TestController {
    private static Logger log = LoggerFactory.getLogger(TestController.class);
    @Resource
    private TradeService huiftTradeService;

    @RequestMapping(value="/thirdPayResult",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询V家园订单交易状态")
    public ResponseData thirdPayResult(@RequestParam(value = "preOrderNo" , required = true) String preOrderNo,
                                       @RequestParam(value = "shopCode" , required = true) String shopCode,
                                       @RequestParam(value = "signSecret" , required = true) String signSecret) {
        try {

            return ResponseMsgUtil.success(huiftTradeService.thirdPayResult(preOrderNo,shopCode,signSecret));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/haiOilLogin",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "嗨加油登陆参数")
    public ResponseData haiOilLogin(@RequestBody JSONObject body) {
        try {

            if (body == null|| StringUtils.isBlank(body.getString("phone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            Map<String , Object> map = new HashMap<>();
            map.put("appid" , CommonSysConst.getSysConfig().getHaiOilAppid());
            map.put("phone" , body.getString("phone"));
            map.put("sign" , SignatureUtil.createSign(map , CommonSysConst.getSysConfig().getHaiOilAppSecret()));


            return ResponseMsgUtil.success(map);

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
