package com.younike.system.service.impl;

import com.younike.common.core.domain.AjaxResult;
import com.younike.system.domain.Category;
import com.younike.system.domain.ServerUser;
import com.younike.system.domain.ServerUserFollow;
import com.younike.system.mapper.ServerUserFollowMapper;
import com.younike.system.param.ServerUserFollowParam;
import com.younike.system.service.ICategoryService;
import com.younike.system.service.IServerUserFollowService;
import com.younike.system.service.IServerUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务人员关注发布类型关联Service业务层处理
 *
 * @author elev
 * @date 2022-01-20
 */
@Service
public class ServerUserFollowServiceImpl implements IServerUserFollowService {
    @Autowired
    private ServerUserFollowMapper serverUserFollowMapper;

    @Autowired
    private IServerUserService serverUserService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询服务人员关注发布类型关联
     *
     * @param serverUserId 服务人员关注发布类型关联主键
     * @return 服务人员关注发布类型关联
     */
    @Override
    public ServerUserFollow selectServerUserFollowByServerUserId(Long serverUserId) {
        return serverUserFollowMapper.selectServerUserFollowByServerUserId(serverUserId);
    }

    /**
     * 查询服务人员关注发布类型关联列表
     *
     * @param serverUserFollow 服务人员关注发布类型关联
     * @return 服务人员关注发布类型关联
     */
    @Override
    public List<ServerUserFollow> selectServerUserFollowList(ServerUserFollow serverUserFollow) {
        return serverUserFollowMapper.selectServerUserFollowList(serverUserFollow);
    }

    /**
     * 新增服务人员关注发布类型关联
     *
     * @param serverUserFollow 服务人员关注发布类型关联
     * @return 结果
     */
    @Override
    public int insertServerUserFollow(ServerUserFollow serverUserFollow) {
        return serverUserFollowMapper.insertServerUserFollow(serverUserFollow);
    }

    /**
     * 修改服务人员关注发布类型关联
     *
     * @param serverUserFollow 服务人员关注发布类型关联
     * @return 结果
     */
    @Override
    public int updateServerUserFollow(ServerUserFollow serverUserFollow) {
        return serverUserFollowMapper.updateServerUserFollow(serverUserFollow);
    }

    /**
     * 批量删除服务人员关注发布类型关联
     *
     * @param serverUserIds 需要删除的服务人员关注发布类型关联主键
     * @return 结果
     */
    @Override
    public int deleteServerUserFollowByServerUserIds(Long[] serverUserIds) {
        return serverUserFollowMapper.deleteServerUserFollowByServerUserIds(serverUserIds);
    }

    /**
     * 删除服务人员关注发布类型关联信息
     *
     * @param serverUserId 服务人员关注发布类型关联主键
     * @return 结果
     */
    @Override
    public int deleteServerUserFollowByServerUserId(Long serverUserId)
    {
        return serverUserFollowMapper.deleteServerUserFollowByServerUserId(serverUserId);
    }

