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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.entity.WmDaTrack;
import com.qunzhixinxi.hnqz.admin.mapper.WmDaProjectMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmDaTrackMapper;
import com.qunzhixinxi.hnqz.admin.service.WmDaProjectService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.qunzhixinxi.hnqz.admin.entity.WmDaProject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 档案-项目管理
 *
 * @author Liujh
 * @date 2020-12-8
 */
@Service
@AllArgsConstructor
public class WmDaProjectServiceImpl extends ServiceImpl<WmDaProjectMapper, WmDaProject> implements WmDaProjectService {
	private final WmDaProjectMapper wmDaProjectMapper;

	private final WmDaTrackMapper wmDaTrackMapper;

	@Override
	public WmDaProject selectProjects(WmDaProject wmDaProject) {
		wmDaProject = wmDaProjectMapper.selectByPrimaryKey(wmDaProject.getId());
		WmDaTrack wmDaTrack = new WmDaTrack();
		wmDaTrack.setProjectId(wmDaProject.getProjectId());
		wmDaProject.setWmDaTrackList(wmDaTrackMapper.listWmDaTrack(wmDaTrack));
		return wmDaProject;
	}

	@Override
	public IPage<Map<String, Object>> selectWmDaProjectList(Page page, WmDaProject wmDaProject) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
		return wmDaProjectMapper.selectWmDaProjectList(page, wmDaProject);
	}

//	@Override
//	public int updatewmDaProjectById(Map paramMap, Integer currentUserId) {
//
////		String oId = StringUtil.toString(paramMap.get("id"));
//		String oId = paramMap.get("id").toString();
//		if (oId == null || "".equals(oId)) {
//			return Integer.valueOf(-1);
//		}
//		WmDaProject wmDaProject = wmDaProjectMapper.selectByPrimaryKey(Integer.valueOf(oId));
//		if (wmDaProject == null) {
//			return Integer.valueOf(-3);
//		}
//		//项目名称不能相同
//		List<WmDaProject> list = wmDaProjectMapper.selectWmDaProjectByParam(paramMap);
//		if (null != list && list.size() > 0) {
//			return Integer.valueOf(-4);
//		}
//		WmDaTrack wmDaTrack = new WmDaTrack().generateWmDaTrack(paramMap);
//		wmDaTrack.setUpdateTime(new Date());
//		wmDaTrack.setUpdateUser(currentUserId);
//		wmDaTrackMapper.deleteByprojectId(wmDaProject.getId());
//
//		List<Map> wmDaTrackList = (List<Map>) paramMap.get("wmDaTrackList");
//		if (null != wmDaTrackList && wmDaTrackList.size() > 0) {
//			for (Map map : wmDaTrackList) {
//				WmDaTrack wmDaTrack1 = new WmDaTrack().generateWmDaTrack(map);
//				wmDaTrack1.setProjectId(wmDaProject.getId());
//				wmDaTrackMapper.insertSelective(wmDaTrack1);
//			}
//		}
////		WmDaProject wmDaProject1 = new WmDaProject().generatewmDaProject(paramMap);
//		WmDaProject wmDaProject1 = new WmDaProject();
//		wmDaProject1.setUpdateUser(currentUserId);
//		wmDaProject1.setUpdateTime(new Date());
//		wmDaProjectMapper.updateByPrimaryKeySelective(wmDaProject1);
//		return wmDaTrack.getProjectId();
//	}

