/*
 *      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 lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.desk.feign.IXYunClient;
import org.security.system.user.vo.UserVO;
import org.security.trans.applyAndReport.dto.WorkFilingDTO;
import org.security.trans.applyAndReport.entity.WorkFiling;
import org.security.trans.applyAndReport.vo.WorkFilingVO;
import org.security.trans.applyAndReport.mapper.WorkFilingMapper;
import org.security.trans.applyAndReport.service.IWorkFilingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.security.trans.common.service.IFileService;
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 org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 工作报备表 服务实现类
 *
 * @author BladeX
 * @since 2022-08-05
 */
@Slf4j
@Service
@AllArgsConstructor
public class WorkFilingServiceImpl extends ServiceImpl<WorkFilingMapper, WorkFiling> implements IWorkFilingService {

	private final IFileService fileService;
	private final IXYunClient xYunClient;

	@Override
	public IPage<WorkFilingVO> selectWorkFilingPage(IPage<WorkFilingVO> page, WorkFilingVO workFiling) {
		String userId =String.valueOf(AuthUtil.getUserId());
		workFiling.setQueryUserId(userId);
		return page.setRecords(baseMapper.selectWorkFilingPage(page, workFiling));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R handin(WorkFilingDTO workFilingDTO){
		workFilingDTO.setState("1");
		//		知会报备对象
		//获取消息通知人员工号
		List<String> userCodes = getNoticeUserCode(workFilingDTO);
		String msg = "您收到一条[安全管理信息化系统工作报备]，事项名称为[" + workFilingDTO.getName() + "]";
		xYunClient.sendMessageToXYun(Func.join(userCodes,","),msg);
		return saveData(workFilingDTO);
	}
	/**
	 * 获取消息通知人员工号
	 */
	private List<String> getNoticeUserCode(WorkFilingDTO workFilingDTO) {
		List<String> userCodes = new ArrayList<>();
		//获取消息通知人员工号
		List<String> list = Arrays.asList(workFilingDTO.getReceiveUserId().split(","));
		if (Func.isNotEmpty(list)){
			list.forEach(userId ->{
				UserVO user = baseMapper.getUserById(userId);
				if (Func.isNotEmpty(user)){
						userCodes.add(user.getCode());
				}
			});
		}
		return userCodes;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R saveData(WorkFilingDTO workFilingDTO) {
		WorkFiling workFiling = Func.copy(workFilingDTO, WorkFiling.class);
		this.saveOrUpdate(workFiling);
		//保存相关附件
		fileService.saveAttachments(workFilingDTO.getFileList(),workFiling.getId());
		return R.data(workFiling);
	}

	@Override
	public WorkFilingVO getData(String id) {
		WorkFilingVO vo = new WorkFilingVO();
		WorkFiling workFiling = this.getById(id);
		if (Func.isNotEmpty(workFiling)) {
			vo = Func.copy(workFiling, WorkFilingVO.class);
			vo.setFileList(fileService.getAttachments(workFiling.getId(),workFiling.getGroupName()));
		}
		return vo;
	}

}
