package com.bweb.controller;

import com.alibaba.fastjson.JSONArray;
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.*;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.BsOilPriceAdjustDateService;
import com.hfkj.service.discount.BsDiscountService;
import com.hfkj.service.quiz.BsOilPriceQuizRewardService;
import com.hfkj.service.quiz.BsOilPriceQuizUserService;
import com.hfkj.sysenum.OilPriceAdjustDateResultEnum;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.agent.AgentPriceTypeEnum;
import com.hfkj.sysenum.agent.AgentTypeEnum;
import com.hfkj.sysenum.discount.DiscountStatusEnum;
import com.hfkj.sysenum.gas.GasOilNoEnum;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsOilPriceAdjustController
 * @author: HuRui
 * @date: 2025/11/18
 **/
@Controller
@RequestMapping(value = "/oilPriceQuizReward")
@Api(value = "调价用户竞猜奖励")
public class BsOilPriceQuizRewardController {
    private static Logger log = LoggerFactory.getLogger(BsOilPriceQuizRewardController.class);
    @Resource
    private BsOilPriceQuizRewardService oilPriceQuizRewardService;
    @Resource
    private BsOilPriceAdjustDateService oilPriceAdjustDateService;
    @Resource
    private BsDiscountService discountService;

    @RequestMapping(value = "/editData", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑数据")
    public ResponseData editData(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || body.getLong("priceAdjustDateId") == null
                    || body.getInteger("type") == null
                    || StringUtils.isBlank(body.getString("discountNo"))
                    || body.getInteger("number") == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 调价日
            BsOilPriceAdjustDate oilPriceAdjustDate = oilPriceAdjustDateService.getDetail(body.getLong("priceAdjustDateId"));
            if (oilPriceAdjustDate == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知调价日数据");
            }
            // 优惠券
            BsDiscount discount = discountService.getDetail(body.getString("discountNo"));
            if (discount == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券数据");
            }
            BsOilPriceQuizReward data = null;
            if (body.getLong("id") != null) {
                // 查询调价价格
                data = oilPriceQuizRewardService.getDetail(body.getLong("id"));
                if (data == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到调价价格数据");
                }
            } else {
                data = new BsOilPriceQuizReward();
            }
            data.setPriceAdjustDateId(oilPriceAdjustDate.getId());
            data.setPriceAdjustDate(oilPriceAdjustDate.getDate());
            data.setType(body.getInteger("type"));
            data.setDiscountId(discount.getId());
            data.setDiscountNo(discount.getDiscountNo());
            data.setNumber(body.getInteger("number"));
            oilPriceQuizRewardService.edit(data);

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

        } catch (Exception e) {
            log.error("BsOilPriceAdjustController --> editData() 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, "");
            }
            oilPriceQuizRewardService.del(body.getLong("id"));

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

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

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

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

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

}
