/*
 *    Copyright (c) 2018-2025, hnqz 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 pig4cloud.com 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: hnqz
 */

package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableMap;
import com.qunzhixinxi.hnqz.admin.api.dto.UserDTO;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.api.entity.SysUser;
import com.qunzhixinxi.hnqz.admin.api.vo.ErrorInfoVo;
import com.qunzhixinxi.hnqz.admin.api.vo.UserVO;
import com.qunzhixinxi.hnqz.admin.api.vo.WmScorePackageVO;
import com.qunzhixinxi.hnqz.admin.entity.*;
import com.qunzhixinxi.hnqz.admin.enums.DelEnum;
import com.qunzhixinxi.hnqz.admin.enums.EnableEnum;
import com.qunzhixinxi.hnqz.admin.enums.WmScoreTaskTypeEnum;
import com.qunzhixinxi.hnqz.admin.mapper.*;
import com.qunzhixinxi.hnqz.admin.service.*;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.common.security.service.HnqzUser;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;


/**
 * 积分包
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:42
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wmscorepackage")
@Api(value = "wmscorepackage", tags = "积分包管理")
public class WmScorePackageController {

	private final WmScorePackageService wmScorePackageService;
	private final WmScorePackageStatusService wmScorePackageStatusService;
	private final WmDaDrugEntDrugtableService wmDaDrugEntDrugtableService;
	private final WmScorePackageMapper wmScorePackageMapper;
	private final SysDeptService sysDeptService;
	private final WmDaAgentService wmDaAgentService;
	private final SysUserService sysUserService;
	private final WmTaskService wmTaskService;
	private final WmTaskMapper wmTaskMapper;
	private final WmTaskRuleService wmTaskRuleService;
	private final WmScoreTaskTypeService wmScoreTaskTypeService;
	private final WmDaDrugEntService wmDaDrugEntService;
	private final WmTaskTypeService wmTaskTypeService;
	private final WmScoreTaskTypeMapper wmScoreTaskTypeMapper;
	private final WmDaDrugEntMapper wmDaDrugEntMapper;
	private final WmDaAgentMapper wmDaAgentMapper;
	private final SysUserMapper sysUserMapper;
	private final WmReportMapper wmReportMapper;


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getWmScorePackagePage(Page page, WmScorePackage wmScorePackage) {

		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		String ruleId = wmScorePackage.getTaskRuleId();
		String pakcageName = wmScorePackage.getScoreName();
		wmScorePackage.setScoreName(null);
		if (SecurityUtils.getRoles().contains(3)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
		} else if (SecurityUtils.getRoles().contains(4)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
			if ("1".equals(wmScorePackage.getAgentTypeId())) {
				wmScorePackage.setDeptId(SecurityUtils.getUser().getDeptId() + "");
			}
		}
		wmScorePackage.setTaskRuleId(null);
		QueryWrapper queryWrapper = Wrappers.query(wmScorePackage);

		if (null != pakcageName) {
			queryWrapper.like("score_name", pakcageName);
		}

		if ("4".equals(wmScorePackage.getTypeid())) {
			if (null != ruleId) {
				WmTaskRule wmTaskRule = new WmTaskRule();

				if (null != wmScorePackage.getDrugEntId()) {
					wmTaskRule.setDrugentId(wmScorePackage.getDrugEntId());
					wmTaskRule.setRuleMonth(ruleId);
				} else {
					wmTaskRule.setRuleMonth(ruleId);
				}

				List<WmTaskRule> wmTaskRuleList = wmTaskRuleService.list(Wrappers.query(wmTaskRule));
				List<String> stringList = new ArrayList<>();
				if (wmTaskRuleList.size() > 0) {
					for (WmTaskRule wmt : wmTaskRuleList) {
						stringList.add(wmt.getId());
					}
				} else {
					stringList.add("-1");
				}

				queryWrapper.in(true, "task_rule_id", stringList);
			}
		}


		return R.ok(wmScorePackageService.page(page, queryWrapper));
	}


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getWmScorePackagePageTask")
	public R getWmScorePackagePageTask(Page page, WmScorePackage wmScorePackage) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
		SysDept sysDept = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
		QueryWrapper queryWrapper = Wrappers.query();
		queryWrapper.eq("del_flag", "0");
		queryWrapper.eq("enable_flag", "0");
		if (!SecurityUtils.getRoles().contains(2)) {
			queryWrapper.eq("drug_ent_id", SecurityUtils.getUser().getDrugEntId());
		}

		if (null != wmScorePackage.getPackageFinishStatus()) {
			if ("1".equals(wmScorePackage.getPackageFinishStatus())) {
				if (SecurityUtils.getRoles().contains(3)) {
					queryWrapper.eq("package_audit_status", "1");
				}

				if (SecurityUtils.getRoles().contains(4) || SecurityUtils.getRoles().contains(5)) {
					queryWrapper.eq("package_finish_status", "1");
				}
			} else {
				if (SecurityUtils.getRoles().contains(3)) {
					queryWrapper.eq("package_audit_status", "0");
				}

				if (SecurityUtils.getRoles().contains(4) || SecurityUtils.getRoles().contains(5)) {
					queryWrapper.eq("package_finish_status", "0");
				}
			}
		}

		if (null != wmScorePackage.getScoreId()) {
			queryWrapper.like("score_id", wmScorePackage.getScoreId());
		}

		if (null != wmScorePackage.getTaskRuleId()) {
			queryWrapper.eq("task_rule_id", wmScorePackage.getTaskRuleId());
		}

		if (null != wmScorePackage.getScoreName()) {
			queryWrapper.like("score_name", wmScorePackage.getScoreName());
		}
//		if(null!=wmScorePackage.getAgentTypeId())
//		{
//			queryWrapper.eq("agent_type_id",wmScorePackage.getAgentTypeId());
//		}
		if (null != wmScorePackage.getLevel1Id()) {
			queryWrapper.eq("level1_id", wmScorePackage.getLevel1Id());
		}
		List<String> typeList = new ArrayList<>();
		List<WmScorePackage> listExt = new ArrayList<>();
		WmDaDrugEnt wmDaDrugEnt = new WmDaDrugEnt();
		wmDaDrugEnt.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
		WmDaDrugEnt wmDaDrugEnt1 = wmDaDrugEntMapper.selectDeptId(wmDaDrugEnt);
		WmDaAgent wmDaAgent = new WmDaAgent();
		wmDaAgent.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
		WmDaAgent wmDaAgent1 = wmDaAgentMapper.selectDeptId(wmDaAgent);
		if (3 == sysDept.getLevel()) {
			if (null != wmScorePackage.getTypeid() && "4".equals(wmScorePackage.getTypeid())) {
				queryWrapper.eq("level1_id", wmDaDrugEnt1.getId());
				wmScorePackage.setLevel1Id(sysDept.getLevel2Id() + "");
				queryWrapper.eq("typeid", 4);
				wmScorePackage.setPackageFinishStatus("1");
				queryWrapper.eq("package_finish_status", 1);
			} else {
				queryWrapper.eq("agent_type_id", 1);
				queryWrapper.eq("level1_id", sysDept.getLevel2Id());
				wmScorePackage.setAgentTypeId("1");
				wmScorePackage.setLevel1Id(sysDept.getLevel2Id() + "");
				listExt = wmScorePackageService.listWmScorePackageAndTaskExtOld(wmScorePackage);
			}


		} else if (4 == sysDept.getLevel()) {
			queryWrapper.eq("agent_type_id", 2);
			queryWrapper.eq("level2_id", wmDaAgent1.getId());
			queryWrapper.ne("typeid", 2);
			wmScorePackage.setAgentTypeId("2");
			wmScorePackage.setTypeid("2");
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt2Old(wmScorePackage);

		} else if (2 == sysDept.getLevel()) {
			queryWrapper.eq("agent_type_id", 1);
			queryWrapper.eq("typeid", 1);
			wmScorePackage.setAgentTypeId("1");
			wmScorePackage.setTypeid("1");
			listExt = wmScorePackageService.listWmScorePackageAndTaskExtOld(wmScorePackage);

		}

		if (SecurityUtils.getRoles().contains(2)) {
			queryWrapper.eq("agent_type_id", 1);
			queryWrapper.eq("typeid", 1);
			wmScorePackage.setAgentTypeId("1");
			wmScorePackage.setTypeid("1");
			listExt = wmScorePackageService.listWmScorePackageAndTaskExtOld(wmScorePackage);
		}

		queryWrapper.eq("old_package", 1);
		Page pageAnswer = wmScorePackageService.page(page, queryWrapper);
		List<WmScorePackage> list = pageAnswer.getRecords();


		for (WmScorePackage wmS :
				list) {
			for (WmScorePackage ext :
					listExt) {
				if (wmS.getId().equals(ext.getId())) {
					wmS.setYsh(ext.getYsh());
					wmS.setYtj(ext.getYtj());
					if (2 == sysDept.getLevel()) {
						wmS.setDsh(ext.getDsh());
					} else {
						int pros = 0;
						if (null == ext.getYtj() || null == ext.getScore()) {
							wmS.setDsh(0);
						} else {
							if (ext.getYtj() >= ext.getScore()) {
								pros = 100;
								wmS.setDsh(100);
							} else {
								wmS.setDsh(new Double(Math.ceil(100 * ext.getYtj() / ext.getScore())).intValue());
							}
						}
					}
					break;
				}
			}
			if (SecurityUtils.getRoles().contains(3)) {
				wmS.setPackageFinishStatus("0");
				if ("1".equals(wmS.getPackageAuditStatus())) {
					wmS.setPackageFinishStatus("1");
				}
			}

		}
		pageAnswer.setRecords(list);
		return R.ok(pageAnswer);
	}


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getWmScorePageTask")
	public R getWmScorePageTask(Page page, WmScorePackage wmScorePackage) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
		QueryWrapper queryWrapper = Wrappers.query();
		if (null != wmScorePackage.getId()) {
			queryWrapper.like("id", wmScorePackage.getId());
		}
		if (null != wmScorePackage.getSendPackageDeptId()) {
			queryWrapper.like("send_package_dept_id", wmScorePackage.getSendPackageDeptId());
		}
		if (null != wmScorePackage.getPackageStatus()) {
			queryWrapper.like("package_status", wmScorePackage.getPackageStatus());
		}
		SysDept sysDept = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
		queryWrapper.eq("del_flag", "0");
		queryWrapper.eq("enable_flag", "0");

		if (null != wmScorePackage.getPackageFinishStatus()) {
			if ("1".equals(wmScorePackage.getPackageFinishStatus())) {
				if (SecurityUtils.getRoles().contains(3)) {
					queryWrapper.eq("package_audit_status", "1");
				}

				if (SecurityUtils.getRoles().contains(4) || SecurityUtils.getRoles().contains(5)) {
					queryWrapper.eq("package_finish_status", "1");
				}
			} else {
				if (SecurityUtils.getRoles().contains(3)) {
					queryWrapper.eq("package_audit_status", "0");
				}

				if (SecurityUtils.getRoles().contains(4) || SecurityUtils.getRoles().contains(5)) {
					queryWrapper.eq("package_finish_status", "0");
				}
			}
		}

		List<WmScorePackage> listExt = new ArrayList<>();
		if (3 == sysDept.getLevel()) {
			queryWrapper.eq("dept_id", SecurityUtils.getUser().getDeptId());
			queryWrapper.eq("typeid", 1);
			wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
			wmScorePackage.setTypeid(String.valueOf(1));
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt(wmScorePackage);


		} else if (4 == sysDept.getLevel()) {
			queryWrapper.eq("dept_id", SecurityUtils.getUser().getDeptId());
			wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
			wmScorePackage.setTypeid(String.valueOf(2));
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt2(wmScorePackage);

		} else if (2 == sysDept.getLevel()) {
			queryWrapper.eq("typeid", 0);
			queryWrapper.eq("dept_id", SecurityUtils.getUser().getDeptId());
			wmScorePackage.setTypeid(String.valueOf(0));
			wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
			wmScorePackage.setSendPackageDeptId(wmScorePackage.getSendPackageDeptId());
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt(wmScorePackage);

		}

		if (SecurityUtils.getRoles().contains(2)) {
			queryWrapper.eq("dept_id", SecurityUtils.getUser().getDeptId());
			wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
			wmScorePackage.setSendPackageDeptId(wmScorePackage.getSendPackageDeptId());
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt(wmScorePackage);
		}

		Page<WmScorePackage> page1 = new Page<>();
		int current = new Long(page.getCurrent()).intValue();
		int size = new Long(page.getSize()).intValue();

		int count = listExt.size();
		List<WmScorePackage> pageList = new ArrayList<>();
		//计算当前页第一条数据的下标
		int currId = current > 1 ? (current - 1) * size : 0;
		for (int i = 0; i < size && i < count - currId; i++) {
			pageList.add(listExt.get(currId + i));
		}
		page1.setSize(size);
		page1.setCurrent(current);
		page1.setTotal(count);
		//计算分页总页数
		page1.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
		page1.setRecords(pageList);

		for (WmScorePackage ext : listExt) {
			List<WmScoreTaskType> taskTypelist = wmScoreTaskTypeMapper.getScoreTaskTypeByScoreId(ext.getId());
			if (taskTypelist != null && taskTypelist.size() > 0) {
				String taskTypes = taskTypelist.stream()
						.map(WmScoreTaskType::getTaskTypeName)
						.collect(Collectors.joining(","));
				ext.setTaskTypes(taskTypes);
			}
			if (!StringUtil.isEmpty(ext.getRelationScoreId())) {
				WmScorePackage wm1 = wmScorePackageService.getById(ext.getRelationScoreId());
				ext.setRelationScoreId(wm1.getScorePackageName());
			}
			WmScorePackage wmScorePackage2 = wmScorePackageService.getById(ext.getId());
			wmScorePackage2.setRelationScoreId(wmScorePackage2.getId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.selectLists(wmScorePackage2);
//			List<WmTask> list1 = new ArrayList<>();
			Integer score = 0;
			Integer total1 = 0;
			Integer ytjtt = 0;
			SysDept sysDept1 = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
			if (wmScorePackageList.size() > 0) {
				List<String> ids2 = wmScorePackageList.stream().map(p -> p.getId()).collect(Collectors.toList());
				for (String id : ids2) {
					WmScorePackage wmScorePackage1 = new WmScorePackage();
					wmScorePackage1.setRelationScoreId(id);
					List<WmScorePackage> wmScorePackageList1 = wmScorePackageService.selectLists(wmScorePackage1);
					WmTask wmTask1 = new WmTask();
					wmTask1.setScorePackageId(id);
					wmTask1.setTaskStatus(String.valueOf(3));
					List<WmTask> wmTaskList = wmTaskMapper.selectListByScorePackageId(wmTask1);
					if (sysDept1.getLevel() == 2) {
						List<WmTask> score1 = wmTaskList.stream()
								.filter(task -> task.getReportDrugApprovalStatus().equals("6"))
								.collect(Collectors.toList());
						for (WmTask item : score1) {
							if ("1".equals(item.getReportDrugApprovalOpinion())) {
								total1 += item.getScore();

							}
							ytjtt += item.getScore();
						}


					}
					if (sysDept1.getLevel() == 3) {
						List<WmTask> score1 = wmTaskList.stream()
								.filter(task -> task.getReportOneApprovalStatus().equals("6"))
								.collect(Collectors.toList());
						for (WmTask item : score1) {
							if ("1".equals(item.getReportOneApprovalOpinion())) {
								total1 += item.getScore();

							}
							ytjtt += item.getScore();
						}
					}
					if (sysDept1.getLevel() == 4) {
						List<WmTask> score1 = wmTaskList.stream()
								.filter(task -> task.getReportSecondApprovalStatus().equals("6"))
								.collect(Collectors.toList());
						for (WmTask item : score1) {
							if ("1".equals(item.getReportSecondApprovalOpinion())) {
								total1 += item.getScore();

							}
							ytjtt += item.getScore();
						}
					}
					List<Integer> scoreList1 = wmTaskList.stream().map(p -> p.getScore()).collect(Collectors.toList());
					Integer score2 = 0;
					for (Integer score1 : scoreList1) {
						score2 += score1;
					}
					WmScorePackage wmScorePackage3 = wmScorePackageService.getById(id);
//						if (score2>wmScorePackage3.getScore()){
//							score += wmScorePackage3.getScore();
//						}else{
					score += score2;
//						}
//					List<String> ids3 = wmTaskList.stream().map(p -> p.getId()).collect(Collectors.toList());
					List<String> ids4 = wmScorePackageList1.stream().map(p -> p.getId()).collect(Collectors.toList());
					ids4.add(ext.getId());
					if (ids4.size() > 0) {
						for (String id1 : ids4) {
							WmTask wmTask2 = new WmTask();
							wmTask2.setScorePackageId(id1);
							wmTask2.setTaskStatus(String.valueOf(3));
							List<WmTask> wmTaskList1 = wmTaskMapper.selectListByScorePackageId(wmTask2);
							if (sysDept1.getLevel() == 2) {
								List<WmTask> score1 = wmTaskList1.stream()
										.filter(task -> task.getReportDrugApprovalStatus().equals("6"))
										.collect(Collectors.toList());
								for (WmTask item : score1) {
									if ("1".equals(item.getReportDrugApprovalOpinion())) {
										total1 += item.getScore();

									}
									ytjtt += item.getScore();
								}

							}
							if (sysDept1.getLevel() == 3) {
								List<WmTask> score1 = wmTaskList1.stream()
										.filter(task -> task.getReportOneApprovalStatus().equals("6"))
										.collect(Collectors.toList());
								for (WmTask item : score1) {
									if ("1".equals(item.getReportOneApprovalOpinion())) {
										total1 += item.getScore();

									}
									ytjtt += item.getScore();
								}
							}
							if (sysDept1.getLevel() == 4) {
								List<WmTask> score1 = wmTaskList1.stream()
										.filter(task -> task.getReportSecondApprovalStatus().equals("6"))
										.collect(Collectors.toList());
								for (WmTask item : score1) {
									if ("1".equals(item.getReportSecondApprovalOpinion())) {
										total1 += item.getScore();

									}
									ytjtt += item.getScore();
								}
							}
							List<Integer> scoreList2 = wmTaskList1.stream()
									.map(p -> p.getScore())
									.collect(Collectors.toList());
							Integer score3 = 0;
							if (scoreList2.size() != 0) {
								for (Integer score1 : scoreList2) {
									score3 += score1;
								}
								WmScorePackage wmScorePackage4 = wmScorePackageService.getById(id1);
//								if (score3>wmScorePackage4.getScore()){
//									score += wmScorePackage4.getScore();
//								}else{
								score += score3;
//								}
							}
//							List<String> ids5 = wmTaskList1.stream().map(p -> p.getId()).collect(Collectors.toList());
//							for (String id2 : ids5) {
//								WmTask wmTaskList2 = wmTaskMapper.selectById(id2);
//								list1.add(wmTaskList2);
//							}
						}
					}
//					for (String id1 : ids3) {
//						WmTask wmTaskList1 = wmTaskMapper.selectById(id1);
//						list1.add(wmTaskList1);
//					}
				}
			} else {
				WmTask wmTask1 = new WmTask();
				wmTask1.setScorePackageId(ext.getId());
				wmTask1.setTaskStatus(String.valueOf(3));
				List<WmTask> wmTaskList1 = wmTaskMapper.selectListByScorePackageId(wmTask1);
				if (sysDept1.getLevel() == 2) {
					List<WmTask> score1 = wmTaskList1.stream()
							.filter(task -> task.getReportDrugApprovalStatus().equals("6"))
							.collect(Collectors.toList());
					for (WmTask item : score1) {
						if ("1".equals(item.getReportDrugApprovalOpinion())) {
							total1 += item.getScore();

						}
						ytjtt += item.getScore();
					}
				}
				if (sysDept1.getLevel() == 3) {
					List<WmTask> score1 = wmTaskList1.stream()
							.filter(task -> task.getReportOneApprovalStatus().equals("6"))
							.collect(Collectors.toList());
					for (WmTask item : score1) {
						if ("1".equals(item.getReportOneApprovalOpinion())) {
							total1 += item.getScore();

						}
						ytjtt += item.getScore();
					}
				}
				if (sysDept1.getLevel() == 4) {
					List<WmTask> score1 = wmTaskList1.stream()
							.filter(task -> task.getReportSecondApprovalStatus().equals("6"))
							.collect(Collectors.toList());
					for (WmTask item : score1) {
						if ("1".equals(item.getReportSecondApprovalOpinion())) {
							total1 += item.getScore();

						}
						ytjtt += item.getScore();
					}
				}
				List<Integer> scoreList1 = wmTaskList1.stream().map(p -> p.getScore()).collect(Collectors.toList());
				Integer score4 = 0;
				for (Integer score1 : scoreList1) {
					score4 += score1;
				}
				score = score4;
			}
			ext.setYsh(score);
			ext.setYtjtt(ytjtt);

			ext.setPayScore(total1);
			BigDecimal fin = new BigDecimal(ext.getYsh()).multiply(BigDecimal.valueOf(100))
					.divide(new BigDecimal(ext.getScore()), 1, BigDecimal.ROUND_HALF_UP);
			BigDecimal fin1 = BigDecimal.valueOf(100.00);
			if (fin.compareTo(fin1) == 1) {
				fin = BigDecimal.valueOf(100.00);
			}
			ext.setFinishRate(fin + "0");
			BigDecimal payRate = new BigDecimal(ext.getPayScore()).multiply(BigDecimal.valueOf(100))
					.divide(new BigDecimal(ext.getScore()), 1, BigDecimal.ROUND_HALF_UP);
			if (payRate.compareTo(fin1) == 1) {
				payRate = BigDecimal.valueOf(100.00);
			}
			ext.setPayRate(payRate + "0");
			if (ext.getPayRate().equals("100.00")) {
				ext.setPackageFinishStatus("1");
			}
		}
		return R.ok(page1);
	}


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getWmScorePackageTask")
	public R getWmScorePackageTask(Page page, WmScorePackage wmScorePackage) {

		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

		if (!SecurityUtils.getRoles().contains(2) && !SecurityUtils.getRoles().contains(1)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
		}
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		if (SecurityUtils.getRoles().contains(4)) {
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
			SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
			wmScorePackage.setLevel1Id(sysDept.getLevel2Id() + "");
		}

		List<WmScorePackage> listExt = new ArrayList<>();
		SysDept sysDept1 = new SysDept();
		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
		SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
		if (sysDept.getLevel() == 3) {
			wmScorePackage.setAgentTypeId("1");
			wmScorePackage.setLevel1Id(sysDept.getLevel2Id() + "");

			listExt = wmScorePackageService.listWmScorePackageAndTaskExt(wmScorePackage);
		} else if (sysDept.getLevel() == 4) {
			wmScorePackage.setAgentTypeId("2");
			wmScorePackage.setLevel2Id(sysDept.getLevel3Id() + "");
			wmScorePackage.setTypeid("2");
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt2(wmScorePackage);
		} else if (sysDept.getLevel() == 2) {
			wmScorePackage.setAgentTypeId("1");
			wmScorePackage.setTypeid("1");
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt(wmScorePackage);
		}

		if (SecurityUtils.getRoles().contains(2) || SecurityUtils.getRoles().contains(1)) {
			wmScorePackage.setAgentTypeId("1");
			wmScorePackage.setTypeid("1");
			listExt = wmScorePackageService.listWmScorePackageAndTaskExt(wmScorePackage);
		}


		int ytj = 0;
		int ywc = 0;
		int dqf = 0;
		for (WmScorePackage wmS :
				listExt) {
			ywc += wmS.getScore();

			if (null != wmS.getYsh()) {
				//TODO
				//dqf+=wmS.getYsh();
				if (wmS.getYtj() > wmS.getScore()) {
					dqf += wmS.getScore();
				} else {
					dqf += wmS.getYtj();
				}

			}

			if (null != wmS.getYtj()) {
				if (wmS.getYtj() > wmS.getScore()) {
					ytj += wmS.getScore();
				} else {
					ytj += wmS.getYtj();
				}
			}

		}

		Map<String, Object> map = new HashMap<>();
		map.put("ytj", ytj);
		map.put("ywc", ywc);
		map.put("dqf", dqf);

		return R.ok(map);
	}


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getListRule")
	public R getListRule(Page page, WmScorePackage wmScorePackage) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

		wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		if (SecurityUtils.getRoles().contains(3)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
			wmScorePackage.setAgentTypeId("1");
		} else if (SecurityUtils.getRoles().contains(4)) {
			wmScorePackage.setAgentTypeId("2");
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
			SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
			if (null != sysDept) {
				if (3 == sysDept.getLevel()) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(sysDept.getLevel2Id());

					wmScorePackage.setLevel1Id(wmDaAgent.getId());

					List<Integer> list = new ArrayList<>();
					list.add(2);
					list.add(3);
					list.add(4);
					wmScorePackage.setSendPackageDeptId(SecurityUtils.getUser().getDeptId() + "");
					Page pageAnswer = wmScorePackageService.page(page, Wrappers.query(wmScorePackage)
							.lambda()
							.in(WmScorePackage::getTypeid, list));
					List<WmScorePackage> listExt = wmScorePackageService.listWmScorePackageAndTaskLevel3(wmScorePackage);
					List<WmScorePackage> listWmScorePackage = pageAnswer.getRecords();

					for (WmScorePackage wmS :
							listWmScorePackage) {
						for (WmScorePackage ext :
								listExt) {
							if (wmS.getId().equals(ext.getId())) {
								wmS.setYsh(ext.getYsh());
								wmS.setYtj(ext.getYtj());
								wmS.setDsh(ext.getDsh());

								if (2 == sysDept.getLevel()) {
									wmS.setDsh(ext.getDsh());
								} else {
									int pros = 0;
									if (null == ext.getYtj() || null == ext.getScore()) {
										wmS.setYwctt(0);
									} else {
										if (ext.getYtj() >= ext.getScore()) {
											pros = 100;
											wmS.setYwctt(100);
										} else {
											wmS.setYwctt(new Double(Math.ceil(100 * ext.getYtj() / ext.getScore())).intValue());
										}
									}
								}
								break;
							}
						}
					}
					pageAnswer.setRecords(listWmScorePackage);
					return R.ok(pageAnswer);
				}

				if (4 == sysDept.getLevel()) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(sysDept.getLevel3Id());
					wmScorePackage.setLevel2Id(wmDaAgent.getId());
					List<Integer> list = new ArrayList<>();
					list.add(3);
					list.add(4);

					wmScorePackage.setSendPackageDeptId(SecurityUtils.getUser().getDeptId() + "");
					Page pageAnswer = wmScorePackageService.page(page, Wrappers.query(wmScorePackage)
							.lambda()
							.in(WmScorePackage::getTypeid, list));
					List<WmScorePackage> listExt = wmScorePackageService.listWmScorePackageAndTaskLevel1(wmScorePackage);
					List<WmScorePackage> listWmScorePackage = pageAnswer.getRecords();

					for (WmScorePackage wmS :
							listWmScorePackage) {
						for (WmScorePackage ext :
								listExt) {
							if (wmS.getId().equals(ext.getId())) {
								wmS.setYsh(ext.getYsh());
								wmS.setYtj(ext.getYtj());
								if (2 == sysDept.getLevel()) {
									wmS.setYwctt(ext.getDsh());
								} else {
									int pros = 0;
									if (null == ext.getYtj() || null == ext.getScore()) {
										wmS.setYwctt(0);
									} else {
										if (ext.getYtj() > ext.getScore()) {
											pros = 100;
										} else {
											wmS.setYwctt(new Double(Math.ceil(100 * ext.getYtj() / ext.getScore())).intValue());
										}
									}
								}
								break;
							}
						}
					}
					pageAnswer.setRecords(listWmScorePackage);
					return R.ok(pageAnswer);
				}
			}
			sysDept.setDeptId(sysDept.getDeptId());
		}


		return R.ok(wmScorePackageService.page(page, Wrappers.query(wmScorePackage)));
	}

	/**
	 * 分页查询
	 *
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@GetMapping("/getTotalData")
	public R getTotalData(WmScorePackage wmScorePackage) {
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		if (SecurityUtils.getRoles().contains(3)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
		} else if (SecurityUtils.getRoles().contains(4)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
			SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
			if (null != sysDept) {
				if (3 == sysDept.getLevel()) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(sysDept.getLevel2Id());

					wmScorePackage.setLevel1Id(wmDaAgent.getId());
					List<WmScorePackage> list = wmScorePackageService.list(Wrappers.query(wmScorePackage).lambda());
					List<WmScorePackage> newList = new ArrayList<>();
					for (WmScorePackage wmS : list) {
						if ("2".equals(wmS.getTypeid()) || "3".equals(wmS.getTypeid()) || "4".equals(wmS.getTypeid())) {
							if (SecurityUtils.getUser().getDeptId().toString().equals(wmS.getDeptId())) {
								newList.add(wmS);
							}
						} else {
							newList.add(wmS);
						}


					}
					return R.ok(newList);
				}

				if (4 == sysDept.getLevel()) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(sysDept.getLevel3Id());

					wmScorePackage.setLevel2Id(wmDaAgent.getId());
					List<Integer> list = new ArrayList<>();
					list.add(2);
					list.add(3);
					list.add(4);

					return R.ok(wmScorePackageService.list(Wrappers.query(wmScorePackage)
							.lambda()
							.in(WmScorePackage::getTypeid, list)));
				}
			}
		}


		return R.ok(wmScorePackageService.pageList(Wrappers.query(wmScorePackage)));
	}


	/**
	 * 通过id查询积分包
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Integer id) {
		return R.ok(wmScorePackageService.getById(id));
	}

	/**
	 * 新增积分包
	 *
	 * @param wmScorePackage 积分包
	 * @return R
	 */
	@ApiOperation(value = "新增积分包", notes = "新增积分包")
	@SysLog("新增积分包")
	@PostMapping
	public R save(@RequestBody WmScorePackage wmScorePackage) {

		if (wmScorePackage.getScore() <= 0) {
			return R.failed("积分包分值必须大于0");
		}


		if (null != wmScorePackage.getScoreId() && !"".equals(wmScorePackage.getScoreId())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScoreId(wmScorePackage.getScoreId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(wmScorePackage1));
			if (wmScorePackageList.size() > 0) {
				return R.failed("积分包ID已存在");
			}
		}


		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		wmScorePackage.setIsConduct("0");
		wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
		if ("1".equals(wmScorePackage.getTypeid())) {
			if (null != wmScorePackage.getLevel1Id()) {
				WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getLevel1Id());
				wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");
			}

		} else if ("2".equals(wmScorePackage.getTypeid())) {
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
			SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
			if (sysDept.getLevel() == 3) {
				if (null != wmScorePackage.getLevel2Id()) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getLevel2Id());
					wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");

					WmDaAgent wmDaAgent1 = wmDaAgentService.getById(sysDept.getLevel2Id());
					wmScorePackage.setLevel1Id(wmDaAgent1.getId());
				}
			}


		} else if ("3".equals(wmScorePackage.getTypeid())) {
			if (null != wmScorePackage.getXxdbId()) {
				SysUser sysUser = sysUserService.getById(wmScorePackage.getXxdbId());
				if (null != sysUser.getDeptId()) {
					wmScorePackage.setDeptId(sysUser.getDeptId() + "");
					SysDept sysDept1 = new SysDept();
					sysDept1.setDeptId(sysUser.getDeptId());
					SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
					wmScorePackage.setLevel1Id(sysDept.getLevel2Id() == null || sysDept.getLevel2Id() == 0 ? null : sysDept
							.getLevel2Id() + "");
					wmScorePackage.setLevel2Id(sysDept.getLevel3Id() == null || sysDept.getLevel3Id() == 0 ? null : sysDept
							.getLevel3Id() + "");
					wmScorePackage.setPackageStatus("2");
				}

			}

		} else if ("4".equals(wmScorePackage.getTypeid())) {
			wmScorePackage.setDeptId(SecurityUtils.getUser().getDeptId() + "");
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
			SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
			wmScorePackage.setLevel1Id(sysDept.getLevel2Id() == null || sysDept.getLevel2Id() == 0 ? null : sysDept.getLevel2Id() + "");
			wmScorePackage.setLevel2Id(sysDept.getLevel3Id() == null || sysDept.getLevel3Id() == 0 ? null : sysDept.getLevel3Id() + "");
		}

		if (null != wmScorePackage.getScoreName() && !"".equals(wmScorePackage.getScoreName())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScoreName(wmScorePackage.getScoreName());
			wmScorePackage1.setDeptId(wmScorePackage.getDeptId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(wmScorePackage1));
			if (wmScorePackageList.size() > 0) {
				return R.failed("积分包名称已存在");
			}
		}

		wmScorePackage.setCreateTime(LocalDateTime.now());
		wmScorePackage.setPackageFinishStatus("0");
		wmScorePackage.setPackageAuditStatus("0");
		wmScorePackage.setSendPackageDeptId(SecurityUtils.getUser().getDeptId() + "");
		boolean isAnswer = wmScorePackageService.save(wmScorePackage);
		if (isAnswer) {
			if (SecurityUtils.getRoles().contains(4)) {
				WmScorePackage queryWm = new WmScorePackage();
				SysDept sysDept1 = new SysDept();
				sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
				SysDept sysDept = sysDeptService.selectDeptId(sysDept1);
				if (sysDept.getLevel() == 3) {
					queryWm.setAgentTypeId("1");
					queryWm.setTypeid("1");
					queryWm.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
					queryWm.setLevel1Id(sysDept.getLevel2Id() + "");
					queryWm.setTaskRuleId(wmScorePackage.getTaskRuleId());
					queryWm.setDeptId(SecurityUtils.getUser().getDeptId() + "");
				} else if (sysDept.getLevel() == 4) {
					queryWm.setAgentTypeId("2");
					queryWm.setTypeid("2");
					queryWm.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
					queryWm.setLevel1Id(sysDept.getLevel2Id() + "");
					queryWm.setTaskRuleId(wmScorePackage.getTaskRuleId());
				} else {
					queryWm.setId("-1");
				}

				List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(queryWm));
				if (wmScorePackageList.size() > 0) {
					for (WmScorePackage wms : wmScorePackageList) {
						wms.setIsConduct("1");
					}
				}

				wmScorePackageService.updateBatchById(wmScorePackageList);


			}
		}
		return R.ok();
	}

	/**
	 * 修改积分包
	 *
	 * @param wmScorePackage 积分包
	 * @return R
	 */
	@ApiOperation(value = "修改积分包", notes = "修改积分包")
	@SysLog("修改积分包")
	@PutMapping
	public R updateById(@RequestBody WmScorePackage wmScorePackage) {
		if (wmScorePackage.getScore() <= 0) {
			return R.failed("积分包分值必须大于0");
		}
		WmScorePackage oldData = wmScorePackageService.getById(wmScorePackage.getId());
		if (StringUtils.isNotEmpty(wmScorePackage.getScorePackageName())
				&& !oldData.getScorePackageName().equals(wmScorePackage.getScorePackageName())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScorePackageName(wmScorePackage.getScorePackageName());
			wmScorePackage1.setDeptId(wmScorePackage.getDeptId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.listWmScorePackages(wmScorePackage1);
			if (wmScorePackageList.size() > 0) {
				return R.failed("积分包名称已存在");
			}

		}

		if (null != wmScorePackage.getScoreId() && !"".equals(wmScorePackage.getScoreId())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScoreId(wmScorePackage.getScoreId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(wmScorePackage1));
			if (wmScorePackageList.size() > 1) {
				return R.failed("积分包ID已存在");
			}
		}

		if ("4".equals(wmScorePackage.getPackageStatus())) {
			WmTask wmTask = new WmTask();
			wmTask.setScorePackageId(wmScorePackage.getId());
			List<WmTask> wmTaskList = wmTaskService.list(Wrappers.query(wmTask));

			for (WmTask wmt : wmTaskList) {
				wmt.setEnableFlag("1");
			}

			wmTaskService.updateBatchById(wmTaskList);
		}
		return R.ok(wmScorePackageService.updateById(wmScorePackage));
	}


	/**
	 * 修改积分包
	 *
	 * @param wmScorePackageVO 积分包
	 * @return R
	 */
	@ApiOperation(value = "修改积分包折扣和实际结算金额", notes = "修改积分包")
	@SysLog("修改积分包折扣和实际结算金额")
	@PostMapping(value = "/updateByIdWithType")
	public R updateByIdWithType(@RequestBody @Valid WmScorePackageVO wmScorePackageVO) {

//		WmScorePackage wmScorePackage=new WmScorePackage();
//		wmScorePackage.setId(wmScorePackageVO.getId());
//		wmScorePackage.setDiscount(wmScorePackageVO.getDiscount());
//		wmScorePackage.setSettleAmount(wmScorePackageVO.getSettleAmount());
//		wmScorePackage.setInvoiceCategory(wmScorePackageVO.getInvoiceCategory());
//		wmScorePackage.setPackageStatus(wmScorePackageVO.getPackageStatus());
//
//		if(null!=wmScorePackageVO.getType()&&wmScorePackageVO.getType()!=""){
//			if("1".equals(wmScorePackageVO.getType())){//折扣
//				BigDecimal settleAmount=new BigDecimal(wmScorePackageVO.getYtj()).multiply(wmScorePackageVO.getDiscount());
//				wmScorePackage.setSettleAmount(settleAmount);
//			}else if("2".equals(wmScorePackageVO.getType())){//实际结算金额
//				BigDecimal discount=wmScorePackageVO.getSettleAmount().divide(new BigDecimal(wmScorePackageVO.getYtj()));
//				wmScorePackage.setDiscount(discount);
//			}
//		}
		return wmScorePackageService.updateByIdWithType(wmScorePackageVO);
//		return R.ok(wmScorePackageService.updateById(wmScorePackage));
	}


	/**
	 * 通过id删除积分包
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除积分包", notes = "通过id删除积分包")
	@SysLog("通过id删除积分包")
	@DeleteMapping("/{id}")
	public R removeById(@PathVariable Integer id) {
		return R.ok(wmScorePackageService.removeById(id));
	}


	/**
	 * 获取药企列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public R listWmScorePackage(WmScorePackage wmScorePackage) {
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");

		List<WmScorePackage> list = wmScorePackageService.list(Wrappers.query(wmScorePackage));


		List<Map<String, String>> answerList = new ArrayList<>();
		if (list.size() > 0) {
			for (WmScorePackage sysUser : list) {
				Map<String, String> map = new HashMap<>();
				map.put("id", sysUser.getId() + "");
				if (null == sysUser.getScoreId() || "".equals(sysUser.getScoreId().trim())) {
					map.put("scoreId", "-");
				} else {
					map.put("scoreId", sysUser.getScoreId());

				}
				if (null == sysUser.getScoreName() || "".equals(sysUser.getScoreName().trim())) {
					map.put("scoreName", "-");
				} else {
					map.put("scoreName", sysUser.getScoreName() + "");
				}
				answerList.add(map);
			}

		}
		return R.ok(answerList);
	}


	/**
	 * 获取药企列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/listWmScorePackageAndTask")
	public R listWmScorePackageAndTask(WmScorePackage wmScorePackage) {
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		if (SecurityUtils.getRoles().contains(3)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
		} else if (SecurityUtils.getRoles().contains(4)) {
			wmScorePackage.setDrugEntId(SecurityUtils.getUser().getDrugEntId());
//			wmScorePackage.setDeptId(SecurityUtils.getUser().getDeptId()+"");
		}
		return R.ok(wmScorePackageService.listWmScorePackageAndTask(wmScorePackage));
	}

	/**
	 * 获取药企列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/packageIsZZ")
	public R packageIsZZ(WmScorePackage wmScorePackage) {
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");

		return R.ok(wmScorePackageService.listWmScorePackageAndTask(wmScorePackage));
	}


	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getPlatTotalData")
	public R getPlatTotalData(Page page, WmScorePackage wmScorePackage, String[] dates) {

		if (null != dates && dates.length > 0) {
			// 将时间戳转为当前时间

			wmScorePackage.setDelFlag(dates[0].substring(0, 10));
			wmScorePackage.setEnableFlag(dates[1].substring(0, 10));

		}


		return R.ok(wmScorePackageService.getPlatTotalData(page, wmScorePackage));
	}


	/**
	 * 获取药企列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/listNoDel")
	public R listWmScorePackageNoDel(WmScorePackage wmScorePackage) {

		List<WmScorePackage> list = wmScorePackageService.list(Wrappers.query(wmScorePackage));


		List<Map<String, String>> answerList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (WmScorePackage scorePackage : list) {
				Map<String, String> map = new HashMap<>();
				map.put("id", scorePackage.getId() + "");
				if (StringUtils.isEmpty(scorePackage.getScoreId())) {
					map.put("scoreId", "-");
				} else {
					map.put("scoreId", scorePackage.getScoreId());

				}
				if (StringUtils.isEmpty(scorePackage.getScorePackageName())) {
					map.put("scoreName", "-");
				} else {
					map.put("scoreName", scorePackage.getScorePackageName());
				}
				answerList.add(map);
			}

		}
		return R.ok(answerList);
	}

	/**
	 * 分页查询
	 *
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@GetMapping("/selectLists")
	public R selectLists(WmScorePackage wmScorePackage) {
		wmScorePackage.getRelationScoreId();
		return R.ok(wmScorePackageService.selectLists(wmScorePackage));
	}

	/**
	 * 查看分配积分信息 二期   业务平台
	 */
	@PostMapping("/selectWmScorePackage")
	public R selectWmScorePackage(@RequestBody WmScorePackage wmScorePackage) {
		try {
			return R.ok(wmScorePackageService.selectWmScorePackage(wmScorePackage));
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}


	// /**
	//  * 分页查询积分管理信息 二期   业务平台
	//  */
	// @GetMapping("/selectWmScorePackageLists")
	// public R selectWmScorePackageList(Page page, WmScorePackage wmScorePackage) {
	//
	//
	// 	//查询积分包相关信息
	// 	wmScorePackage.setOldPackage("0");
	//
	// 	IPage<WmScorePackage> pageAnswer = wmScorePackageService.selectWmScorePackageLists(page, wmScorePackage);
	//
	// 	//根据积分包id查询计算完成积分值 已付积分值
	// 	List<WmScorePackage> wmList = pageAnswer.getRecords();
	// 	for (WmScorePackage wmS : wmList) {
	// 		WmScorePackage scorePackage = wmScorePackageMapper.finishRate(wmS);
	// 		BigDecimal payRate = new BigDecimal(scorePackage.getPayScore()).multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(wmS.getScore()), 2, BigDecimal.ROUND_HALF_UP);
	// 		if (payRate.compareTo(new BigDecimal("100")) >= 0) {
	// 			wmS.setPayRate(100 + ".00");
	// 		} else {
	// 			wmS.setPayRate(payRate + "");
	// 		}
	// 		BigDecimal fin = new BigDecimal(scorePackage.getYsh()).multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(wmS.getScore()), 2, BigDecimal.ROUND_HALF_UP);
	// 		if (fin.compareTo(new BigDecimal("100")) > 0) {
	// 			wmS.setFinishRate(100 + ".00");
	// 			if ("0".equals(wmS.getPackageFinishStatus())) {
	// 				wmS.setPackageFinishStatus("1");
	// 			}
	// 		} else {
	// 			wmS.setFinishRate(fin + "");
	// 		}
	// 		if (wmS.getFinishRate().equals("100.00")) {
	// 			if ("0".equals(wmS.getPackageFinishStatus())) {
	// 				wmS.setPackageFinishStatus("1");
	// 			}
	// 		}
	// 		wmS.setPayScore(scorePackage.getPayScore());
	// 		wmS.setYsh(scorePackage.getYsh());
	// 	}
	// 	pageAnswer.setRecords(wmList);
	// 	return R.ok(pageAnswer);
	// }

	/**
	 * 分页查询积分管理信息 二期   业务平台
	 */
	@GetMapping("/selectWmScorePackageLists")
	public R selectWmScorePackageList(@RequestParam(value = "current") Integer current,
									  @RequestParam(value = "size") Integer size,
									  @RequestParam(value = "scorePackageName", required = false) String scorePackageName,
									  @RequestParam(value = "acceptSug", required = false) String acceptSug,
									  @RequestParam(value = "allocationDate", required = false) LocalDate allocationDate,
									  @RequestParam(value = "endLabelDate", required = false) LocalDate endLabelDate) {

		Page<WmScorePackage> pageAnswer = wmScorePackageService.selectWmScorePackageLists(new Page<WmScorePackage>(current, size),
				scorePackageName, acceptSug, allocationDate, endLabelDate);

		//根据积分包id查询计算完成积分值 已付积分值
		List<WmScorePackage> wmList = pageAnswer.getRecords();

		List<Future<WmScorePackage>> futureList = new ArrayList<>(wmList.size());


		for (WmScorePackage wmS : wmList) {
			Future<WmScorePackage> future = wmScorePackageService.finishRate(wmS);
			futureList.add(future);
		}


		List<WmScorePackage> finalRecords = new ArrayList<>(wmList.size());
		try {
			for (Future<WmScorePackage> future : futureList) {
				WmScorePackage wmScorePackage = future.get();
				finalRecords.add(wmScorePackage);

			}

			pageAnswer.setRecords(finalRecords);
		} catch (ExecutionException | InterruptedException e) {
			log.error("获取分页信息失败");
			return R.failed("获取分页信息失败");
		}

		return R.ok(pageAnswer);
	}


	/**
	 * 获取关联积分包名称 二期  业务平台
	 *
	 * @return
	 */
	@GetMapping("/lists")
	public R listWmScorePackages(WmScorePackage wmScorePackage) {
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		List<WmScorePackage> list = wmScorePackageService.list(Wrappers.query(wmScorePackage));
//		SysDept sysDept1 = new SysDept();
//		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept wmId = sysDeptMapper.selectDeptId(sysDept1);
		SysDept wmId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

		List<Map<String, Object>> answerList = new ArrayList<>();
		if (wmId.getLevel() == 3) {
			for (WmScorePackage wmScorePackage1 : list) {
				Map<String, Object> map1 = new HashMap<>();
				if (SecurityUtils.getUser().getDeptId().equals(Integer.valueOf(wmScorePackage1.getDeptId()))) {
					map1.put("relationScoreId", wmScorePackage1.getId());
					map1.put("scorePackageName", wmScorePackage1.getScorePackageName());
					answerList.add(map1);
				}
			}

		}
		return R.ok(answerList);
	}


	/**
	 * 根据上级药企获取关联积分包 二期  业务平台
	 *
	 * @return
	 */
	@GetMapping("/selectRelationScoreId")
	public R selectRelationScoreId(WmScorePackage wmScorePackage) {
//		SysDept sysDept1 = new SysDept();
//		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept selfId = sysDeptMapper.selectDeptId(sysDept1);

		SysDept selfId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

//		SysDept sysDept2 = new SysDept();
//		sysDept2.setDeptId(Integer.valueOf(wmScorePackage.getRelatedService()));
//		SysDept wmId = sysDeptMapper.selectDeptId(sysDept2);

		SysDept sysDept = sysDeptService.getById(wmScorePackage.getRelatedService());

		List<Map<String, Object>> answerList = new ArrayList<>();

//		if(wmScorePackage.getRelatedService().equals(SecurityUtils.getUser().getDeptId()+"")){
//			Map<String, Object> map2 = new HashMap<>();
//			map2.put("id", "-1");
//			map2.put("relationScoreName", "无");
//			answerList.add(map2);
//			return R.ok(answerList);
//		}

		//获取关联积分包
		if (sysDept.getLevel() == 2 && selfId.getLevel() != 2) {
			wmScorePackage.setSendPackageDeptId(wmScorePackage.getRelatedService());
			wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
			List<WmScorePackage> list = wmScorePackageService.selectByReRelatedService(wmScorePackage);
			if (list.size() > 0) {
				for (WmScorePackage wmScorePackage1 : list) {
					if (wmScorePackage1.getKfpjf().equals(0)) {
						// 过滤掉可分配积分值为0的积分包
						continue;
					}
					Map<String, Object> map1 = new HashMap<>();
					map1.put("relationScoreName", wmScorePackage1.getScorePackageName());
					map1.put("drugProducerList", wmScorePackage1.getDrugProducerList());
					map1.put("id", wmScorePackage1.getId());
					answerList.add(map1);
				}
			} else if (SecurityUtils.getRoles().contains(3)) {
				Map<String, Object> map2 = new HashMap<>();
				map2.put("id", "-1");
				map2.put("relationScoreName", "无");
				answerList.add(map2);
			}
		} else if (sysDept.getLevel() == 3) {
			wmScorePackage.setSendPackageDeptId(wmScorePackage.getRelatedService());
			wmScorePackage.setDeptId(wmScorePackage.getRelatedService());
			List<WmScorePackage> list = wmScorePackageService.selectByReRelatedService(wmScorePackage);
			for (WmScorePackage wmScorePackage1 : list) {
				if (wmScorePackage1.getKfpjf().equals(0)) {
					// 过滤掉可分配积分值为0的积分包
					continue;
				}
				Map<String, Object> map3 = new HashMap<>();
				map3.put("id", wmScorePackage1.getId());
				map3.put("drugProducerList", wmScorePackage1.getDrugProducerList());
				map3.put("relationScoreName", wmScorePackage1.getScorePackageName());
				answerList.add(map3);
			}
			Map<String, Object> map4 = new HashMap<>();
			map4.put("id", "-1");
			map4.put("relationScoreName", "无");
			answerList.add(map4);
		} else if (selfId.getLevel() == 2) {
			Map<String, Object> map5 = new HashMap<>();
			map5.put("id", "-1");
			map5.put("relationScoreName", "无");
			answerList.add(map5);
		}
		return R.ok(answerList);
	}


	/**
	 * 获取可分配积分 二期  业务平台
	 *
	 * @return
	 */
	@GetMapping("/selectKfpjf")
	public R selectKfpjf(WmScorePackage wmScorePackage) {
//		SysDept sysDept1 = new SysDept();
//		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept wmId = sysDeptMapper.selectDeptId(sysDept1);

		SysDept wmId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
		WmDaAgent anId = wmDaAgentService.selectByDeptId(wmId.getDeptId());
		List<Map<String, Object>> answerList = new ArrayList<>();
		Map<String, Object> map2 = new HashMap<>();

		//获取接包对象和可分配积分
		List wmDaDrugEnts = new ArrayList();
		if ("-1".equals(wmScorePackage.getId()) && wmId.getLevel() == 3) {
			map2.put("kfpjf", "");
//			wmDaDrugEnts = wmDaAgentService.selectByRelationId(Integer.valueOf(anId.getId()));
//			wmDaDrugEnts = wmDaAgentService.selectWmDaAgentByDeptIds(anId.getDeptId());
//			List<WmDaAgent> list = wmDaAgentService.selectWmDaAgentByDeptIds(anId.getDeptId());
			List<WmDaAgent> list = wmDaAgentService.listAgentByDeptRelation(Integer.parseInt(anId.getDeptId()));
			for (int i = 0; i < list.size(); i++) {
				if (Integer.parseInt(list.get(i).getEnableFlag()) == 0) {
					wmDaDrugEnts.add(list.get(i));
				}
			}
		} else if ("-1".equals(wmScorePackage.getId()) && wmId.getDeptId()
				.equals(SecurityUtils.getUser().getDeptId())) {
			WmDaDrugEnt wmDaDrugEnt = wmDaDrugEntService.selectByDeptId(wmId.getDeptId());
//			wmDaDrugEnts = wmDaAgentService.selectByDrugId(Integer.valueOf(wmDaDrugEnt.getId()));
//			wmDaDrugEnts = wmDaAgentService.selectWmDaAgentByDeptId(wmDaDrugEnt.getDeptId());
			List<WmDaAgent> list = wmDaAgentService.selectWmDaAgentByDeptId(wmDaDrugEnt.getDeptId());
			for (int i = 0; i < list.size(); i++) {
				if (Integer.parseInt(list.get(i).getEnableFlag()) == 0) {
					wmDaDrugEnts.add(list.get(i));
				}
			}
		} else {
//			wmDaDrugEnts = wmDaAgentService.selectByRelationIds(Integer.valueOf(anId.getId()));
//			wmDaDrugEnts = wmDaAgentService.selectWmDaAgentByRelationIds(wmId.getDeptId());
			List<WmDaAgent> list = wmDaAgentService.selectWmDaAgentByRelationIds(wmId.getDeptId());
			for (int i = 0; i < list.size(); i++) {
				if (Integer.parseInt(list.get(i).getEnableFlag()) == 0) {
					wmDaDrugEnts.add(list.get(i));
				}
			}
		}

		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		List<WmScorePackage> list = wmScorePackageService.list(Wrappers.query(wmScorePackage));
		map2.put("acceptSug", wmDaDrugEnts);
		for (WmScorePackage wmScorePackage1 : list) {
			if (SecurityUtils.getUser().getDeptId().equals(Integer.valueOf(wmScorePackage1.getDeptId()))) {
				WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
				wmScoreTaskType.setScoreId(wmScorePackage1.getId());
				wmScorePackage1.setWmScoreTaskTypeList(wmScoreTaskTypeService.listWmScoreTaskTypes(wmScoreTaskType));
				map2.put("kfpjf", wmScorePackage1.getKfpjf());
			}
		}
		answerList.add(map2);
//获取数据
		if (wmId.getLevel() == 3) {
			for (WmScorePackage wmScorePackage1 : list) {
				Map<String, Object> map1 = new HashMap<>();
				List<String> stringList = new ArrayList<>();
				if (SecurityUtils.getUser().getDeptId().equals(Integer.valueOf(wmScorePackage1.getDeptId()))) {
					WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
					wmScoreTaskType.setScoreId(wmScorePackage1.getId());
					List<WmScoreTaskType> taskTypeList = wmScoreTaskTypeMapper.listWmScoreTaskType(wmScoreTaskType);
					Set taskTypeSet = taskTypeList.stream()
							.map(WmScoreTaskType::getTaskTypeId)
							.collect(Collectors.toSet());
					wmScorePackage.setTaskTypeSet(taskTypeSet);
					map1.put("scoreId", wmScorePackage1.getScoreId());
					map1.put("id", wmScorePackage1.getId());
					map1.put("kfpjf", wmScorePackage1.getKfpjf());
					map1.put("belongDate", wmScorePackage1.getBelongDate());
					map1.put("packsgeType1", wmScorePackage1.getPackageType1());
					map1.put("wmScoreTaskTypeList", wmScorePackage.getTaskTypeSet());
					map1.put("wmScoreTaskTypeList1", wmScorePackage1.getWmScoreTaskTypeList());
					stringList.add(wmScorePackage1.getScoreId());
					answerList.add(map1);
				}
			}

		}
		return R.ok(answerList);
	}

	/**
	 * 新增分配积分包  二期  业务平台
	 *
	 * @param wmScorePackage 积分包
	 * @return R
	 */
	@ApiOperation(value = "新增积分包", notes = "新增积分包")
	@SysLog("新增积分包")
	@PostMapping("saves")
	public R saves(@RequestBody WmScorePackage wmScorePackage) {

		if (wmScorePackage.getScore() <= 0) {
			return R.failed("积分包分值必须大于0");
		}


		wmScorePackage.setTypeid("");
		wmScorePackage.setPackageType1("1");
		wmScorePackage.setDelFlag("0");
		wmScorePackage.setEnableFlag("0");
		wmScorePackage.setPackageIsConduct("0");
		wmScorePackage.setDrugEntId("");//药企ID
		wmScorePackage.setCreateUser(SecurityUtils.getUser().getId());//用户ID
//		SysDept sysDeptId = new SysDept();
//		sysDeptId.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept sysDept = sysDeptMapper.selectDeptId(sysDeptId);
		SysDept sysDept = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

//		SysDept sysDeptRelatedServiceId = new SysDept();
//		sysDeptRelatedServiceId.setDeptId(Integer.valueOf(wmScorePackage.getRelatedService()));
//		SysDept sysDeptRelatedService = sysDeptMapper.selectDeptId(sysDeptRelatedServiceId);

		if (StringUtils.isNotEmpty(wmScorePackage.getRelationScoreId()) && ArrayUtils.isEmpty(wmScorePackage.getDrugProducerList())) {
			WmScorePackage relateSp = wmScorePackageService.getById(wmScorePackage.getRelationScoreId());
			if (relateSp != null) {
				Set<String> producerNames = wmDaDrugEntDrugtableService.listDrugProducerByDeptId(Integer.parseInt(relateSp
						.getDeptId()));
				wmScorePackage.setDrugProducerList(producerNames.toArray(new String[producerNames.size()]));
			}
		}

		SysDept sysDeptRelatedService = sysDeptService.getById(Integer.valueOf(wmScorePackage.getRelatedService()));
		if ("".equals(wmScorePackage.getTypeid())) {
			WmDaDrugEnt wmDaDrugEnt = wmDaDrugEntService.selectByDeptId(sysDept.getDeptId());
			if (wmDaDrugEnt != null) {
				if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug()
						.equals(wmDaDrugEnt.getId())) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());
//					SysDept sysDept2 = new SysDept();
//					sysDept2.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//					SysDept sysDepts = sysDeptMapper.selectDeptId(sysDept2);

					SysDept sysDepts = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));
					if (sysDepts.getLevel() == 3) {
						wmScorePackage.setLevel1Id(wmDaAgent.getId());
					} else if (sysDepts.getLevel() == 4) {
						wmScorePackage.setLevel2Id(wmDaAgent.getId());
					}
					wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");
					wmScorePackage.setKfpjf(wmScorePackage.getScore());
				} else if (wmScorePackage.getAcceptSug().equals(wmDaDrugEnt.getId())) {
					wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
					wmScorePackage.setKfpjf(wmScorePackage.getScore());
				}
			} else {
				if (sysDeptRelatedService.getLevel() == 2) {
					WmDaDrugEnt wmDaDrugEnts = wmDaDrugEntService.selectByDeptId(Integer.valueOf(wmScorePackage.getRelatedService()));
					if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug()
							.equals(wmDaDrugEnts.getId())) {
						WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());
//						SysDept sysDept2 = new SysDept();
//						sysDept2.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//						SysDept sysDepts = sysDeptMapper.selectDeptId(sysDept2);

						SysDept sysDepts = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));

						if (sysDepts.getLevel() == 3) {
							wmScorePackage.setLevel1Id(wmDaAgent.getId());
						} else if (sysDepts.getLevel() == 4) {
							wmScorePackage.setLevel2Id(wmDaAgent.getId());
						}
						wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");
						wmScorePackage.setKfpjf(wmScorePackage.getScore());
					} else if (wmScorePackage.getAcceptSug().equals(wmDaDrugEnt.getId())) {
						wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
						wmScorePackage.setKfpjf(wmScorePackage.getScore());
					}
				} else if (sysDeptRelatedService.getLevel() == 3) {
					WmDaAgent wmDaAgent1 = wmDaAgentService.selectByDeptId(Integer.valueOf(wmScorePackage.getRelatedService()));
					if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug()
							.equals(wmDaAgent1.getId())) {
						WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());

