package com.hai.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.HttpsUtils;
import com.hai.dao.HighGasOilPriceOfficialMapper;
import com.hai.entity.*;
import com.hai.service.CommonService;
import com.hai.service.HighGasOilPriceOfficialService;
import com.hai.service.HighGasOilPriceService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
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 HighGasOilPriceOfficialServiceImpl implements HighGasOilPriceOfficialService {

    @Resource
    private HighGasOilPriceOfficialMapper gasOilPriceOfficialMapper;

    @Resource
    private HighGasOilPriceService highGasOilPriceService;

    @Resource
    private CommonService commonService;

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

            priceOfficial = new HighGasOilPriceOfficial();
            priceOfficial.setRegionId(region.getRegionId());
            priceOfficial.setRegionName(region.getRegionName());
            priceOfficial.setOilNo(Integer.valueOf(oil.getCodeValue()));
            priceOfficial.setOilNoName(oil.getCodeName());
            priceOfficial.setPriceOfficial(price);
            priceOfficial.setOilType(Integer.valueOf(oil.getExt1()));
            priceOfficial.setOilTypeName(oil.getExt2());
            priceOfficial.setStatus(1);
            gasOilPriceOfficialMapper.insert(priceOfficial);
        }
    }

    @Override
    public HighGasOilPriceOfficial getPrice(Long regionId, Integer oilNo) {
        HighGasOilPriceOfficialExample example = new HighGasOilPriceOfficialExample();
        example.createCriteria().andRegionIdEqualTo(regionId).andOilNoEqualTo(oilNo);
        List<HighGasOilPriceOfficial> list = gasOilPriceOfficialMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighGasOilPriceOfficial> getPriceList(Map<String, Object> param) {
        HighGasOilPriceOfficialExample example = new HighGasOilPriceOfficialExample();
        HighGasOilPriceOfficialExample.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.getInteger(param, "oilNo") != null) {
            criteria.andOilNoEqualTo(MapUtils.getInteger(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 oilNo) {
        Map<String, Object> param = new HashMap<>();
        param.put("regionId", regionId);
        param.put("oilNo", oilNo);

        List<HighGasOilPriceOfficial> priceList = getPriceList(param);
        for (HighGasOilPriceOfficial priceOfficial : priceList) {
            // 查询区域下的油品
            List<HighGasOilPrice> list = highGasOilPriceService.getPriceListByRegionAndOilNo(priceOfficial.getRegionId(), priceOfficial.getOilNo());
            for (HighGasOilPrice gasOilPrice : list) {
                gasOilPrice.setPriceOfficial(priceOfficial.getPriceOfficial());
                gasOilPrice.setPriceGun(priceOfficial.getPriceOfficial().subtract(gasOilPrice.getGasStationDrop()));
                gasOilPrice.setPriceVip(gasOilPrice.getPriceGun().subtract(gasOilPrice.getPreferentialMargin()));
                highGasOilPriceService.editGasOilPrice(gasOilPrice);
            }
        }
    }

}
