package jnpf.notice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import jnpf.model.notice.domain.FtbNoticeManager;
import jnpf.model.notice.dto.FtbNoticeManagerDto;
import jnpf.model.notice.enums.NoticeEnums;
import jnpf.model.notice.req.manager.AddNoticeManagerReq;
import jnpf.model.notice.req.manager.QueryNoticeManagerReq;
import jnpf.notice.mapper.FtbNoticeManagerMapper;
import jnpf.notice.service.FtbNoticeManagerService;
import jnpf.notice.utils.NoticeUtils;
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.List;
import java.util.stream.Collectors;

/**
 * @author 许贵林
 * @description 针对表【ftb_notice_manager(公告管理员表)】的数据库操作Service实现
 * @createDate 2024-05-08 09:42:09
 */
@Service
public class FtbNoticeManagerServiceImpl extends ServiceImpl<FtbNoticeManagerMapper, FtbNoticeManager>
        implements FtbNoticeManagerService {

    @Autowired
    private NoticeUtils noticeUtils;

    /**
     * 分页查询公告管理员列表
     *
     * @param req 查询条件对象，包含查询公告所需的信息
     * @return
     */
    @Override
    public PageInfo<FtbNoticeManagerDto> getPageLists(QueryNoticeManagerReq req) {
        Page<FtbNoticeManagerDto> queryPage = baseMapper.pagingQuery(Page.of(req.getCurrentPage(), req.getPageSize()), req);
        List<FtbNoticeManagerDto> records = queryPage.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            noticeUtils.fillFlowerAndHeadLog(records);
        }
        //构建分页返回数据
        PageInfo<FtbNoticeManagerDto> pageInfo = new PageInfo<>();
        pageInfo.setList(records);
        pageInfo.setPageSize((int) queryPage.getSize());
        pageInfo.setPageNum((int) queryPage.getCurrent());
        pageInfo.setTotal((int) queryPage.getTotal());
        return pageInfo;
    }

    /**
     * 插入公告管理员
     *
     * @param req 插入公告请求对象，包含需要插入的公告信息
     */
    @Override
    @Transactional
    public void insertData(AddNoticeManagerReq req) {
        List<String> userList = noticeUtils.uniqueStringList(req.getUserList());
        if (CollectionUtil.isEmpty(userList)) {
            throw new RuntimeException("请选择管理员");
        }

        List<String> realAddUserList = new ArrayList<>();

        //查询已经是的管理员
        QueryWrapper<FtbNoticeManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .in(FtbNoticeManager::getUserId, userList)
                .eq(FtbNoticeManager::getEnabledMark, NoticeEnums.DeleteType.VALID.getCode());
        List<FtbNoticeManager> oldList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(oldList)) {
            List<String> exiestUser = oldList.stream()
                    .map(FtbNoticeManager::getUserId)
                    .collect(Collectors.toList());
            for (String userId : userList) {
                if (!exiestUser.contains(userId)) {
                    realAddUserList.add(userId);
                }
            }
        } else {
            realAddUserList = userList;
        }
        if (CollectionUtil.isEmpty(realAddUserList)) {
            //选择的用户已经全部是管理员 无需操作 返回
            throw new RuntimeException("添加的用户已经是管理员");
        }
        //添加管理员
        List<FtbNoticeManager> managerList = new ArrayList<>();
        for (String userId : realAddUserList) {
            FtbNoticeManager manager = new FtbNoticeManager();
            manager.setUserId(userId);
            manager.setEnabledMark(NoticeEnums.DeleteType.VALID.getCode());
            managerList.add(manager);
        }
        saveBatch(managerList);
    }

    /**
     * 查询并检查公告是否存在
     *
     * @param id
     * @return
     */
    private FtbNoticeManager queryAndCheckById(String id) {
        FtbNoticeManager entity = baseMapper.selectById(id);
        if (null == entity) {
            throw new RuntimeException("数据不存在");
        }
        return entity;
    }

    /**
     * 删除公告
     *
     * @param id 公告的唯一标识符，用于标识需要删除的公告
     */
    @Override
    public void deleteData(String id) {
        queryAndCheckById(id);
        baseMapper.deleteById(id);
    }

    /**
     * 根据用户ID查询公告
     *
     * @param userId 用户ID，用于查找该用户相关的公告
     * @return 返回与指定用户ID相关的公告管理器对象
     */
    @Override
    public FtbNoticeManager queryByUserId(String userId) {

        QueryWrapper<FtbNoticeManager> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FtbNoticeManager::getUserId, userId)
                .eq(FtbNoticeManager::getEnabledMark, NoticeEnums.DeleteType.VALID.getCode());
        List<FtbNoticeManager> managerList = baseMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(managerList)) {
            return null;
        }
        return managerList.get(0);
    }
}




