package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighGasOilGunNo;
import com.hai.entity.HighGasOilPrice;
import com.hai.entity.SecDictionary;
import com.hai.model.HighMerchantStoreModel;
import com.hai.model.ResponseData;
import com.hai.service.CommonService;
import com.hai.service.HighGasOilGunNoService;
import com.hai.service.HighGasOilPriceService;
import com.hai.service.HighMerchantStoreService;
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.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/gasOilGunNo")
@Api(value = "加油站枪号配置")
public class HighGasOilGunNoController {

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

    @Resource
    private HighGasOilGunNoService gasOilGunNoService;

    @Resource
    private HighGasOilPriceService gasOilPriceService;

    @Resource
    private CommonService commonService;

    @RequestMapping(value="/addGasOilPrice",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加油站枪号")
    public ResponseData addGasOilPrice(@RequestBody JSONObject body) {
        try {
            if (body.getLong("oilPriceId") == null || body.getInteger("gunNo") == null) {
                log.error("HighGasDiscountOilPriceController -> editGasOilPrice() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 油品价格
            HighGasOilPrice oilPrice = gasOilPriceService.getGasOilPriceById(body.getLong("oilPriceId"));
            if (oilPrice == null) {
                log.error("HighGasDiscountOilPriceController -> editGasOilPrice() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到油品价格");
            }
            HighGasOilGunNo gasOilGunNo = new HighGasOilGunNo();
            gasOilGunNo.setGasOilPriceId(body.getLong("oilPriceId"));
            gasOilGunNo.setStoreId(oilPrice.getMerchantStoreId());
            gasOilGunNo.setOilNo(oilPrice.getOilNo());
            gasOilGunNo.setOilNoName(oilPrice.getOilNoName());
            gasOilGunNo.setOilType(oilPrice.getOilType());
            gasOilGunNo.setOilTypeName(oilPrice.getOilTypeName());
            gasOilGunNo.setGunNo(body.getInteger("gunNo"));
            gasOilGunNoService.editGunNo(gasOilGunNo);

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

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

    @RequestMapping(value="/delGunNo",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除油枪号")
    public ResponseData delGunNo(@RequestBody JSONObject body) {
        try {
            if (body.getLong("gunNoId") == null) {
                log.error("HighGasDiscountOilPriceController -> editGasOilPrice() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询价格
            HighGasOilGunNo gunNo = gasOilGunNoService.getGunNoById(body.getLong("gunNoId"));
            if (gunNo == null) {
                log.error("HighGasDiscountOilPriceController -> editGasOilPrice() error!","未找到油品价格");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到油品价格");
            }
            gunNo.setStatus(0);
            gasOilGunNoService.editGunNo(gunNo);

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

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

    @RequestMapping(value="/getGunNoListByOilPrice",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油品价格列表")
    public ResponseData getGunNoListByOilPrice(@RequestParam(name = "oilPriceId", required = true) Long oilPriceId) {
        try {

            return ResponseMsgUtil.success(gasOilGunNoService.getGunNoListByOilPrice(oilPriceId));

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

}
