package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.HighGasDiscountOilPrice;
import com.hai.model.ResponseData;
import com.hai.service.CommonService;
import com.hai.service.HighGasDiscountOilPriceService;
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.*;

@Controller
@RequestMapping(value = "/highGasDiscountOilPrice")
@Api(value = "油品价格配置")
public class HighGasDiscountOilPriceController {

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

    @Resource
    private HighGasDiscountOilPriceService highGasDiscountOilPriceService;

    @Resource
    private CommonService commonService;

    @RequestMapping(value="/editGasDiscountOilPrice",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑油品价格")
    public ResponseData editGasDiscountOilPrice(@RequestBody JSONObject body) {
        try {
            if (StringUtils.isBlank(body.getString("oilNo")) || body.getBigDecimal("priceRate") == null) {
                log.error("HighGasDiscountOilPriceController -> editGasDiscountOilPrice() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            String oilNoName = commonService.getDictionaryCodeName("GAS_OIL_TYPE", body.getString("oilNo"));
            if (StringUtils.isBlank(oilNoName)) {
                log.error("HighGasDiscountOilPriceController -> editGasDiscountOilPrice() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未在系统中找到此油品");
            }

            HighGasDiscountOilPrice detail;
            detail = highGasDiscountOilPriceService.getDetailByOilNo(body.getString("oilNo"));
            if (detail == null) {
                detail = new HighGasDiscountOilPrice();
                detail.setOilNo(body.getString("oilNo"));
                detail.setOilNoName(oilNoName);
                detail.setPriceRate(body.getBigDecimal("priceRate"));
                detail.setCreateTime(new Date());
                detail.setUpdateTime(new Date());
            } else {
                detail.setPriceRate(body.getBigDecimal("priceRate"));
                detail.setUpdateTime(new Date());
            }
            highGasDiscountOilPriceService.editGasDiscountOilPrice(detail);

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

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

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

            if (highGasDiscountOilPriceService.getDetail(body.getLong("id")) == null) {
                log.error("HighGasDiscountOilPriceController -> delete() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
            }

            // 删除
            highGasDiscountOilPriceService.delete(body.getLong("id"));

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

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


    @RequestMapping(value="/getDetailById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询")
    public ResponseData getDetailById(@RequestParam(name = "id", required = true) Long id) {
        try {

            return ResponseMsgUtil.success(highGasDiscountOilPriceService.getDetail(id));

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

    @RequestMapping(value="/getDetailByOilNoName",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据油品查询")
    public ResponseData getDetailByOilNoName(@RequestParam(name = "oilNoName", required = true) String oilNoName) {
        try {

            return ResponseMsgUtil.success(highGasDiscountOilPriceService.getDetailByOilNoName(oilNoName));

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

    @RequestMapping(value="/getList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getList(@RequestParam(name = "oilNo", required = false) String oilNo,
                                @RequestParam(name = "oilNoName", required = false) String oilNoName,
                                @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

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

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highGasDiscountOilPriceService.getList(map)));

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