package com.ruoyi.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.demo.entity.AttentionPool;
import com.ruoyi.demo.entity.AttentionPoolStatistics;
import com.ruoyi.demo.entity.BusinessOpportunitiesClue;
import com.ruoyi.demo.entity.FollowUpRecord;
import com.ruoyi.demo.entity.bo.AttentionPoolStatisticsBo;
import com.ruoyi.demo.entity.bo.WorkTableBo;
import com.ruoyi.demo.mapper.AttentionPoolDao;
import com.ruoyi.demo.mapper.AttentionPoolStatisticsDao;
import com.ruoyi.demo.mapper.BusinessOpportunitiesDao;
import com.ruoyi.demo.mapper.FollowUpDao;
import com.ruoyi.demo.service.DatabackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DatabackServiceImpl implements DatabackService {
    @Autowired
    AttentionPoolStatisticsDao attentionPoolStatisticsDao;
    @Autowired
    AttentionPoolDao attentionPoolDao;
    @Autowired
    BusinessOpportunitiesDao businessOpportunitiesDao;
    @Autowired
    FollowUpDao followUpDao;

    @Override
    public AttentionPoolStatisticsBo searchAbout(String userId, WorkTableBo vo) {
        int attentionCount = 0;
        QueryWrapper<AttentionPool> attentionPoolQueryWrapper = new QueryWrapper<>();
        attentionPoolQueryWrapper.eq("user_id",userId);
        if (vo.getStartTime() != null)
            attentionPoolQueryWrapper.apply("DATE_FORMAT(`attention_time`,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getStartTime());
        if (vo.getEndTime() != null)
            attentionPoolQueryWrapper.apply("DATE_FORMAT(`attention_time`,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getEndTime());
        List<AttentionPool> attentionPools = attentionPoolDao.selectList(attentionPoolQueryWrapper);
        if (attentionPools != null)
            attentionCount = attentionPools.size();

        int clueCount = 0;
        QueryWrapper<BusinessOpportunitiesClue> businessOpportunitiesClueQueryWrapper = new QueryWrapper<>();
        businessOpportunitiesClueQueryWrapper.eq("user_id",userId);
        if (vo.getStartTime() != null)
            businessOpportunitiesClueQueryWrapper.apply("DATE_FORMAT(`create_time`,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getStartTime());
        if (vo.getEndTime() != null)
            businessOpportunitiesClueQueryWrapper.apply("DATE_FORMAT(`create_time`,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getEndTime());
        List<BusinessOpportunitiesClue> businessOpportunitiesClues = businessOpportunitiesDao.selectList(businessOpportunitiesClueQueryWrapper);
        if (businessOpportunitiesClues != null)
            clueCount = businessOpportunitiesClues.size();

        int onlineCount = 0;
        QueryWrapper<FollowUpRecord> followUpRecordQueryWrapper = new QueryWrapper<>();
        followUpRecordQueryWrapper.eq("user_id",userId);
        followUpRecordQueryWrapper.eq("follow_way","线上电话");
        if (vo.getStartTime() != null)
            followUpRecordQueryWrapper.apply("DATE_FORMAT(`follow_time`,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getStartTime());
        if (vo.getEndTime() != null)
            followUpRecordQueryWrapper.apply("DATE_FORMAT(`follow_time`,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getEndTime());
        List<FollowUpRecord> followUpRecords = followUpDao.selectList(followUpRecordQueryWrapper);
        if (followUpRecords != null)
            onlineCount = followUpRecords.size();

        int offlineCount = 0;
        QueryWrapper<FollowUpRecord> followUpRecordQueryWrapper1 = new QueryWrapper<>();
        followUpRecordQueryWrapper1.eq("user_id",userId);
        followUpRecordQueryWrapper1.eq("follow_way","线下实地");
        if (vo.getStartTime() != null)
            followUpRecordQueryWrapper1.apply("DATE_FORMAT(`follow_time`,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getStartTime());
        if (vo.getEndTime() != null)
            followUpRecordQueryWrapper1.apply("DATE_FORMAT(`follow_time`,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT({0},'%Y-%m-%d %H:%i:%s')",vo.getEndTime());
        List<FollowUpRecord> followUpRecords1 = followUpDao.selectList(followUpRecordQueryWrapper1);
        if (followUpRecords1 != null)
            offlineCount = followUpRecords1.size();

        AttentionPoolStatisticsBo bo = new AttentionPoolStatisticsBo();
        bo.setAttentionCount(attentionCount);
        bo.setClueCount(clueCount);
        bo.setOnlineCount(onlineCount);
        bo.setOfflineCount(offlineCount);
        return bo;
    }

    @Override
    public List<AttentionPoolStatisticsBo> searchTrand(String userId,String condition) {
        if (condition == null)
            condition = "day";
        List<AttentionPoolStatisticsBo> list = new ArrayList<>();
        if (condition != null){
            if (condition.equals("day")){
                List<String> days = new ArrayList<>();
                for (int i = 1;i <= 30;i++){
                    days.add(LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusDays(-i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).toString());
                }
//                System.out.println(days);
                list = attentionPoolStatisticsDao.selectAllByUserIdAndDay(userId,days);
            }
            if (condition.equals("month")){
                List<String> months = new ArrayList<>();
                for (int i = 0;i < 12;i++){
                    months.add(LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusMonths(-i).format(DateTimeFormatter.ofPattern("yyyy-MM")).toString());
                }
//                System.out.println(months);
                list = attentionPoolStatisticsDao.selectAllByUserIdAndMonth(userId,months);
            }
            if (condition.equals("year")){
                List<String> years = new ArrayList<>();
                for (int i = 0;i < 5;i++){
                    years.add(LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusYears(-i).format(DateTimeFormatter.ofPattern("yyyy")).toString());
                }
//                System.out.println(years);
                list = attentionPoolStatisticsDao.selectAllByUserIdAndYear(userId,years);
            }
        }
//        List<AttentionPoolStatistics> list = attentionPoolStatisticsDao.searchAllByUserId(userId);
        return list;
    }
}
