package com.cweb.controller;

import com.alibaba.fastjson.JSONArray;
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.pay.util.SignatureUtil;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.CommonSysConst;
import com.hfkj.entity.CmsContent;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.cms.CmsContentReadService;
import com.hfkj.service.cms.CmsContentService;
import com.hfkj.service.cms.impl.CmsContentServiceImpl;
import com.hfkj.sysenum.cms.CmsStatusEnum;
import com.hfkj.sysenum.cms.CmsTypeEnum;
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.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/phg")
@Api(value = "普惠go业务")
public class PhgController {
    private static Logger log = LoggerFactory.getLogger(PhgController.class);
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "普惠GO登录参数")
    public ResponseData login(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("phone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("appid", CommonSysConst.getSysConfig().getPhgAppid());
            param.put("phone", body.getString("phone"));
            param.put("sign", SignatureUtil.createSign(param, CommonSysConst.getSysConfig().getPhgAppSecret()));
            return ResponseMsgUtil.success(param);

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getSgUrl",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取山高链接")
    public ResponseData getSgUrl() {
        try {
            // 用户信息
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);

            Map<String,Object> param = new HashMap<>();
            param.put("appid", CommonSysConst.getSysConfig().getPhgAppid());
            param.put("phone", userSessionObject.getUser().getPhone());
            param.put("type", 1);
            param.put("sign", SignatureUtil.createSign(param, CommonSysConst.getSysConfig().getPhgAppSecret()));
            JSONObject obj = HttpsUtils.doPost(CommonSysConst.getSysConfig().getPhgReqUrl()+"/crest/output/getShanGaoUrl", JSONObject.toJSONString(param));
            if (obj != null) {
                return ResponseMsgUtil.success(obj.getString("return_data"));
            }
            return ResponseMsgUtil.success(null);

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryPhgList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询普惠GO优惠券列表")
    public ResponseData queryPhgList(@RequestParam(name = "status", required = true) Integer status) {
        try {
            // 用户信息
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            // 状态转换
            if (status == 3) {
                status = 0; // 已过期
            }
            Map<String,Object> param = new HashMap<>();
            param.put("appid", CommonSysConst.getSysConfig().getPhgAppid());
            param.put("phone", userSessionObject.getUser().getPhone());
            param.put("status", status); // 状态 0:已过期 1：未使用 2：已使用
            param.put("sign", SignatureUtil.createSign(param, CommonSysConst.getSysConfig().getPhgAppSecret()));
            JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getPhgReqUrl()+"/crest/output/getListUserDiscount", JSONObject.toJSONString(param));
            if (!response.getString("return_code").equals("000000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("return_msg"));
            }
            return ResponseMsgUtil.success(JSONArray.parse(response.getString("return_data")));

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

}
