package com.qunzhixinxi.hnqz.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDeptRelation;
import com.qunzhixinxi.hnqz.admin.entity.WmDaAgent;
import com.qunzhixinxi.hnqz.admin.entity.WmDaDrugEnt;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskType;
import com.qunzhixinxi.hnqz.admin.enums.DelEnum;
import com.qunzhixinxi.hnqz.admin.mapper.SysDeptMapper;
import com.qunzhixinxi.hnqz.admin.mapper.SysDeptRelationMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmDaAgentMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmDaDrugEntMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmTaskTypeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 批量补充新增的任务类型
 * 
 * @author lixuesong
 * @date 2021年11月22日 18:55
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {HnqzAdminApplication.class, Test.class})
public class WmTaskTypeServiceTest {

	@Autowired
	private WmTaskTypeMapper wmTaskTypeMapper;

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Autowired
	private SysDeptRelationMapper sysDeptRelationMapper;

	@Autowired
	private WmDaDrugEntMapper wmDaDrugEntMapper;

	@Autowired
	private WmDaAgentMapper wmDaAgentMapper;
	
	private static final String taskTypeId = "34";

	private static final String taskTypeName = "医院患者教育培训";

	/**
	 * 批量补充新增的任务类型33-药店拜访
	 */
	@Test
	public void test() {
		WmTaskType wmTaskTypeQ = new WmTaskType();
//		wmTaskTypeQ.setTaskTypeLevel("1");
//		wmTaskTypeQ.setStatus("0");
//		wmTaskTypeQ.setDelFlag("0");
		wmTaskTypeQ.setId(taskTypeId);
		List<WmTaskType> tempList = wmTaskTypeMapper.selectList(Wrappers.query(wmTaskTypeQ));

		// 1.第一级是药企的情况
//		List<SysDept> deptList = sysDeptMapper.selectList(Wrappers.<SysDept>lambdaQuery()
//				.eq(SysDept::getLevel, 2));
		List<SysDept> sysDepts1 = sysDeptMapper.listAllDept();
		List<SysDept> deptList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(sysDepts1)) {
			deptList = sysDepts1.stream()
					.filter(sysDept -> sysDept.getLevel().equals(2))
					.collect(Collectors.toList());
		}
		// 遍历药企
		for (SysDept dept : deptList) {
			// 生成药企任务类型
			WmDaDrugEnt wmDaDrugEnt = wmDaDrugEntMapper.selectOne(Wrappers.<WmDaDrugEnt>lambdaQuery()
					.eq(WmDaDrugEnt::getDeptId, dept.getDeptId())
//					.eq(WmDaDrugEnt::getEnableFlag, EnableEnum.ENABLE.val())
					.eq(WmDaDrugEnt::getDelFlag, DelEnum.NOT_DEL.val()));
			if (wmDaDrugEnt == null) {
				continue;
			}
			for (WmTaskType wmTaskType1 : tempList) {
				WmTaskType newOne = BeanUtil.copyProperties(wmTaskType1, WmTaskType.class);
				newOne.setCreateTime(null);
				newOne.setId(null);
				newOne.setDurgentId(wmDaDrugEnt.getId());
				newOne.setTaskTypeLevel("2");
				newOne.setTaskEntId(wmDaDrugEnt.getId());
				newOne.setDeptId(wmDaDrugEnt.getDeptId());
				newOne.setStatus("0");
				newOne.setBaseId(wmTaskType1.getId());
				newOne.setRuleId(wmDaDrugEnt.getId());
				newOne.setParentId(wmTaskType1.getParentId());
				List<WmTaskType> taskTypeList = wmTaskTypeMapper.selectList(Wrappers.<WmTaskType>lambdaQuery()
						.eq(WmTaskType::getTaskTypeName, taskTypeName)
						.eq(WmTaskType::getRuleId, newOne.getRuleId())
						.eq(WmTaskType::getDeptId, newOne.getDeptId())
						.eq(WmTaskType::getTaskEntId, newOne.getTaskEntId()));
				if (CollectionUtil.isEmpty(taskTypeList)) {
					newOne.setCreateTime(LocalDateTime.now());
					wmTaskTypeMapper.insert(newOne);
				}
			}

			// 查询一级CSO
			List<SysDeptRelation> deptRelationList = sysDeptRelationMapper.selectList(Wrappers.<SysDeptRelation>lambdaQuery()
					.eq(SysDeptRelation::getAncestor, dept.getDeptId())
					.ne(SysDeptRelation::getDescendant, dept.getDeptId()));
			if (CollectionUtil.isEmpty(deptRelationList)) {
				continue;
			}
			List<Integer> oneCSODeptIdList = deptRelationList.stream()
					.map(SysDeptRelation::getDescendant)
					.collect(Collectors.toList());
			List<SysDept> oneCSODeptList = sysDeptMapper.selectByIds(oneCSODeptIdList);
//			List<SysDept> oneCSODeptList = sysDeptMapper.selectList(Wrappers.<SysDept>lambdaQuery()
//					.eq(SysDept::getParentId, dept.getDeptId())
//					.eq(SysDept::getDelFlag, DelEnum.NOT_DEL.val())
//					.eq(SysDept::getLevel, 3));
			for (SysDept oneCSODept : oneCSODeptList) {
				// 生成药企给一级CSO的任务类型
				WmDaAgent wmDaAgent = wmDaAgentMapper.selectOne(Wrappers.<WmDaAgent>lambdaQuery()
						.eq(WmDaAgent::getDelFlag, DelEnum.NOT_DEL.val())
//						.eq(WmDaAgent::getEnableFlag, EnableEnum.ENABLE.val())
						.eq(WmDaAgent::getDeptId, oneCSODept.getDeptId()));
				if (wmDaAgent == null) {
					continue;
				}
				for (WmTaskType wmTaskType1 : tempList) {
					WmTaskType newOne = BeanUtil.copyProperties(wmTaskType1, WmTaskType.class);
					newOne.setCreateTime(null);
					newOne.setId(null);
					newOne.setLevel1Id(wmDaAgent.getId());
					newOne.setDurgentId(wmDaDrugEnt.getId());
					newOne.setTaskTypeLevel("2");
					newOne.setRuleId(wmDaDrugEnt.getId() + wmDaAgent.getId());
					newOne.setTaskEntId(wmDaAgent.getId());
					newOne.setDeptId(wmDaAgent.getDeptId());
					newOne.setStatus("0");
					newOne.setBaseId(wmTaskType1.getId());
					List<WmTaskType> taskTypeList = wmTaskTypeMapper.selectList(Wrappers.<WmTaskType>lambdaQuery()
							.eq(WmTaskType::getTaskTypeName, taskTypeName)
							.eq(WmTaskType::getRuleId, newOne.getRuleId())
							.eq(WmTaskType::getDeptId, newOne.getDeptId())
							.eq(WmTaskType::getTaskEntId, newOne.getTaskEntId()));
					if (CollectionUtil.isEmpty(taskTypeList)) {
						newOne.setCreateTime(LocalDateTime.now());
						wmTaskTypeMapper.insert(newOne);
					}
				}
				// 生成一级CSO的任务类型
				for (WmTaskType wmTaskType2 : tempList) {
					WmTaskType newOne = BeanUtil.copyProperties(wmTaskType2, WmTaskType.class);
					newOne.setCreateTime(null);
					newOne.setId(null);
					newOne.setLevel1Id(wmDaAgent.getId());
					newOne.setTaskTypeLevel("2");
					newOne.setTaskEntId(wmDaAgent.getId());
					newOne.setRuleId(wmDaAgent.getId());
					newOne.setDeptId(wmDaAgent.getDeptId());
					newOne.setStatus("0");
					newOne.setBaseId(wmTaskType2.getId());
					List<WmTaskType> taskTypeList = wmTaskTypeMapper.selectList(Wrappers.<WmTaskType>lambdaQuery()
							.eq(WmTaskType::getTaskTypeName, taskTypeName)
							.eq(WmTaskType::getRuleId, newOne.getRuleId())
							.eq(WmTaskType::getDeptId, newOne.getDeptId())
							.eq(WmTaskType::getTaskEntId, newOne.getTaskEntId()));
					if (CollectionUtil.isEmpty(taskTypeList)) {
						newOne.setCreateTime(LocalDateTime.now());
						wmTaskTypeMapper.insert(newOne);
					}
				}

				// 查询二级CSO
				List<SysDept> twoCSODeptList = sysDeptMapper.selectList(Wrappers.<SysDept>lambdaQuery()
						.eq(SysDept::getParentId, oneCSODept.getDeptId())
//						.eq(SysDept::getDelFlag, DelEnum.NOT_DEL.val())
						.eq(SysDept::getLevel, 4));
				for (SysDept twoCSODept : twoCSODeptList) {
					// 生成二级CSO任务类型
					WmDaAgent wmDaAgent2 = wmDaAgentMapper.selectOne(Wrappers.<WmDaAgent>lambdaQuery()
							.eq(WmDaAgent::getDelFlag, DelEnum.NOT_DEL.val())
//							.eq(WmDaAgent::getEnableFlag, EnableEnum.ENABLE.val())
							.eq(WmDaAgent::getDeptId, twoCSODept.getDeptId()));
					if (wmDaAgent2 == null) {
						continue;
					}
					for (WmTaskType wmTaskType2 : tempList) {
						WmTaskType newOne = BeanUtil.copyProperties(wmTaskType2, WmTaskType.class);
						newOne.setCreateTime(null);
						newOne.setId(null);
						newOne.setLevel1Id(wmDaAgent2.getId());
						newOne.setTaskTypeLevel("2");
						newOne.setTaskEntId(wmDaAgent2.getId());
						newOne.setRuleId(wmDaAgent2.getId());
						newOne.setDeptId(wmDaAgent2.getDeptId());
						newOne.setStatus("0");
						newOne.setBaseId(wmTaskType2.getId());
						List<WmTaskType> taskTypeList = wmTaskTypeMapper.selectList(Wrappers.<WmTaskType>lambdaQuery()
								.eq(WmTaskType::getTaskTypeName, taskTypeName)
								.eq(WmTaskType::getRuleId, newOne.getRuleId())
								.eq(WmTaskType::getDeptId, newOne.getDeptId())
								.eq(WmTaskType::getTaskEntId, newOne.getTaskEntId()));
						if (CollectionUtil.isEmpty(taskTypeList)) {
							newOne.setCreateTime(LocalDateTime.now());
							wmTaskTypeMapper.insert(newOne);
						}
					}
				}

			}

		}

