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.HighGasOilPrice;
import com.hai.entity.HighGasOilPriceOfficial;
import com.hai.model.ResponseData;
import com.hai.service.HighGasOilPriceOfficialService;
import com.hai.service.HighGasOilPriceService;
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.List;
import java.util.Map;

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

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

    @Resource
    private HighGasOilPriceOfficialService gasOilPriceOfficialService;

    private HighGasOilPriceService highGasOilPriceService;

    @RequestMapping(value="/updateOilPriceOfficial",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改油品国标价价格")
    public ResponseData updateOilPriceOfficial(@RequestBody JSONObject body) {
        try {
            if (body.getLong("regionId") == null
                    || body.getInteger("oilNo") == null
                    || body.getBigDecimal("price") == null
            ) {
                log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            HighGasOilPriceOfficial price = gasOilPriceOfficialService.getPrice(body.getLong("regionId"), body.getInteger("oilNo"));
            if (price == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到价格");
            }
            price.setPriceOfficial(body.getBigDecimal("price"));
            gasOilPriceOfficialService.editPrice(body.getLong("regionId"), body.getInteger("oilNo"), body.getBigDecimal("price"));

            new Thread(() -> {
                // 查询区域下的油品
                List<HighGasOilPrice> list = highGasOilPriceService.getPriceListByRegionAndOilNo(body.getLong("regionId"), body.getInteger("oilNo"));
                for (HighGasOilPrice gasOilPrice : list) {
                    gasOilPrice.setPriceOfficial(body.getBigDecimal("price"));
                    highGasOilPriceService.editGasOilPrice(gasOilPrice);
                }
            }).start();
            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/getOilPriceOfficialDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油品国标价详情")
    public ResponseData getOilPriceOfficialDetail(@RequestParam(name = "regionId", required = true) Long regionId,
                                                  @RequestParam(name = "oilNo", required = true) Integer oilNo) {
        try {

            return ResponseMsgUtil.success(gasOilPriceOfficialService.getPrice(regionId,oilNo));

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

    @RequestMapping(value="/getOilPriceOfficialList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油品国标价列表")
    public ResponseData getOilPriceOfficialList(@RequestParam(name = "regionName", required = false) String regionName,
                                        @RequestParam(name = "oilNo", required = false) Integer oilNo,
                                        @RequestParam(name = "oilType", required = false) Integer oilType,
                                        @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                        @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

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

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

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