//						SysDept sysDept2 = new SysDept();
//						sysDept2.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//						SysDept sysDepts = sysDeptMapper.selectDeptId(sysDept2);

						SysDept sysDepts = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));

						if (sysDepts.getLevel() == 3) {
							wmScorePackage.setLevel1Id(wmDaAgent.getId());
						} else if (sysDepts.getLevel() == 4) {
							wmScorePackage.setLevel2Id(wmDaAgent.getId());
						}
						wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");
						wmScorePackage.setKfpjf(wmScorePackage.getScore());
					} else if (wmScorePackage.getAcceptSug().equals(wmDaAgent1.getId())) {
						wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
						wmScorePackage.setKfpjf(wmScorePackage.getScore());
					}
				}
			}
		}

		if (null != wmScorePackage.getScorePackageName() && !"".equals(wmScorePackage.getScorePackageName())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScorePackageName(wmScorePackage.getScorePackageName());
			wmScorePackage1.setDeptId(wmScorePackage.getDeptId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(wmScorePackage1));
			if (wmScorePackageList.size() > 0) {
				return R.failed("积分包名称已存在");
			}
		}

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		wmScorePackage.setCreateTime(LocalDateTime.now());
		wmScorePackage.setPackageFinishStatus("0");
		wmScorePackage.setPackageAuditStatus("0");
		wmScorePackage.setSendPackageDeptId(SecurityUtils.getUser().getDeptId() + "");
		wmScorePackage.setAllocationDate(df.format(new Date()));

		//药企规则ID
