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.*;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsOilPriceAdjustDateService;
import com.hfkj.service.BsOilPriceAdjustService;
import com.hfkj.service.CommonService;
import com.hfkj.service.quiz.BsOilPriceQuizUserService;
import com.hfkj.sysenum.OilPriceAdjustDateResultEnum;
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 = "/oilPriceQuizUser")
@Api(value = "调价用户竞猜")
public class BsOilPriceQuizUserController {
    private static Logger log = LoggerFactory.getLogger(BsOilPriceQuizUserController.class);
    @Resource
    private BsOilPriceQuizUserService oilPriceQuizUserService;

    @RequestMapping(value = "/countQuiz", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "统计竞猜")
    public ResponseData countQuiz(@RequestParam(value = "priceAdjustDateId", required = true) Long priceAdjustDateId,
                                  @RequestParam(value = "provinceCode", required = false) Long provinceCode,
                                  @RequestParam(value = "cityCode", required = false) Long cityCode,
                                  @RequestParam(value = "userPhone", required = false) String userPhone,
                                  @RequestParam(value = "quizOption", required = false) Integer quizOption,
                                  @RequestParam(value = "quizResult", required = false) Integer quizResult) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("priceAdjustDateId", priceAdjustDateId);
            param.put("provinceCode", provinceCode);
            param.put("cityCode", cityCode);
            param.put("userPhone", userPhone);
            param.put("quizOption", quizOption);
            param.put("quizResult", quizResult);

            List<BsOilPriceQuizUser> data = oilPriceQuizUserService.getList(param);

            Long riseTotal = data.stream().filter(o -> o.getQuizOption().equals(OilPriceAdjustDateResultEnum.status1.getType())).count();
            Long fallTotal = data.stream().filter(o -> o.getQuizOption().equals(OilPriceAdjustDateResultEnum.status2.getType())).count();
            Long noAdjustTotal = data.stream().filter(o -> o.getQuizOption().equals(OilPriceAdjustDateResultEnum.status3.getType())).count();

            Map<String,Object> map = new HashMap<>();
            map.put("noAdjustTotal", noAdjustTotal); // 不调整
            map.put("noAdjustRate", new BigDecimal(noAdjustTotal.toString()).divide(new BigDecimal(""+data.size()),2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100")));
            map.put("riseTotal", riseTotal);     // 上涨
            map.put("riseRate", new BigDecimal(riseTotal.toString()).divide(new BigDecimal(""+data.size()),2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100")));
            map.put("fallTotal", fallTotal);     // 下跌
            map.put("fallRate", new BigDecimal(fallTotal.toString()).divide(new BigDecimal(""+data.size()),2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100")));
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(value = "priceAdjustDateId", required = true) Long priceAdjustDateId,
                                  @RequestParam(value = "provinceCode", required = false) Long provinceCode,
                                  @RequestParam(value = "cityCode", required = false) Long cityCode,
                                  @RequestParam(value = "userPhone", required = false) String userPhone,
                                  @RequestParam(value = "quizOption", required = false) Integer quizOption,
                                  @RequestParam(value = "quizResult", required = false) Integer quizResult,
                                  @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("provinceCode", provinceCode);
            param.put("cityCode", cityCode);
            param.put("userPhone", userPhone);
            param.put("quizOption", quizOption);
            param.put("quizResult", quizResult);

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

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

}
