package com.gxwebsoft.oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.oa.entity.Task;
import com.gxwebsoft.oa.entity.TaskRecord;
import com.gxwebsoft.oa.mapper.NoticeMapper;
import com.gxwebsoft.oa.service.NoticeService;
import com.gxwebsoft.oa.entity.Notice;
import com.gxwebsoft.oa.param.NoticeParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.oa.service.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.gxwebsoft.oa.constants.NoticeConstants.*;

/**
 * 消息记录表Service实现
 *
 * @author 科技小王子
 * @since 2023-03-22 14:07:26
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Resource
    private UserService userService;
    @Resource
    private TaskService taskService;

    @Override
    public PageResult<Notice> pageRel(NoticeParam param) {
        PageParam<Notice, NoticeParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<Notice> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Notice> listRel(NoticeParam param) {
        List<Notice> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Notice, NoticeParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Notice getByIdRel(Integer noticeId) {
        NoticeParam param = new NoticeParam();
        param.setNoticeId(noticeId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public void add() {
    }

    @Override
    public void addNotice(Task task) {
      Notice notice = new Notice();
      notice.setType(NOTICE);
      notice.setIcon("NotificationFilled");
      notice.setSourceId(task.getTaskId());
      notice.setUserId(task.getPromoter());
      notice.setTitle(task.getName());
      notice.setContent("您有一个新指派的工单(" + task.getTaskId() + ")需要处理！");
      save(notice);
    }

    @Override
    public void addLetter(TaskRecord taskRecord) {
      // 通知发起人
      User user = userService.getById(taskRecord.getUserId());
      Task task = taskService.getById(taskRecord.getTaskId());
      Notice notice = new Notice();
      notice.setType(LETTER);
      notice.setIcon(user.getAvatar());
      notice.setSourceId(task.getTaskId());
      notice.setUserId(task.getPromoter());
      notice.setTitle(user.getNickname() + " 回复了你的工单(" + taskRecord.getTaskId() + ")");
      notice.setContent(taskRecord.getContent());
      save(notice);
      // 通知代发人
      notice.setUserId(task.getUserId());
      save(notice);
    }

    @Override
    public void addTodo(Task task) {
      Task record = taskService.getById(task.getTaskId());
      if (task.getCommander() != null) {
        Notice notice = new Notice();
        notice.setType(TODO);
        notice.setIcon("HistoryOutlined");
        notice.setSourceId(task.getTaskId());
        notice.setUserId(task.getCommander());
        notice.setTitle("您有一个新的工单需要处理！");
        notice.setContent(record.getName().concat("("+ task.getTaskId() +")"));
        notice.setProgress(record.getProgress());
        save(notice);
      }
    }

}
