/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.applyAndReport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.security.common.Constant;
import org.security.trans.applyAndReport.dto.NoteRemindDTO;
import org.security.trans.applyAndReport.entity.NoteRemind;
import org.security.trans.applyAndReport.vo.NoteRemindVO;
import org.security.trans.applyAndReport.mapper.NoteRemindMapper;
import org.security.trans.applyAndReport.service.INoteRemindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.security.trans.basic.service.IDepartmentService;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.security.common.Constant.*;

/**
 * 便签提醒记录表 服务实现类
 *
 * @author BladeX
 * @since 2022-08-05
 */
@Service
@AllArgsConstructor
public class NoteRemindServiceImpl extends ServiceImpl<NoteRemindMapper, NoteRemind> implements INoteRemindService {
	private final IDepartmentService departmentService;

	@Override
	public IPage<NoteRemindVO> selectNoteRemindPage(IPage<NoteRemindVO> page, NoteRemindDTO noteRemind) {
		return page.setRecords(baseMapper.selectNoteRemindPage(page, noteRemind));
	}

	@Override
	public IPage<NoteRemindVO> selectNoteRemindFromOthers(IPage<NoteRemindVO> page, NoteRemindDTO noteRemind) {
		if (Func.isEmpty(noteRemind.getReceiveUsers())) {
			noteRemind.setReceiveUsers(String.valueOf(AuthUtil.getUserId()));
		}
		List<NoteRemind> list = this.list(new QueryWrapper<NoteRemind>().lambda()
			.eq(NoteRemind::getDelFlag, Constant.DELETE_FLAG_0)
			.eq(NoteRemind::getState, REMINDER_STATE_SUBMIT)
			.like(Func.isNotEmpty(noteRemind.getReceiveUsers()), NoteRemind::getReceiveUsers, noteRemind.getReceiveUsers())
			.gt(NoteRemind::getEndTime, new Date())
			.le(NoteRemind::getStartTime, new Date())
			.orderByAsc(NoteRemind::getEndTime)
			.orderByDesc(NoteRemind::getCreatedTime).orderByDesc(NoteRemind::getUpdatedTime));
		return page.setRecords(Func.copy(list, NoteRemindVO.class));
	}

	@Override
	public boolean saveNoteRemind(NoteRemind noteRemind) {
		if (Func.isBlank(noteRemind.getDeptId())) {
			noteRemind.setDeptId(AuthUtil.getDeptId());
		}
		noteRemind.setDeptName(departmentService.getById(noteRemind.getDeptId()).getDeptName());
		return saveOrUpdate(noteRemind);
	}

	@Override
	public boolean handin(NoteRemind noteRemind) {
		if (Func.isBlank(noteRemind.getDeptId())) {
			noteRemind.setDeptId(AuthUtil.getDeptId());
		}
		noteRemind.setDeptName(departmentService.getById(noteRemind.getDeptId()).getDeptName());
		noteRemind.setState(REMINDER_STATE_SUBMIT);
		return saveOrUpdate(noteRemind);
	}

	@Override
	public R closeByIds(List<Long> toLongList) {
		ArrayList<NoteRemind> list = new ArrayList<>();
		if (Func.isNotEmpty(toLongList)) {
			toLongList.forEach(item -> {
				//根据id获取工作提醒信息
				NoteRemind noteRemind = this.getById(item);
				list.add(noteRemind);
			});
		}
		if (Func.isNotEmpty(list)) {
			list.forEach(item -> {
				item.setState(REMINDER_STATE_DONE);
			});
		}
		return R.status(this.saveOrUpdateBatch(list));
	}

	@Override
	public List<NoteRemind> getUsingList(){
		QueryWrapper<NoteRemind> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("STATE", REMINDER_STATE_SUBMIT);
		queryWrapper.eq("DEL_FLAG",Constant.DELETE_FLAG_0);
		return baseMapper.selectList(queryWrapper);
	}
}
