嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-service/src/main/java/com/hai/service/impl/HighGasOilPriceOfficialServ...

105 lines
4.1 KiB

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.HighGasOilPriceOfficial;
import com.hai.entity.HighGasOilPriceOfficialExample;
import com.hai.entity.SecDictionary;
import com.hai.entity.SecRegion;
import com.hai.service.CommonService;
import com.hai.service.HighGasOilPriceOfficialService;
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 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 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);
}
}
}
}
}
}