package com.ningdatech.pmapi.sys.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.IdVo;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.file.entity.vo.result.AttachFileVo;
import com.ningdatech.file.service.FileService;
import com.ningdatech.pmapi.common.util.BizUtils;
import com.ningdatech.pmapi.sys.model.entity.Notice;
import com.ningdatech.pmapi.sys.model.req.NoticeListReq;
import com.ningdatech.pmapi.sys.model.req.NoticeSaveReq;
import com.ningdatech.pmapi.sys.model.req.NoticeStatusModifyReq;
import com.ningdatech.pmapi.sys.model.vo.NoticeDetailVO;
import com.ningdatech.pmapi.sys.model.vo.NoticeListItemVO;
import com.ningdatech.pmapi.sys.service.INoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * MsgManage
 * </p>
 *
 * @author WendyYang
 * @since 21:30 2022/7/21
 */
@Component
@RequiredArgsConstructor
public class NoticeManage {

    private final INoticeService noticeService;
    private final FileService fileService;

    @Transactional(rollbackFor = Exception.class)
    public IdVo<Long> saveOrModify(NoticeSaveReq req) {
        Notice notice = BeanUtil.copyProperties(req, Notice.class);
        noticeService.saveOrUpdate(notice);
        return IdVo.of(notice.getId());
    }

    public NoticeDetailVO detail(Long id) {
        Notice notice = noticeService.getById(id);
        NoticeDetailVO detail = BeanUtil.copyProperties(notice, NoticeDetailVO.class);
        List<Long> fileIds = BizUtils.splitToLong(notice.getAttachment());
        if (!fileIds.isEmpty()) {
            List<AttachFileVo> attachFiles = fileService.getByIds(fileIds);
            detail.setAttachments(attachFiles);
        }
        return detail;
    }

    public boolean changeEnabled(NoticeStatusModifyReq req) {
        Notice notice = new Notice();
        notice.setId(req.getId());
        notice.setEnabled(req.getStatus());
        return noticeService.updateById(notice);
    }

    public void topped(Long id) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setToppedTime(LocalDateTime.now());
        noticeService.updateById(notice);
    }


    public PageVo<NoticeListItemVO> dashboardList(Integer limit, Integer type) {
        NoticeListReq req = new NoticeListReq();
        req.setPageSize(limit);
        req.setEnabled(true);
        req.setType(type);
        return listByManager(req);
    }

    public PageVo<NoticeListItemVO> listByManager(NoticeListReq req) {
        LambdaQueryWrapper<Notice> wrapper = Wrappers.lambdaQuery(Notice.class)
                .eq(req.getEnabled() != null, Notice::getEnabled, req.getEnabled())
                .like(StrUtil.isNotBlank(req.getTitle()), Notice::getTitle, req.getTitle())
                .eq(req.getType() != null, Notice::getType, req.getType())
                .orderByDesc(Notice::getToppedTime, Notice::getUpdateOn);
        Page<Notice> page = noticeService.page(req.page(), wrapper);
        if (page.getTotal() == 0) {
            return PageVo.empty();
        }
        List<NoticeListItemVO> tempDataList = CollUtils.convert(page.getRecords(), w -> NoticeListItemVO
                .builder()
                .id(w.getId())
                .type(w.getType())
                .title(w.getTitle())
                .enabled(w.getEnabled())
                .createOn(w.getCreateOn())
                .topped(w.getToppedTime() != null)
                .build());
        return PageVo.of(tempDataList, page.getTotal());
    }

    public void delNotice(Long id) {
        noticeService.removeById(id);
    }

}
