package com.kym.service.admin.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.kym.common.utils.CommUtil;
import com.kym.common.utils.IDGenerator;
import com.kym.entity.admin.StationNotice;
import com.kym.entity.admin.SystemNotice;
import com.kym.entity.admin.queryParams.NoticeQueryParams;
import com.kym.entity.admin.vo.NoticeStationVo;
import com.kym.entity.common.PageBean;
import com.kym.mapper.admin.SystemNoticeMapper;
import com.kym.service.admin.StationNoticeService;
import com.kym.service.admin.SystemNoticeService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 系统公告通知表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2024-08-20
 */
@Service
public class SystemNoticeServiceImpl extends MyBaseServiceImpl<SystemNoticeMapper, SystemNotice> implements SystemNoticeService {

    private final StationNoticeService stationNoticeService;

    public SystemNoticeServiceImpl(StationNoticeService stationNoticeService) {
        this.stationNoticeService = stationNoticeService;
    }

    /**
     * 新增公告
     *
     * @param vo
     */
    @Override
    @Transactional
    public void createNotice(NoticeStationVo vo) {
        var systemNotice = new SystemNotice();
        BeanUtils.copyProperties(vo, systemNotice);
        save(systemNotice.setAdminUserId(StpUtil.getLoginIdAsLong()).setAdminUserName(StpUtil.getSession().getString("username")).setBaseId(IDGenerator.INS().nextId()));
        stationNoticeService.saveBatch(vo.getStationIdList().stream().map(stationId -> new StationNotice().setNoticeId(systemNotice.getId()).setStationId(stationId)).toList());
    }

    /**
     * 分页查询公告列表
     *
     * @param params
     * @return
     */
    @Override
    public PageBean<NoticeStationVo> listSystemNotice(NoticeQueryParams params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        MPJLambdaWrapper<SystemNotice> wrapper = JoinWrappers.lambda(SystemNotice.class)
                .selectAsClass(SystemNotice.class, NoticeStationVo.class)
                .eq(CommUtil.isNotEmptyAndNull(params.getStatus()), SystemNotice::getStatus, params.getStatus())
                .like(CommUtil.isNotEmptyAndNull(params.getTitle()), SystemNotice::getTitle, params.getTitle())
                .like(CommUtil.isNotEmptyAndNull(params.getContent()), SystemNotice::getContent, params.getContent())
                .orderByDesc(SystemNotice::getUpdateTime);
        List<NoticeStationVo> res = this.selectJoinList(NoticeStationVo.class, wrapper);
        var notice2stations = stationNoticeService.lambdaQuery().in(StationNotice::getNoticeId, res.stream().map(NoticeStationVo::getId).toList()).list().stream().collect(Collectors.groupingBy(StationNotice::getNoticeId, Collectors.mapping(StationNotice::getStationId, Collectors.toList())));
        res.forEach(item -> item.setStationIdList(notice2stations.get(item.getId())));
        return new PageBean<>(res);
    }

    /**
     * 公告详情
     * @param noticeId
     * @return
     */
    @Override
    public NoticeStationVo getNoticeById(Long noticeId) {
        var systemNotice = getById(noticeId);
        var stationNoticeVo = new NoticeStationVo();
        BeanUtils.copyProperties(systemNotice, stationNoticeVo);
        var stationNoticeList = stationNoticeService.lambdaQuery().eq(StationNotice::getNoticeId, noticeId).list();
        stationNoticeVo.setStationIdList(stationNoticeList.stream().map(StationNotice::getStationId).toList());
        return stationNoticeVo;
    }

}
