package com.hai.schedule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.config.QianZhuConfig;
import com.hai.entity.ApiKfcStores;
import com.hai.entity.ApiStarbucksProducts;
import com.hai.service.ApiProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

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

@Configuration
public class ApiThirdProductSchedule {


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

    @Resource
    private ApiProductService apiProductService;


    @Scheduled(cron = "0 0 1 * * ?")   // 获取星巴克产品 每日凌晨1点执行一次
    public void getStarbucksProducts() throws Exception {
        JSONObject jsonObject = QianZhuConfig.getStarbucksProducts(1 , 200);

        JSONObject object = jsonObject.getJSONObject("data");
        JSONArray array = object.getJSONArray("items");
        for (Object data : array) {
            JSONObject dataObject = (JSONObject) data;

            ApiStarbucksProducts starbucksProducts = apiProductService.findStarbucksProductsByGoodsId(dataObject.getLong("id"));

            if (dataObject.getInteger("shelfStatus") == 5) {
                if (starbucksProducts == null) {
                    starbucksProducts = new ApiStarbucksProducts();
                }
                starbucksProducts.setCategoryName(dataObject.getString("categoryName"));
                starbucksProducts.setCream(dataObject.getString("cream"));
                starbucksProducts.setCupSize(dataObject.getString("cupSize"));
                starbucksProducts.setDefaultImage(dataObject.getString("defaultImage"));
                starbucksProducts.setDes(dataObject.getString("des"));
                starbucksProducts.setEspresso(dataObject.getString("espresso"));
                starbucksProducts.setMarketGrandePrice(dataObject.getBigDecimal("marketGrandePrice"));
                starbucksProducts.setMarketTallPrice(dataObject.getBigDecimal("marketTallPrice"));
                starbucksProducts.setMarketVentiPrice(dataObject.getBigDecimal("marketVentiPrice"));
                starbucksProducts.setSalesGrandePrice(dataObject.getBigDecimal("salesGrandePrice"));
                starbucksProducts.setSalesTallPrice(dataObject.getBigDecimal("salesTallPrice"));
                starbucksProducts.setSalesVentiPrice(dataObject.getBigDecimal("salesVentiPrice"));
                starbucksProducts.setMilk(dataObject.getString("milk"));
                starbucksProducts.setMilkBubble(dataObject.getString("milkBubble"));
                starbucksProducts.setName(dataObject.getString("name"));
                starbucksProducts.setTemperature(dataObject.getString("temperature"));
                if (starbucksProducts.getId() == null) {
                    starbucksProducts.setCreateTime(new Date());
                    starbucksProducts.setOperatorId(9999L);
                    starbucksProducts.setOperatorName("系统生成");
                    starbucksProducts.setStatus(100);
                    starbucksProducts.setGoodsId(dataObject.getLong("id"));
                    apiProductService.insertStarbucksProducts(starbucksProducts);
                } else {
                    starbucksProducts.setUpdateTime(new Date());
                    apiProductService.updateStarbucksProducts(starbucksProducts);
                }
            }
        }
    }

    @Scheduled(cron = "0 0 1 * * ?")   // 获取肯德基门店 每日凌晨1点执行一次
    public void getKfcStore() throws Exception {

        JSONObject jsonObject = QianZhuConfig.getKfcStore();

        JSONArray object = jsonObject.getJSONArray("data");
        for (Object data : object) {
            JSONObject dataObject = (JSONObject) data;
            Map<String, Object> mapStore = new HashMap<>();
            mapStore.put("storeCode" , dataObject.getString("storeCode"));
            ApiKfcStores apiKfcStores = apiProductService.findKfcStores(mapStore);

            if (apiKfcStores == null) {
                apiKfcStores = new ApiKfcStores();
            }
            apiKfcStores.setProvinceCode(dataObject.getString("provinceCode"));
            apiKfcStores.setProvinceName(dataObject.getString("provinceName"));
            apiKfcStores.setCityCode(dataObject.getString("cityCode"));
            apiKfcStores.setCityName(dataObject.getString("cityName"));
            apiKfcStores.setCityNameFirstLetter(dataObject.getString("cityNameFirstLetter"));
            apiKfcStores.setKfcCityId(dataObject.getInteger("kfcCityId"));
            apiKfcStores.setKfcCityCode(dataObject.getString("kfcCityCode"));
            apiKfcStores.setStoreName(dataObject.getString("storeName"));
            apiKfcStores.setStoreCode(dataObject.getString("storeCode"));
            apiKfcStores.setLon(dataObject.getString("lon"));
            apiKfcStores.setLat(dataObject.getString("lat"));
            apiKfcStores.setAddress(dataObject.getString("address"));
            apiKfcStores.setStartTime(dataObject.getString("startTime"));
            apiKfcStores.setEndTime(dataObject.getString("endTime"));
            apiKfcStores.setStoreStatus(dataObject.getInteger("storeStatus"));
            apiKfcStores.setShelfStatus(dataObject.getInteger("shelfStatus"));

            if (apiKfcStores.getId() == null) {
                apiKfcStores.setCreateTime(new Date());
                apiKfcStores.setOperatorId(9999L);
                apiKfcStores.setOperatorName("系统生成");
                apiKfcStores.setStatus(100);
                apiProductService.insertKfcStore(apiKfcStores);
            } else {
                apiKfcStores.setUpdateTime(new Date());
                apiProductService.updateKfcStore(apiKfcStores);
            }
        }
    }

}
