package com.cweb.controller.meituan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.RandomStringGeneratorUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.meituan.MeiTuanService;
import com.hfkj.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/meiTuan")
@Api(value="美团")
public class MeiTuanController {

    private static final Logger log = LoggerFactory.getLogger(MeiTuanController.class);

    @RequestMapping(value = "/loginFree", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "H5免登接口")
    public ResponseData loginFree(@RequestParam(name = "staffPhone", required = true) String staffPhone,
                                  @RequestParam(name = "productType", required = true) String productType) {
        try {

            Map<String , Object> map = new HashMap<>();
            Map<String , Object> staffInfo = new HashMap<>();

            staffInfo.put("staffPhone", staffPhone);

            map.put("staffInfo", staffInfo);
            map.put("entId", 104310L);
            map.put("ts", new Date().getTime());
            map.put("productType", productType);
            map.put("nonce" , RandomStringGeneratorUtil.generateRandomString(36));
            JSONObject jsonObject = MeiTuanService.loginFree(map);
            JSONArray jsonArray = jsonObject.getJSONArray("url");
            JSONObject json = jsonArray.getJSONObject(0);
            return ResponseMsgUtil.success(json.getString("value"));

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