package com.cweb.controller;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.hfkj.common.alipay.AlipayUtils;
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.MemberValidateUtil;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.WxMaConfiguration;
import com.hfkj.config.WxMsgConfig;
import com.hfkj.entity.BsAgentApiParam;
import com.hfkj.entity.BsUserMsgSub;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.agent.BsAgentApiParamService;
import com.hfkj.service.user.BsUserMsgSubService;
import com.hfkj.service.user.BsUserService;
import com.hfkj.sysenum.user.UserLoginPlatform;
import com.hfkj.sysenum.user.UserLoginType;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsUserMsgSubController
 * @author: HuRui
 * @date: 2023/3/18
 **/
@Controller
@RequestMapping(value = "/userMsg")
@Api(value = "用户消息订阅")
public class BsUserMsgSubController {
    private static Logger log = LoggerFactory.getLogger(BsUserMsgSubController.class);
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsUserMsgSubService userMsgSubService;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "消息订阅")
    public ResponseData test() {
        try {

            WxMsgConfig.priceDiscountActivity(
                    "测试内容"
                    ,new Date()
                    ,new Date()
                    , "无"
                    ,"oUGn_4unIjp90MY-oEXluY4laDrQ");

            return ResponseMsgUtil.success("订阅成功");

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

    @RequestMapping(value = "/subscribe", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "消息订阅")
    public ResponseData subscribe(@RequestBody JSONArray body) {
        try {
            // 用户session
            UserSessionObject userSessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (userSessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            if (body == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            for (Object obj : body) {
                JSONObject jsonObject = (JSONObject) obj;
                if (StringUtils.isBlank(jsonObject.getString("subscribeId"))) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                BsUserMsgSub sub = userMsgSubService.getDetail(userSessionObject.getUser().getId(), jsonObject.getString("subscribeId"));
                if (sub == null) {
                    sub = new BsUserMsgSub();
                    sub.setUserId(userSessionObject.getUser().getId());
                    sub.setSubscribeId(jsonObject.getString("subscribeId"));
                    userMsgSubService.editData(sub);
                }
            }

            return ResponseMsgUtil.success("订阅成功");

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

}
