package com.hfkj.service.gas.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.dao.BsGasOilPriceOfficialMapper;
import com.hfkj.entity.*;
import com.hfkj.service.gas.BsGasOilPriceOfficialService;
import com.hfkj.service.gas.BsGasOilPriceService;
import com.hfkj.service.CommonService;
import com.hfkj.service.gas.BsGasOilPriceWeekService;
import com.hfkj.sysenum.gas.GasOilNoEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("gasOilPriceOfficialService")
public class BsGasOilPriceOfficialServiceImpl implements BsGasOilPriceOfficialService {

    @Resource
    private BsGasOilPriceOfficialMapper gasOilPriceOfficialMapper;
    @Resource
    private BsGasOilPriceService highGasOilPriceService;
    @Resource
    private BsGasOilPriceWeekService gasOilPriceWeekService;
    @Resource
    private CommonService commonService;

    @Override
    public void editPrice(Long regionId, Integer oilPriceZoneId, String oilNo, BigDecimal price) {
        BsGasOilPriceOfficial priceOfficial = getPrice(regionId, oilPriceZoneId, oilNo);
        if (priceOfficial != null) {
            priceOfficial.setPriceOfficial(price);
            gasOilPriceOfficialMapper.updateByPrimaryKey(priceOfficial);
        } else {
            SecDictionary oil = commonService.mappingSysCode("OIL_NO", oilNo.toString());
            SecRegion region = commonService.getRegionsById(regionId);

            priceOfficial = new BsGasOilPriceOfficial();
            priceOfficial.setOilPriceZoneId(oilPriceZoneId);
            priceOfficial.setRegionId(region.getRegionId());
            priceOfficial.setRegionName(region.getRegionName());
            priceOfficial.setOilNo(oil.getCodeValue());
            priceOfficial.setOilNoName(oil.getCodeName());
            priceOfficial.setPriceOfficial(price);
            priceOfficial.setOilType(Integer.valueOf(oil.getExt1()));
            priceOfficial.setOilTypeName(oil.getExt2());
            priceOfficial.setStatus(1);
            if (oilPriceZoneId != null) {
                // 查询价区
                SecDictionary oilPriceZone = commonService.mappingSysCode("OIL_PRICE_ZONE", "" + oilPriceZoneId);
                if (oilPriceZone != null) {
                    priceOfficial.setOilPriceZoneId(Integer.valueOf(oilPriceZone.getCodeValue()));
                    priceOfficial.setOilPriceZoneName(oilPriceZone.getCodeName());
                }
            }
            gasOilPriceOfficialMapper.insert(priceOfficial);
        }
    }

    @Override
    public BsGasOilPriceOfficial getPrice(Long regionId, Integer oilPriceZoneId,String oilNo) {
        BsGasOilPriceOfficialExample example = new BsGasOilPriceOfficialExample();
        BsGasOilPriceOfficialExample.Criteria criteria = example.createCriteria().andRegionIdEqualTo(regionId).andOilNoEqualTo(oilNo);
        if (oilPriceZoneId != null) {
            criteria.andOilPriceZoneIdEqualTo(oilPriceZoneId);
        }
        List<BsGasOilPriceOfficial> list = gasOilPriceOfficialMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsGasOilPriceOfficial> getPriceList(Map<String, Object> param) {
        BsGasOilPriceOfficialExample example = new BsGasOilPriceOfficialExample();
        BsGasOilPriceOfficialExample.Criteria criteria = example.createCriteria().andStatusEqualTo(1);

        if (StringUtils.isNotBlank(MapUtils.getString(param, "regionName"))) {
            criteria.andRegionNameLike("%" + MapUtils.getString(param, "regionName") + "%");
        }

        if (MapUtils.getLong(param, "regionId") != null) {
            criteria.andRegionIdEqualTo(MapUtils.getLong(param, "regionId"));
        }

        if (MapUtils.getLong(param, "oilPriceZoneId") != null) {
            criteria.andOilPriceZoneIdEqualTo(MapUtils.getInteger(param, "oilPriceZoneId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "oilNo"))) {
            criteria.andOilNoEqualTo(MapUtils.getString(param, "oilNo"));
        }

        if (MapUtils.getInteger(param, "oilType") != null) {
            criteria.andOilTypeEqualTo(MapUtils.getInteger(param, "oilType"));
        }

        return gasOilPriceOfficialMapper.selectByExample(example);
    }

    @Override
    public void refreshPriceOfficial() {
        Map<String, String> heardParam = new HashMap<>();
        heardParam.put("Content-Type", "application/json");
        heardParam.put("Authorization", "APPCODE d7fb8449b8424fdbb60f01f53a04ce90");
        JSONObject dataObject = HttpsUtils.doGet("http://ali-todayoil.showapi.com/todayoil", new HashMap<>(), heardParam);

/*        if (dataObject.getInteger("showapi_res_code").equals(0)) {
            JSONObject resBody = dataObject.getJSONObject("showapi_res_body");

            JSONArray oilArray = resBody.getJSONArray("list");
            for (Object oil : oilArray) {
                JSONObject oilObject = (JSONObject) oil;

                SecRegion region = commonService.getRegionsByName(oilObject.getString("prov"));
                if (region != null) {
                    BigDecimal oilNo92 = oilObject.getBigDecimal("p92");
                    if (oilNo92 != null) {
                        editPrice(region.getRegionId(), "92", oilNo92);
                    }

                    BigDecimal oilNo95 = oilObject.getBigDecimal("p95");
                    if (oilNo95 != null) {
                        editPrice(region.getRegionId(), "95", oilNo95);
                    }

                    BigDecimal oilNo98 = oilObject.getBigDecimal("p98");
                    if (oilNo98 != null) {
                        editPrice(region.getRegionId(), "98", oilNo98);
                    }

                    BigDecimal oilNo0 = oilObject.getBigDecimal("p0");
                    if (oilNo0 != null) {
                        editPrice(region.getRegionId(), "0", oilNo0);
                    }

                }
            }
        }*/
    }

    @Override
    public void refreshGasPriceOfficial(Long regionId,Integer oilPriceZoneId, String oilNo) {
        // 查询国标价
        BsGasOilPriceOfficial price = getPrice(regionId, oilPriceZoneId, oilNo);
        if (price != null) {
            // 查询区域下的油品
            List<BsGasOilPrice> list = highGasOilPriceService.getPriceListByRegionAndOilNo(regionId, oilPriceZoneId, oilNo);
            for (BsGasOilPrice gasOilPrice : list) {
                gasOilPrice.setPriceOfficial(price.getPriceOfficial());
                gasOilPrice.setGasStationDrop(price.getPriceOfficial().subtract(gasOilPrice.getPriceVip()));
                highGasOilPriceService.editOilPrice(gasOilPrice);

                Map<String,Object> param = new HashMap<>();
                param.put("merNo", gasOilPrice.getMerNo());
                param.put("oilNo", gasOilPrice.getOilNo());
                List<BsGasOilPriceWeek> oilPriceList = gasOilPriceWeekService.getOilPriceList(param);
                for (BsGasOilPriceWeek week : oilPriceList) {
                    week.setPriceOfficial(gasOilPrice.getPriceOfficial());
                    gasOilPriceWeekService.editData(week);
                }
            }
        }

    }

}
