package com.bweb.controller;

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.entity.BsGasOilPriceWeek;
import com.hfkj.model.ResponseData;
import com.hfkj.service.gas.BsGasOilPriceWeekService;
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.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsGasOilPriceWeekController
 * @author: HuRui
 * @date: 2024/7/29
 **/
@Controller
@RequestMapping(value = "/gasOilPriceWeek")
@Api(value = "油品价格配置")
public class BsGasOilPriceWeekController {

    private static Logger log = LoggerFactory.getLogger(BsGasOrderController.class);
    @Resource
    private BsGasOilPriceWeekService gasOilPriceWeekService;

    @RequestMapping(value = "/updatePrice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改价格")
    public ResponseData updatePrice(@RequestBody BsGasOilPriceWeek body) {
        try {
            if (body.getId() == null
                || body.getPriceOfficial() == null
                || body.getPriceGun() == null
                || body.getPriceVip() == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询价格
            BsGasOilPriceWeek oilPrice = gasOilPriceWeekService.getOilPrice(body.getId());
            if (oilPrice == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的数据");
            }
            oilPrice.setPriceOfficial(body.getPriceOfficial());
            oilPrice.setPriceGun(body.getPriceGun());
            oilPrice.setPriceVip(body.getPriceVip());
            gasOilPriceWeekService.updatePrice(oilPrice);

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

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

    @RequestMapping(value = "/getPriceList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询价格列表")
    public ResponseData getPriceList(@RequestParam(name = "merNo", required = true) String merNo,
                                     @RequestParam(name = "oilNo", required = true) String oilNo) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);
            param.put("oilNo", oilNo);

            return ResponseMsgUtil.success(gasOilPriceWeekService.getOilPriceList(param));

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