package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.BsOilPriceAdjustDate;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsOilPriceAdjustDateService;
import com.hfkj.sysenum.OilPriceAdjustDateResultEnum;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsOilPriceAdjustDateController
 * @author: HuRui
 * @date: 2025/11/18
 **/
@Controller
@RequestMapping(value = "/oilPriceAdjustDate")
@Api(value = "调价日配置")
public class BsOilPriceAdjustDateController {
    private static Logger log = LoggerFactory.getLogger(BsOilPriceAdjustDateController.class);
    @Resource
    private BsOilPriceAdjustDateService oilPriceAdjustDateService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建")
    public ResponseData create(@RequestBody JSONObject body) {
        try {
            if (body == null
                || body.getLong("date") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            BsOilPriceAdjustDate data = new BsOilPriceAdjustDate();
            data.setResult(OilPriceAdjustDateResultEnum.status0.getType());
            data.setDate(new Date(body.getLong("date")));
            oilPriceAdjustDateService.editData(data);

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

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

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

            oilPriceAdjustDateService.del(body.getLong("id"));

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

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

    @RequestMapping(value = "/result", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "调价结果")
    public ResponseData result(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || body.getLong("id") == null
                    || body.getInteger("result") == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            oilPriceAdjustDateService.result(body.getLong("id"), OilPriceAdjustDateResultEnum.getDataByType(body.getInteger("result")));

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

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

    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(value = "date", required = false) Long date,
                                  @RequestParam(value = "result", required = false) Integer result,
                                  @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                  @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("date", date);
            param.put("result", result);

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

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

}
