package com.hai.schedule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.config.QianZhuConfig;
import com.hai.entity.ApiBlxCoupon;
import com.hai.entity.ApiStarbucksProducts;
import com.hai.openApi.config.BlxConfig;
import com.hai.openApi.service.ApiBlxCouponService;
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.List;

@Configuration
public class ApiBlxCouponSchedule {

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

    @Resource
    private ApiBlxCouponService apiBlxCouponService;

    @Resource
    private BlxConfig blxConfig;

    @Scheduled(cron="0 0/60  * * * ?")   //每60分钟执行一次
    public void getBlxCoupon() {
        JSONObject object = blxConfig.queryCouponPage();

        if (object.getInteger("code") == 1) {

            JSONArray jsonArray = object.getJSONObject("data").getJSONArray("records");

            for (int i = 0; i < jsonArray.size(); i++) {
                if (jsonArray.getJSONObject(i).getString("couponCode") != null) {
                    ApiBlxCoupon apiBlxCoupon = apiBlxCouponService.findBlxCoupon(jsonArray.getJSONObject(i).getString("couponCode"));
                    if (apiBlxCoupon == null) {
                        apiBlxCoupon = new ApiBlxCoupon();
                        apiBlxCoupon.setCouponCode(jsonArray.getJSONObject(i).getString("couponCode"));
                        apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                        apiBlxCoupon.setCreateTime(new Date());
                        apiBlxCoupon.setCouponName(jsonArray.getJSONObject(i).getString("couponName"));
                        apiBlxCoupon.setDenomination(jsonArray.getJSONObject(i).getInteger("denomination"));
                        apiBlxCoupon.setUpdateTime(new Date());
                        apiBlxCoupon.setMinPrice(jsonArray.getJSONObject(i).getInteger("minPrice"));
                        apiBlxCoupon.setUseStartTime(jsonArray.getJSONObject(i).getDate("useStartTime"));
                        apiBlxCoupon.setUseEndTime(jsonArray.getJSONObject(i).getDate("useEndTime"));
                        apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                        apiBlxCoupon.setReceiveDays(jsonArray.getJSONObject(i).getInteger("receiveDays"));
                        apiBlxCoupon.setReceiveLimit(jsonArray.getJSONObject(i).getInteger("receiveLimit"));
                        apiBlxCouponService.insertBlxCoupon(apiBlxCoupon);

                    } else {
                        apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                        apiBlxCoupon.setCouponName(jsonArray.getJSONObject(i).getString("couponName"));
                        apiBlxCoupon.setDenomination(jsonArray.getJSONObject(i).getInteger("denomination"));
                        apiBlxCoupon.setUpdateTime(new Date());
                        apiBlxCoupon.setMinPrice(jsonArray.getJSONObject(i).getInteger("minPrice"));
                        apiBlxCoupon.setUseStartTime(jsonArray.getJSONObject(i).getDate("useStartTime"));
                        apiBlxCoupon.setUseEndTime(jsonArray.getJSONObject(i).getDate("useEndTime"));
                        apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                        apiBlxCoupon.setReceiveDays(jsonArray.getJSONObject(i).getInteger("receiveDays"));
                        apiBlxCoupon.setReceiveLimit(jsonArray.getJSONObject(i).getInteger("receiveLimit"));
                        apiBlxCouponService.editBlxCoupon(apiBlxCoupon);
                    }
                }
            }
        }


    }

}
