package com.qunzhixinxi.hnqz.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.entity.WmProject;
import com.qunzhixinxi.hnqz.admin.entity.WmProjectEvidence;
import com.qunzhixinxi.hnqz.admin.mapper.WmProjectEvidenceMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmProjectMapper;
import com.qunzhixinxi.hnqz.admin.service.WmProjectService;
import com.qunzhixinxi.hnqz.common.security.service.HnqzUser;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * CRO关联的项目管理表
 *
 * @author lixuesong
 * @date 2021年06月21日 13:19
 */
@Slf4j
@Service
@AllArgsConstructor
public class WmProjectServiceImpl extends ServiceImpl<WmProjectMapper, WmProject> implements WmProjectService {

	private final WmProjectMapper wmProjectMapper;
	private final WmProjectEvidenceMapper wmProjectEvidenceMapper;

	/**
	 * 分页查询
	 *
	 * @param page
	 * @param deptId
	 * @param projectName
	 * @param delegate
	 * @return
	 */
	@Override
	public IPage<WmProject> listProjectPage(Page page, Integer deptId, String projectName, String delegate) {
		return wmProjectMapper.listProjectPage(page, deptId, projectName, delegate);
	}

	/**
	 * 根据id查询
	 *
	 * @param id
	 * @return
	 */
	@Override
	public WmProject getProjectById(Integer id) {
		return wmProjectMapper.getProjectById(id);
	}

	/**
	 * 保存项目
	 *
	 * @param wmProject
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveProject(WmProject wmProject) {
		HnqzUser user = SecurityUtils.getUser();
		// 当前时间
		LocalDateTime now = LocalDateTime.now();
		// 当前用户
		Integer currentUserId = user.getId();
		wmProject.setDeptId(user.getDeptId());
		wmProject.setCreateTime(now);
		wmProject.setCreateUser(currentUserId);
		wmProject.setUpdateTime(now);
		wmProject.setUpdateUser(currentUserId);
		// 保存主表
		this.save(wmProject);
		// 保存静态证据字表
		if (CollectionUtil.isNotEmpty(wmProject.getEvidenceList())) {
			wmProject.getEvidenceList().forEach(projectEvidence -> {
				projectEvidence.setProjectId(wmProject.getId());
				projectEvidence.setCreateTime(now);
				projectEvidence.setCreateUser(currentUserId);
				wmProjectEvidenceMapper.insert(projectEvidence);
			});
		}
	}

	/**
	 * 更新项目
	 *
	 * @param wmProject
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateProjectById(WmProject wmProject) {
		HnqzUser user = SecurityUtils.getUser();
		// 当前时间
		LocalDateTime now = LocalDateTime.now();
		// 当前用户
		Integer currentUserId = user.getId();
		wmProject.setDeptId(user.getDeptId());
		wmProject.setUpdateTime(now);
		wmProject.setUpdateUser(currentUserId);
		// 保存主表
		this.updateById(wmProject);
		// 先删除旧数据
		wmProjectEvidenceMapper.delete(Wrappers.<WmProjectEvidence>lambdaQuery()
				.eq(WmProjectEvidence::getProjectId, wmProject.getId()));
		// 保存静态证据字表
		if (CollectionUtil.isNotEmpty(wmProject.getEvidenceList())) {
			wmProject.getEvidenceList().forEach(projectEvidence -> {
				projectEvidence.setProjectId(wmProject.getId());
				projectEvidence.setCreateTime(now);
				projectEvidence.setCreateUser(currentUserId);
				wmProjectEvidenceMapper.insert(projectEvidence);
			});
		}
	}

	/**
	 * 删除项目
	 *
	 * @param projectId
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void removeProjectById(Integer projectId) {
		// 删除主表数据
		this.removeById(projectId);
		// 删除子表数据
		wmProjectEvidenceMapper.delete(Wrappers.<WmProjectEvidence>lambdaQuery()
				.eq(WmProjectEvidence::getProjectId, projectId));
	}
}
