package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaChannel;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.entity.TsActivityDaily;
import com.huiju.estateagents.excel.ActivityStatistics.ChannelAddRegistNum;
import com.huiju.estateagents.excel.ActivityStatistics.ChannelVisitNum;
import com.huiju.estateagents.excel.ActivityStatistics.ChannelVisitPersonNum;
import com.huiju.estateagents.excel.ActivityStatistics.TsChannelDaily;
import com.huiju.estateagents.mapper.TaChannelMapper;
import com.huiju.estateagents.service.TaChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-09-17
 */
@Service
public class TaChannelServiceImpl extends ServiceImpl<TaChannelMapper, TaChannel> implements TaChannelService {

    @Autowired
    private TaChannelMapper taChannelMapper;

    /**
     * 渠道引进客户统计
     *
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param startDate
     * @param endDate
     * @param channelType
     * @param buildingId
     * @return
     */
    @Override
    public ResponseBean getChannelIntroductionList(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String channelType, String buildingId, Integer channelId,String sortField,String sortOrder, List<TaPersonBuilding> taPersonBuildings) {
        IPage<TsActivityDaily> pg = new Page<>(pageNum, pageSize);
        String orderType = "asc";
        if ("descend".equals(sortOrder)){
            orderType = "desc";
        }else if ("ascend".equals(sortOrder)){
            orderType = "asc";
        }
        if(StringUtils.isEmpty(sortField)){
            sortField = "new_customers";
            orderType = "desc";
        }
        IPage<TsActivityDaily> data = taChannelMapper.getChannelIntroductionList(pg, orgId,startDate,endDate,channelType,buildingId, channelId, sortField, orderType,taPersonBuildings);
        List<TsActivityDaily> records = data.getRecords();
        records.forEach(e -> {
            List<ChannelVisitPersonNum> channelPersonNumListExport = taChannelMapper.getChannelPersonNumListExport(orgId, channelId, e.getTargetId(), e.getTargetType(), buildingId, startDate, endDate);
            e.setVisitPersons(channelPersonNumListExport.size());
        });
        return ResponseBean.success(data);
    }

    /**
     * 获取注册用户列表
     *
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param channelId
     * @param targetId
     * @param targetType
     * @param buildingId
     * @return
     */
    @Override
    public ResponseBean getNewuserList(Integer pageNum, Integer pageSize, Integer orgId, Integer channelId, String targetId, String targetType, String buildingId, LocalDate startDate, LocalDate endDate) {
        IPage<ChannelAddRegistNum> iPage = new Page<>(pageNum, pageSize);
        IPage<ChannelAddRegistNum> data = taChannelMapper.getChannelNewuserList(iPage, orgId, channelId, targetId,targetType, buildingId,startDate,endDate);
        return ResponseBean.success(data);
    }

    /**
     * 获取渠道下的访问次数列表
     *
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param channelId
     * @param targetId
     * @param targetType
     * @param buildingId
     * @return
     */
    @Override
    public ResponseBean getVisitNumList(Integer pageNum, Integer pageSize, Integer orgId, Integer channelId, String targetId, String targetType, String buildingId, LocalDate startDate, LocalDate endDate) {
        IPage<ChannelVisitNum> iPage = new Page<>(pageNum, pageSize);
        IPage<ChannelVisitNum> data = taChannelMapper.getChannelVisitNumList(iPage, orgId, channelId, targetId, targetType, buildingId,startDate,endDate);
        return ResponseBean.success(data);
    }

    /**
     * 获取渠道下的访问人数列表
     *
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param channelId
     * @param targetId
     * @param targetType
     * @param buildingId
     * @return
     */
    @Override
    public ResponseBean getPersonNumList(Integer pageNum, Integer pageSize, Integer orgId, Integer channelId, String targetId, String targetType, String buildingId, LocalDate startDate, LocalDate endDate) {
        IPage<ChannelVisitPersonNum> iPage = new Page<>(pageNum, pageSize);
        IPage<ChannelVisitPersonNum> data = taChannelMapper.getChannelPersonNumList(iPage, orgId, channelId, targetId, targetType, buildingId,startDate,endDate);
        return ResponseBean.success(data);
    }

    /**
     * 导出
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @param channelType
     * @param buildingId
     * @param channelId
     * @return
     */
    @Override
    public List<TsChannelDaily> getChannelIntroductionListExport(Integer orgId, LocalDate startDate, LocalDate endDate, String channelType, String buildingId, Integer channelId, List<TaPersonBuilding> taPersonBuildings) {
        List<TsChannelDaily> channelIntroductionListExport = taChannelMapper.getChannelIntroductionListExport(orgId, startDate, endDate, channelType, buildingId, channelId,taPersonBuildings);
        channelIntroductionListExport.forEach(e -> {
            List<ChannelVisitPersonNum> channelPersonNumListExport = taChannelMapper.getChannelPersonNumListExport(orgId, channelId, e.getTargetId(), e.getTargetType(), buildingId, startDate, endDate);
            e.setVisitPersons(channelPersonNumListExport.size());

            String message = "";
            switch (e.getTargetType()){
                case "building":
                    message = "项目";
                    break;
                case "activity":
                    message = "常规活动";
                    break;
                case "help":
                    message = "助力活动";
                    break;
                case "group":
                    message = "拼团活动";
                    break;
                case "h5":
                    message = "H5活动";
                    break;
                case "live":
                    message = "直播活动";
                    break;
                case "news":
                    message = "资讯";
                    break;
                case "house":
                    message = "在线选房";
                    break;
            }
            e.setTargetType(message);
        });
        return channelIntroductionListExport;
    }

    /**
     * 导出
     *
     * @param orgId
     * @param channelId
     * @param targetId
     * @param targetType
     * @param buildingId
     * @return
     */
    @Override
    public List<ChannelAddRegistNum> getNewuserListExport(Integer orgId, Integer channelId, String targetId, String targetType, String buildingId, LocalDate startDate, LocalDate endDate) {
        return taChannelMapper.getChannelNewuserListExport(orgId, channelId, targetId,targetType, buildingId,startDate,endDate);
    }

    /**
     * 导出
     *
     * @param orgId
     * @param channelId
     * @param targetId
     * @param targetType
     * @param buildingId
     * @return
     */
    @Override
    public List<ChannelVisitNum> getVisitNumListExport(Integer orgId, Integer channelId, String targetId, String targetType, String buildingId, LocalDate startDate, LocalDate endDate) {
        return taChannelMapper.getChannelVisitNumListExport(orgId, channelId, targetId, targetType, buildingId,startDate,endDate);
    }

    /**
     * 导出
     *
     * @param orgId
     * @param channelId
     * @param targetId
     * @param targetType
     * @param buildingId
     * @return
     */
    @Override
    public List<ChannelVisitPersonNum> getPersonNumListExport(Integer orgId, Integer channelId, String targetId, String targetType, String buildingId, LocalDate startDate, LocalDate endDate) {
        return taChannelMapper.getChannelPersonNumListExport(orgId, channelId, targetId, targetType, buildingId,startDate,endDate);
    }
}
