package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.PageUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsAgentDiscount;
import com.hfkj.entity.BsDiscount;
import com.hfkj.entity.BsDiscountPk;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.agent.BsAgentDiscountService;
import com.hfkj.service.discount.BsDiscountPkService;
import com.hfkj.service.discount.BsDiscountService;
import com.hfkj.service.discount.BsDiscountUserService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.discount.DiscountStatusEnum;
import com.hfkj.sysenum.discount.DiscountStockCodeObtainTypeEnum;
import com.hfkj.sysenum.discount.DiscountTypeEnum;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/agentDiscount")
@Api(value = "优惠券管理")
public class BsAgentDiscountController {
    private static Logger log = LoggerFactory.getLogger(BsAgentDiscountController.class);
    @Resource
    private BsAgentDiscountService agentDiscountService;
    @Resource
    private BsDiscountService discountService;
    @Resource
    private BsDiscountPkService discountPkService;

    @RequestMapping(value="/create",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建")
    public ResponseData create(@RequestBody JSONObject body)  {
        try {
            if (body.getLong("agentId") == null
                    || body.getInteger("type") == null
                    || StringUtils.isBlank(body.getString("objectNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            if (agentDiscountService.getDetail(body.getLong("agentId"),body.getInteger("type"),body.getString("objectNo")) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "已绑定过优惠【"+body.getString("objectNo")+"】");
            }
            BsAgentDiscount agentDiscount = new BsAgentDiscount();
            agentDiscount.setAgentId(body.getLong("agentId"));
            agentDiscount.setType(body.getInteger("type"));

            // 类型  1：优惠券   2：优惠卷包
            if (body.getInteger("type") == 1) {
                // 查询优惠券
                BsDiscount discount = discountService.getDetail(body.getString("objectNo"));
                if (discount == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券");
                }
                agentDiscount.setObjectId(discount.getId());
                agentDiscount.setObjectNo(discount.getDiscountNo());
                agentDiscount.setObjectName(discount.getDiscountName());

            } else if (body.getInteger("type") == 2) {
                // 查询优惠券
                BsDiscountPk discountPk = discountPkService.getDetail(body.getString("objectNo"));
                if (discountPk == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券");
                }
                agentDiscount.setObjectId(discountPk.getId());
                agentDiscount.setObjectNo(discountPk.getDiscountPkNo());
                agentDiscount.setObjectName(discountPk.getDiscountPkName());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的类型");
            }
            agentDiscountService.create(agentDiscount);

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/delete",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            agentDiscountService.delete(body.getLong("id"));

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(name = "agentId", required = true) Long agentId,
                                  @RequestParam(name = "type", required = false) Integer type,
                                  @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                  @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            List<BsAgentDiscount> list = agentDiscountService.getList(agentId);
            if (type != null) {
                list = list.stream().filter(o-> o.getType().equals(type)).collect(Collectors.toList());
            }
            return ResponseMsgUtil.success(PageUtil.initPageInfoObj(pageNum, list.size(),pageSize,new PageInfo<>(list)));

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

}
