package com.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.config.CommonSysConst;
import com.hai.entity.HighDiscountAgentCode;
import com.hai.entity.HighDiscountUserRel;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.*;
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 javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/discount")
@Api(value = "优惠券接口")
public class DiscountController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private HighDiscountUserRelService discountUserRelService;


    @RequestMapping(value="/getUserDiscountList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户优惠券列表")
    public ResponseData getUserDiscountList(@RequestParam(name = "status", required = false) Integer status,
                                            @RequestParam(name = "useScope", required = false) Integer useScope,
                                            @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            Map<String, Object> param = new HashMap<>();
            param.put("userId", userInfoModel.getHighUser().getId());
            if (useScope != 1) {
                param.put("discountUseScope", useScope);
            }
            param.put("status", status);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(discountUserRelService.getDiscountList(param)));

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

    @RequestMapping(value="/getUserDiscountDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户优惠券详情")
    public ResponseData getUserDiscountDetail(@RequestParam(name = "userDiscountId", required = true) Long userDiscountId) {
        try {

            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            HighDiscountUserRel rel = discountUserRelService.getRelById(userDiscountId);
            if (rel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券");
            }
            JSONObject param = new JSONObject();
            param.put("id", rel.getId());
            param.put("userId", rel.getUserId());
            param.put("discountPlatform", rel.getHighDiscount().getPlatform());
            param.put("discountId", rel.getDiscountId());
            param.put("discountName", rel.getDiscountName());
            param.put("discountType", rel.getDiscountType());
            param.put("discountCondition", rel.getDiscountCondition());
            param.put("discountPrice", rel.getDiscountPrice());
            param.put("receiveTime", rel.getCreateTime());
            param.put("status", rel.getStatus());
            return ResponseMsgUtil.success(param);

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

    @RequestMapping(value="/queryUserDiscountList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户优惠券列表")
    public ResponseData queryUserDiscountList(@RequestParam(name = "platform", required = false) Integer platform,
                                              @RequestParam(name = "storeNo", required = false) String storeNo,
                                              @RequestParam(name = "useScope", required = false) Integer useScope,
                                              @RequestParam(name = "status", required = false) Integer status,
                                              @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                              @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("userId", userInfoModel.getHighUser().getId());
            param.put("platform", platform);
            param.put("storeNo", storeNo);
            param.put("useScope", useScope);
            param.put("status", status);

            PageHelper.startPage(pageNum,pageSize);
            PageInfo<HighDiscountUserRel> pageInfo = new PageInfo<>(discountUserRelService.getDiscountList(param));



            return ResponseMsgUtil.success(pageInfo);

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


    @RequestMapping(value="/hzfUserDiscountUse",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【惠支付】优惠券使用")
    public ResponseData hzfUserDiscountUse(@RequestParam(name = "userDiscountId", required = true) Long userDiscountId,
                                           @RequestParam(name = "userPhone", required = true) String userPhone) {
        try {

            discountUserRelService.hzfUserDiscountUse(userDiscountId, userPhone);
            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/hzfUserDiscountReturn",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【惠支付】优惠券退款")
    public ResponseData hzfUserDiscountReturn(@RequestParam(name = "userDiscountId", required = true) Long userDiscountId,
                                              @RequestParam(name = "userPhone", required = true) String userPhone) {
        try {

            discountUserRelService.hzfUserDiscountReturn(userDiscountId, userPhone);
            return ResponseMsgUtil.success("操作成功");

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


    @RequestMapping(value="/queryHzfDiscountList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询【惠支付】优惠列表")
    public ResponseData queryHzfDiscountList(@RequestParam(name = "status", required = false) Integer status,
                                             @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                             @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("phone", userInfoModel.getHighUser().getPhone());
            param.put("status", status);
            param.put("pageNum", pageNum);
            param.put("pageSize", pageSize);

            JSONObject response = HttpsUtils.doGet(CommonSysConst.getSysConfig().getHzfDomainName()+"/crest/storeDiscountActivityPartakeUser/getDiscountListByPhone", param);
            if (!response.getString("return_code").equals("000000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("return_msg"));
            }
            return ResponseMsgUtil.success(response.getJSONObject("return_data"));

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

    @RequestMapping(value="/queryHzfDiscountTotalNum",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询【惠支付】优惠总次数")
    public ResponseData queryHzfDiscountTotalNum() {
        try {
            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String, Object> param = new HashMap<>();
            param.put("phone", userInfoModel.getHighUser().getPhone());

            JSONObject response = HttpsUtils.doGet(CommonSysConst.getSysConfig().getHzfDomainName() + "/crest/storeDiscountActivityPartakeUser/getDiscountSurplusTotalNum", param);
            if (!response.getString("return_code").equals("000000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("return_msg"));
            }
            return ResponseMsgUtil.success(response.getInteger("return_data"));

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

}
