package org.linlinjava.jz.admin.service;

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

import org.linlinjava.jz.admin.vo.AssignTaskDto;
import org.linlinjava.jz.admin.vo.BuiltTaskDto;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.db.domain.JzBuiltList;
import org.linlinjava.jz.db.service.JzBuiltListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AdminBuiltListService {

	@Autowired
	private JzBuiltListService builtListService;

	public Object assignTasks(AssignTaskDto assignTaskDto) {
		List<BuiltTaskDto> tempList = new ArrayList<BuiltTaskDto>();// 剩下的人平均分
		tempList.addAll(assignTaskDto.getBuiltTaskList());
		
		// 总任务数量，随着分配会减少
		List<JzBuiltList> builtListList = builtListService.selectByUserId(assignTaskDto.getUserId(),assignTaskDto.getCampaignId());

		// 验证总任务数有没有超过
		int ct = 0;// 手动分配数量
		for (BuiltTaskDto t : assignTaskDto.getBuiltTaskList()) {
			ct = ct + t.getAssignNumber();
		}
		if (ct > builtListList.size()) {
			return ResponseUtil.fail(101, "手动分配的任务数量超过已有总数");
		}

		// 有手动分配
		if (ct > 0) {
			int remain = builtListList.size() - ct;// 剩下的

			// 都分完了，执行手动分配的
			if (remain == 0) {
				// 执行手动分配的
				distributionInput(assignTaskDto, tempList, builtListList);
			}

			// 还有没分完的, 分了的保留不变,余下的平均分
			if (remain > 0) {
				// 执行手动分配的
				distributionInput(assignTaskDto, tempList, builtListList);
				
				// 余下的平均分
				if(tempList.size()>0) {
					distributionAvg(tempList, builtListList);
				}
			}
			return ResponseUtil.ok();
		}

		// 没有手动分配，自动平均分
		if (ct == 0) {
			// 自动平均分
			if(tempList.size()>0) {
				distributionAvg(tempList, builtListList);
			}
		}

		return ResponseUtil.ok();
	}

	/**
	 * 手动分配的任务数量
	 * 
	 * @param assignTaskDto
	 * @param tempList
	 * @param builtListList
	 */
	private void distributionInput(AssignTaskDto assignTaskDto, List<BuiltTaskDto> tempList,
			List<JzBuiltList> builtListList) {
		for (BuiltTaskDto t : assignTaskDto.getBuiltTaskList()) {
			// 分了的保留不变
			if (t.getAssignNumber() > 0) {
				tempList.remove(t);
				List<JzBuiltList> numberValue = getNumberValue(builtListList, t.getAssignNumber());
				for (JzBuiltList b : numberValue) {
					b.setUserId(t.getUserId());
				}
				if(numberValue.size()>0) {
					builtListService.runUpdateBuiltBatch(numberValue);
				}
			}

		}
	}

	/**
	 * 系统自动平均分配的任务
	 * 
	 * @param builtTaskList //剩下分配的人的信息
	 * @param builtListList 剩余的任务量
	 * 
	 */
	private void distributionAvg(List<BuiltTaskDto> builtTaskList, List<JzBuiltList> builtListList) {
		// 余下的平均分; 没有手动分配，也算余下的平均分
		int avg = builtListList.size() / builtTaskList.size();// 平均分多少
		int rem = builtListList.size() % builtTaskList.size();// 余下

		int kf = 1;//余下的处理
		for (BuiltTaskDto t : builtTaskList) {
			if (rem <= 0) {
				kf = 0;
			}
			rem--;// 负数不管
			List<JzBuiltList> numberValue = getNumberValue(builtListList, avg + kf);
			for (JzBuiltList b : numberValue) {
				b.setUserId(t.getUserId());
			}
			if(numberValue.size()>0) {
				builtListService.runUpdateBuiltBatch(numberValue);
			}
		}
	}

	/**
	 * 获取相应数量的值
	 * 
	 * @param list
	 * @param number
	 * @return
	 */
	public List<JzBuiltList> getNumberValue(List<JzBuiltList> list, int number) {
		List<JzBuiltList> tempList = new ArrayList<JzBuiltList>();

		for (JzBuiltList b : list) {
			tempList.add(b);
			number--;
			if (number == 0) {
				break;
			}
		}

		list.removeAll(tempList);
		return tempList;
	}

}