//	@Override
//	public int insertWmDaProject(Map paramMap, Integer currentUserId) {
//		//项目名字不能相同
//		List list = wmDaProjectMapper.selectWmDaProjectByParam(paramMap);
//
//		if (null != list && list.size() > 0) {
//			return Integer.valueOf(-1);
//		}
//		//新增项目基本信息
////		WmDaProject wmDaProject = new WmDaProject().generatewmDaProject(paramMap);
//		WmDaProject wmDaProject = new WmDaProject();
//		wmDaProject.setCreateUser(currentUserId);
//		wmDaProject.setStatus(2);
//		wmDaProject.setCreateTime(new Date());
//		wmDaProject.setCreateUser(currentUserId);
//		wmDaProjectMapper.insertSelective(wmDaProject);
//		//新增跟踪主体、客体名称信息 如果有多个，前端传过来一个list，采用遍历的形式获取传来的值
//		List<Map> wmDaTrackList = (List<Map>) paramMap.get("wmDaTrackList");
//		if (null != wmDaTrackList && wmDaTrackList.size() > 0) {
//			for (Map map : wmDaTrackList) {
//
//				WmDaTrack wmDaTrack = new WmDaTrack().generateWmDaTrack(map);
//				wmDaTrack.setCreateTime(new Date());
//				wmDaTrack.setCreateUser(currentUserId);
//				wmDaTrack.setProjectId(wmDaProject.getId());
//				wmDaTrack.setStatus(2);
//				wmDaTrackMapper.insertSelective(wmDaTrack);
//			}
//		}
//		return wmDaProject.getId();
//	}

	@Override
	public R<WmDaProject> insertWmDaProjects(WmDaProject wmDaProject,Integer currentUserId) {
		//项目名称和ID不能相同
		String projectName=wmDaProject.getProjectName();
		Long projectId=wmDaProject.getProjectId();
		Integer wmId=wmDaProject.getId();
		Map<String, Object> map=new HashMap<>();
		map.put("projectName",projectName);
		map.put("id",wmId);
		List<WmDaProject> listParam=wmDaProjectMapper.selectWmDaProjectByParam(map);
		if(listParam!=null && listParam.size()>0){
			return R.failed("已经存在此项目名称");
		}
		Map<String, Object> map1=new HashMap<>();
		map1.put("projectId",projectId);
		map1.put("id",wmId);
		List<WmDaProject> listParam1=wmDaProjectMapper.selectWmDaProjectByParam(map1);
		if(listParam1!=null && listParam1.size()>0){
			return R.failed("已经存在此项目ID");
		}
		wmDaProject.setCreateUser(currentUserId);
		wmDaProject.setProjectStatus(2);
		wmDaProject.setCreateTime(new Date());
		wmDaProject.setCreateUser(currentUserId);
		wmDaProjectMapper.insertSelective(wmDaProject);

		List<WmDaTrack> wmDaTrackList = wmDaProject.getWmDaTrackList();
		if (null != wmDaTrackList && wmDaTrackList.size() > 0) {
			for (WmDaTrack wmDaTrack : wmDaTrackList) {
				wmDaTrack.setCreateTime(new Date());
				wmDaTrack.setCreateUser(currentUserId);
				wmDaTrack.setProjectId(wmDaProject.getProjectId());
				wmDaTrackMapper.insertSelective(wmDaTrack);
			}
		}
		return R.ok();
	}

	@Override
	public R<WmDaProject> updateWmDaProjectsById(WmDaProject wmDaProject, Integer currentUserId) {
		Integer id = wmDaProject.getId();
		if (id == null || "".equals(id)) {
			return R.failed("id不存在");
		}
		WmDaProject wmDaProjects = wmDaProjectMapper.selectByPrimaryKey(id);
		if (wmDaProjects == null) {
			return R.failed("问查询到该项目信息");
		}
		//项目名称和ID不能相同
		String projectName=wmDaProject.getProjectName();
		Long projectId=wmDaProject.getProjectId();
		Integer wmId=wmDaProject.getId();
		Map<String, Object> map=new HashMap<>();
		map.put("projectName",projectName);
		map.put("id",wmId);
		List<WmDaProject> listParam=wmDaProjectMapper.selectWmDaProjectByParam(map);
		if(listParam!=null && listParam.size()>0){
			return R.failed("已经存在此项目名称");
		}
		Map<String, Object> map1=new HashMap<>();
		map1.put("projectId",projectId);
		map1.put("id",wmId);
		List<WmDaProject> listParam1=wmDaProjectMapper.selectWmDaProjectByParam(map1);
		if(listParam1!=null && listParam1.size()>0){
			return R.failed("已经存在此项目ID");
		}
		wmDaTrackMapper.deleteByProjectId(wmDaProject.getProjectId());

		List<WmDaTrack> wmDaTrackList = wmDaProject.getWmDaTrackList();
		if (null != wmDaTrackList && wmDaTrackList.size() > 0) {
			for (WmDaTrack wmDaTrack : wmDaTrackList) {
				wmDaTrack.setProjectId(wmDaProject.getProjectId());
				wmDaTrack.setUpdateTime(new Date());
				wmDaTrack.setUpdateUser(currentUserId);
				wmDaTrackMapper.insertSelective(wmDaTrack);
			}
		}
		wmDaProject.setUpdateUser(currentUserId);
		wmDaProject.setUpdateTime(new Date());
		wmDaProjectMapper.updateByPrimaryKeySelective(wmDaProject);
		return R.ok();
	}

}