		// 2.第一级是CSO的情况
//		List<SysDept> deptList2 = sysDeptMapper.selectList(Wrappers.<SysDept>lambdaQuery()
//				.eq(SysDept::getLevel, 3)
//				.isNull(SysDept::getParentId));
		List<SysDept> sysDepts = sysDeptMapper.listAllDept();
		List<SysDept> deptList2 = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(sysDepts)) {
			deptList2 = sysDepts.stream()
					.filter(sysDept -> sysDept.getLevel().equals(3) && sysDept.getParentId() == null)
					.collect(Collectors.toList());
		}
		// 查询一级CSO
		for (SysDept oneCSODept : deptList2) {
			// 生成药企给一级CSO的任务类型
			WmDaAgent wmDaAgent = wmDaAgentMapper.selectOne(Wrappers.<WmDaAgent>lambdaQuery()
					.eq(WmDaAgent::getDelFlag, DelEnum.NOT_DEL.val())
//					.eq(WmDaAgent::getEnableFlag, EnableEnum.ENABLE.val())
					.eq(WmDaAgent::getDeptId, oneCSODept.getDeptId()));
			if (wmDaAgent == null) {
				continue;
			}
			// 生成一级CSO的任务类型
			for (WmTaskType wmTaskType2 : tempList) {
				WmTaskType newOne = BeanUtil.copyProperties(wmTaskType2, WmTaskType.class);
				newOne.setCreateTime(null);
				newOne.setId(null);
				newOne.setLevel1Id(wmDaAgent.getId());
				newOne.setTaskTypeLevel("2");
				newOne.setTaskEntId(wmDaAgent.getId());
				newOne.setRuleId(wmDaAgent.getId());
				newOne.setDeptId(wmDaAgent.getDeptId());
				newOne.setStatus("0");
				newOne.setBaseId(wmTaskType2.getId());
				List<WmTaskType> taskTypeList = wmTaskTypeMapper.selectList(Wrappers.<WmTaskType>lambdaQuery()
						.eq(WmTaskType::getTaskTypeName, taskTypeName)
						.eq(WmTaskType::getRuleId, newOne.getRuleId())
						.eq(WmTaskType::getDeptId, newOne.getDeptId())
						.eq(WmTaskType::getTaskEntId, newOne.getTaskEntId()));
				if (CollectionUtil.isEmpty(taskTypeList)) {
					newOne.setCreateTime(LocalDateTime.now());
					wmTaskTypeMapper.insert(newOne);
				}
			}

			// 查询二级CSO
			List<SysDept> twoCSODeptList = sysDeptMapper.selectList(Wrappers.<SysDept>lambdaQuery()
					.eq(SysDept::getParentId, oneCSODept.getDeptId())
//					.eq(SysDept::getDelFlag, DelEnum.NOT_DEL.val())
					.eq(SysDept::getLevel, 4));
			for (SysDept twoCSODept : twoCSODeptList) {
				// 生成二级CSO任务类型
				WmDaAgent wmDaAgent2 = wmDaAgentMapper.selectOne(Wrappers.<WmDaAgent>lambdaQuery()
						.eq(WmDaAgent::getDelFlag, DelEnum.NOT_DEL.val())
//						.eq(WmDaAgent::getEnableFlag, EnableEnum.ENABLE.val())
						.eq(WmDaAgent::getDeptId, twoCSODept.getDeptId()));
				if (wmDaAgent2 == null) {
					continue;
				}
				for (WmTaskType wmTaskType2 : tempList) {
					WmTaskType newOne = BeanUtil.copyProperties(wmTaskType2, WmTaskType.class);
					newOne.setCreateTime(null);
					newOne.setId(null);
					newOne.setLevel1Id(wmDaAgent2.getId());
					newOne.setTaskTypeLevel("2");
					newOne.setTaskEntId(wmDaAgent2.getId());
					newOne.setRuleId(wmDaAgent2.getId());
					newOne.setDeptId(wmDaAgent2.getDeptId());
					newOne.setStatus("0");
					newOne.setBaseId(wmTaskType2.getId());
					List<WmTaskType> taskTypeList = wmTaskTypeMapper.selectList(Wrappers.<WmTaskType>lambdaQuery()
						.eq(WmTaskType::getTaskTypeName, taskTypeName)
						.eq(WmTaskType::getRuleId, newOne.getRuleId())
						.eq(WmTaskType::getDeptId, newOne.getDeptId())
						.eq(WmTaskType::getTaskEntId, newOne.getTaskEntId()));
					if (CollectionUtil.isEmpty(taskTypeList)) {
						newOne.setCreateTime(LocalDateTime.now());
						wmTaskTypeMapper.insert(newOne);
					}
				}
			}
		}
	}
}
