package com.ruoyi.ibs.list.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.ibs.list.domain.MarketingCustomerListVo;
import com.ruoyi.ibs.list.domain.MarketingList;
import com.ruoyi.ibs.list.domain.MarketingListVo;
import com.ruoyi.ibs.list.domain.SysGroupVo;
import com.ruoyi.ibs.list.mapper.SysListMapper;
import com.ruoyi.ibs.list.service.ISysListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.ruoyi.common.utils.SecurityUtils.*;

/**
 * 角色 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysListServiceImpl implements ISysListService
{
    @Autowired
    private SysListMapper listMapper;



    @Override
    public List<MarketingListVo> selectMarketList(String keyword,String groupType) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        String roleName = user.getRoleName();
        List<MarketingListVo> vos = new ArrayList<>();
        List<MarketingList> marketingLists = new ArrayList<>();
        //总行管理员查询到所有客群
        if ("超级管理员".equals(roleName)){
            marketingLists = listMapper.selectMarketingList(keyword,null);
        }
        if ("总行管理员".equals(roleName)){
            marketingLists = listMapper.selectMarketingList(keyword,getDeptId()/1000);
        }
        if ("支行管理员".equals(roleName)){
            //支行管理员，我创建的，查询该机构下所有的客群
            if ("1".equals(groupType)){
                marketingLists = listMapper.selectMarketingList2(keyword,getDeptId(),null,"1");
            }else {
                marketingLists = listMapper.selectMarketingList2(keyword,getDeptId(),null,"0");
            }
        }
        if ("普通客户经理".equals(roleName)){
           marketingLists = listMapper.selectMarketingList2(keyword,getDeptId(),getUserId(),null);
        }
         Map<String,List<MarketingList>> listMap =  marketingLists.stream().collect(Collectors.groupingBy(MarketingList::getGroupId));
        List<MarketingList> newMarketingLists = new ArrayList<>();
         for (String key : listMap.keySet()){
             List<MarketingList> marks =  listMap.get(key);
             Map<String,List<MarketingList>> listMapByCustId =  marks.stream().collect(Collectors.groupingBy(MarketingList::getCustId));
             MarketingList marketingList = new MarketingList();
             if (marks!=null){
                 BeanUtils.copyProperties(marks.get(0),marketingList);
                 Long size = Long.parseLong(String.valueOf(listMapByCustId.size()));
                 marketingList.setCustomerNum(size);
             }
             newMarketingLists.add(marketingList);

         }
         newMarketingLists.forEach(marketingList -> {
             MarketingListVo marketingListVo = new MarketingListVo();
             BeanUtils.copyProperties(marketingList,marketingListVo);
             Date startTime = marketingList.getStartTime();
             Date endTime = marketingList.getEndTime();
             Date now = new Date();
             if (startTime!=null&&endTime!=null){
                 if (now.getTime()<startTime.getTime()){
                     marketingListVo.setCampaignProgress("待开始");
                 } else if (now.getTime()>startTime.getTime()&&now.getTime()<endTime.getTime()) {
                     marketingListVo.setCampaignProgress("进行中");
                 }else if (now.getTime()>endTime.getTime()){
                     marketingListVo.setCampaignProgress("已结束");
                 }

             }
             vos.add(marketingListVo);

         });
         return vos;
    }

    @Override
    public List<MarketingCustomerListVo> selectCustomerList(String groupId, String campaignId, String keyword) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        String roleName = user.getRoleName();
        List<MarketingList> marketingLists = new ArrayList<>();
        List<MarketingCustomerListVo> vos = new ArrayList<>();
        if ("超级管理员".equals(roleName)){
            marketingLists = listMapper.selectMarketingCustomerList(groupId,campaignId,keyword,null);
        }
        if ("总行管理员".equals(roleName)){
            marketingLists = listMapper.selectMarketingCustomerList(groupId,campaignId,keyword,getDeptId()/1000);
        }
        if ("支行管理员".equals(roleName)){
            //支行管理员，我创建的，查询该机构下所有的客群
            marketingLists = listMapper.selectMarketingCustomerList2(groupId,campaignId,keyword,getDeptId(),null);
        }
        if ("普通客户经理".equals(roleName)){
            marketingLists = listMapper.selectMarketingCustomerList2(groupId,campaignId,keyword,getDeptId(),getUserId());
        }
        marketingLists.forEach(marketingList -> {
            MarketingCustomerListVo marketingCustomerListVo = new MarketingCustomerListVo();
            BeanUtils.copyProperties(marketingList,marketingCustomerListVo);
            vos.add(marketingCustomerListVo);

        });
        return vos;
    }
}
