/*
 *      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.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.ht.vo.common.UploadVO;
import org.security.trans.basic.service.IDepartmentService;
import org.security.trans.common.service.IFileService;
import org.security.trans.task.dto.TaskDTO;
import org.security.trans.task.entity.Task;
import org.security.trans.task.mapper.TaskMapper;
import org.security.trans.task.service.ITaskService;
import org.security.trans.task.vo.TaskVO;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.security.system.entity.Role;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 任务-任务下发 服务实现类
 *
 * @author BladeX
 * @since 2022-03-10
 */
@Service
@Slf4j
@AllArgsConstructor
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

	private final IFileService fileService;
	private final IDepartmentService departmentService;

	@Override
	public IPage<Task> selectTaskPage(IPage<Task> page, TaskDTO task) {
		log.info("===task:{}",task);
		return page.setRecords(baseMapper.selectTaskPage(page,task));
	}

	@Override
	public TaskVO getDetail(String id) {
		TaskVO taskVO = new TaskVO();
		if (Func.isNotEmpty(id)) {
			Task detail = super.getById(id);
			taskVO = Func.copy(detail, TaskVO.class);
			if (Func.isNotEmpty(taskVO)) {
				//如果是内部检查，给检查机构赋名称
				if ("daily".equals(taskVO.getModule()) && "2".equals(taskVO.getCheckType())) {
					taskVO.setInspectDepName(departmentService.getById(taskVO.getInspectDep()).getDeptName());
				}
				taskVO.setFileList(fileService.getAttachments(detail.getId(), detail.getGroupName()));
			}
		}
		return taskVO;
	}
	@Override
	public R saveTask(TaskDTO taskDTO){
		Task task = BeanUtil.copyProperties(taskDTO, Task.class);
		if (Func.isNull(task.getDeptId()) || Func.isBlank(task.getDeptId())) {
			task.setDeptId(String.valueOf(AuthUtil.getDeptId()));
		}
		if(Func.isNull(task.getNum()) || Func.isBlank(task.getNum())){
			//任务编号
			task.setNum(Constant.TASK_PREFIX + (new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())));
		}
		super.saveOrUpdate(task);
		List<UploadVO> fileList = taskDTO.getFileList();
		if (Objects.nonNull(fileList) && !fileList.isEmpty()) {
			fileService.saveAttachments(fileList, task.getId());
		}
		return R.success("操作成功");
	}

	@Override
	public List<Task> getTaskCheckList(String frequency, String state, Date beginDate, Date endDate) {
		QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(StringUtil.isNotBlank(frequency), "FREQUENCY", frequency);
		queryWrapper.eq(StringUtil.isNotBlank(state), "STATE", state);
		queryWrapper.le(beginDate != null, "BEGIN_DATE", beginDate);
		queryWrapper.ge(endDate != null, "END_DATE", endDate);
		return baseMapper.selectList(queryWrapper);
	}

	@Override
	public List<Role> getRoleList() {
		return baseMapper.selectRoleList();
	}

}
