package com.mcwl.communityCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.communityCenter.domain.CommunityUser;
import com.mcwl.communityCenter.domain.dto.BlackListRes;
import com.mcwl.communityCenter.domain.dto.CommunityUserListPageRes;
import com.mcwl.communityCenter.domain.dto.CommunityUserManageRes;
import com.mcwl.communityCenter.domain.dto.UnBlackListRes;
import com.mcwl.communityCenter.domain.vo.CommunityUserVo;
import com.mcwl.communityCenter.mapper.CommunityUserMapper;
import com.mcwl.communityCenter.service.CommunityUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class CommunityUserServiceImpl extends ServiceImpl<CommunityUserMapper, CommunityUser> implements CommunityUserService {


    /**
     * 社区用户列表
     */
    @Override
    public TableDataInfo getCommunityUserList(CommunityUserListPageRes communityUserListPageRes) {
        Page<CommunityUser> page = new Page<>(communityUserListPageRes.getPageNum(), communityUserListPageRes.getPageSize());
//        page.addOrder(new OrderItem("cu.create_time", false));

        List<CommunityUserVo> communityUserList = baseMapper.getCommunityUserList(page, communityUserListPageRes);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(communityUserList);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    @Override
    public R<Object> black(BlackListRes blackListRes) {
        Long tenantId = blackListRes.getTenantId();
        Long communityId = blackListRes.getCommunityId();
        Long userId = blackListRes.getUserId();
        Integer blackDay = blackListRes.getBlackDay();
        String blackReason = blackListRes.getBlackReason();

        // 拉黑用户
        CommunityUser blackCommunityUser = baseMapper.selectCommunityUser(
                tenantId, communityId, userId
        );

        // 当前用户
        CommunityUser currentCommunityUser = baseMapper.selectCommunityUser(
                tenantId, communityId, SecurityUtils.getUserId()
        );

        if (currentCommunityUser.getUserType() == 0) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是群主/管理员，无法拉黑");
        }


        if (Objects.isNull(blackCommunityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该用户不在该社区中");
        }

        if ("1".equals(blackCommunityUser.getIsBlack())) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该用户已拉黑");
        }

        if (blackCommunityUser.getUserType() != 0) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该用户是群主/管理员，无法拉黑");
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, blackDay);

        blackCommunityUser.setIsBlack("1");
        blackCommunityUser.setBlackEndTime(calendar.getTime());
        blackCommunityUser.setBlackReason(blackReason);

        baseMapper.updateBlackCommunityUser(blackCommunityUser);


        return R.ok();
    }

    @Override
    public List<CommunityUser> getAllCommunityUser() {
        return baseMapper.getAllCommunityUser();
    }

    @Override
    public void updateBlankCommunityUser(CommunityUser communityUser) {
        baseMapper.updateBlackCommunityUser(communityUser);
    }

    @Override
    public R<Object> unBlack(UnBlackListRes unBlackListRes) {
        Long tenantId = unBlackListRes.getTenantId();
        Long communityId = unBlackListRes.getCommunityId();
        Long unBlankUserId = unBlackListRes.getUserId();
        Long currentUserId = SecurityUtils.getUserId();


        // 当前用户
        CommunityUser currentCommunityUser = baseMapper.selectCommunityUser(tenantId, communityId, currentUserId);
        if (Objects.isNull(currentCommunityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是该社区用户");
        }
        if (Objects.equals(currentCommunityUser.getUserType(), 0)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是群主/管理员，无法取消拉黑");
        }

        // 被取消拉黑的用户
        CommunityUser unBlackCommunityUser = baseMapper.selectCommunityUser(tenantId, communityId, unBlankUserId);

        if (Objects.isNull(unBlackCommunityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该用户不在该社区中");
        }

        CommunityUser communityUser = BeanUtil.toBean(unBlackCommunityUser, CommunityUser.class);
        communityUser.setIsBlack("0");
        communityUser.setBlackReason(null);
        communityUser.setBlackEndTime(null);
        baseMapper.updateBlackCommunityUser(communityUser);
        return R.ok("取消成功");
    }

    @Override
    public R<Object> manage(CommunityUserManageRes communityUserManageRes) {
        Long tenantId = communityUserManageRes.getTenantId();
        Long communityId = communityUserManageRes.getCommunityId();
        Long manageUserId = communityUserManageRes.getUserId();
        Long currentUserId = SecurityUtils.getUserId();

        // 当前用户
        CommunityUser currentCommunityUser = baseMapper.selectCommunityUser(tenantId, communityId, currentUserId);
        if (Objects.isNull(currentCommunityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是该社区用户");
        }
        if (!Objects.equals(currentCommunityUser.getUserType(), 2)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "您不是群主，无法设置管理");
        }

        // 设置管理的用户
        CommunityUser manageCommunityUser = baseMapper.selectCommunityUser(tenantId, communityId, manageUserId);

        if (Objects.isNull(manageCommunityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该用户不在该社区中");
        }

        if (Objects.equals(manageCommunityUser.getUserType(), 2)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该用户已经是群主");
        }

        if (Objects.equals(manageCommunityUser.getUserType(), 1)) {
            manageCommunityUser.setUserType(0);
        } else {
            manageCommunityUser.setUserType(1);
        }
        baseMapper.updateManageCommunityUser(manageCommunityUser);

        return R.ok("设置成功");
    }
}
