package com.hai.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.config.CommonSysConst;
import com.hai.config.JinZhuJiaYouService;
import com.hai.config.ShellGroupService;
import com.hai.entity.HighGasOilGunNo;
import com.hai.entity.HighGasOilPrice;
import com.hai.entity.HighMerchantStore;
import com.hai.entity.SecRegion;
import com.hai.enum_type.JinZhuJiaYouOilNo;
import com.hai.enum_type.MerchantStoreSourceType;
import com.hai.model.HighMerchantModel;
import com.hai.model.HighMerchantStoreModel;
import com.hai.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service("gasService")
public class HighGasServiceImpl implements HighGasService {

    @Resource
    private ShellGroupService shellGroupService;

    @Resource
    private JinZhuJiaYouService jinZhuJiaYouService;

    @Resource
    private HighMerchantService highMerchantService;

    @Resource
    private HighMerchantStoreService highMerchantStoreService;

    @Resource
    private HighGasOilPriceService highGasOilPriceService;

    @Resource
    private HighGasOilGunNoService gasOilGunNoService;

    @Resource
    private CommonService commonService;

    @Override
    public void getJiaHaoYouAllStation() throws Exception {
        HighMerchantModel merchant = highMerchantService.getDetailByKey("HF0801103821");

        JSONObject pObject = shellGroupService.gasPageQueryAllStation(1, 50);
        JSONObject pageInfo = JSON.parseObject(pObject.getString("pageInfo"), JSONObject.class);

        // 总页数
        Integer totalPageNo = pageInfo.getBigDecimal("totalCount").divide(pageInfo.getBigDecimal("pageSize"),0, BigDecimal.ROUND_CEILING).intValue();
        for (int pageNum = 1; pageNum <= totalPageNo; pageNum++) {

            // 查询加油站列表
            JSONObject respObject = shellGroupService.gasPageQueryAllStation(pageNum, 50);
            JSONArray stationArray = JSONObject.parseObject(respObject.getString("infoList"), JSONArray.class);
            for (Object stationObject : stationArray) {
                JSONObject station = (JSONObject) stationObject;

                // 查询油站
                HighMerchantStore store = highMerchantStoreService.getMerStoreDetailByKey(station.getString("stationCode"));

                if (store != null) {
                    store.setPrestoreType(0);
                    store.setMerchantId(merchant.getId());
                    store.setCompanyId(merchant.getCompanyId());
                    store.setStoreKey(station.getString("stationCode"));
                    store.setStoreName(station.getString("stationName"));
                    store.setStoreLogo(CommonSysConst.getSysConfig().getGasDefaultOilStationImg());
                    store.setRegionId(station.getLong("provinceId"));
                    store.setRegionName(station.getString("provinceName"));
                    store.setAddress(station.getString("stationAddress"));
                    store.setLongitude(station.getString("longitude"));
                    store.setLatitude(station.getString("latitude"));
                    store.setStatus(station.getString("status").equals("ABLE")?1:2);
                    store.setOperatorId(0L);
                    store.setOperatorName("系统创建");
                    store.setUpdateTime(new Date());
                    highMerchantStoreService.updateMerchantStoreDetail(store);

                    JSONArray oilPriceList = station.getJSONArray("oilPriceList");
                    for (Object oilPrice : oilPriceList) {
                        JSONObject oilPriceObject = JSONObject.parseObject(JSONObject.toJSONString(oilPrice));
                        // 查询门店油号
                        HighGasOilPrice highGasOilPrice = highGasOilPriceService.getGasOilPriceByStoreAndOilNo(store.getId(), oilPriceObject.getInteger("goodsCode"));
                        if (highGasOilPrice == null) {
                            highGasOilPrice = new HighGasOilPrice();
                            highGasOilPrice.setMerchantStoreId(store.getId());
                            highGasOilPrice.setOilNo(oilPriceObject.getInteger("goodsCode"));
                            highGasOilPrice.setOilNoName(oilPriceObject.getString("goodsCode") + "#");
                            highGasOilPrice.setGasStationDrop(oilPriceObject.getJSONObject("listedPrice").getBigDecimal("amount").subtract(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount")));
                            highGasOilPrice.setPriceVip(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount"));
                            highGasOilPrice.setPriceGun(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount"));
                            highGasOilPrice.setPriceOfficial(oilPriceObject.getJSONObject("listedPrice").getBigDecimal("amount"));

                            // 油品类型  1:汽油:2:柴油;3:天然气
                            if (oilPriceObject.getString("goodsGroupType").equals("GASOLINE")) {
                                highGasOilPrice.setOilType(1);
                                highGasOilPrice.setOilTypeName("汽油");
                            } else if (oilPriceObject.getString("goodsGroupType").equals("DIESEL_OIL")) {
                                highGasOilPrice.setOilType(2);
                                highGasOilPrice.setOilTypeName("柴油");
                            } else if (oilPriceObject.getString("goodsGroupType").equals("NATURAL_GAS")) {
                                highGasOilPrice.setOilType(3);
                                highGasOilPrice.setOilTypeName("天然气");
                            }
                            highGasOilPriceService.editGasOilPrice(highGasOilPrice);

                            JSONArray oilGunNoList = oilPriceObject.getJSONArray("oilGunNoList");
                            for (Object o : oilGunNoList) {
                                HighGasOilGunNo gasOilGunNo = gasOilGunNoService.getDetailByStoreAndGunNo(highGasOilPrice.getMerchantStoreId(), Integer.parseInt(o.toString()));
                                if (gasOilGunNo == null) {
                                    gasOilGunNo = new HighGasOilGunNo();
                                    gasOilGunNo.setGasOilPriceId(highGasOilPrice.getId());
                                    gasOilGunNo.setStoreId(highGasOilPrice.getMerchantStoreId());
                                    gasOilGunNo.setOilNo(highGasOilPrice.getOilNo());
                                    gasOilGunNo.setOilNoName(highGasOilPrice.getOilNoName());
                                    gasOilGunNo.setOilType(highGasOilPrice.getOilType());
                                    gasOilGunNo.setOilTypeName(highGasOilPrice.getOilTypeName());
                                    gasOilGunNo.setGunNo(Integer.parseInt(o.toString()));
                                    gasOilGunNo.setStatus(1);
                                    gasOilGunNoService.editGunNo(gasOilGunNo);
                                }
                            }

                        } else {
                            highGasOilPrice.setMerchantStoreId(store.getId());
                            highGasOilPrice.setOilNo(oilPriceObject.getInteger("goodsCode"));
                            highGasOilPrice.setOilNoName(oilPriceObject.getString("goodsCode") + "#");
                            highGasOilPrice.setGasStationDrop(oilPriceObject.getJSONObject("listedPrice").getBigDecimal("amount").subtract(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount")));
                            highGasOilPrice.setPriceVip(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount"));
                            highGasOilPrice.setPriceGun(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount"));
                            highGasOilPrice.setPriceOfficial(oilPriceObject.getJSONObject("listedPrice").getBigDecimal("amount"));

                            // 油品类型  1:汽油:2:柴油;3:天然气
                            if (oilPriceObject.getString("goodsGroupType").equals("GASOLINE")) {
                                highGasOilPrice.setOilType(1);
                                highGasOilPrice.setOilTypeName("汽油");
                            } else if (oilPriceObject.getString("goodsGroupType").equals("DIESEL_OIL")) {
                                highGasOilPrice.setOilType(2);
                                highGasOilPrice.setOilTypeName("柴油");
                            } else if (oilPriceObject.getString("goodsGroupType").equals("NATURAL_GAS")) {
                                highGasOilPrice.setOilType(3);
                                highGasOilPrice.setOilTypeName("天然气");
                            }
                            highGasOilPriceService.editGasOilPrice(highGasOilPrice);

                            JSONArray oilGunNoList = oilPriceObject.getJSONArray("oilGunNoList");
                            for (Object o : oilGunNoList) {
                                HighGasOilGunNo gasOilGunNo = gasOilGunNoService.getDetailByStoreAndGunNo(highGasOilPrice.getMerchantStoreId(), Integer.parseInt(o.toString()));
                                if (gasOilGunNo == null) {
                                    gasOilGunNo = new HighGasOilGunNo();
                                    gasOilGunNo.setGasOilPriceId(highGasOilPrice.getId());
                                    gasOilGunNo.setStoreId(highGasOilPrice.getMerchantStoreId());
                                    gasOilGunNo.setOilNo(highGasOilPrice.getOilNo());
                                    gasOilGunNo.setOilNoName(highGasOilPrice.getOilNoName());
                                    gasOilGunNo.setOilType(highGasOilPrice.getOilType());
                                    gasOilGunNo.setOilTypeName(highGasOilPrice.getOilTypeName());
                                    gasOilGunNo.setGunNo(Integer.parseInt(o.toString()));
                                    gasOilGunNo.setStatus(1);
                                    gasOilGunNoService.editGunNo(gasOilGunNo);
                                }
                            }
                        }
                    }
                } else {
                    store = new HighMerchantStore();
                    store.setPrestoreType(0);
                    store.setType(1);
                    store.setSourceType(MerchantStoreSourceType.type3.getNumber());
                    store.setMerchantId(merchant.getId());
                    store.setCompanyId(merchant.getCompanyId());
                    store.setStoreKey(station.getString("stationCode"));
                    store.setStoreName(station.getString("stationName"));
                    store.setStoreLogo(CommonSysConst.getSysConfig().getGasDefaultOilStationImg());
                    store.setRegionId(station.getLong("provinceId"));
                    store.setRegionName(station.getString("provinceName"));
                    store.setAddress(station.getString("stationAddress"));
                    store.setLongitude(station.getString("longitude"));
                    store.setLatitude(station.getString("latitude"));
                    store.setStatus(station.getString("status").equals("ABLE")?1:2);
                    store.setOperatorId(0L);
                    store.setOperatorName("系统创建");
                    store.setCreateTime(new Date());
                    store.setUpdateTime(new Date());

                    HighMerchantStoreModel merchantStoreModel = new HighMerchantStoreModel();
                    BeanUtils.copyProperties(store, merchantStoreModel);
                    highMerchantStoreService.insertMerchantStore(merchantStoreModel);

                    JSONArray oilPriceList = station.getJSONArray("oilPriceList");
                    for (Object oilPrice : oilPriceList) {
                        JSONObject oilPriceObject = JSONObject.parseObject(JSONObject.toJSONString(oilPrice));
                        HighGasOilPrice highGasOilPrice = new HighGasOilPrice();
                        highGasOilPrice.setMerchantStoreId(merchantStoreModel.getId());
                        highGasOilPrice.setOilNo(oilPriceObject.getInteger("goodsCode"));
                        highGasOilPrice.setOilNoName(oilPriceObject.getString("goodsCode") + "#");
                        highGasOilPrice.setPreferentialMargin(new BigDecimal("0"));
                        highGasOilPrice.setGasStationDrop(oilPriceObject.getJSONObject("listedPrice").getBigDecimal("amount").subtract(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount")));
                        highGasOilPrice.setPriceVip(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount"));
                        highGasOilPrice.setPriceGun(oilPriceObject.getJSONObject("goodsPrice").getBigDecimal("amount"));
                        highGasOilPrice.setPriceOfficial(oilPriceObject.getJSONObject("listedPrice").getBigDecimal("amount"));

                        // 油品类型  1:汽油:2:柴油;3:天然气
                        if (oilPriceObject.getString("goodsGroupType").equals("GASOLINE")) {
                            highGasOilPrice.setOilType(1);
                            highGasOilPrice.setOilTypeName("汽油");
                        } else if (oilPriceObject.getString("goodsGroupType").equals("DIESEL_OIL")) {
                            highGasOilPrice.setOilType(2);
                            highGasOilPrice.setOilTypeName("柴油");
                        } else if (oilPriceObject.getString("goodsGroupType").equals("NATURAL_GAS")) {
                            highGasOilPrice.setOilType(3);
                            highGasOilPrice.setOilTypeName("天然气");
                        }
                        highGasOilPriceService.editGasOilPrice(highGasOilPrice);

                        JSONArray oilGunNoList = oilPriceObject.getJSONArray("oilGunNoList");
                        for (Object o : oilGunNoList) {
                            HighGasOilGunNo gasOilGunNo = new HighGasOilGunNo();
                            gasOilGunNo.setGasOilPriceId(highGasOilPrice.getId());
                            gasOilGunNo.setStoreId(highGasOilPrice.getMerchantStoreId());
                            gasOilGunNo.setOilNo(highGasOilPrice.getOilNo());
                            gasOilGunNo.setOilNoName(highGasOilPrice.getOilNoName());
                            gasOilGunNo.setOilType(highGasOilPrice.getOilType());
                            gasOilGunNo.setOilTypeName(highGasOilPrice.getOilTypeName());
                            gasOilGunNo.setGunNo(Integer.parseInt(o.toString()));
                            gasOilGunNo.setStatus(1);
                            gasOilGunNoService.editGunNo(gasOilGunNo);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void getJinZhuAllStation() throws Exception {
        HighMerchantModel merchant = highMerchantService.getDetailByKey("HF0802153624");

        JSONObject pageInfo = jinZhuJiaYouService.getStationListPage(0, 50).getJSONObject("data");

        // 总页数
        for (int pageNum = 0; pageNum < pageInfo.getInteger("totalPages"); pageNum++) {
            // 查询加油站列表
            JSONObject respObject = jinZhuJiaYouService.getStationListPage(pageNum, 50).getJSONObject("data");
            JSONArray stationArray = respObject.getJSONArray("content");

            for (Object stationObject : stationArray) {
                JSONObject station = (JSONObject) stationObject;

                // 查询油站
                HighMerchantStore store = highMerchantStoreService.getMerStoreDetailByKey(station.getString("code"));

                // 查询区域
                SecRegion region = commonService.getRegionsById(station.getLong("provinceId"));

                if (store != null) {
                    store.setPrestoreType(0);
                    store.setMerchantId(merchant.getId());
                    store.setCompanyId(merchant.getCompanyId());
                    store.setStoreKey(station.getString("code"));
                    store.setStoreName(station.getString("name"));
                    store.setStoreLogo(CommonSysConst.getSysConfig().getGasDefaultOilStationImg());
                    store.setRegionId(station.getLong("provinceId"));
                    store.setRegionName(region!=null?region.getRegionName():null);
                    store.setAddress(station.getString("address"));
                    store.setLongitude(station.getString("longitude"));
                    store.setLatitude(station.getString("latitude"));
                    store.setStatus(station.getString("status").equals("VALID")?1:2);
                    store.setOperatorId(0L);
                    store.setOperatorName("系统创建");
                    store.setUpdateTime(new Date());
                    highMerchantStoreService.updateMerchantStoreDetail(store);

                    JSONArray oilPriceList = station.getJSONArray("oils");
                    for (Object oilPrice : oilPriceList) {
                        JSONObject oilPriceObject = JSONObject.parseObject(JSONObject.toJSONString(oilPrice));

                        // 查询油号
                        JinZhuJiaYouOilNo oilNo = JinZhuJiaYouOilNo.getDataByCode(oilPriceObject.getString("oilNo"));
                        if (oilNo != null) {
                            // 查询门店油号
                            HighGasOilPrice highGasOilPrice = highGasOilPriceService.getGasOilPriceByStoreAndOilNo(store.getId(), oilNo.getOilNo());
                            if (highGasOilPrice == null) {
                                highGasOilPrice = new HighGasOilPrice();
                                highGasOilPrice.setMerchantStoreId(store.getId());
                                highGasOilPrice.setOilType(oilNo!=null?oilNo.getOilType():null);
                                highGasOilPrice.setOilTypeName(oilNo!=null?oilNo.getOilTypeName():null);
                                highGasOilPrice.setOilNo(oilNo!=null?oilNo.getOilNo():null);
                                highGasOilPrice.setOilNoName(oilNo!=null?oilNo.getOilNoName():null);

                                highGasOilPrice.setPreferentialMargin(new BigDecimal("0"));
                                highGasOilPrice.setGasStationDrop(oilPriceObject.getBigDecimal("guidancePrice").subtract(oilPriceObject.getBigDecimal("listingPrice")));
                                highGasOilPrice.setPriceCost(oilPriceObject.getBigDecimal("merchantStrikePrice"));
                                highGasOilPrice.setPriceVip(oilPriceObject.getBigDecimal("listingPrice"));
                                highGasOilPrice.setPriceGun(oilPriceObject.getBigDecimal("listingPrice"));
                                highGasOilPrice.setPriceOfficial(oilPriceObject.getBigDecimal("guidancePrice"));
                                highGasOilPriceService.editGasOilPrice(highGasOilPrice);

                                JSONArray oilGunNoList = oilPriceObject.getJSONArray("guns");
                                for (Object o : oilGunNoList) {
                                    HighGasOilGunNo gasOilGunNo = gasOilGunNoService.getDetailByStoreAndGunNo(highGasOilPrice.getMerchantStoreId(), Integer.parseInt(o.toString()));
                                    if (gasOilGunNo == null) {
                                        gasOilGunNo = new HighGasOilGunNo();
                                        gasOilGunNo.setGasOilPriceId(highGasOilPrice.getId());
                                        gasOilGunNo.setStoreId(highGasOilPrice.getMerchantStoreId());
                                        gasOilGunNo.setOilNo(highGasOilPrice.getOilNo());
                                        gasOilGunNo.setOilNoName(highGasOilPrice.getOilNoName());
                                        gasOilGunNo.setOilType(highGasOilPrice.getOilType());
                                        gasOilGunNo.setOilTypeName(highGasOilPrice.getOilTypeName());
                                        gasOilGunNo.setGunNo(Integer.parseInt(o.toString()));
                                        gasOilGunNo.setStatus(1);
                                        gasOilGunNoService.editGunNo(gasOilGunNo);
                                    }
                                }

                            } else {
                                highGasOilPrice.setGasStationDrop(oilPriceObject.getBigDecimal("guidancePrice").subtract(oilPriceObject.getBigDecimal("listingPrice")));
                                highGasOilPrice.setPriceCost(oilPriceObject.getBigDecimal("merchantStrikePrice"));
                                highGasOilPrice.setPriceVip(oilPriceObject.getBigDecimal("listingPrice"));
                                highGasOilPrice.setPriceGun(oilPriceObject.getBigDecimal("listingPrice"));
                                highGasOilPrice.setPriceOfficial(oilPriceObject.getBigDecimal("guidancePrice"));
                                highGasOilPriceService.editGasOilPrice(highGasOilPrice);

                                JSONArray oilGunNoList = oilPriceObject.getJSONArray("guns");
                                for (Object o : oilGunNoList) {
                                    HighGasOilGunNo gasOilGunNo = gasOilGunNoService.getDetailByStoreAndGunNo(highGasOilPrice.getMerchantStoreId(), Integer.parseInt(o.toString()));
                                    if (gasOilGunNo == null) {
                                        gasOilGunNo = new HighGasOilGunNo();
                                        gasOilGunNo.setGasOilPriceId(highGasOilPrice.getId());
                                        gasOilGunNo.setStoreId(highGasOilPrice.getMerchantStoreId());
                                        gasOilGunNo.setOilNo(highGasOilPrice.getOilNo());
                                        gasOilGunNo.setOilNoName(highGasOilPrice.getOilNoName());
                                        gasOilGunNo.setOilType(highGasOilPrice.getOilType());
                                        gasOilGunNo.setOilTypeName(highGasOilPrice.getOilTypeName());
                                        gasOilGunNo.setGunNo(Integer.parseInt(o.toString()));
                                        gasOilGunNo.setStatus(1);
                                        gasOilGunNoService.editGunNo(gasOilGunNo);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    store = new HighMerchantStore();
                    store.setPrestoreType(0);
                    store.setType(1);
                    store.setSourceType(MerchantStoreSourceType.type4.getNumber());
                    store.setMerchantId(merchant.getId());
                    store.setCompanyId(merchant.getCompanyId());
                    store.setStoreKey(station.getString("code"));
                    store.setStoreName(station.getString("name"));
                    store.setStoreLogo(CommonSysConst.getSysConfig().getGasDefaultOilStationImg());
                    store.setRegionId(station.getLong("provinceId"));
                    store.setRegionName(station.getString("provinceName"));
                    store.setAddress(station.getString("address"));
                    store.setLongitude(station.getString("longitude"));
                    store.setLatitude(station.getString("latitude"));
                    store.setStatus(station.getString("status").equals("VALID")?1:2);
                    store.setOperatorId(0L);
                    store.setOperatorName("系统创建");
                    store.setCreateTime(new Date());
                    store.setUpdateTime(new Date());

                    HighMerchantStoreModel merchantStoreModel = new HighMerchantStoreModel();
                    BeanUtils.copyProperties(store, merchantStoreModel);
                    highMerchantStoreService.insertMerchantStore(merchantStoreModel);

                    JSONArray oilPriceList = station.getJSONArray("oils");
                    for (Object oilPrice : oilPriceList) {
                        JSONObject oilPriceObject = JSONObject.parseObject(JSONObject.toJSONString(oilPrice));

                        // 查询油号
                        JinZhuJiaYouOilNo oilNo = JinZhuJiaYouOilNo.getDataByCode(oilPriceObject.getString("oilNo"));

                        HighGasOilPrice highGasOilPrice = new HighGasOilPrice();
                        highGasOilPrice.setMerchantStoreId(merchantStoreModel.getId());
                        highGasOilPrice.setOilType(oilNo!=null?oilNo.getOilType():null);
                        highGasOilPrice.setOilTypeName(oilNo!=null?oilNo.getOilTypeName():null);
                        highGasOilPrice.setOilNo(oilNo!=null?oilNo.getOilNo():null);
                        highGasOilPrice.setOilNoName(oilNo!=null?oilNo.getOilNoName():null);

                        highGasOilPrice.setPreferentialMargin(new BigDecimal("0"));
                        highGasOilPrice.setGasStationDrop(oilPriceObject.getBigDecimal("guidancePrice").subtract(oilPriceObject.getBigDecimal("listingPrice")));
                        highGasOilPrice.setPriceCost(oilPriceObject.getBigDecimal("merchantStrikePrice"));
                        highGasOilPrice.setPriceVip(oilPriceObject.getBigDecimal("listingPrice"));
                        highGasOilPrice.setPriceGun(oilPriceObject.getBigDecimal("listingPrice"));
                        highGasOilPrice.setPriceOfficial(oilPriceObject.getBigDecimal("guidancePrice"));

                        highGasOilPriceService.editGasOilPrice(highGasOilPrice);

                        JSONArray oilGunNoList = oilPriceObject.getJSONArray("guns");
                        for (Object o : oilGunNoList) {
                            HighGasOilGunNo gasOilGunNo = new HighGasOilGunNo();
                            gasOilGunNo.setGasOilPriceId(highGasOilPrice.getId());
                            gasOilGunNo.setStoreId(highGasOilPrice.getMerchantStoreId());
                            gasOilGunNo.setOilNo(highGasOilPrice.getOilNo());
                            gasOilGunNo.setOilNoName(highGasOilPrice.getOilNoName());
                            gasOilGunNo.setOilType(highGasOilPrice.getOilType());
                            gasOilGunNo.setOilTypeName(highGasOilPrice.getOilTypeName());
                            gasOilGunNo.setGunNo(Integer.parseInt(o.toString()));
                            gasOilGunNo.setStatus(1);
                            gasOilGunNoService.editGunNo(gasOilGunNo);
                        }
                    }
                }
            }

        }
    }
}
