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.BsOilPriceAdjust;
import com.hfkj.entity.BsOilPriceAdjustDate;
import com.hfkj.entity.SecDictionary;
import com.hfkj.entity.SecRegion;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsOilPriceAdjustDateService;
import com.hfkj.service.BsOilPriceAdjustService;
import com.hfkj.service.CommonService;
import com.hfkj.sysenum.OilPriceAdjustDateResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
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: BsOilPriceAdjustController
 * @author: HuRui
 * @date: 2025/11/18
 **/
@Controller
@RequestMapping(value = "/oilPriceAdjust")
@Api(value = "调价日油价配置")
public class BsOilPriceAdjustController {
    private static Logger log = LoggerFactory.getLogger(BsOilPriceAdjustController.class);
    @Resource
    private BsOilPriceAdjustService oilPriceAdjustService;
    @Resource
    private BsOilPriceAdjustDateService oilPriceAdjustDateService;
    @Resource
    private CommonService commonService;

    @RequestMapping(value = "/editData", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑数据")
    public ResponseData editData(@RequestBody JSONObject body) {
        try {
            if (body == null
                || body.getLong("priceAdjustDateId") == null
                || body.getLong("provinceCode") == null
                || StringUtils.isBlank(body.getString("oilNo"))
                || body.getBigDecimal("priceOfficial") == 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, "未知调价日数据");
            }
            // 获取油品信息
            SecDictionary oilNo = commonService.mappingSysCode("OIL_NO", body.getString("oilNo"));
            if (oilNo == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油品不存在");
            }
            // 获取油品类型
            SecDictionary oilNoType = commonService.mappingSysCode("OIL_NO_TYPE", oilNo.getExt1());
            if (oilNoType == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的的油品类型");
            }
            // 地区
            SecRegion region = commonService.getRegionsById(body.getLong("provinceCode"));
            if (region == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到区域");
            }
            BsOilPriceAdjust data = null;
            if (body.getLong("id") != null) {
                // 查询调价价格
                data = oilPriceAdjustService.getDetail(body.getLong("id"));
                if (data == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到调价价格数据");
                }
            } else {
                data = new BsOilPriceAdjust();
            }
            data.setPriceAdjustDateId(oilPriceAdjustDate.getId());
            data.setPriceAdjustDate(oilPriceAdjustDate.getDate());
            data.setProvinceCode(region.getRegionId());
            data.setProvinceName(region.getRegionName());

            if (body.getLong("oilPriceZoneId") != null) {
                // 查询价区
                SecDictionary oilPriceZone = commonService.mappingSysCode("OIL_PRICE_ZONE", "" + body.getLong("oilPriceZoneId"));
                if (oilPriceZone != null) {
                    data.setOilPriceZoneId(Integer.valueOf(oilPriceZone.getCodeValue()));
                    data.setOilPriceZoneName(oilPriceZone.getCodeName());
                }
            }
            data.setOilType(Integer.valueOf(oilNoType.getCodeValue()));
            data.setOilTypeName(oilNoType.getCodeName());
            data.setOilNo(oilNo.getCodeValue());
            data.setOilNoName(oilNo.getCodeName());
            data.setPriceOfficial(body.getBigDecimal("priceOfficial"));
            oilPriceAdjustService.editData(data);

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

        } catch (Exception e) {
            log.error("BsOilPriceAdjustController --> 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, "");
            }

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

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

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

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

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

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

}