    /**
     * 取消关注服务类型
     * @param follow
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelAttention(ServerUserFollow follow) {
//        根据服务人员id查询数据
        ServerUser serverUser = serverUserService.selectServerUserByServerId(follow.getServerUserId());
        if(ObjectUtils.isEmpty(serverUser)){
            throw new ArithmeticException("服务人员不存在");
        }
        /** 1 关注全部，0 非全部 **/
        if(serverUser.getFollowAll()==1){
            //删除所有已经关注的数据
            serverUserFollowMapper.deleteServerUserFollowByServerUserId(follow.getServerUserId());
            //添加关注
            //查询所有一级分类
            List<Category> categories = categoryService.categoriesPublishIdList();
            if(categories.size()<1){
               return AjaxResult.success();
            }
            ArrayList<ServerUserFollow> addServerUserFollows = new ArrayList<ServerUserFollow>();

            for (Category category:categories) {
                if(!category.getCategoryId().equals(follow.getCategoryId())){
                    ServerUserFollow serverUserFollow = new ServerUserFollow();
                    serverUserFollow.setCategoryId(category.getCategoryId());
                    serverUserFollow.setServerUserId(follow.getServerUserId());
                    addServerUserFollows.add(serverUserFollow);
                }
            }

            if(serverUserFollowMapper.insertServerUserFollowList(addServerUserFollows)<1){
                throw new ArithmeticException("关注失败");
            }
            serverUser.setFollowAll(0);
            if(serverUserService.updateServerUser(serverUser)<1){
                throw new ArithmeticException("更新服务人员是否全部关注失败");
            }
            return AjaxResult.success();

        }else {
            List<ServerUserFollow> serverUserFollows = serverUserFollowMapper.selectServerUserFollowList(follow);
            if(serverUserFollows.size()<1){
                return AjaxResult.success("您已取消关注该分类，不可重复取消");
            }
            if(serverUserFollowMapper.cancelAttention(follow)<1){
               return AjaxResult.error("取消关注失败");
           }
        }
        return AjaxResult.success();
    }

    /**
     * 提交关注服务类型
     * @param param
     * @return
     */
    @Override
    public AjaxResult attention(ServerUserFollowParam param) {

        //根据服务人员id查询已经关注的服务类型
        ServerUserFollow serverUserFollow = new ServerUserFollow();
        serverUserFollow.setServerUserId(param.getServerUserId());
        List<ServerUserFollow> serverUserFollows = this.selectServerUserFollowList(serverUserFollow);
        ServerUser serverUser = new ServerUser();
        serverUser.setServerId(param.getServerUserId());
        serverUser.setFollowAll(param.getFollowAll());
        serverUserService.updateServerUser(serverUser);

        if(ObjectUtils.isEmpty(param.getCategoryIdList()) && param.getCategoryIdList().length<1 ){
            //取消所有已经关注的数据
            serverUserFollowMapper.deleteServerUserFollowByServerUserId(param.getServerUserId());
            return AjaxResult.success();
        }

        //要添加的数据
        ArrayList<ServerUserFollow> addServerUserFollows = new ArrayList<>();
        //要删除的数据
        ArrayList<ServerUserFollow> delServerUserFollows = new ArrayList<>();

        //当已经关注的数据为空是，传来的数据全部都进行添加
        if(serverUserFollows.size()<1){
            for (Long categoryId: param.getCategoryIdList()) {
                ServerUserFollow follow = new ServerUserFollow();
                follow.setServerUserId(param.getServerUserId());
                follow.setCategoryId(categoryId);
                addServerUserFollows.add(follow);
            }
            //进行添加
            if(serverUserFollowMapper.insertServerUserFollowList(addServerUserFollows)<1){
                throw new ArithmeticException("关注失败");
            }
            return AjaxResult.success();
        }
        //计算出新增的关注
        Map<Long, ServerUserFollow> collect = serverUserFollows.stream().collect(Collectors.toMap(ServerUserFollow::getCategoryId, Function.identity()));

        for (Long categoryId: param.getCategoryIdList()) {
            ServerUserFollow serverUserFollow1 = collect.get(categoryId);
            if(ObjectUtils.isEmpty(serverUserFollow1)){
                ServerUserFollow follow = new ServerUserFollow();
                follow.setServerUserId(param.getServerUserId());
                follow.setCategoryId(categoryId);
                addServerUserFollows.add(follow);
            }
        }
        //计算出取消关注的数据
        Map<Long, Long> collectMap =  Arrays.asList(param.getCategoryIdList()).stream().collect(Collectors.toMap(Long::longValue, Function.identity()));
        serverUserFollows.forEach(follow -> {
            Long aLong = collectMap.get(follow.getCategoryId());
           if(aLong==null){
               delServerUserFollows.add(follow);
           }
        });

        if(addServerUserFollows.size()>0){
            if(serverUserFollowMapper.insertServerUserFollowList(addServerUserFollows)<1){
                throw new ArithmeticException("关注失败");
            }
        }

        if(delServerUserFollows.size()>0){
            if(serverUserFollowMapper.deleteServerUserFollowList(delServerUserFollows)<1){
                throw new ArithmeticException("取消关注失败");
            }
        }
        return AjaxResult.success();
    }
}
