package com.hai.schedule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.config.TuanYouConfig;
import com.hai.entity.HighGasOilPrice;
import com.hai.entity.HighGasOilPriceOfficial;
import com.hai.entity.HighMerchantStore;
import com.hai.enum_type.GasOilPriceStatusEnum;
import com.hai.model.HighMerchantModel;
import com.hai.model.HighMerchantStoreModel;
import com.hai.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

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

@Configuration
public class HighGasSchedule {

    private static final Logger log = LoggerFactory.getLogger(HighGasSchedule.class);

    @Resource
    private HighMerchantService highMerchantService;

    @Resource
    private HighMerchantStoreService highMerchantStoreService;

    @Resource
    private HighGasOilPriceService highGasOilPriceService;

    @Resource
    private HighGasOilPriceOfficialService gasOilPriceOfficialService;

    @Resource
    private HighGasService gasService;

    @Scheduled(cron = "0 0 1 * * ?")   //每日凌晨1点执行一次
    public void certification() throws Exception {
        HighMerchantModel merchant = highMerchantService.getMerchantById(39L);
        if (merchant == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户");
        }
        JSONObject jsonObjectP = TuanYouConfig.queryGasInfoListByPage(1, 1000);
        JSONObject resultObjectP = jsonObjectP.getObject("result", JSONObject.class);

        for (int i = 1; i < resultObjectP.getInteger("totalPageNum").intValue();i++) {
            JSONObject jsonObject = TuanYouConfig.queryGasInfoListByPage(i, 1000);
            JSONObject resultObject = jsonObject.getObject("result", JSONObject.class);
            JSONArray jsonArray = resultObject.getJSONArray("gasInfoList");
            HighMerchantStore highMerchantStore;
            HighGasOilPrice highGasOilPrice;
            for (Object gasObject : jsonArray) {
                JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(gasObject));
                HighMerchantStoreModel store = highMerchantStoreService.getMerchantStoreByKey(object.getString("gasId"));
                if (store != null) {
                    store.setType(1);
                    store.setSourceType(2);
                    store.setMerchantId(merchant.getId());
                    store.setCompanyId(merchant.getCompanyId());
                    store.setStoreKey(object.getString("gasId"));
                    store.setStoreName(object.getString("gasName"));
                    store.setStoreLogo(object.getString("gasLogoSmall"));
                    store.setRegionId(object.getLong("provinceCode"));
                    store.setRegionName(object.getString("provinceName"));
                    store.setAddress(object.getString("gasAddress"));
                    store.setLongitude(object.getString("gasAddressLongitude"));
                    store.setLatitude(object.getString("gasAddressLatitude"));
                    store.setStatus(object.getInteger("gasStatus"));
                    store.setOperatorId(0L);
                    store.setOperatorName("系统创建");
                    store.setUpdateTime(new Date());
                    store.setExt1(object.getString("gasSourceId"));
                    highMerchantStoreService.updateMerchantStoreDetail(store);

                    JSONArray oilPriceList = object.getJSONArray("oilPriceList");
                    for (Object oilPrice : oilPriceList) {
                        JSONObject oilPriceObject = JSONObject.parseObject(JSONObject.toJSONString(oilPrice));
                        // 查询门店油号
                        highGasOilPrice = highGasOilPriceService.getGasOilPriceByStoreAndOilNo(store.getId(), oilPriceObject.getInteger("oilNo"));
                        if (highGasOilPrice == null) {
                            highGasOilPrice = new HighGasOilPrice();
                            highGasOilPrice.setMerchantStoreId(store.getId());
                            highGasOilPrice.setOilNo(oilPriceObject.getInteger("oilNo"));
                            highGasOilPrice.setOilNoName(oilPriceObject.getString("oilNoName"));
                            highGasOilPrice.setPriceVip(oilPriceObject.getBigDecimal("priceVip"));
                            highGasOilPrice.setPriceGun(oilPriceObject.getBigDecimal("priceGun"));
                            highGasOilPrice.setPriceOfficial(oilPriceObject.getBigDecimal("priceOfficial"));
                            highGasOilPrice.setOilType(oilPriceObject.getInteger("oilType"));
                            highGasOilPrice.setOilTypeName(oilPriceObject.getString("oilTypeName"));
                            highGasOilPrice.setStatus(GasOilPriceStatusEnum.status1.getStatus());
                        } else {
                            highGasOilPrice.setMerchantStoreId(store.getId());
                            highGasOilPrice.setOilNo(oilPriceObject.getInteger("oilNo"));
                            highGasOilPrice.setOilNoName(oilPriceObject.getString("oilNoName"));
                            highGasOilPrice.setPriceVip(oilPriceObject.getBigDecimal("priceVip"));
                            highGasOilPrice.setPriceGun(oilPriceObject.getBigDecimal("priceGun"));
                            highGasOilPrice.setPriceOfficial(oilPriceObject.getBigDecimal("priceOfficial"));
                            highGasOilPrice.setOilType(oilPriceObject.getInteger("oilType"));
                            highGasOilPrice.setOilTypeName(oilPriceObject.getString("oilTypeName"));
                        }
                        highGasOilPriceService.editGasOilPrice(highGasOilPrice);
                    }
                } else {
                    highMerchantStore = new HighMerchantStore();
                    highMerchantStore.setType(1);
                    highMerchantStore.setSourceType(2);
                    highMerchantStore.setMerchantId(merchant.getId());
                    highMerchantStore.setCompanyId(merchant.getCompanyId());
                    highMerchantStore.setStoreKey(object.getString("gasId"));
                    highMerchantStore.setStoreName(object.getString("gasName"));
                    highMerchantStore.setStoreLogo(object.getString("gasLogoSmall"));
                    highMerchantStore.setRegionId(object.getLong("provinceCode"));
                    highMerchantStore.setRegionName(object.getString("provinceName"));
                    highMerchantStore.setAddress(object.getString("gasAddress"));
                    highMerchantStore.setLongitude(object.getString("gasAddressLongitude"));
                    highMerchantStore.setLatitude(object.getString("gasAddressLatitude"));
                    highMerchantStore.setStatus(1);
                    highMerchantStore.setOperatorId(0L);
                    highMerchantStore.setOperatorName("系统创建");
                    highMerchantStore.setCreateTime(new Date());
                    highMerchantStore.setUpdateTime(new Date());
                    highMerchantStore.setExt1(object.getString("gasSourceId"));
                    HighMerchantStoreModel merchantStoreModel = new HighMerchantStoreModel();
                    BeanUtils.copyProperties(highMerchantStore, merchantStoreModel);
                    highMerchantStoreService.insertMerchantStore(merchantStoreModel);

                    JSONArray oilPriceList = object.getJSONArray("oilPriceList");
                    for (Object oilPrice : oilPriceList) {
                        JSONObject oilPriceObject = JSONObject.parseObject(JSONObject.toJSONString(oilPrice));
                        highGasOilPrice = new HighGasOilPrice();
                        highGasOilPrice.setMerchantStoreId(merchantStoreModel.getId());
                        highGasOilPrice.setOilNo(oilPriceObject.getInteger("oilNo"));
                        highGasOilPrice.setOilNoName(oilPriceObject.getString("oilNoName"));
                        highGasOilPrice.setPriceVip(oilPriceObject.getBigDecimal("priceVip"));
                        highGasOilPrice.setPriceGun(oilPriceObject.getBigDecimal("priceGun"));
                        highGasOilPrice.setPriceOfficial(oilPriceObject.getBigDecimal("priceOfficial"));
                        highGasOilPrice.setOilType(oilPriceObject.getInteger("oilType"));
                        highGasOilPrice.setOilTypeName(oilPriceObject.getString("oilTypeName"));
                        highGasOilPriceService.editGasOilPrice(highGasOilPrice);
                    }
                }
            }
        }
    }

/*    @Scheduled(cron = "0 1 0 * * ?")   //每日凌晨12点01分执行一次
    public void getJiaHaoYouAllStation() throws Exception {
        gasService.getJiaHaoYouAllStation();
    }

    @Scheduled(cron = "0 1 0 * * ?")   //每日凌晨12点01分执行一次
    public void getJinZhuAllStation() throws Exception {
        gasService.getJinZhuAllStation();
    }*/

    @Scheduled(cron = "0 40 7 13 * ?")
    public void getJinZhuAllStation() throws Exception {
        HighMerchantStoreModel store = highMerchantStoreService.getMerchantStoreByKey("11052456");
        if (store != null) {
            store.setStatus(0);
            highMerchantStoreService.updateMerchantStoreDetail(store);
        }
    }

   /* @Scheduled(cron = "0 30 7 * * ?")   //每日7点1分执行一次
    public void refreshPriceOfficial() throws Exception {
        gasOilPriceOfficialService.refreshPriceOfficial();


        gasOilPriceOfficialService.refreshGasPriceOfficial(null, null);
    }*/
}
