package com.bweb.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.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.PageUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighActivityAward;
import com.hai.entity.HighActivityInfo;
import com.hai.entity.HighActivityPrize;
import com.hai.enum_type.ActivityAwardStatus;
import com.hai.enum_type.ActivityInfoStatus;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.HighActivityAwardService;
import com.hai.service.HighActivityAwardUserService;
import com.hai.service.HighActivityInfoService;
import com.hai.service.HighActivityUserLotteryNumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
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.*;

/**
 *
 * @author hurui
 */
@Controller
@RequestMapping(value = "/activityAward")
@Api(value = "活动奖励接口")
public class HighActivityController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private HighActivityAwardService highActivityAwardService;

    @Resource
    private HighActivityUserLotteryNumService highActivityUserLotteryNumService;

    @Resource
    private HighActivityAwardUserService highActivityAwardUserService;

    @RequestMapping(value="/editActivityAward",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "配置奖励")
    public ResponseData editActivityAward(@RequestBody JSONObject body, HttpServletRequest request) {
        try {

            if (body == null
                    || body.getLong("activityInfoId") == null
                    || StringUtils.isBlank(body.getString("img"))
                    || StringUtils.isBlank(body.getString("name"))
                    || body.getInteger("num") == null
            ) {
                log.error("HighActivityController -> editActivityAward() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            HighActivityPrize activityPrize;
            List<HighActivityPrize> activityPrizes = new ArrayList<>();

            // 组装奖品
            if (body.getJSONArray("activityPrizes") != null && body.getJSONArray("activityPrizes").size() > 0) {
                for (Object object : body.getJSONArray("activityPrizes")) {
                    JSONObject activityObject = (JSONObject)object;
                    if (
                            activityObject.getInteger("agentId") == null ||
                            activityObject.getLong("prizeId") == null ||
                            StringUtils.isBlank(activityObject.getString("prizeName")) ||
                            activityObject.getInteger("prizeNum") == null
                    ) {
                        log.error("HighActivityController -> editActivityAward() error!","参数错误");
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                    }

                    activityPrize = new HighActivityPrize();

                    activityPrize.setPrizeType(2);
                    activityPrize.setPrizeId(activityObject.getLong("prizeId"));
                    activityPrize.setPrizeName(activityObject.getString("prizeName"));
                    activityPrize.setPrizeNum(activityObject.getInteger("prizeNum"));
                    activityPrize.setExt1(activityObject.getString("agentId"));
                    activityPrizes.add(activityPrize);

                }
            }

            HighActivityAward highActivityAward;

            if (body.getLong("id") != null) {
                highActivityAward = highActivityAwardService.getDetailById(body.getLong("id"));
                if (highActivityAward == null) {
                    log.error("HighActivityInfoController -> editActivityInfo() error!","未找到活动信息");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到活动信息");
                }
                if (highActivityAward.getStatus().equals(ActivityAwardStatus.status0.getNumber())) {
                    log.error("HighActivityInfoController -> editActivityInfo() error!","状态错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
                }
            } else {
                highActivityAward = new HighActivityAward();
            }

            // 组装奖励
            highActivityAward.setActivityInfoId(body.getLong("activityInfoId"));
            highActivityAward.setImg(body.getString("img"));
            highActivityAward.setName(body.getString("name"));
            highActivityAward.setNum(body.getInteger("num"));


            highActivityAwardService.editHighActivateAward(highActivityAward , activityPrizes);

            return ResponseMsgUtil.success("成功");

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

    @RequestMapping(value="/getWinLotteryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询活动中奖记录")
    public ResponseData getWinLotteryList(@RequestParam(name = "activityId", required = true) Long activityId,
                                          @RequestParam(name = "userPhone", required = false) String userPhone,
                                          @RequestParam(name = "awardName", required = false) String awardName,
                                          @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                          @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("userPhone", userPhone);
            param.put("awardName", awardName);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highActivityAwardUserService.getWinLotteryUserList(activityId, param)));

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


    @RequestMapping(value="/getListByActivityId",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询配置奖励记录")
    public ResponseData getListByActivityId(@RequestParam(name = "activityId", required = true) Long activityId) {
        try {

            return ResponseMsgUtil.success(highActivityAwardService.getListByActivityId(activityId));

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

    @RequestMapping(value="/deleteActivateAward",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData deleteActivateAward(@RequestParam(name = "activityId", required = true) Long activityId) {
        try {
            highActivityAwardService.deleteActivateAward(activityId);
            return ResponseMsgUtil.success("删除成功");

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

    @RequestMapping(value="/checkNumberPrizes",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询剩余数量")
    public ResponseData checkNumberPrizes(@RequestParam(name = "activityId", required = true) Long activityId) {
        try {

            return ResponseMsgUtil.success(highActivityAwardService.checkNumberPrizes(activityId));

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

    @RequestMapping(value="/numberOfNewRewards",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据奖励id新增奖励数量")
    public ResponseData numberOfNewRewards(
            @RequestBody JSONObject body
    ) {
        try {

            if (body == null
                    || body.getLong("activityAwardId") == null
                    || body.getInteger("num") == null
            ) {
                log.error("HighActivityController -> editActivityAward() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            highActivityAwardService.numberOfNewRewards( body.getLong("activityAwardId") , body.getInteger("num") );
            return ResponseMsgUtil.success("新增成功");

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