package com.mortals.xhx.modules.service;

import com.mortals.framework.model.PageInfo;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.MenuLinkType;
import com.mortals.xhx.common.utils.DateByAffairUtils;
import com.mortals.xhx.modules.information.model.InformationEntity;
import com.mortals.xhx.modules.information.model.InformationQuery;
import com.mortals.xhx.modules.information.service.InformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @Date: 2021-08-2021/8/12-10:13
 * @Description: 查询推送表是否存在未处理的数据
 */
@Service
@Slf4j
public class QueryInformationServiceImpl {
    @Autowired
    private InformationService informationService;

    public List<InformationEntity> queryDataThatHasNotBeenPushed(String nodeType, String isSuccess, String isReport,String excuteParam) {
        List<InformationEntity> informationEntities = new ArrayList<>();
        InformationQuery informationQuery = new InformationQuery();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPrePageResult(Integer.valueOf(excuteParam));
        informationQuery.setNodeType(nodeType);
        if (StringUtils.isNotEmpty(isSuccess)) {
            informationQuery.setIsSuccess(isSuccess);
        }
        informationQuery.setIsReport(isReport);
        //失败次数
        informationQuery.setFailTimes(0);
        //本月第一天
        String theFirstDayOfTheMonth = new SimpleDateFormat("yyyy-MM-dd").format(DateByAffairUtils.getMonthFirstDate(new Date()));
        //当月第几天
        String theDayOfTheMonth = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //每月第一天，去查询上月的数据是否存在未处理的数据
        if (theFirstDayOfTheMonth.equals(theDayOfTheMonth)) {
            //获取上月第一天的开始时间
            informationQuery.setCreateTimeStart(
                    DateByAffairUtils.getLastMonthFirstDate(new Date(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").toPattern()));
            //获取上月最后一天的结束时间
            informationQuery.setCreateTimeEnd(
                    DateByAffairUtils.getLastMonthLastDate(new Date(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").toPattern()));
            try {
                informationEntities = informationService.find(informationQuery, pageInfo, null).getList();
            } catch (Exception e) {
                //报错表不存在时，查询当月表数据
                log.info("报错属于业务正常，查询当月表数据");
                informationEntities = queryCurrentMonthTableData(informationQuery, pageInfo);
                return informationEntities;
            }
            if (informationEntities.size() <= 0) {
                informationEntities = queryCurrentMonthTableData(informationQuery, pageInfo);
            }
        } else {
            //上月未存在未处理的数据，则查询当月数据
            informationEntities = queryCurrentMonthTableData(informationQuery, pageInfo);
        }
        return informationEntities;
    }

    private List<InformationEntity> queryCurrentMonthTableData(InformationQuery informationQuery, PageInfo pageInfo) {
        //获取当月的开始时间
        informationQuery.setCreateTimeStart(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateByAffairUtils.getMonthFirstDate(new Date())));
        //获取当前时间
        informationQuery.setCreateTimeEnd(
                DateUtils.convertTime2Str(new Date().getTime(), DateUtils.P_yyyy_MM_dd_HH_mm_ss));
        //表示上个月已全部处理完
        return informationService.find(informationQuery, pageInfo, null).getList();
    }



}
