package com.huimv.acquisition.timmertask;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.acquisition.entity.BaseFarmInfoEntity;
import com.huimv.acquisition.entity.DailyTabulateDataEntity;
import com.huimv.acquisition.entity.SowRankTableEntity;
import com.huimv.acquisition.mapper.*;
import com.huimv.acquisition.service.BaseFarmInfoService;
import com.huimv.acquisition.service.EartagRegisterService;
import com.huimv.acquisition.service.MovePigpenService;
import com.huimv.acquisition.service.SowRankTableService;
import com.huimv.acquisition.socket.eartagrecive.util.DateUtil;
import com.huimv.acquisition.socket.eartagrecive.util.DateUtilsPublic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.huimv.acquisition.socket.eartagrecive.util.DateUtilsPublic.DATE_FORMAT;

/**
 * @Author Anchor
 * @Date 2021/8/4 15:03
 * @Version 1.0.1
 */

@Slf4j
@Component
public class provideNewRankData {


//将存储的数据更新到最新的排名表--首先列出所有牧场

    @Autowired
    private EartagDataDao eartagDataDao;

    @Autowired
    private BasePigpenDao basePigpenDao;

    @Autowired
    private CollectorRegisterDao collectorRegisterDao;

    @Autowired
    private MovePigpenService movePigpenService;

    @Autowired
    private MovePigpenDao movePigpenDao;
    @Autowired
    private EartagRegisterService eartagRegisterService;

    @Autowired
    private EartagRegisterDao eartagRegisterDao;

    @Autowired
    private BaseFarmInfoDao baseFarmInfoDao;

    @Autowired
    private BaseFarmInfoService baseFarmInfoService;


    @Autowired
    private DailyTabulateDataDao dailyTabulateDataDao;


    @Autowired
    private SowRankTableService sowRankTableService;

    @Autowired
    private SowRankTableDao sowRankTableDao;


    //首先查找出所有的牧场id

    // @Scheduled(cron = "0 0 12 * * ?")
  //  @Scheduled(cron = "0/10 * * * * ?")
    private void settingRegisting() throws ParseException {

        String date = DateUtilsPublic.formatDate(new Date(), DATE_FORMAT);
        String datebegan = date + " 00:00:00";
        String dateend = date + " 23:59:00";
        Date datebegan1 = DateUtil.parseDateTime(datebegan);
        Date dateeend = DateUtil.parseDateTime(dateend);

        LambdaQueryWrapper<DailyTabulateDataEntity> lambdaQuery2 = Wrappers.lambdaQuery();
        lambdaQuery2.between(DailyTabulateDataEntity::getStatisticDate, datebegan1, dateeend);
        lambdaQuery2.orderByDesc(DailyTabulateDataEntity::getStockTotal);//母猪总存栏
        List<DailyTabulateDataEntity> dailyTabulateDataEntit = dailyTabulateDataDao.selectList(lambdaQuery2);
        int a = 0;
        int b = 0;
        System.out.println();

        //拿出一条判断时间--判断空
        LambdaQueryWrapper<SowRankTableEntity> lambdaQuery3 = Wrappers.lambdaQuery();
        lambdaQuery3.orderByDesc(SowRankTableEntity::getId).last("limit 1");
        SowRankTableEntity sowRankTableEntity1 = sowRankTableDao.selectOne(lambdaQuery3);

        Map<String, Object> map = new HashMap<>(   );
        for (DailyTabulateDataEntity dailyTabulateDataEnt : dailyTabulateDataEntit) {
            b++;
            map.put(dailyTabulateDataEnt.getFarmCode(),b);
        }

        if (sowRankTableEntity1 == null) {
            //为空，自动执行刷新流程--添加所有数据
            for (DailyTabulateDataEntity dailyTabulateDataEnt : dailyTabulateDataEntit) {
                System.out.println(dailyTabulateDataEnt.getId());
                SowRankTableEntity sowRankTableEntity = new SowRankTableEntity();
                sowRankTableEntity.setFarmCode(dailyTabulateDataEnt.getFarmCode());
                sowRankTableEntity.setCreate_time(new Date());
                a++;
                sowRankTableEntity.setRank(String.valueOf(a));
                sowRankTableEntity.setSowLivestock(dailyTabulateDataEnt.getStockTotal());//总存栏量--
                sowRankTableEntity.setOutStockCount(1);//
                sowRankTableService.save(sowRankTableEntity);
                //if(data.getTime().after(startTime)&data.getTime().before(DateUtilsPublic.addDateHour(startTime,1))){
            }
        }else {
            //现在是不为空的情况----旧的保存，刷新排名--新的就直接添加进去----目前没有解决新增牧场排名的问题
            //找出所有的
            System.out.println("数据   更新"); //找出所有数据--更新排名
            LambdaQueryWrapper<SowRankTableEntity> lambdaQuery4 = Wrappers.lambdaQuery();
            List<SowRankTableEntity> sowRankTableEntities = sowRankTableDao.selectList(lambdaQuery4);
            System.out.println(sowRankTableEntities);

            for (SowRankTableEntity sowRankTableEntity : sowRankTableEntities) {
                if (map.containsKey(sowRankTableEntity.getFarmCode() ) ){

                    LambdaQueryWrapper<BaseFarmInfoEntity> lambdaQuery5 = Wrappers.lambdaQuery();
                    lambdaQuery5.eq(BaseFarmInfoEntity::getFarmCode,sowRankTableEntity.getFarmCode() );
                    BaseFarmInfoEntity baseFarmInfoEntity = baseFarmInfoDao.selectOne(lambdaQuery5);

                    sowRankTableEntity.setFarmName(baseFarmInfoEntity.getFarmName());
                    sowRankTableEntity.setRank(String.valueOf( map.get( (sowRankTableEntity.getFarmCode() ))));

                    sowRankTableEntity.setCreate_time(new Date());
                    sowRankTableService.updateById(sowRankTableEntity );
                    System.out.println("新的排名数据为"+sowRankTableEntity);
                    map.remove(sowRankTableEntity.getFarmCode());
                   // sowRankTableService.save();
                }
                //如果不包含，说明猪舍不参与排名，，删除
                //sowRankTableService.removeById(sowRankTableEntity.getId());
            }
           if(!map.isEmpty()){
               //操作结束。如果Map还有数据，就进行新增操作
               for (Map.Entry<String, Object> entry : map.entrySet()) {

                   SowRankTableEntity sowRankTableEntity = new SowRankTableEntity();
                   sowRankTableEntity.setFarmCode(entry.getKey());
                   sowRankTableEntity.setCreate_time(new Date());
                   a++;
                   sowRankTableEntity.setRank(String.valueOf(a));
                   for (DailyTabulateDataEntity dailyTabulateDataEntity : dailyTabulateDataEntit) {
                       if(dailyTabulateDataEntity.getFarmCode().equals(entry.getKey()));
                       sowRankTableEntity.setSowLivestock(dailyTabulateDataEntity.getSowStock());//总存栏量
                   }

                   sowRankTableEntity.setOutStockCount(1);
                   sowRankTableService.save(sowRankTableEntity);


                   System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
               }


           }






        }
        //对于新增的如何处理，不包括的有如何处理






    }
}