//		SysDept sysDept2 = new SysDept();
//		sysDept2.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept sysDeptRuleId = sysDeptMapper.selectDeptId(sysDept2);

		SysDept sysDeptRuleId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

		if (sysDeptRuleId.getLevel() == 2) {
			WmDaDrugEnt wmDaDrugEnt = wmDaDrugEntService.selectByDeptId(sysDeptRuleId.getDeptId());
			if (wmScorePackage.getAcceptSug().equals(wmDaDrugEnt.getId())) {
				wmScorePackage.setRuleId(wmScorePackage.getAcceptSug());
				wmScorePackage.setDrugEntId(wmDaDrugEnt.getId());
			} else if (!wmScorePackage.getAcceptSug().equals(wmDaDrugEnt.getId())) {
				WmTaskType wmTaskType = new WmTaskType();
				wmTaskType.setRuleId(wmDaDrugEnt.getId() + wmScorePackage.getAcceptSug());
				List<WmTaskType> wmTaskTypeList = wmTaskTypeService.selectByRuleId(wmTaskType);
				if (wmTaskTypeList.size() > 0) {
					wmScorePackage.setRuleId(wmDaDrugEnt.getId() + wmScorePackage.getAcceptSug());
					wmScorePackage.setDrugEntId(wmDaDrugEnt.getId());
				} else {
					return R.failed("请药企给下级cso分配积分规则");
				}

			}
		} else if (sysDeptRuleId.getLevel() == 3) {
			//一级cso规则ID
			WmDaAgent daAgent = wmDaAgentService.getOne(Wrappers.<WmDaAgent>lambdaQuery()
					.eq(WmDaAgent::getEnableFlag, EnableEnum.ENABLE.val())
					.eq(WmDaAgent::getDelFlag, DelEnum.NOT_DEL.val())
					.eq(WmDaAgent::getDeptId, String.valueOf(sysDeptRuleId.getDeptId())));
			if (StringUtils.isNotBlank(wmScorePackage.getRelationScoreId())) {
				// 关联了积分包的情况
				WmScorePackage relationScorePackage = wmScorePackageService.getById(wmScorePackage.getRelationScoreId());
				if (relationScorePackage != null) {
					wmScorePackage.setRuleId(relationScorePackage.getRuleId());
					wmScorePackage.setDrugEntId(relationScorePackage.getDrugEntId());
				} else {
					return R.failed("cso没有分配积分规则");
				}
			} else {
				// 没有关联积分包的情况
				List<WmTaskType> taskTypeList = wmTaskTypeService.list(Wrappers.<WmTaskType>lambdaQuery()
						.eq(WmTaskType::getRuleId, daAgent.getId()));
				if (CollectionUtil.isNotEmpty(taskTypeList)) {
					wmScorePackage.setRuleId(daAgent.getId());
					wmScorePackage.setDrugEntId("");
				} else {
					return R.failed("cso没有分配积分规则");
				}
			}
		}

		boolean isAnswer = wmScorePackageService.save(wmScorePackage);

		if (null != wmScorePackage.getWmScoreTaskTypeList() && wmScorePackage.getWmScoreTaskTypeList().size() > 0) {
			List<WmScoreTaskType> wmScoreTaskTypess = new ArrayList<>();
			for (WmScoreTaskType wmScoreTaskTypes : wmScorePackage.getWmScoreTaskTypeList()) {
				WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
				SysDept sysDept1 = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
				if ("".equals(wmScorePackage.getRelationScoreId())) {
					wmScoreTaskType.setScoreId(wmScorePackage.getId());
					wmScoreTaskType.setType("1");
					wmScoreTaskType.setTaskTypeId(wmScoreTaskTypes.getTaskTypeId());
					wmScoreTaskTypess.add(wmScoreTaskType);
					wmScoreTaskTypeService.deleteByScoreId(wmScorePackage.getId());
					wmScoreTaskTypeService.batchInsertType(wmScoreTaskTypess);
				} else if (!"".equals(wmScorePackage.getRelationScoreId()) && sysDept1.getLevel() == 3) {
					wmScoreTaskType.setScoreId(wmScorePackage.getId());
					wmScoreTaskType.setType("1");
					wmScoreTaskType.setTaskTypeId(wmScoreTaskTypes.getTaskTypeId());
					wmScoreTaskTypess.add(wmScoreTaskType);
					wmScoreTaskTypeService.deleteByScoreId(wmScoreTaskType.getScoreId());
					wmScoreTaskTypeService.batchInsertType(wmScoreTaskTypess);
				}
			}
		}
		if (isAnswer) {

			if (SecurityUtils.getRoles().contains(4) || SecurityUtils.getRoles().contains(3)) {

				//赋值typeid
//				SysDept sysDept1 = new SysDept();
//				sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//				SysDept sysDeptTypeId = sysDeptMapper.selectDeptId(sysDept1);

				SysDept sysDeptTypeId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

				if (sysDeptTypeId.getLevel() == 2 && wmScorePackage.getDeptId()
						.equals(String.valueOf(sysDeptTypeId.getDeptId()))) {
					wmScorePackage.setTypeid("0");
				} else if (sysDeptTypeId.getLevel() == 2) {
					wmScorePackage.setTypeid("1");
				} else if (sysDeptTypeId.getLevel() == 3 && wmScorePackage.getDeptId()
						.equals(String.valueOf(sysDeptTypeId.getDeptId()))) {
					wmScorePackage.setTypeid("1");
				} else if (sysDeptTypeId.getLevel() == 3) {
					wmScorePackage.setTypeid("2");
				}
				wmScorePackageService.updateById(wmScorePackage);
				WmScorePackage queryWm = new WmScorePackage();

//				SysDept sysDept4 = new SysDept();
//				sysDept4.setDeptId(SecurityUtils.getUser().getDeptId());
//				SysDept sysDept3 = sysDeptMapper.selectDeptId(sysDept4);

				SysDept sysDept3 = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
				if (sysDept3.getLevel() == 3) {
					queryWm.setDeptId(SecurityUtils.getUser().getDeptId() + "");
				} else {
					queryWm.setId("-1");
				}

				List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(queryWm));
				WmScorePackage wmId = wmScorePackageService.getById(wmScorePackage.getId());
				if (wmScorePackageList.size() > 0) {
					for (WmScorePackage wms : wmScorePackageList) {
						if (wms.getId().equals(wmId.getRelationScoreId())) {
							wms.setKfpjf(new Double(Math.ceil(wms.getKfpjf() - wmId.getScore())).intValue());
							wmScorePackage.setPackageIsConduct("0");
							wmScorePackage.setPackageType1(wmScorePackage.getPackageType1());
							//判断积分包是否被发放
							List<WmScorePackage> wmScorePackages = wmScorePackageService.selectRelationScoreId(wmScorePackage
									.getId());
							if (wmScorePackages.size() > 0) {
								for (WmScorePackage wmScorePackage1 : wmScorePackages) {
									if (wmScorePackage1.getId().equals(wmScorePackage.getRelationScoreId())) {
										wms.setPackageIsConduct("1");
										break;
									} else {
										wms.setPackageIsConduct("0");
									}
								}
							}
						}
					}
				}

				wmScorePackageService.updateBatchById(wmScorePackageList);


			}
		}
		return R.ok();
	}

	/**
	 * 终止
	 *
	 * @param wmScorePackage
	 * @return
	 */
	@PostMapping("/stopWmScorePackage")
	public R stopWmScorePackage(@RequestBody WmScorePackage wmScorePackage) {
		if (StringUtils.isEmpty(wmScorePackage.getId())) {
			return R.failed("参数异常");
		}
		wmScorePackage = wmScorePackageService.getById(wmScorePackage.getId());

		if (wmScorePackage.getScore() <= 0) {
			return R.failed("积分包分值必须大于0");
		}

		WmScorePackage query = new WmScorePackage();
		query.setRelationScoreId(wmScorePackage.getId());
		query.setDelFlag("0");
		List<WmScorePackage> rePacks = wmScorePackageService.list(Wrappers.query(query).lambda().
				in(WmScorePackage::getTypeid, "0", "1", "2"));

		if (CollectionUtils.isNotEmpty(rePacks)) {
			for (WmScorePackage rePack : rePacks) {
				if ("0".equals(rePack.getEnableFlag())) {
					return R.failed("积分包已被分派，请联系下属CSO终止关联积分包");
				}
			}
		}

		WmScorePackage updateEntity = new WmScorePackage();

		updateEntity.setId(wmScorePackage.getId());
		updateEntity.setPackageFinishStatus("2");
		updateEntity.setEnableType("1");
		updateEntity.setEnableFlag("1");
		updateEntity.setScorePackageStatus("6");
		wmScorePackageService.updateById(updateEntity);

		return R.ok();
	}


	/**
	 * 终止积分包 new 终止 二期  业务平台
	 *
	 * @param wmScorePackage 积分包
	 * @return R
	 */
	@ApiOperation(value = "终止积分包", notes = "终止积分包")
	@SysLog("终止积分包")
	@PostMapping("/updateStatusByIds")
	public R updateStatusByIds(@RequestBody WmScorePackage wmScorePackage) {
		if (wmScorePackage.getScore() <= 0) {
			return R.failed("积分包分值必须大于0");
		}

		wmScorePackage.setUpdateUser(SecurityUtils.getUser().getId());
		wmScorePackage.setUpdateTime(LocalDateTime.now());
		if (null != wmScorePackage.getRelationScoreId() && !"".equals(wmScorePackage.getRelationScoreId())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setRelationScoreId(wmScorePackage.getRelationScoreId());
		}

		if (null != wmScorePackage.getScorePackageName() && !"".equals(wmScorePackage.getScorePackageName())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setEnableFlag("0");
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(wmScorePackage1));
			WmScorePackage wmId = wmScorePackageService.getById(wmScorePackage.getId());
			if (wmScorePackageList.size() > 0) {
				for (WmScorePackage wms : wmScorePackageList) {
					if (wmId.getId().equals(wms.getRelationScoreId())) {
						return R.failed("积分包已被分派，请联系下属CSO终止关联积分包");
					} else if (null != wms.getRelationScoreId() && "".equals(wms.getRelationScoreId())) {
						wmScorePackage.setEnableFlag("1");
						wmScorePackage.setPackageFinishStatus("2");
						List<WmScorePackage> wmScorePackages = wmScorePackageService.selectRelationScoreId(wmId.getId());
						if (wmScorePackages.size() > 0) {
							for (WmScorePackage wmScorePackage3 : wmScorePackages) {
								if (wmId.getRelationScoreId().equals(wmScorePackage3.getRelationScoreId())) {
									wms.setPackageIsConduct("1");
									break;
								} else {
									wms.setPackageIsConduct("0");
								}
							}
						}
						wmScorePackage.setPackageIsConduct("0");
					}
				}
			}
		}


		if (SecurityUtils.getRoles().contains(4) || SecurityUtils.getRoles().contains(3)) {
			WmScorePackage queryWm = new WmScorePackage();
//			SysDept sysDept4 = new SysDept();
//			sysDept4.setDeptId(SecurityUtils.getUser().getDeptId());
//			SysDept sysDept = sysDeptMapper.selectDeptId(sysDept4);

			SysDept sysDept = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

			if (sysDept.getLevel() == 3) {
				queryWm.setEnableFlag("0");
			} else {
				queryWm.setId("-1");
			}

			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(queryWm));
			WmScorePackage wmId = wmScorePackageService.getById(wmScorePackage.getId());
			if (wmScorePackageList.size() > 0) {
				for (WmScorePackage wmScorePackage2 : wmScorePackageList) {
					if (wmId.getId().equals(wmScorePackage2.getRelationScoreId())) {
						return R.failed("积分包已被分派，请联系下属CSO终止关联积分包");
					} else if (wmScorePackage2.getId().equals(wmId.getRelationScoreId())) {
						List<WmScorePackage> wmScorePackages = wmScorePackageService.selectRelationScoreId(wmId.getId());
						if (wmScorePackages.size() > 0) {
							for (WmScorePackage wmScorePackage1 : wmScorePackages) {
								if (wmId.getRelationScoreId().equals(wmScorePackage1.getRelationScoreId())) {
									wmScorePackage2.setPackageIsConduct("1");
									break;
								} else {
									wmScorePackage2.setPackageIsConduct("0");

								}
							}
						}
						wmScorePackage.setEnableFlag("1");
						wmScorePackage.setPackageFinishStatus("2");
						wmScorePackage2.setKfpjf(new Double(Math.ceil(wmScorePackage2.getKfpjf() + wmId.getScore())).intValue());

						wmScorePackage.setPackageIsConduct("0");

					} else if (null == wmScorePackage.getRelationScoreId() || "".equals(wmScorePackage.getRelationScoreId())) {
						wmScorePackage.setEnableFlag("1");
						wmScorePackage.setPackageFinishStatus("2");
						List<WmScorePackage> wmScorePackages = wmScorePackageService.selectRelationScoreId(wmId.getId());
						if (wmScorePackages.size() > 0) {
							for (WmScorePackage wmScorePackage1 : wmScorePackages) {
								if (wmId.getRelationScoreId().equals(wmScorePackage1.getRelationScoreId())) {
									wmScorePackage2.setPackageIsConduct("1");
									break;
								} else {
									wmScorePackage2.setPackageIsConduct("0");

								}
							}
						}
						wmScorePackage.setPackageIsConduct("0");
					}
				}
			}

			wmScorePackageService.updateBatchById(wmScorePackageList);

		}

		return R.ok(wmScorePackageService.updateById(wmScorePackage));

	}

	/**
	 * 修改积分包 new  二期   业务平台
	 *
	 * @param wmScorePackage 积分包
	 * @return R
	 */
	@ApiOperation(value = "修改积分包", notes = "修改积分包")
	@SysLog("修改积分包")
	@PostMapping("/updateByIds")
	public R updateByIds(@RequestBody WmScorePackage wmScorePackage) {
		if (wmScorePackage.getScore() <= 0) {
			return R.failed("积分包分值必须大于0");
		}
		wmScorePackage.setPackageType1("1");
		wmScorePackage.setUpdateUser(SecurityUtils.getUser().getId());
		wmScorePackage.setUpdateTime(LocalDateTime.now());
		WmScorePackage oldData = wmScorePackageService.getById(wmScorePackage.getId());
		if (StringUtils.isNotEmpty(wmScorePackage.getScorePackageName())
				&& !oldData.getScorePackageName().equals(wmScorePackage.getScorePackageName())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScorePackageName(wmScorePackage.getScorePackageName());
			wmScorePackage1.setDeptId(wmScorePackage.getDeptId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.listWmScorePackages(wmScorePackage1);
			if (wmScorePackageList.size() > 0) {
				return R.failed("积分包名称已存在");
			}

		}
//		SysDept sysDept4 = new SysDept();
//		sysDept4.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept sysDeptDrugEntId = sysDeptMapper.selectDeptId(sysDept4);

		SysDept sysDeptDrugEntId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

		WmDaDrugEnt wmDaDrugEnt = wmDaDrugEntService.selectByDeptId(sysDeptDrugEntId.getDeptId());

		if (null != wmDaDrugEnt) {
			if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug().equals(wmDaDrugEnt.getId())) {
				WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());
//				SysDept sysDept8 = new SysDept();
//				sysDept8.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//				SysDept sysDepts = sysDeptMapper.selectDeptId(sysDept8);

				SysDept sysDepts = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));

				if (sysDepts.getLevel() == 3) {
					wmScorePackage.setLevel2Id("");
					wmScorePackage.setLevel1Id(wmScorePackage.getAcceptSug());
				} else if (sysDepts.getLevel() == 4) {
					wmScorePackage.setLevel1Id("");
					wmScorePackage.setLevel2Id(wmScorePackage.getAcceptSug());

				}

			} else {
				wmScorePackage.setLevel1Id("");
			}
		} else {
			WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());
