package com.yunzhi.marketing.xlk.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.center.taUser.entity.TaTagUser;
import com.yunzhi.marketing.center.taUser.entity.TaUser;
import com.yunzhi.marketing.center.taUser.mapper.TaUserMapper;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.dto.StatisticsDTO;
import com.yunzhi.marketing.entity.TaRecommendCustomer;
import com.yunzhi.marketing.xlk.dto.PcStatisticsDTO;
import com.yunzhi.marketing.xlk.entity.Institution;
import com.yunzhi.marketing.xlk.mapper.InstitutionMapper;
import com.yunzhi.marketing.xlk.mapper.PcStatisticsMapper;
import com.yunzhi.marketing.xlk.service.IInstitutionService;
import com.yunzhi.marketing.xlk.service.IPcStatisticsService;
import com.yunzhi.marketing.xlk.vo.PcStatisticsActivityVO;
import com.yunzhi.marketing.xlk.vo.PcStatisticsGenderVO;
import com.yunzhi.marketing.xlk.vo.PcStatisticsLeaderboardVO;
import com.yunzhi.marketing.xlk.vo.PcStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IPcStatisticsServiceimpl implements IPcStatisticsService {

    @Autowired
    private PcStatisticsMapper pcStatisticsMapper;

    @Autowired
    private InstitutionMapper institutionMapper;

    @Autowired
    private TaUserMapper taUserMapper;

    /**
     * 获取首页六个卡片的数值
     *
     * @param taUser
     * @return
     */
    @Override
    public ResponseBean getUserStatistics(TaUser taUser) {
        // 获取当前人员的渠道code
        PcStatisticsDTO statisticsDTO = new PcStatisticsDTO();
        statisticsDTO.setOrgId(taUser.getOrgId());
        statisticsDTO.setTodayTime(LocalDateTime.now());
        statisticsDTO.setInstitutionIds(getInstitutionIds(taUser.getUserId()));
        // 总用户
        int allPersonNum = pcStatisticsMapper.selectAllPerson(statisticsDTO);
        // 总注册用户
        int registerPersonNum = pcStatisticsMapper.selectRegisterPerson(statisticsDTO);
        // 今日访问次数
        int todayVisitNum = pcStatisticsMapper.selectTodayVisitNum(statisticsDTO);
        // 今日新增用户
        int todayAddPersonNum = pcStatisticsMapper.selectTodayAddPerson(statisticsDTO);
        // 渠道数量
        int channelNum = pcStatisticsMapper.selectChannelNum(statisticsDTO);
        // 经济人数量
        int estateAgentNum = pcStatisticsMapper.selectEstateAgentNum(statisticsDTO);

        Map<String,Object> map = new HashMap<>();
        map.put("allPersonNum",allPersonNum);
        map.put("registerPersonNum",registerPersonNum);
        map.put("todayVisitNum",todayVisitNum);
        map.put("todayAddPersonNum",todayAddPersonNum);
        map.put("channelNum",channelNum);
        map.put("estateAgentNum",estateAgentNum);
        return ResponseBean.success(map);
    }

    /**
     * 首页用户来源统计
     *
     * @param taUser
     * @return
     */
    @Override
    public ResponseBean getUserOriginStatistics(TaUser taUser) {
        PcStatisticsDTO statisticsDTO = new PcStatisticsDTO();
        statisticsDTO.setOrgId(taUser.getOrgId());
        PcStatisticsVO userOriginStatistics = pcStatisticsMapper.getUserOriginStatistics(statisticsDTO);
        Integer otherNum = userOriginStatistics.getAllPersonNum() - userOriginStatistics.getConsultantNum() - userOriginStatistics.getCustomerNum() -userOriginStatistics.getEstageNum();
        userOriginStatistics.setOrherNum(otherNum);
        return ResponseBean.success(userOriginStatistics);
    }

    /**
     * 性别比例
     *
     * @param taUser
     * @return
     */
    @Override
    public ResponseBean getuserGenderStatistics(TaUser taUser) {
        PcStatisticsDTO statisticsDTO = new PcStatisticsDTO();
        statisticsDTO.setOrgId(taUser.getOrgId());
        PcStatisticsGenderVO pcStatisticsGenderVO = pcStatisticsMapper.getuserGenderStatistics(statisticsDTO);
        return ResponseBean.success(pcStatisticsGenderVO);
    }

    /**
     * 手机授权转换率
     *
     * @param taUser
     * @return
     */
    @Override
    public ResponseBean getUserAuthorStatistics(TaUser taUser) {
        // 获取当前人员的渠道code
        PcStatisticsDTO statisticsDTO = new PcStatisticsDTO();
        statisticsDTO.setOrgId(taUser.getOrgId());
        statisticsDTO.setTodayTime(LocalDateTime.now());
        statisticsDTO.setInstitutionIds(getInstitutionIds(taUser.getUserId()));
        // 总用户
        int allPersonNum = pcStatisticsMapper.selectAllPerson(statisticsDTO);
        // 总注册用户
        int registerPersonNum = pcStatisticsMapper.selectRegisterPerson(statisticsDTO);
        Map<String,Object> map = new HashMap<>();
        map.put("allPersonNum",allPersonNum);
        map.put("registerPersonNum",registerPersonNum);
        return ResponseBean.success(map);
    }

    /**
     * 项目统计排行榜
     *
     * @param taUser
     * @param pcStatisticsDTO
     * @return
     */
    @Override
    public ResponseBean getStatsBarList(TaUser taUser, PcStatisticsDTO pcStatisticsDTO) {
        pcStatisticsDTO.setInstitutionIds(getInstitutionIds(taUser.getUserId()));
        // 获取公客排行榜
        List<PcStatisticsLeaderboardVO> gkList = pcStatisticsMapper.getGkLeaderboard(pcStatisticsDTO);
        // 获取私客排行榜
        List<PcStatisticsLeaderboardVO> skList = pcStatisticsMapper.getSkLeaderboard(pcStatisticsDTO);
        // 获取项目访问次数排行榜
        List<PcStatisticsLeaderboardVO> visitList = pcStatisticsMapper.getVisitNumLeaderboard(pcStatisticsDTO);
        // 获取项目成交排行
        List<PcStatisticsLeaderboardVO> successList = pcStatisticsMapper.getSuccessLeaderboard(pcStatisticsDTO);

        Map<String,Object> map = new HashMap<>();
        map.put("gkList",gkList);
        map.put("skList",skList);
        map.put("visitList",visitList);
        map.put("successList",successList);
        return ResponseBean.success(map);
    }

    /**
     * 获取活动列表接口
     *
     * @param taUser
     * @param pcStatisticsDTO
     * @return
     */
    @Override
    public ResponseBean selectActivityList(TaUser taUser, PcStatisticsDTO pcStatisticsDTO) {
        pcStatisticsDTO.setInstitutionIds(getInstitutionIds(taUser.getUserId()));

        IPage<PcStatisticsActivityVO> page = new Page<>(pcStatisticsDTO.getPageNum(),pcStatisticsDTO.getPageSize());
        IPage<PcStatisticsActivityVO> result = pcStatisticsMapper.selectActivityList(page,pcStatisticsDTO);
        return ResponseBean.success(result);
    }

    /**
     * 获取组织机构id
     *
     * @param userId
     * @return
     */
    public List<String> getInstitutionIds(Integer userId) {
        List<Institution> list = institutionMapper.getAllByUser(userId);
        return list.stream().map(Institution::getInstitutionId).collect(Collectors.toList());
    }
}
