package com.jwipc.sys.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.constant.NoticeStatus;
import com.jwipc.sys.convert.MsgNoticeConvert;
import com.jwipc.sys.dto.MsgNoticeDTO;
import com.jwipc.sys.entity.DealtWithTargetInfo;
import com.jwipc.sys.entity.MsgNotice;
import com.jwipc.sys.entity.MsgNoticeContent;
import com.jwipc.sys.logic.NoticeLogic;
import com.jwipc.sys.model.po.DealtWithTargetInfoPO;
import com.jwipc.sys.model.po.MsgNoticePO;
import com.jwipc.sys.model.query.LoginLogQuery;
import com.jwipc.sys.model.query.MsgNoticeQuery;
import com.jwipc.sys.service.IMsgNoticeContentService;
import com.jwipc.sys.service.IMsgNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/1/31 15:32
 * @desc 公告
 */
@Service
@RequiredArgsConstructor
public class NoticeLogicImpl implements NoticeLogic {
    private final IMsgNoticeService msgNoticeService;
    private final IMsgNoticeContentService msgNoticeContentService;
    private final MsgNoticeConvert msgNoticeConvert;
    @Override
    public PageView findByPage(MsgNoticeQuery query) {
        final Page<MsgNotice> dbPage = PageUtil.toPage(query);
        List<MsgNoticePO> msgNoticePOS = msgNoticeService.findPageByQuery(dbPage, query);
        List<MsgNoticeDTO> msgNoticeDTOS = msgNoticeConvert.po2DTOList(msgNoticePOS);

        return PageUtil.getPV(msgNoticeDTOS, dbPage.getTotal());
    }

    @Override
    public MsgNoticeDTO getById(Long id) {
        MsgNoticePO msgNoticePO = msgNoticeService.getDataById(id);
        return msgNoticeConvert.po2DTO(msgNoticePO);
    }

    @Override
    public void saveOrUpdate(MsgNoticeDTO param) {
        MsgNotice msgNotice = msgNoticeConvert.dto2Entity(param);
        if (param.getId()==null){
            msgNotice.setStatus(NoticeStatus.ADD.getStatus());
            msgNotice.setCreateTime(LocalDateTime.now());
            msgNotice.setCreateUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        }
        msgNoticeService.saveOrUpdate(msgNotice);
        MsgNoticeContent msgNoticeContent = msgNoticeContentService.getOne(new LambdaQueryWrapper<MsgNoticeContent>().eq(MsgNoticeContent::getNoticeId,msgNotice.getId()).last(" limit 1"));
        if (ObjectUtil.isNull(msgNoticeContent)){
            msgNoticeContent = new MsgNoticeContent();
        }
        msgNoticeContent.setNoticeId(msgNotice.getId());
        msgNoticeContent.setContent(param.getContent());
        msgNoticeContentService.saveOrUpdate(msgNoticeContent);

    }

    /**
     * 发布
     *
     * @param id 参数
     */
    @Override
    public void publish(Long id) {
        MsgNotice msgNotice = new MsgNotice();
        msgNotice.setId(id);
        msgNotice.setStatus(NoticeStatus.PUBLISH.getStatus());
        msgNotice.setPublishTime(LocalDateTime.now());
        msgNotice.setPublishUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        msgNoticeService.updateById(msgNotice);
    }

    /**
     * 删除
     *
     * @param id 参数
     */
    @Override
    public void remove(Long id) {
        msgNoticeService.removeById(id);
    }
}
