package com.goafanti.common.task;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.PublicReleaseCountMapper;
import com.goafanti.common.dao.PublicReleaseMapper;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.model.OutPublicReleaseCount;
import com.goafanti.common.model.PublicRelease;
import com.goafanti.common.model.PublicReleaseCount;
import com.goafanti.common.model.outUserFollowCount;
import com.goafanti.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Component
public class UserFollowTask {

    @Autowired
    private PublicReleaseMapper publicReleaseMapper;
    @Autowired
    private PublicReleaseCountMapper publicReleaseCountMapper;
    @Autowired
    private TOrderNewMapper tOrderNewMapper;

    /**
     * 	每月1号凌晨5点轮询统计
     *
     */
    @Scheduled(cron = "0 0 5 1 * ?")
    public void autoTask() throws InterruptedException {
        userFollowStatistics(null);
    }

//    @RequestMapping(value = "/open/userFollowStatistics", method = RequestMethod.GET)
    public void manualTask(Integer m) throws InterruptedException {
        userFollowStatistics(m);
    }
    public void 	userFollowStatistics(Integer m) throws InterruptedException {
        //获取上月1号到本月1号的外出
        Calendar now=getNowDate(m);
        Calendar date=getNowDate(m);
        String ym=getYearMonth(m);
        date.set(Calendar.MONTH,now.get(Calendar.MONTH)-1);
        List<OutPublicReleaseCount> list =publicReleaseMapper.getTimeUserFollow(date.getTime(),now.getTime());
        for (OutPublicReleaseCount out : list) {
            out.setDate(DateUtils.formatDate(date.getTime(),AFTConstants.YYYYMM));
            List<BigDecimal> aList=tOrderNewMapper.selectByUidAndNewUser(out.getUid(),out.getAid(),out.getNewUser(),date.getTime(),now.getTime());
            if (aList.isEmpty()){
                out.setSign(0);
                out.setTotal(new BigDecimal(0));
            }else{
                out.setSign(aList.size());
                BigDecimal total=new BigDecimal(0);
                for (BigDecimal bigDecimal : aList) {
                    total=total.add(bigDecimal);
                }
                out.setTotal(total);
            }
        }
        List<PublicReleaseCount> newList=new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                newList.add((PublicReleaseCount)list.get(i));
                if (50 == newList.size() || i == list.size() - 1) {
                    if (newList.size() > 0) {
                        publicReleaseCountMapper.insertBatch(newList);
                    }
                    newList.clear();
                    Thread.sleep(2000);
                }
            }
        }


    }

    public Calendar getNowDate(Integer m){
        Calendar now=Calendar.getInstance();
        if (m!=null)now.set(2021,m,1);
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.clear(Calendar.MINUTE);
        now.clear(Calendar.SECOND);
        now.clear(Calendar.MILLISECOND);
        return now;
    }
    public String getYearMonth(Integer m){
        Calendar now=Calendar.getInstance();
        if (m!=null)now.set(2021,m,1);
        int y=now.get(Calendar.YEAR);
        int mo=now.get(Calendar.MONTH);
        if(mo<1){
            mo=12;
            y--;
        }
        return y+"-"+mo;
    }


}
