package com.cool.store.job;

import cn.hutool.core.date.DateUtil;
import com.cool.store.entity.SyncEcCustomerDO;
import com.cool.store.mapper.HyPartnerLineInfoMapper;
import com.cool.store.mapper.HyPartnerUserInfoMapper;
import com.cool.store.sdk.ec.EcClient;
import com.cool.store.sdk.ec.request.SyncEcCustomerRequest;
import com.cool.store.service.EcSyncService;
import com.cool.store.utils.StringUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class EcSyncDataJob {

    @XxlJob("SyncUserSourceJob")
    public void syncUserSourceJob() {
        XxlJobHelper.log("-------------------------------定时同步客户来源开始-------------------------------");
        syncUserSourceExecute();
        XxlJobHelper.log("-------------------------------定时同步客户来源结束-------------------------------");
        XxlJobHelper.handleSuccess();
    }


    @Value("${ec.baseUrl:null}")
    private String baseUrl;

    @XxlJob("SyncAppletToEcJob")
    public void syncAppletToEcJob() {
        XxlJobHelper.log("-------------------------------定时同步小程序数据到ec开始-------------------------------");
        syncAppletToEcExecute();
        XxlJobHelper.log("-------------------------------定时同步小程序数据到ec结束-------------------------------");
        XxlJobHelper.handleSuccess();
    }

    @Resource
    private EcSyncService ecSyncService;

    @Resource
    private HyPartnerUserInfoMapper hyPartnerUserInfoMapper;


    //定时同步用户来源开始
    private void syncUserSourceExecute() {
        ecSyncService.getChannelSource();
    }

    private final  Integer count=50;

    //定时同步小程序数据到ec
    private void syncAppletToEcExecute() {
        // 获取参数
        String param = XxlJobHelper.getJobParam();
        String startTime = "";
        if (StringUtil.isNotEmpty(param)) {
            XxlJobHelper.log("输入参数为：" + param);
            startTime = param;
        } else {
            startTime = getHourDayDate(-5, 0);
        }
        String endTime = DateUtil.now();
        //获取总数
        int size = hyPartnerUserInfoMapper.selectByHourDateCount(startTime, endTime);
        //执行数
        int counts = size / count;
        //取余，如果大于1，就再加一
        int yu = size % count;
        if (yu > 0) {
            counts += 1;
        }
        XxlJobHelper.log("开始时间为：" + startTime+"，结束时间为："+endTime);
        for (int i = 1; i <= counts; i++) {
            XxlJobHelper.log("执行limit1：" + count * (i - 1)+"，执行limit2："+count);
            List<SyncEcCustomerDO> list = hyPartnerUserInfoMapper.selectByHourDate(startTime, endTime,count * (i - 1),count);
            SyncEcCustomerRequest syncEcCustomerRequest = new SyncEcCustomerRequest();
            syncEcCustomerRequest.setParameter(list);
            EcClient ecClient = new EcClient();
            ecClient.exec(baseUrl,syncEcCustomerRequest);
        }
    }


    private static String getHourDayDate(int hour, int day) {
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.HOUR, hour);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        Date twoDay = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(twoDay);
    }



}
