package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.discount.BsDiscountStockBatchService;
import com.hfkj.service.discount.BsDiscountStockCodeService;
import com.hfkj.service.discount.BsDiscountUseMerService;
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.List;
import java.util.stream.Collectors;

/**
 * @className: BsDiscountUseMerController
 * @author: HuRui
 * @date: 2024/8/30
 **/
@Controller
@RequestMapping(value = "/discountUseMer")
@Api(value = "优惠可使用油站配置管理")
public class BsDiscountUseMerController {
    private static Logger log = LoggerFactory.getLogger(BsDiscountStockController.class);
    @Resource
    private BsDiscountUseMerService discountUseMerService;

    @RequestMapping(value="/edit",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑可使用油站")
    public ResponseData edit(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("discountNo"))
                    || body.getJSONArray("merNoList") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            discountUseMerService.configUseMer(
                    body.getString("discountNo"),
                    body.getJSONArray("merNoList").stream().map(Object::toString) .collect(Collectors.toList())
            );

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

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


    @RequestMapping(value="/getUseMerList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询可使用的油站列表")
    public ResponseData getUseMerList(@RequestParam(name = "discountNo", required = true) String discountNo) {
        try {

            return ResponseMsgUtil.success(discountUseMerService.getList(discountNo));

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