//			SysDept sysDept8 = new SysDept();
//			sysDept8.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//			SysDept sysDeptAgent = sysDeptMapper.selectDeptId(sysDept8);

			SysDept sysDeptAgent = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));

			if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug().equals(wmDaAgent.getId())) {
//				SysDept sysDept9 = new SysDept();
//				sysDept9.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//				SysDept sysDepts = sysDeptMapper.selectDeptId(sysDept9);

				SysDept sysDepts = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));
				if (sysDepts.getLevel() == 3) {
					wmScorePackage.setLevel2Id("");
					wmScorePackage.setLevel1Id(wmScorePackage.getAcceptSug());
				} else if (sysDepts.getLevel() == 4) {
					wmScorePackage.setLevel1Id("");
					wmScorePackage.setLevel2Id(wmScorePackage.getAcceptSug());

				}

			} else {
				wmScorePackage.setLevel1Id("");
			}

		}
		if (SecurityUtils.getRoles().contains(3) || SecurityUtils.getRoles().contains(4)) {

			//药企规则ID
//			SysDept sysDeptRule = new SysDept();
//			sysDeptRule.setDeptId(SecurityUtils.getUser().getDeptId());
//			SysDept sysDeptRuleId = sysDeptMapper.selectDeptId(sysDeptRule);

			SysDept sysDeptRuleId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

			if (sysDeptRuleId.getLevel() == 2) {
				WmDaDrugEnt wmDaDrugEntRuleId = wmDaDrugEntService.selectByDeptId(sysDeptRuleId.getDeptId());
				if (wmScorePackage.getAcceptSug().equals(wmDaDrugEntRuleId.getId())) {
					wmScorePackage.setRuleId(wmScorePackage.getAcceptSug());
					wmScorePackage.setDrugEntId(wmDaDrugEntRuleId.getId());
				} else if (!wmScorePackage.getAcceptSug().equals(wmDaDrugEntRuleId.getId())) {
					wmScorePackage.setRuleId(wmDaDrugEntRuleId.getId() + wmScorePackage.getAcceptSug());
					wmScorePackage.setDrugEntId(wmDaDrugEntRuleId.getId());
				}
			} else if (sysDeptRuleId.getLevel() == 3) {
				//一级cso规则ID
//				WmScorePackage wmScorePackageRule = new WmScorePackage();
//				wmScorePackageRule.setDelFlag("0");
//				List<WmScorePackage> wmScorePackageRuleList = wmScorePackageService.list(Wrappers.query(wmScorePackageRule));
//				for (WmScorePackage wm : wmScorePackageRuleList) {
//					if (wmScorePackage.getRelationScoreId().equals(wm.getId())) {
//						wmScorePackage.setRuleId(wm.getRuleId());
//						wmScorePackage.setDrugEntId(wm.getDrugEntId());
//					} else if (wmScorePackage.getRelationScoreId().equals("")) {
//						wmScorePackage.setRuleId(wmScorePackage.getAcceptSug());
//						wmScorePackage.setDrugEntId("");
//					}
//				}
				//一级cso规则ID
				WmDaAgent daAgent = wmDaAgentService.getOne(Wrappers.<WmDaAgent>lambdaQuery()
						.eq(WmDaAgent::getEnableFlag, EnableEnum.ENABLE.val())
						.eq(WmDaAgent::getDelFlag, DelEnum.NOT_DEL.val())
						.eq(WmDaAgent::getDeptId, String.valueOf(sysDeptRuleId.getDeptId())));
				if (StringUtils.isNotBlank(wmScorePackage.getRelationScoreId())) {
					// 关联了积分包的情况
					WmScorePackage relationScorePackage = wmScorePackageService.getById(wmScorePackage.getRelationScoreId());
					if (relationScorePackage != null) {
						wmScorePackage.setRuleId(relationScorePackage.getRuleId());
						wmScorePackage.setDrugEntId(relationScorePackage.getDrugEntId());
					} else {
						return R.failed("cso没有分配积分规则");
					}
				} else {
					// 没有关联积分包的情况
					List<WmTaskType> taskTypeList = wmTaskTypeService.list(Wrappers.<WmTaskType>lambdaQuery()
							.eq(WmTaskType::getRuleId, daAgent.getId()));
					if (CollectionUtil.isNotEmpty(taskTypeList)) {
						wmScorePackage.setRuleId(wmScorePackage.getAcceptSug());
						wmScorePackage.setDrugEntId("");
					} else {
						return R.failed("cso没有分配积分规则");
					}
				}
			}/* else if (sysDeptRuleId.getLevel() == 3 && "".equals(wmScorePackage.getRelationScoreId())) {
				wmScorePackage.setRuleId(wmScorePackage.getAcceptSug());
				wmScorePackage.setDrugEntId("");
			}*/


			WmScorePackage queryWm = new WmScorePackage();
//			SysDept sysDeptUser = new SysDept();
//			sysDeptUser.setDeptId(SecurityUtils.getUser().getDeptId());
//			SysDept sysDept = sysDeptMapper.selectDeptId(sysDeptUser);

			SysDept sysDept = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

			if (sysDept.getLevel() == 3) {//判断是cso
				queryWm.setEnableFlag("0");
			} else if (sysDept.getLevel() == 2) {//判断是药企
				WmScorePackage wmId = wmScorePackageService.getById(wmScorePackage.getId());
				queryWm.setScorePackageName(wmId.getScorePackageName());//获取当前包的scoreId
			} else {
				queryWm.setId("-1");
			}


			WmScorePackage wmIds = wmScorePackageService.getById(wmScorePackage.getId());
			if (wmDaDrugEnt != null) {
				if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug()
						.equals(wmDaDrugEnt.getId())) {
					WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());
					wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");
					wmScorePackage.setKfpjf(wmScorePackage.getScore() - wmIds.getScore() + wmIds.getKfpjf());
				} else if (wmScorePackage.getAcceptSug().equals(wmDaDrugEnt.getId())) {
					wmScorePackage.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
					wmScorePackage.setKfpjf(wmScorePackage.getScore());
				}
			} else {
				WmDaAgent wmDaAgent = wmDaAgentService.getById(wmScorePackage.getAcceptSug());
//				SysDept sysDeptAgents = new SysDept();
//				sysDeptAgents.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//				SysDept sysDeptAgent = sysDeptMapper.selectDeptId(sysDeptAgents);

				SysDept sysDeptAgent = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));

				if (null != wmScorePackage.getAcceptSug() && !wmScorePackage.getAcceptSug().equals(wmDaAgent.getId())) {
					wmScorePackage.setDeptId(wmDaAgent.getDeptId() + "");
					wmScorePackage.setKfpjf(wmScorePackage.getScore() - wmIds.getScore() + wmIds.getKfpjf());
				} else if (wmScorePackage.getAcceptSug().equals(wmDaAgent.getId())) {
					wmScorePackage.setDeptId(String.valueOf(wmDaAgent.getDeptId()));
					wmScorePackage.setKfpjf(wmScorePackage.getScore());
				}
			}

