package com.fs.company.controller.crm;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.TimeUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.crm.param.CrmCustomerStatisticsParam;
import com.fs.crm.service.ICrmCustomerService;
import com.fs.crm.service.ICrmCustomerVisitService;
import com.fs.crm.vo.*;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 统计
 *
 * @author fs
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/crm/statistics")
public class CrmStatisticsController extends BaseController
{

    @Autowired
    private ICompanyUserService userService;

    @Autowired
    private ICrmCustomerService crmCustomerService;
    @Autowired
    private ICrmCustomerVisitService crmCustomerVisitService;
    @Autowired
    private TokenService tokenService;
    @PreAuthorize("@ss.hasPermi('crm:statistics:customer')")
    @GetMapping("/customer")
    public R customer(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerStatisticsVO> list= crmCustomerService.selectCrmCustomerStatisticsList(param);

            TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
            timeEntity.setUserIds(param.getUsers());
            Integer cycleNum = timeEntity.getCycleNum();
            Integer beginTime = timeEntity.getBeginTime();
            List<Integer> timeList = new ArrayList<>();
            for (int i = 1; i <= cycleNum; i++) {
                timeList.add(beginTime);
                beginTime = TimeUtils.formatTime(beginTime);
            }
            List<JSONObject> jsonObjectList = crmCustomerService.selectCrmCustomerCounts(timeEntity.toMap());
            List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
            List<Integer> receiveCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("receiveCount")).collect(Collectors.toList());
            List<Integer> poolCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("poolCount")).collect(Collectors.toList());
            return R.ok().put("list",list).put("dates",dates).put("receiveCount",receiveCount).put("poolCount",poolCount);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics::exportCustomer')")
    @GetMapping("/exportCustomer")
    public AjaxResult exportCustomer(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        List<CrmCustomerStatisticsVO> list= crmCustomerService.selectCrmCustomerStatisticsList(param);
        ExcelUtil<CrmCustomerStatisticsVO> util = new ExcelUtil<CrmCustomerStatisticsVO>(CrmCustomerStatisticsVO.class);
        return util.exportExcel(list, "customer");
    }

    @PreAuthorize("@ss.hasPermi('crm:statistics:customerFull')")
    @GetMapping("/customerFull")
    public R customerFull(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerStatisticsVO> list= crmCustomerService.selectCrmCustomerFullStatisticsList(param);

            TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
            timeEntity.setUserIds(param.getUsers());
            Integer cycleNum = timeEntity.getCycleNum();
            Integer beginTime = timeEntity.getBeginTime();
            List<Integer> timeList = new ArrayList<>();
            for (int i = 1; i <= cycleNum; i++) {
                timeList.add(beginTime);
                beginTime = TimeUtils.formatTime(beginTime);
            }
            List<JSONObject> jsonObjectList = crmCustomerService.selectCustomerFullCounts(timeEntity.toMap());
            List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
            List<Integer> receiveCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("receiveCount")).collect(Collectors.toList());
            List<Integer> poolCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("poolCount")).collect(Collectors.toList());
            return R.ok().put("list",list).put("dates",dates).put("receiveCount",receiveCount).put("poolCount",poolCount);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics::exportCustomerFull')")
    @GetMapping("/exportCustomerFull")
    public AjaxResult exportCustomerFull(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        List<CrmCustomerStatisticsVO> list= crmCustomerService.selectCrmCustomerFullStatisticsList(param);
        ExcelUtil<CrmCustomerStatisticsVO> util = new ExcelUtil<CrmCustomerStatisticsVO>(CrmCustomerStatisticsVO.class);
        return util.exportExcel(list, "customer");
    }

    @PreAuthorize("@ss.hasPermi('crm:statistics:customerNum')")
    @GetMapping("/customerNum")
    public R customerNum(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerNumStatisticsVO> list= crmCustomerService.selectCrmCustomerNumStatisticsList(param);

            TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
            timeEntity.setUserIds(param.getUsers());
            Integer cycleNum = timeEntity.getCycleNum();
            Integer beginTime = timeEntity.getBeginTime();
            List<Integer> timeList = new ArrayList<>();
            for (int i = 1; i <= cycleNum; i++) {
                timeList.add(beginTime);
                beginTime = TimeUtils.formatTime(beginTime);
            }
            List<JSONObject> jsonObjectList = crmCustomerService.selectCrmCustomerNumCounts(timeEntity.toMap());
            List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
            List<Integer> customerNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerNum")).collect(Collectors.toList());
            List<Integer> customerOkNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerOkNum")).collect(Collectors.toList());
            return R.ok().put("list",list).put("dates",dates).put("customerNum",customerNum).put("customerOkNum",customerOkNum);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics::exportCustomerNum')")
    @GetMapping("/exportCustomerNum")
    public AjaxResult exportCustomerNum(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        List<CrmCustomerNumStatisticsVO> list= crmCustomerService.selectCrmCustomerNumStatisticsList(param);
        ExcelUtil<CrmCustomerNumStatisticsVO> util = new ExcelUtil<CrmCustomerNumStatisticsVO>(CrmCustomerNumStatisticsVO.class);
        return util.exportExcel(list, "customer");
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics:customerSource')")
    @GetMapping("/customerSource")
    public R customerSource(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerSourceStatisticsVO> list= crmCustomerService.selectCrmCustomerSourceStatisticsList(param);
            param.setIsPay(1L);
            List<CrmCustomerSourceStatisticsVO> list2= crmCustomerService.selectCrmCustomerSourceStatisticsList(param);
            return R.ok().put("list",list).put("payList",list2);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics:customerMap')")
    @GetMapping("/customerMap")
    public R customerMap(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerMapStatisticsVO> list= crmCustomerService.selectCrmCustomerMapStatisticsList(param);
            param.setIsPay(1L);
            List<CrmCustomerMapStatisticsVO> list2= crmCustomerService.selectCrmCustomerMapStatisticsList(param);
            return R.ok().put("list",list).put("payList",list2);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics:customerRate')")
    @GetMapping("/customerRate")
    public R customerRate(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerRateStatisticsVO> list= crmCustomerService.selectCrmCustomerRateStatisticsList(param);

            TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
            timeEntity.setUserIds(param.getUsers());
            Integer cycleNum = timeEntity.getCycleNum();
            Integer beginTime = timeEntity.getBeginTime();
            List<Integer> timeList = new ArrayList<>();
            for (int i = 1; i <= cycleNum; i++) {
                timeList.add(beginTime);
                beginTime = TimeUtils.formatTime(beginTime);
            }
            List<JSONObject> jsonObjectList = crmCustomerService.selectCrmCustomerRateCounts(timeEntity.toMap());
            List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
            List<Integer> customerNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerNum")).collect(Collectors.toList());
            List<Integer> customerOkNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerOkNum")).collect(Collectors.toList());
            List<Integer> customerRate = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerRate")).collect(Collectors.toList());
            return R.ok().put("list",list).put("dates",dates).put("customerNum",customerNum).put("customerOkNum",customerOkNum).put("customerRate",customerRate);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics::exportCustomerRate')")
    @GetMapping("/exportCustomerRate")
    public AjaxResult exportCustomerRate(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        List<CrmCustomerRateStatisticsVO> list= crmCustomerService.selectCrmCustomerRateStatisticsList(param);
        ExcelUtil<CrmCustomerRateStatisticsVO> util = new ExcelUtil<CrmCustomerRateStatisticsVO>(CrmCustomerRateStatisticsVO.class);
        return util.exportExcel(list, "customer");
    }

    @PreAuthorize("@ss.hasPermi('crm:statistics:customerCycle')")
    @GetMapping("/customerCycle")
    public R customerCycle(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerCycleStatisticsVO> list= crmCustomerService.selectCrmCustomerCycleStatisticsList(param);

            TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
            timeEntity.setUserIds(param.getUsers());
            Integer cycleNum = timeEntity.getCycleNum();
            Integer beginTime = timeEntity.getBeginTime();
            List<Integer> timeList = new ArrayList<>();
            for (int i = 1; i <= cycleNum; i++) {
                timeList.add(beginTime);
                beginTime = TimeUtils.formatTime(beginTime);
            }
            List<JSONObject> jsonObjectList = crmCustomerService.selectCrmCustomerCycleCounts(timeEntity.toMap());
            List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
            List<Integer> customerOkNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerOkNum")).collect(Collectors.toList());
            List<Integer> customerCycle = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerCycle")).collect(Collectors.toList());

            return R.ok().put("list",list).put("dates",dates).put("customerOkNum",customerOkNum).put("customerCycle",customerCycle);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics::exportCustomerCycle')")
    @GetMapping("/exportCustomerCycle")
    public AjaxResult exportCustomerCycle(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        List<CrmCustomerCycleStatisticsVO> list= crmCustomerService.selectCrmCustomerCycleStatisticsList(param);
        ExcelUtil<CrmCustomerCycleStatisticsVO> util = new ExcelUtil<CrmCustomerCycleStatisticsVO>(CrmCustomerCycleStatisticsVO.class);
        return util.exportExcel(list, "customer");
    }

    @PreAuthorize("@ss.hasPermi('crm:statistics:customerVisit')")
    @GetMapping("/customerVisit")
    public R customerVisit(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取部门下的所有用户
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        if(param.getUsers()!=null&&param.getUsers().length>0){
            List<CrmCustomerVisitStatisticsVO> list= crmCustomerVisitService.selectCrmCustomerVisitStatisticsList(param);

            TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
            timeEntity.setUserIds(param.getUsers());
            Integer cycleNum = timeEntity.getCycleNum();
            Integer beginTime = timeEntity.getBeginTime();
            List<Integer> timeList = new ArrayList<>();
            for (int i = 1; i <= cycleNum; i++) {
                timeList.add(beginTime);
                beginTime = TimeUtils.formatTime(beginTime);
            }
            List<JSONObject> jsonObjectList = crmCustomerVisitService.selectCrmCustomerVisitCounts(timeEntity.toMap());
            List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
            List<Integer> customerCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("customerCount")).collect(Collectors.toList());
            List<Integer> visitCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("visitCount")).collect(Collectors.toList());
            return R.ok().put("list",list).put("dates",dates).put("customerCount",customerCount).put("visitCount",visitCount);
        }
        else {
            return R.ok("未查找到数据");
        }
    }
    @PreAuthorize("@ss.hasPermi('crm:statistics::exportCustomerVisit')")
    @GetMapping("/exportCustomerVisit")
    public AjaxResult exportCustomerVisit(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = userService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
        }
        List<CrmCustomerVisitStatisticsVO> list= crmCustomerVisitService.selectCrmCustomerVisitStatisticsList(param);
        ExcelUtil<CrmCustomerVisitStatisticsVO> util = new ExcelUtil<CrmCustomerVisitStatisticsVO>(CrmCustomerVisitStatisticsVO.class);
        return util.exportExcel(list, "visit");
    }


}
