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

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ibs.list.domain.VisitCampaignComment;
import com.ruoyi.ibs.list.domain.VisitCampaignCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ibs.list.mapper.CampaignCountMapper;
import com.ruoyi.ibs.list.domain.CampaignCount;
import com.ruoyi.ibs.list.service.ICampaignCountService;

import static com.ruoyi.common.utils.SecurityUtils.userRole;
import static com.ruoyi.ibs.list.domain.Constans.*;

/**
 * 走访任务统计卡片Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class CampaignCountServiceImpl implements ICampaignCountService 
{
    @Autowired
    private CampaignCountMapper campaignCountMapper;



    /**
     * 查询走访任务统计卡片
     * 
     * @param dt 走访任务统计卡片主键
     * @return 走访任务统计卡片
     */
    @Override
    public CampaignCount selectCampaignCountByDt(String dt)
    {
        return campaignCountMapper.selectCampaignCountByDt(dt);
    }

    /**
     * 查询走访任务统计卡片列表
     * 
     * @param campaignCount 走访任务统计卡片
     * @return 走访任务统计卡片
     */
    @Override
    public List<CampaignCount> selectCampaignCountList(CampaignCount campaignCount)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String roleName = userRole();
        List<CampaignCount> vo = new ArrayList<>();
        campaignCount.setSumType("-1");
        switch (roleName) {
            case ROLE_HEAD: campaignCount.setSumType("0"); break;
            case ROLE_HeadPrivate: campaignCount.setSumType("1"); break;
            case ROLE_HeadPublic: campaignCount.setSumType("2"); break;
            case ROLE_HeadOps: campaignCount.setSumType("3"); break;
            case ROLE_BRANCH: campaignCount.setSumType("5"); break;
        }
        campaignCount.setDeptId(loginUser.getDeptId());
        campaignCount.setUserId(loginUser.getUsername());
        vo = campaignCountMapper.selectCampaignCountList(campaignCount);
        return vo;
    }

    public List<VisitCampaignCount> selectVisitCampaignCountList(VisitCampaignCount visitCampaignCount){
        List<VisitCampaignCount> vo = new ArrayList<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String roleName = userRole();
        switch (roleName) {
            case ROLE_HEAD: visitCampaignCount.setCreateRole("0"); break;
            case ROLE_HeadPrivate: visitCampaignCount.setCreateRole("1"); break;
            case ROLE_HeadPublic: visitCampaignCount.setCreateRole("2"); break;
            case ROLE_HeadOps: visitCampaignCount.setCreateRole("3"); break;
            case ROLE_BRANCH: visitCampaignCount.setCreateRole("5"); break;
        }
        visitCampaignCount.setDeptId(user.getDeptId());

        if(ROLE_BRANCH.equalsIgnoreCase(roleName)){
            vo = campaignCountMapper.selectVisitCampaignBranchList(visitCampaignCount);
        }else if(ROLE_Outlet.equalsIgnoreCase(roleName)){

            visitCampaignCount.setIsOutlet("1");
            vo = campaignCountMapper.selectOutletList(visitCampaignCount);

        }else if("manager".equalsIgnoreCase(roleName)){
            visitCampaignCount.setDeptId(null);
            visitCampaignCount.setUserId(user.getUserName());
            vo = campaignCountMapper.selectPersonList(visitCampaignCount);
        }else{
            vo = campaignCountMapper.selectVisitCampaignCountList(visitCampaignCount);
        }
        return vo;
    }

    public List<VisitCampaignCount> selectBranchList(VisitCampaignCount visitCampaignCount){
        List<VisitCampaignCount> vo = new ArrayList<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        vo = campaignCountMapper.selectVisitCampaignCountCheckList(visitCampaignCount);
        return vo;
    }

    public List<VisitCampaignCount> selectOutletList(VisitCampaignCount visitCampaignCount){
        List<VisitCampaignCount> vo = new ArrayList<>();
        vo = campaignCountMapper.selectOutletList(visitCampaignCount);

        return vo;
    }

    public List<VisitCampaignCount> selectPersonList(VisitCampaignCount visitCampaignCount){
        if(visitCampaignCount.getDeptId() == null){
            visitCampaignCount.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        }
        List<VisitCampaignCount> vo = campaignCountMapper.selectPersonList(visitCampaignCount);
        return vo;
    }

    public List<VisitCampaignCount> selectCommentList(VisitCampaignComment visitCampaignComment){
        List<VisitCampaignCount> vo = campaignCountMapper.selectCommentList(visitCampaignComment);
        return vo;
    }

    public int insertVisitCampaignComment(VisitCampaignComment visitCampaignComment){
        visitCampaignComment.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        visitCampaignComment.setNickName(SecurityUtils.getLoginUser().getUser().getNickName());
        visitCampaignComment.setCreateTime(DateUtils.getNowDate());
        visitCampaignComment.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        return campaignCountMapper.insertVisitCampaignComment(visitCampaignComment);
    }


    /**
     * 新增走访任务统计卡片
     * 
     * @param campaignCount 走访任务统计卡片
     * @return 结果
     */
    @Override
    public int insertCampaignCount(CampaignCount campaignCount)
    {
        return campaignCountMapper.insertCampaignCount(campaignCount);
    }

    /**
     * 修改走访任务统计评论
     * 
     * @param visitCampaignComment 走访任务统计
     * @return 结果
     */
    @Override
    public int updateCampaignCount(VisitCampaignComment visitCampaignComment)
    {
        return campaignCountMapper.updateCampaignCount(visitCampaignComment);
    }

    /**
     * 批量删除走访任务统计卡片
     * 
     * @param dts 需要删除的走访任务统计卡片主键
     * @return 结果
     */
    @Override
    public int deleteCampaignCountByDts(String[] dts)
    {
        return campaignCountMapper.deleteCampaignCountByDts(dts);
    }

    /**
     * 删除走访任务统计卡片信息
     * 
     * @param dt 走访任务统计卡片主键
     * @return 结果
     */
    @Override
    public int deleteCampaignCountByDt(String dt)
    {
        return campaignCountMapper.deleteCampaignCountByDt(dt);
    }
}
