package com.agileboot.system.notice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.agileboot.common.core.enums.BasicEnumUtil;
import com.agileboot.common.core.enums.common.NoticeTypeEnum;
import com.agileboot.common.core.enums.common.StatusEnum;
import com.agileboot.common.core.exception.BizException;
import com.agileboot.common.core.exception.error.ErrorCode;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.notice.dto.NoticeAddDTO;
import com.agileboot.system.notice.dto.NoticeQuery;
import com.agileboot.system.notice.dto.NoticeUpdateDTO;
import com.agileboot.system.notice.dto.NoticeVO;
import com.agileboot.system.notice.entity.SysNotice;
import com.agileboot.system.notice.mapper.SysNoticeMapper;
import com.agileboot.system.notice.service.ISysNoticeService;
import com.agileboot.system.user.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    private final ISysUserService userService;


    @Override
    public PageR<NoticeVO> getNoticeList(NoticeQuery query) {
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<SysNotice>()
                .like(StrUtil.isNotEmpty(query.getNoticeTitle()), "notice_title", query.getNoticeTitle())
                .eq(StrUtil.isNotEmpty(query.getNoticeType()), "notice_type", query.getNoticeType())
                .eq("n.deleted", 0)
                .like(StrUtil.isNotEmpty(query.getCreatorName()), "u.username", query.getCreatorName());
        Page<SysNotice> page = this.baseMapper.getNoticeList(query.toPage(), queryWrapper);

        Set<Long> userIds = page.getRecords().stream().map(SysNotice::getCreateBy).collect(Collectors.toSet());
        Map<Long, String> idNameMap = userService.geIdNameByIds(userIds);
        page.getRecords().forEach(sysNotice -> {
            Long creatorId = sysNotice.getCreateBy();
            String creatorName = idNameMap.get(creatorId);
            sysNotice.setSearchValue(creatorName);
        });
        IPage<NoticeVO> convert = page.convert(NoticeVO::new);
        return new PageR<>(convert);
    }

    @Override
    public NoticeVO getNoticeInfo(Long noticeId) {
        SysNotice byId = super.getById(noticeId);
        if (byId == null) {
            throw new BizException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND.message(), noticeId, "通知公告");
        }
        return new NoticeVO(byId);
    }

    @Override
    public void addNotice(NoticeAddDTO addDTO) {
        BasicEnumUtil.fromValue(NoticeTypeEnum.class, addDTO.getNoticeType());
        BasicEnumUtil.fromValue(StatusEnum.class, addDTO.getStatus());

        SysNotice sysNotice = new SysNotice();
        BeanUtil.copyProperties(addDTO, sysNotice);
        super.save(sysNotice);
    }

    @Override
    public void updateNotice(NoticeUpdateDTO updateDTO) {
        BasicEnumUtil.fromValue(NoticeTypeEnum.class, updateDTO.getNoticeType());
        BasicEnumUtil.fromValue(StatusEnum.class, updateDTO.getStatus());

        Long noticeId = updateDTO.getNoticeId();
        SysNotice byId = super.getById(noticeId);
        if (byId == null) {
            throw new BizException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, noticeId, "通知公告");
        }
        BeanUtil.copyProperties(updateDTO, byId, "noticeId");
        super.updateById(byId);
    }

    @Override
    public void deleteNotice(List<Integer> noticeIds) {
        super.removeBatchByIds(noticeIds);
    }
}
