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.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.discount.*;
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.Map;

@Controller
@RequestMapping(value = "/discountPkStock")
@Api(value = "优惠券包管理")
public class BsDiscountPkStockController {
    private static Logger log = LoggerFactory.getLogger(BsDiscountPkStockController.class);
    @Resource
    private BsDiscountPkStockBatchService discountPkStockBatchService;
    @Resource
    private BsDiscountPkStockCodeService discountPkStockCodeService;

    @RequestMapping(value="/addStock",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加库存")
    public ResponseData addStock(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("discountPkNo"))
                    || body.getInteger("stockCount") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            discountPkStockBatchService.addStock(body.getString("discountPkNo"), body.getInteger("stockCount"));

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

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

    @RequestMapping(value="/queryStockBatchList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询库存批次")
    public ResponseData queryStockBatchList(@RequestParam(name = "discountPkNo", required = false) String discountPkNo,
                                            @RequestParam(name = "discountPkName", required = false) String discountPkName,
                                            @RequestParam(name = "batchNo", required = false) String batchNo,
                                            @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("discountPkNo", discountPkNo);
            param.put("discountPkName", discountPkName);
            param.put("batchNo", batchNo);


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

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

    @RequestMapping(value="/queryStockBatchDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询库存批次详情")
    public ResponseData queryStockBatchDetail(@RequestParam(name = "batchNo", required = true) String batchNo) {
        try {

            return ResponseMsgUtil.success(discountPkStockBatchService.getStockBatchDetail(batchNo));

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

    @RequestMapping(value="/queryStockBatchCodeList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询库存批次优惠券code")
    public ResponseData queryStockBatchCodeList(@RequestParam(name = "codeId", required = false) String codeId,
                                                @RequestParam(name = "discountPkNo", required = false) String discountPkNo,
                                                @RequestParam(name = "batchNo", required = false) String batchNo,
                                                @RequestParam(name = "userPhone", required = false) String userPhone,
                                                @RequestParam(name = "status", required = false) Integer status,
                                                @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("codeId", codeId);
            param.put("discountPkNo", discountPkNo);
            param.put("batchNo", batchNo);
            param.put("userPhone", userPhone);
            param.put("status", status);

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

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

}