//			SysDept sysDeptAgent1 = new SysDept();
//			sysDeptAgent1.setDeptId(SecurityUtils.getUser().getDeptId());
//			SysDept sysDept5 = sysDeptMapper.selectDeptId(sysDeptAgent1);

			SysDept sysDept5 = sysDeptService.getById(SecurityUtils.getUser().getDeptId());

//			SysDept sysDeptAgent2 = new SysDept();
//			sysDeptAgent2.setDeptId(Integer.valueOf(wmScorePackage.getDeptId()));
//			SysDept sysDept6 = sysDeptMapper.selectDeptId(sysDeptAgent2);

			SysDept sysDept6 = sysDeptService.getById(Integer.valueOf(wmScorePackage.getDeptId()));

			if (sysDept5.getLevel() == 2 && sysDept6.getLevel() == 2 && wmScorePackage.getDeptId()
					.equals(String.valueOf(sysDept5.getDeptId()))) {
				wmScorePackage.setTypeid("0");
			} else if (sysDept5.getLevel() == 2) {
				wmScorePackage.setTypeid("1");
			} else if (sysDept5.getLevel() == 3 && sysDept6.getLevel() == 3 && wmScorePackage.getDeptId()
					.equals(String.valueOf(sysDept5.getDeptId()))) {
				wmScorePackage.setTypeid("1");
			} else if (sysDept5.getLevel() == 3) {
				wmScorePackage.setTypeid("2");
			}
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.list(Wrappers.query(queryWm));
			WmScorePackage wmId = wmScorePackageService.getById(wmScorePackage.getId());
			if (wmScorePackageList.size() > 0) {
				for (WmScorePackage wms : wmScorePackageList) {
					if (wmScorePackageList.size() > 0) {
						if (wms.getId().equals(wmScorePackage.getRelationScoreId())) {//判断是否有scoreId与当前关联积分包匹配
							List<WmScorePackage> wmScorePackagess = wmScorePackageService.selectRelationScoreId(wmId.getId());
							if (wmScorePackagess.size() > 0) {
								for (WmScorePackage wmScorePackage13 : wmScorePackagess) {
									if (wmScorePackage.getRelationScoreId().equals(wmScorePackage13.getId())) {
										wms.setPackageIsConduct("1");
										break;
									} else {
										wms.setPackageIsConduct("0");
									}
								}
							}
							if (!wmScorePackage.getRelationScoreId().equals(wmId.getRelationScoreId())) {
								if (wmScorePackage.getScore() < wms.getKfpjf() + wmId.getScore()) {
									wms.setKfpjf(new Double(Math.ceil(wms.getKfpjf() - wmScorePackage.getScore())).intValue());
								} else {
									wms.setKfpjf(new Double(Math.ceil(wms.getKfpjf() - wmScorePackage.getScore())).intValue());
								}
							} else if (wmScorePackage.getScore() > wms.getKfpjf() + wmId.getScore()) {
								return R.failed("可分配积分不足");
							} else if (wmScorePackage.getScore() < wms.getKfpjf() + wmId.getScore()) {
								wms.setKfpjf(new Double(Math.ceil(wms.getKfpjf() + wmId.getScore() - wmScorePackage.getScore()))
										.intValue());
								wmId.setScore(wmScorePackage.getScore());
							} else {
								wms.setKfpjf(new Double(Math.ceil(wms.getKfpjf() + wmId.getScore() - wmScorePackage.getScore()))
										.intValue());
								wmId.setScore(wmScorePackage.getScore());
							}
						} else if (wms.getKfpjf() != null && !wmScorePackage.getRelationScoreId()
								.equals("") && wmScorePackage.getScore() > wms.getKfpjf() + wmId.getScore()) {
							return R.failed("可分配积分不足");
						} else if ("".equals(wmScorePackage.getRelationScoreId()) && wms.getId()
								.equals(wmId.getRelationScoreId())) {//判断关联积分包是否为无
							wms.setKfpjf(wmId.getScore() + wms.getKfpjf());
						} else if (wms.getId().equals(wmId.getRelationScoreId())) {
							wms.setKfpjf(new Double(Math.ceil(wms.getKfpjf() + wmId.getScore())).intValue());
							//判断是否进行中
							List<WmScorePackage> wmScorePackages = wmScorePackageService.selectRelationScoreId(wmId.getId());
							if (wmScorePackages.size() > 0) {
								for (WmScorePackage wmScorePackage1 : wmScorePackages) {
									if (null != wmScorePackage1.getRelationScoreId() && wmScorePackage1.getRelationScoreId()
											.equals(wmId.getRelationScoreId())) {
										wms.setPackageIsConduct("1");
										break;
									} else {
										wms.setPackageIsConduct("0");
									}
								}
							}

						}
					}
				}
				if (null != wmScorePackage.getWmScoreTaskTypeList() && wmScorePackage.getWmScoreTaskTypeList()
						.size() > 0) {
					List<WmScoreTaskType> wmScoreTaskTypess = new ArrayList<>();
					for (WmScoreTaskType wmScoreTaskTypes : wmScorePackage.getWmScoreTaskTypeList()) {
						WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
						//将积分表id和前端传的tasktypeId赋值给wmScoreTaskTypess
						if ("".equals(wmScorePackage.getRelationScoreId())) {
							wmScoreTaskType.setScoreId(wmScorePackage.getId());
							wmScoreTaskType.setType("1");
							wmScoreTaskType.setTaskTypeId(wmScoreTaskTypes.getTaskTypeId());
							wmScoreTaskTypess.add(wmScoreTaskType);
							wmScoreTaskTypeService.deleteByScoreId(wmScorePackage.getId());
							wmScoreTaskTypeService.batchInsertType(wmScoreTaskTypess);
						} else {
							wmScoreTaskType.setScoreId(wmScorePackage.getId());
							wmScoreTaskType.setType("1");
							wmScoreTaskType.setTaskTypeId(wmScoreTaskTypes.getTaskTypeId());
							wmScoreTaskTypess.add(wmScoreTaskType);
							wmScoreTaskTypeService.deleteByScoreId(wmScoreTaskType.getScoreId());
							wmScoreTaskTypeService.batchInsertType(wmScoreTaskTypess);
						}
					}

				}
				wmScorePackageService.updateBatchById(wmScorePackageList);
			}
		}


		return R.ok(wmScorePackageService.updateById(wmScorePackage));

	}


	/**
	 * 查询关联积分包列表
	 *
	 * @param wmScorePackage
	 * @return
	 */
	@ApiOperation(value = "查询关联积分包", notes = "查询关联积分包")
	@GetMapping("/getRelationScoreList")
	public R getRelationScoreList(WmScorePackage wmScorePackage) {
		return wmScorePackageService.getRelationScoreList(wmScorePackage);
	}


	// /**
	//  * 分配积分列表查询--派工平台
	//  *
	//  * @param page           分页对象
	//  * @param wmScorePackage 积分包
	//  * @return
	//  */
	// @ApiOperation(value = "分页查询", notes = "分页查询")
	// @GetMapping("/getScorePackagePage")
	// public R getScorePackagePage(Page page, WmScorePackage wmScorePackage) {
	// 	return wmScorePackageService.getScorePackagePage(page, wmScorePackage);
	// }

	// /**
	//  * 分配积分列表查询--派工平台
	//  *
	//  */
	// @ApiOperation(value = "分页查询", notes = "分页查询")
	// @GetMapping("/getScorePackagePage")
	// public R getScorePackagePage(@RequestParam(value = "current") Integer current,
	// 							 @RequestParam(value = "size") Integer size,
	// 							 @RequestParam(value = "packageType1") Integer packageType1,
	// 							 @RequestParam(value = "packageType2") Integer packageType2,
	// 							 @RequestParam(value = "scorePackageName",required = false) String scorePackageName,
	// 							 @RequestParam(value = "scorePackageStatus",required = false) Integer scorePackageStatus,
	// 							 @RequestParam(value = "typeid",required = false) String typeId
	// ) {
	// 	return wmScorePackageService.getScorePackagePage(new Page<WmScorePackage>(current,size),
	// 			packageType1,packageType2,scorePackageName,scorePackageStatus,typeId);
	// }

	/**
	 * 分配积分列表查询--派工平台
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getScorePackagePage")
	public R getScorePackagePage(@RequestParam(value = "current") Integer current,
								 @RequestParam(value = "size") Integer size,
								 @RequestParam(value = "packageType1") Integer packageType1,
								 @RequestParam(value = "packageType2") Integer packageType2,
								 @RequestParam(value = "scorePackageName", required = false) String scorePackageName,
								 @RequestParam(value = "scorePackageStatus", required = false) Integer scorePackageStatus,
								 @RequestParam(value = "typeid", required = false) String typeId
	) {

		Integer sendDeptId = SecurityUtils.getUser().getDeptId();

		LambdaQueryWrapper<WmScorePackage> queryWrapper = Wrappers.<WmScorePackage>lambdaQuery()
				.eq(WmScorePackage::getPackageType1, packageType1)
				.eq(WmScorePackage::getPackageType2, packageType2)
				.like(StrUtil.isNotEmpty(scorePackageName), WmScorePackage::getScorePackageName, scorePackageName)
				.eq(scorePackageStatus != null, WmScorePackage::getScorePackageStatus, scorePackageStatus)
				.eq(WmScorePackage::getSendPackageDeptId, sendDeptId);

		if (StringUtil.isEmpty(typeId)) {
			queryWrapper.in(WmScorePackage::getTypeid, "3", "4");
		} else {
			List<String> split = StrUtil.split(typeId, StrUtil.C_COMMA);
			queryWrapper.in(WmScorePackage::getTypeid, split);
		}

		Page<WmScorePackage> wmScorePackagePage = wmScorePackageMapper.selectPage(new Page<>(current, size), queryWrapper
				.orderByDesc(WmScorePackage::getCreateTime));

		List<WmScorePackage> pageRecords = wmScorePackagePage.getRecords();

		List<Future<WmScorePackage>> futureList = new ArrayList<>(pageRecords.size());

		for (WmScorePackage scorePackage : pageRecords) {
			Future<WmScorePackage> wmScorePackageFuture = wmScorePackageService.buildScorePackage(scorePackage, packageType1, packageType2);
			futureList.add(wmScorePackageFuture);
		}

		List<WmScorePackage> finalRecords = new ArrayList<>(pageRecords.size());
		try {
			for (Future<WmScorePackage> future : futureList) {
				WmScorePackage wmScorePackage = future.get();
				finalRecords.add(wmScorePackage);

			}

			wmScorePackagePage.setRecords(finalRecords);
		} catch (ExecutionException | InterruptedException e) {
			log.error("获取分页信息失败");
			return R.failed("获取分页信息失败");
		}

		return R.ok(wmScorePackagePage);
	}

	/**
	 * 根据id查询积分包信息
	 *
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getScorePackagePageById")
	public R getScorePackagePageById(WmScorePackage wmScorePackage) {
		return wmScorePackageService.getScorePackagePageById(wmScorePackage);
	}

	/**
	 * 新增积分包
	 *
	 * @param wmScorePackage 积分包
	 * @return R
	 */
	@ApiOperation(value = "新增积分包", notes = "新增积分包")
	@SysLog("新增积分包")
	@PostMapping("/addWmScorePackage")
	public R addWmScorePackage(@RequestBody WmScorePackage wmScorePackage) {
		if (wmScorePackage.getPackageType1().equals("1")) {//定额
			if (wmScorePackage.getScore() <= 0) {
				return R.failed("积分包分值必须大于0");
			}
		}
		return wmScorePackageService.addWmScorePackage(wmScorePackage);
	}


	@ApiOperation(value = "修改积分包--删除、结算", notes = "修改积分包--删除、结算")
	@SysLog("修改积分包--删除、结算")
	@PostMapping("/updateEasy")
	public R updateEasy(@RequestBody WmScorePackage wmScorePackage) {

		return wmScorePackageService.updateEasy(wmScorePackage);
	}

	@ApiOperation(value = "修改积分包", notes = "修改积分包")
	@SysLog("修改积分包")
	@PostMapping("/updateWmScorePackage")
	public R updateWmScorePackage(@RequestBody WmScorePackage wmScorePackage) {
		if (wmScorePackage.getPackageType1().equals("1")) {//定额
			if (wmScorePackage.getScore() <= 0) {
				return R.failed("积分包分值必须大于0");
			}
		}

		/**
		 * 被领取的积分包 不能修改
		 */
		WmScorePackageStatus statusQuery = new WmScorePackageStatus();
		statusQuery.setPackageId(wmScorePackage.getId());
//		statusQuery.setStatus("2");
		int total = wmScorePackageStatusService.count(Wrappers.query(statusQuery).lambda()
				.in(WmScorePackageStatus::getStatus, "1", "2"));
		if (total > 0) {
			return R.failed("积分包已被领取，不能修改");
		}
		WmScorePackage oldData = wmScorePackageService.getById(wmScorePackage.getId());
		if (StringUtils.isNotEmpty(wmScorePackage.getScorePackageName())
				&& !oldData.getScorePackageName().equals(wmScorePackage.getScorePackageName())) {
			WmScorePackage wmScorePackage1 = new WmScorePackage();
			wmScorePackage1.setDelFlag("0");
			wmScorePackage1.setScorePackageName(wmScorePackage.getScorePackageName());
			wmScorePackage1.setDeptId(wmScorePackage.getDeptId());
			List<WmScorePackage> wmScorePackageList = wmScorePackageService.listWmScorePackages(wmScorePackage1);
			if (wmScorePackageList.size() > 0) {
				return R.failed("积分包名称已存在");
			}

		}
		return wmScorePackageService.updateWmScorePackage(wmScorePackage);
	}

	/**
	 * 终止积分包
	 *
	 * @param wmScorePackage
	 * @param
	 * @return
	 */
	@ApiOperation(value = "终止积分包", notes = "终止积分包")
	@SysLog("终止积分包")
	@GetMapping("/updateEnable")
	public R updateEnable(WmScorePackage wmScorePackage) {
		if (StringUtil.isEmpty(wmScorePackage.getEnableType())) {
			return R.failed("缺少参数");
		}

		return wmScorePackageService.updateEnable(wmScorePackage);
	}

	/**
	 * 查询已完成积分值
	 *
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@GetMapping("/selectSettleStatus")
	public Map<String, Object> selectSettleStatus(WmScorePackage wmScorePackage) {
		return wmScorePackageService.selectSettleStatus(wmScorePackage);
	}

	/**
	 * 查询发包对象
	 *
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@GetMapping("/selectSendPackageDeptId")
	public List<SysDept> selectSendPackageDeptId(WmScorePackage wmScorePackage) {
		return wmScorePackageService.selectSendPackageDeptId(wmScorePackage);
	}

	/**
	 * 查询发包对象
	 *
	 * @param wmScorePackage 积分包
	 * @return
	 */
	@GetMapping("/selectRelationScoreIdList")
	public WmScorePackage selectRelationScoreIdList(WmScorePackage wmScorePackage) {
		return wmScorePackageService.selectRelationScoreIdList(wmScorePackage);
	}

	/**
	 * 批量导入积分包
	 *
	 * @param file 积分包信息文件
	 * @return success/false
	 */
	@SysLog("批量导入积分包")
	@PostMapping(value = "/batch")
	@Transactional(rollbackFor = Exception.class)
	public R batchScorePackage(@RequestPart("file") MultipartFile file) {

		// 获取操作员和操作员所在的结算主体
		HnqzUser operator = SecurityUtils.getUser();
		SysDept sysDept = sysDeptService.getById(operator.getDeptId());
		Map<String, Object> result = new HashMap<>(2);

		// 读取excel
		try {
			String fileName = file.getOriginalFilename().toLowerCase();
			InputStream inputStream = file.getInputStream();

			// 校验文件信息
			Workbook wbs = null;
			if (fileName.endsWith("xls")) {
				//2003
				wbs = new HSSFWorkbook(inputStream);
			} else if (fileName.endsWith("xlsx")) {
				//2007
				wbs = new XSSFWorkbook(inputStream);
			} else {
				return R.failed("上传文件格式错误!");
			}
			if (wbs.isSheetHidden(0)) {
				return R.failed("导入表中有隐藏列");
			}

			// 校验页
			Sheet sheet = wbs.getSheetAt(1);

			// 校验模板
			Row row0 = sheet.getRow(0);
			if (row0 == null) {
				return R.failed("导入模版错误！");
			}

			// 校验记录数
			int rowNum = sheet.getLastRowNum();
			if (rowNum < 1) {
				return R.failed("导入表中缺少数据！");
			} else if (rowNum > 1000) {
				return R.failed("单次批量导入最大支持1000行数据！");
			}

			// 校验列信息
			String scorePackageName = row0.getCell(0).getStringCellValue();
			if (!"积分包名称".equals(scorePackageName)) {
				return R.failed("导入模版第一列错误！");
			}
			String typeid = row0.getCell(1).getStringCellValue();
			if (!"接单对象类型".equals(typeid)) {
				return R.failed("导入模版第二列错误！");
			}
			String user = row0.getCell(2).getStringCellValue();
			if (!"接单对象".equals(user)) {
				return R.failed("导入模版第三列错误！");
			}
			String relatedService = row0.getCell(3).getStringCellValue();
			if (!"积分包值".equals(relatedService)) {
				return R.failed("导入模版第四列错误！");
			}
			String relationScoreId = row0.getCell(4).getStringCellValue();
			if (!"关联服务企业".equals(relationScoreId)) {
				return R.failed("导入模版第五列错误！");
			}
			String score = row0.getCell(5).getStringCellValue();
			if (!"关联任务积分包".equals(score)) {
				return R.failed("导入模版第六列错误！");
			}
			String packageUserScope = row0.getCell(6).getStringCellValue();
			if (!"接单对象范围".equals(packageUserScope)) {
				return R.failed("导入模版第七列错误！");
			}
			String taskTypes = row0.getCell(7).getStringCellValue();
			if (!"有效任务类型".equals(taskTypes)) {
				return R.failed("导入模版第八列错误！");
			}
			String description = row0.getCell(8).getStringCellValue();
			if (!"描述".equals(description)) {
				return R.failed("导入模版第九列错误！");
			}

			//数据
			List<WmScorePackage> wmScorePackageList = new ArrayList<>();
			List<ErrorInfoVo> errorList = new ArrayList<>();
			List mapList = new ArrayList<>();
			Map<String, Object> map = new HashMap<>();
			log.info(">>> 通过excel批量创建用户开始 <<<");
			// 行数据
			for (int i = 1; i <= rowNum; i++) {

				// 行编号
				int rNum = i;
				Row row = sheet.getRow(i);
				log.info(">>>当前导入第{}行<<<", i);

				if (row == null) {
					ErrorInfoVo errorInfoVo = new ErrorInfoVo();
					errorInfoVo.setRowNum(rNum);
					errorInfoVo.setColumnName(null);
					errorInfoVo.setContent(null);
					errorInfoVo.setReason("当前行没有数据信息");
					errorList.add(errorInfoVo);
					continue;
				}

				int colNum = row.getLastCellNum();
				WmScorePackage wmScorePackage = new WmScorePackage();

				wmScorePackage.setDeptId(String.valueOf(sysDept.getDeptId()));
				wmScorePackage.setCreateTime(LocalDateTime.now());

				for (int j = 0; j < colNum; j++) {//列数据

					// 获取列名称
					String columnName = row0.getCell(j).getStringCellValue();
					int columnNum = j + 1;

					if (null == row.getCell(j)) {
						ErrorInfoVo errorInfoVo = new ErrorInfoVo();
						errorInfoVo.setRowNum(rNum);
						errorInfoVo.setColumnNum(columnNum);
						errorInfoVo.setColumnName(columnName);
						errorInfoVo.setContent(null);
						errorInfoVo.setReason("列数据为空");
						errorList.add(errorInfoVo);
						continue;
					}
					// 格式化字符串
					row.getCell(j).setCellType(CellType.STRING);
					String value = row.getCell(j).getStringCellValue()
							.replaceAll(" ", "")
							.replaceAll(",", "")
							.replaceAll("\\u00A0", "")
							.replaceAll((char) 12288 + "", "")
							.replace("+", "·");

					// 校验value
					if (!columnName.equals("有效任务类型") && !columnName.equals("接单对象") && !columnName.equals("描述")) {
						if (StringUtil.isEmpty(value)) {
							ErrorInfoVo errorInfoVo = new ErrorInfoVo();
							errorInfoVo.setRowNum(rNum);
							errorInfoVo.setColumnNum(columnNum);
							errorInfoVo.setColumnName(columnName);
							errorInfoVo.setContent(value);
							errorInfoVo.setReason("未填写数据");
							errorList.add(errorInfoVo);
							continue;
						}
					}

					if (j == 0) {
						//判单 积分包名称是否已被占用
						WmScorePackage wmScorePackage1 = new WmScorePackage();
						wmScorePackage1.setScorePackageName(value);
						wmScorePackage1.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
						WmScorePackage wmScorePackage2 = wmScorePackageMapper.selectByName(wmScorePackage1);
						if (wmScorePackage2 != null) {
							ErrorInfoVo errorInfoVo = new ErrorInfoVo();
							errorInfoVo.setRowNum(rNum);
							errorInfoVo.setColumnNum(columnNum);
							errorInfoVo.setColumnName(columnName);
							errorInfoVo.setContent(value);
							errorInfoVo.setReason(String.format("{%s }已被占用", value));
							errorList.add(errorInfoVo);
							continue;
						} else {
							wmScorePackage.setScorePackageName(value);
						}
					} else if (j == 1) {
						if ("兼职学术信息沟通专员".equals(value)) {
							wmScorePackage.setTypeid("4");
							wmScorePackage.setScorePackageStatus(1 + "");
						} else if ("全职学术信息沟通专员".equals(value)) {
							wmScorePackage.setTypeid("3");
							wmScorePackage.setScorePackageStatus(3 + "");
						} else {
							ErrorInfoVo errorInfoVo = new ErrorInfoVo();
							errorInfoVo.setRowNum(rNum);
							errorInfoVo.setColumnNum(columnNum);
							errorInfoVo.setColumnName(columnName);
							errorInfoVo.setContent(value);
							errorInfoVo.setReason("接单对象类型错误");
							errorList.add(errorInfoVo);
							continue;
						}
					} else if (j == 2) {
						if (wmScorePackage.getTypeid().equals("3")) {
							if (!value.equals("") && !value.equals(null)) {
								List list = new ArrayList();
								UserDTO userDTO = new UserDTO();
								userDTO.setDeptId(SecurityUtils.getUser().getDeptId());
								List<UserVO> userList = sysUserMapper.selectByDeptId(userDTO);
//								List<Stream<SysRole>> ids = userList.stream().filter(p->p.getRoleList().stream().filter(k->k.getRoleId().equals(5))).collect(Collectors.toList());
								List<UserVO> list1 = userList.stream()
										.filter(p -> CollectionUtils.isNotEmpty(p.getRoleList()
												.stream()
												.filter(k -> k.getRoleId().equals(5))
												.collect(Collectors.toList())))
										.collect(Collectors.toList());
								if (CollectionUtils.isNotEmpty(list1)) {
									List<String> list2 = list1.stream()
											.map(p -> p.getUsername())
											.collect(Collectors.toList());
									if (list2.contains(value)) {
										list.add(value);
										wmScorePackage.setUserList(list);
									}
								} else {
									ErrorInfoVo errorInfoVo = new ErrorInfoVo();
									errorInfoVo.setRowNum(rNum);
									errorInfoVo.setColumnNum(columnNum);
									errorInfoVo.setColumnName(columnName);
									errorInfoVo.setContent(value);
									errorInfoVo.setReason("接单对象未在系统中");
									errorList.add(errorInfoVo);
									continue;
								}
							} else {
								ErrorInfoVo errorInfoVo = new ErrorInfoVo();
								errorInfoVo.setRowNum(rNum);
								errorInfoVo.setColumnNum(columnNum);
								errorInfoVo.setColumnName(columnName);
								errorInfoVo.setContent(value);
								errorInfoVo.setReason("接单对象错误");
								errorList.add(errorInfoVo);
								continue;
							}
						} else {
							wmScorePackage.setUserList(null);
							wmScorePackage.setScorePackageStatus("1");
						}
					} else if (j == 3) {
						wmScorePackage.setScore(Integer.parseInt(value));
					} else if (j == 4) {
						SysDept sysDept2 = new SysDept();
						sysDept2.setDeptId(SecurityUtils.getUser().getDeptId());
						List<SysDept> list = sysDeptService.getAllParentDept(sysDept2);
						boolean b = list.parallelStream().noneMatch(SysDept -> value.equals(SysDept.getName()));
//						List<String> name = list.stream().map(p->p.getName()).collect(Collectors.toList());
						if (!b) {
							List<SysDept> sysDept3 = sysDeptService.selectByName(value);
							wmScorePackage.setRelatedService(String.valueOf(sysDept3.get(0).getDeptId()));
						} else {
							ErrorInfoVo errorInfoVo = new ErrorInfoVo();
							errorInfoVo.setRowNum(rNum);
							errorInfoVo.setColumnNum(columnNum);
							errorInfoVo.setColumnName(columnName);
							errorInfoVo.setContent(value);
							errorInfoVo.setReason("关联服务企业错误");
							errorList.add(errorInfoVo);
							continue;
						}
					} else if (j == 5) {
						WmScorePackage wmScorePackage1 = new WmScorePackage();
						wmScorePackage1.setScorePackageName(value);
						wmScorePackage1.setDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
						WmScorePackage wmScorePackage2 = wmScorePackageMapper.selectByName(wmScorePackage1);
						if (!wmScorePackage1.getScorePackageName().equals("无") && wmScorePackage2 != null) {
							wmScorePackage.setRelationScoreId(wmScorePackage2.getId());
							if (i != 1) {
								map.put("id1", wmScorePackage.getRelationScoreId());
								mapList.add(map.get("id1"));
								map.put(wmScorePackage.getRelationScoreId(), wmScorePackage.getScore());
							}
							if (map.get("id") != null) {
								if (mapList.contains(wmScorePackage.getRelationScoreId())) {
									map.put(wmScorePackage.getRelationScoreId(), wmScorePackage.getScore() + (Integer) map
											.get(wmScorePackage.getRelationScoreId()));
								} else {
									map.put(wmScorePackage.getRelationScoreId(), wmScorePackage.getScore());
								}
							} else {
								map.put("id", wmScorePackage.getRelationScoreId());
								mapList.add(map.get("id"));
								map.put(wmScorePackage.getRelationScoreId(), wmScorePackage.getScore());
							}
							List<WmScoreTaskType> taskTypelist = wmScoreTaskTypeMapper.getScoreTaskTypeByScoreId(wmScorePackage2
									.getId());
							wmScorePackage.setTaskTypeList1(taskTypelist);
						} else if (wmScorePackage1.getScorePackageName().equals("无")) {
							wmScorePackage.setRelationScoreId(null);
						} else {
							ErrorInfoVo errorInfoVo = new ErrorInfoVo();
							errorInfoVo.setRowNum(rNum);
							errorInfoVo.setColumnNum(columnNum);
							errorInfoVo.setColumnName(columnName);
							errorInfoVo.setContent(value);
							errorInfoVo.setReason("关联任务积分包错误");
							errorList.add(errorInfoVo);
							continue;
						}
					} else if (j == 6) {
						if ("仅内部可见".equals(value)) {
							wmScorePackage.setPackageUserScope("1");
						} else if ("全部可见".equals(value)) {
							wmScorePackage.setPackageUserScope("2");
						} else {
							ErrorInfoVo errorInfoVo = new ErrorInfoVo();
							errorInfoVo.setRowNum(rNum);
							errorInfoVo.setColumnNum(columnNum);
							errorInfoVo.setColumnName(columnName);
							errorInfoVo.setContent(value);
							errorInfoVo.setReason("接单对象范围错误");
							errorList.add(errorInfoVo);
							continue;
						}
					} else if (j == 7) {
						String types = value;
						if (types.contains("&")) {
							String[] strs = types.split("&");
							Arrays.asList(strs);
							List<WmTaskTypeTree> list2 = new ArrayList();
							for (int k = 0; k < strs.length; k++) {
								WmTaskTypeTree wmTaskTypeTree = new WmTaskTypeTree();
								wmTaskTypeTree.setTaskTypeName(strs[k]);
								list2.add(wmTaskTypeTree);
							}
							wmScorePackage.setTaskTypeList(list2);
						} else {
							List<WmTaskTypeTree> list2 = new ArrayList();
							WmTaskTypeTree wmTaskTypeTree = new WmTaskTypeTree();
							wmTaskTypeTree.setTaskTypeName(value);
							list2.add(wmTaskTypeTree);
							wmScorePackage.setTaskTypeList(list2);
						}

					} else if (j == 8) {
						wmScorePackage.setDescription(value);
					}
					wmScorePackage.setPackageType1("1");
					wmScorePackage.setPackageType2("1");
					wmScorePackage.setBelongDate(LocalDateTime.now()
							.format(DateTimeFormatter.ofPattern("yyyy-MM")) + "");
				}

				wmScorePackageList.add(wmScorePackage);
			}

			// 判断是否有错误的数据行，如果没有，执行入库操作
			if (CollectionUtils.isEmpty(errorList)) {
				for (WmScorePackage wmScorePackage : wmScorePackageList) {
					if (wmScorePackage.getRelationScoreId() != null) {
						if (mapList.contains(wmScorePackage.getRelationScoreId())) {
							Map<String, Object> map1 = new HashMap<>();
							map1.put("type", wmScorePackage.getTypeid());
							String deptId = String.valueOf(SecurityUtils.getUser().getDeptId());
							wmScorePackage.setDeptId(deptId);
							wmScorePackage.setTypeid("0,1,2");
							wmScorePackage.setSendPackageDeptId(wmScorePackage.getRelatedService());
							List<WmScorePackage> packageList = wmScorePackageMapper.getScorePackagekfpjf(wmScorePackage);
							wmScorePackage.setTypeid(String.valueOf(map1.get("type")));
							wmScorePackage.setSendPackageDeptId(String.valueOf(SecurityUtils.getUser().getDeptId()));
							List<String> list = packageList.stream().map(p -> p.getId()).collect(Collectors.toList());
							for (String id : list) {
								if (wmScorePackage.getRelationScoreId().equals(id)) {
									WmScorePackage wmScorePackage1 = wmScorePackageService.getById(id);
									if (wmScorePackage1.getKfpjf() < (Integer) map.get(wmScorePackage.getRelationScoreId())) {
										ErrorInfoVo errorInfoVo = new ErrorInfoVo();
										errorInfoVo.setReason("填写积分值总值大于关联积分包可分配积分值");
										errorList.add(errorInfoVo);
										return R.ok(errorList);
									}
								}
							}

						}
					}
				}
				wmScorePackageService.saveBatch(wmScorePackageList);
				for (WmScorePackage wmScorePackage : wmScorePackageList) {

					if (wmScorePackage.getUserList() != null && wmScorePackage.getUserList().size() > 0) {
						WmScorePackageStatus wmScorePackageStatus = new WmScorePackageStatus();
						SysUser sysUser = new SysUser();
						sysUser.setUsername(wmScorePackage.getUserList().get(0));
						sysUser.setDeptId(SecurityUtils.getUser().getDeptId());
						SysUser sysUser1 = sysUserMapper.selectUserByName(sysUser);
						wmScorePackageStatus.setUserId(sysUser1.getUserId() + "");
						wmScorePackageStatus.setStatus("2");
						wmScorePackageStatus.setPackageId(wmScorePackage.getId());
						wmScorePackageStatus.setDeptId(SecurityUtils.getUser().getDeptId() + "");
						wmScorePackageStatus.setUserScore(wmScorePackage.getScore() + "");
						wmScorePackageStatus.setEnableFlag("0");
						wmScorePackageStatus.setDelFlag("0");
						wmScorePackageStatus.setCreateTime(LocalDateTime.now());
						wmScorePackageStatus.setCreateUser(SecurityUtils.getUser().getId());
						wmScorePackageStatusService.save(wmScorePackageStatus);
					}

					if (wmScorePackage.getRelationScoreId() != null) {
						if (wmScorePackage.getRelationScoreId().equals(map.get("id"))) {
							WmScorePackage wmScorePackage1 = wmScorePackageService.getById(wmScorePackage.getRelationScoreId());
							wmScorePackage1.setKfpjf(wmScorePackage1.getKfpjf() - (Integer) map.get(wmScorePackage.getRelationScoreId()));
							wmScorePackageService.updateById(wmScorePackage1);
						}
					}

					if (wmScorePackage.getRelationScoreId() != null && wmScorePackage.getRelationScoreId() != null) {
						List<String> list = wmScorePackage.getTaskTypeList1()
								.stream()
								.map(p -> p.getTaskTypeId())
								.collect(Collectors.toList());
						for (String id : list) {
							WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
							wmScoreTaskType.setScoreId(wmScorePackage.getId());
							wmScoreTaskType.setTaskTypeId(id);
							wmScoreTaskTypeMapper.insert(wmScoreTaskType);
						}
					} else if (wmScorePackage.getTaskTypeList()
							.get(0)
							.getTaskTypeName()
							.equals("全部") && wmScorePackage.getRelationScoreId() == null) {
						WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
						wmScoreTaskType.setScoreId(wmScorePackage.getId());
						wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM1.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType);
						WmScoreTaskType wmScoreTaskType1 = new WmScoreTaskType();
						wmScoreTaskType1.setScoreId(wmScorePackage.getId());
						wmScoreTaskType1.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM2.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType1);
						WmScoreTaskType wmScoreTaskType2 = new WmScoreTaskType();
						wmScoreTaskType2.setScoreId(wmScorePackage.getId());
						wmScoreTaskType2.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM3.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType2);
						WmScoreTaskType wmScoreTaskType3 = new WmScoreTaskType();
						wmScoreTaskType3.setScoreId(wmScorePackage.getId());
						wmScoreTaskType3.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM4.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType3);
						WmScoreTaskType wmScoreTaskType4 = new WmScoreTaskType();
						wmScoreTaskType4.setScoreId(wmScorePackage.getId());
						wmScoreTaskType4.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM5.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType4);
						WmScoreTaskType wmScoreTaskType5 = new WmScoreTaskType();
						wmScoreTaskType5.setScoreId(wmScorePackage.getId());
						wmScoreTaskType5.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM6.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType5);
						WmScoreTaskType wmScoreTaskType6 = new WmScoreTaskType();
						wmScoreTaskType6.setScoreId(wmScorePackage.getId());
						wmScoreTaskType6.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM7.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType6);
						WmScoreTaskType wmScoreTaskType7 = new WmScoreTaskType();
						wmScoreTaskType7.setScoreId(wmScorePackage.getId());
						wmScoreTaskType7.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM8.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType7);
						WmScoreTaskType wmScoreTaskType8 = new WmScoreTaskType();
						wmScoreTaskType8.setScoreId(wmScorePackage.getId());
						wmScoreTaskType8.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM9.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType8);
						WmScoreTaskType wmScoreTaskType9 = new WmScoreTaskType();
						wmScoreTaskType9.setScoreId(wmScorePackage.getId());
						wmScoreTaskType9.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM10.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType9);
						WmScoreTaskType wmScoreTaskType10 = new WmScoreTaskType();
						wmScoreTaskType10.setScoreId(wmScorePackage.getId());
						wmScoreTaskType10.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM11.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType10);
						WmScoreTaskType wmScoreTaskType11 = new WmScoreTaskType();
						wmScoreTaskType11.setScoreId(wmScorePackage.getId());
						wmScoreTaskType11.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM12.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType11);
						WmScoreTaskType wmScoreTaskType12 = new WmScoreTaskType();
						wmScoreTaskType12.setScoreId(wmScorePackage.getId());
						wmScoreTaskType12.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM13.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType12);
						WmScoreTaskType wmScoreTaskType13 = new WmScoreTaskType();
						wmScoreTaskType13.setScoreId(wmScorePackage.getId());
						wmScoreTaskType13.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM14.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType13);
						WmScoreTaskType wmScoreTaskType14 = new WmScoreTaskType();
						wmScoreTaskType14.setScoreId(wmScorePackage.getId());
						wmScoreTaskType14.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM15.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType14);
						WmScoreTaskType wmScoreTaskType15 = new WmScoreTaskType();
						wmScoreTaskType15.setScoreId(wmScorePackage.getId());
						wmScoreTaskType15.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM16.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType15);
						WmScoreTaskType wmScoreTaskType16 = new WmScoreTaskType();
						wmScoreTaskType16.setScoreId(wmScorePackage.getId());
						wmScoreTaskType16.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM19.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType16);
						WmScoreTaskType wmScoreTaskType17 = new WmScoreTaskType();
						wmScoreTaskType17.setScoreId(wmScorePackage.getId());
						wmScoreTaskType17.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM17.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType17);
						WmScoreTaskType wmScoreTaskType18 = new WmScoreTaskType();
						wmScoreTaskType18.setScoreId(wmScorePackage.getId());
						wmScoreTaskType18.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM18.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType18);
						WmScoreTaskType wmScoreTaskType19 = new WmScoreTaskType();
						wmScoreTaskType19.setScoreId(wmScorePackage.getId());
						wmScoreTaskType19.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM20.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType19);
						WmScoreTaskType wmScoreTaskType20 = new WmScoreTaskType();
						wmScoreTaskType20.setScoreId(wmScorePackage.getId());
						wmScoreTaskType20.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM21.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType20);
						WmScoreTaskType wmScoreTaskType21 = new WmScoreTaskType();
						wmScoreTaskType21.setScoreId(wmScorePackage.getId());
						wmScoreTaskType21.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM22.getType());
						wmScoreTaskTypeMapper.insert(wmScoreTaskType21);
					} else if (wmScorePackage.getRelationScoreId() == null && wmScorePackage.getTaskTypeList()
							.size() > 0) {
						List<String> stringList = wmScorePackage.getTaskTypeList()
								.stream()
								.map(p -> p.getTaskTypeName())
								.collect(Collectors.toList());
						for (String name : stringList) {
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM1.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM1.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM2.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM2.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM3.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM3.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM4.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM4.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM5.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM5.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM6.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM6.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM7.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM7.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM8.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM8.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM9.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM9.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM10.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM10.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM11.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM11.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM12.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM12.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM13.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM13.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM14.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM14.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM15.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM15.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM16.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM16.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM19.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM19.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM17.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM17.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM18.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM18.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM20.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM20.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM21.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM21.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
							if (name.equals(WmScoreTaskTypeEnum.TASK_TYPE_ENUM22.getDescription())) {
								WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
								wmScoreTaskType.setScoreId(wmScorePackage.getId());
								wmScoreTaskType.setTaskTypeId(WmScoreTaskTypeEnum.TASK_TYPE_ENUM22.getType());
								wmScoreTaskTypeMapper.insert(wmScoreTaskType);
							}
						}
					}
				}
			}
			return R.ok(errorList);

		} catch (Exception e) {
			return R.failed("批量创建积分包失败");
		}
	}

	/**
	 * 获取当前部门的基于所有积分包的相关信息
	 *
	 * @return 积分包的相关信息
	 */
	@ApiOperation(value = "获取当前部门的基于所有积分包的相关信息", notes = "获取当前部门的基于所有积分包的相关信息")
	@GetMapping("/getDeptScorePackageInfo")
	public R getDeptScorePackageInfo() {
		Integer deptId = SecurityUtils.getUser().getDeptId();
		Map<String, Object> result = wmScorePackageService.getDeptScorePackageInfo(deptId);
		return R.ok(result);
	}

	/**
	 * 获取积分包关联企业的药品列表
	 *
	 * @param page      分页信息
	 * @param packageId 积分包id
	 * @param drugName  药品名称
	 * @return 药品列表
	 */
	@SysLog("获取积分包关联企业的药品列表")
	@GetMapping(value = "/drugs")
	public R listDrugsByPackageId(Page page, @RequestParam(value = "packageId") Integer packageId, @RequestParam(value = "name", required = false) String drugName) {
		return R.ok(wmScorePackageService.listDrugsByPackageId(page, packageId, drugName.trim()));
	}

}
