package xiaoqu.home.open.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.NumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import xiaoqu.home.open.aop.AvoidRepeatSubmit;
import xiaoqu.home.open.controller.export.TaskInfoUserUploadExport;
import xiaoqu.home.open.controller.input.task.GridAndSceneInput;
import xiaoqu.home.open.controller.input.task.TaskInfoUserUploadPageInput;
import xiaoqu.home.open.controller.input.task.TaskWageSet;
import xiaoqu.home.open.controller.output.task.TaskInfoUserUploadDetailOutput;
import xiaoqu.home.open.controller.output.task.TaskInfoUserUploadOutput;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.model.db2.FieldWebpageAuthority;
import xiaoqu.home.open.model.db2.TaskInfo;
import xiaoqu.home.open.model.db2.TaskPic;
import xiaoqu.home.open.model.db2.TaskScenes;
import xiaoqu.home.open.model.db2.TaskStatusLog;
import xiaoqu.home.open.model.db2.TaskUser;
import xiaoqu.home.open.model.db2.Wage;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.IdEntity;
import xiaoqu.home.open.publicBean.MessageNotification;
import xiaoqu.home.open.publicBean.PageOutput;
import xiaoqu.home.open.publicBean.task.TaskCancel;
import xiaoqu.home.open.publicBean.task.TaskPoint;
import xiaoqu.home.open.publicBean.task.TaskProgress;
import xiaoqu.home.open.service.RedisService4;
import xiaoqu.home.open.service.db1.UserService;
import xiaoqu.home.open.service.db2.FieldWebpageAuthorityService;
import xiaoqu.home.open.service.db2.TaskInfoService;
import xiaoqu.home.open.service.db2.TaskPicService;
import xiaoqu.home.open.service.db2.TaskScenesService;
import xiaoqu.home.open.service.db2.TaskStatusLogService;
import xiaoqu.home.open.service.db2.TaskUserService;
import xiaoqu.home.open.service.db2.WageService;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.StringUtil;
import xiaoqu.home.open.utils.UserUtils;
import xiaoqu.home.open.utils.WrapperUtils;
import xiaoqu.home.open.utils.poi.ExcelUtil;

@RestController
@RequestMapping(value = "/taskUserUpload")
@Api(value = "用户上报", description = "用户上报")
public class TaskUserUploadController {
	@Autowired
	private TaskInfoService taskInfoService;
	@Autowired
	private UserService userService;
	@Autowired
	private TaskUserService taskUserService;
	@Autowired
	private TaskScenesService taskScenesService;
	@Autowired
	private TaskStatusLogService taskStatusLogService;
	@Autowired
	private FieldWebpageAuthorityService fieldWebpageAuthorityService;
	@Autowired
	private WageService wageService;

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "/pageList", method = RequestMethod.POST)
	public BaseOutput<PageOutput<TaskInfoUserUploadOutput>> pageList(HttpServletRequest request, @RequestBody TaskInfoUserUploadPageInput input) {
		PageOutput<TaskInfoUserUploadOutput> output = new PageOutput<TaskInfoUserUploadOutput>();
		Long districtId = input.getDistrictId();
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
		wrapper.ge("status", 1);// 条件限定
		if (input.getTaskType() == null) {
			wrapper.in("task_type", 3, 4);
		}
		if (StringUtil.isNotEmpty(input.getGridAndScenes())) {
			Set<Long> taskIds = GetQueryCiteria(input);
			if (taskIds.size() == 0) {
				return ResultResponseBeanUtil.success(output);
			} else {
				wrapper.in("id", taskIds);
			}
			input.setGridAndScenes(null);
		}
		if (StringUtil.isNotEmpty(input.getMatterNames())) {
			Set<Long> taskIds = QueryMaterCiteria(input);
			if (taskIds.size() == 0) {
				return ResultResponseBeanUtil.success(output);
			} else {
				wrapper.in("id", taskIds);
			}
			input.setMatterNames(null);
		}

		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
//		User user = UserUtils.getUser(request);
//		List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getFieldList(input.getWebId(), user.getPropertyAuthorityId());
//		String[] columns = WrapperUtils.entityToColumns(fieldList);
//		wrapper.select(columns);
		Page<TaskInfo> pageList = new Page<TaskInfo>(input.getPage(), input.getRows());
		List<TaskInfo> entityList = taskInfoService.page(pageList, wrapper).getRecords();
		List<TaskInfoUserUploadOutput> outList = new ArrayList<TaskInfoUserUploadOutput>();
		for (TaskInfo bean : entityList) {
			TaskInfoUserUploadOutput outputBean = new TaskInfoUserUploadOutput();
			BeanUtils.copyProperties(bean, outputBean);
			// 查下上报人手机号
			if (bean.getCreateId() != null && bean.getCreateId() != 0) {
				User tUser = userService.getById(bean.getCreateId());
				outputBean.setCreateNameMobile(tUser.getMobile());
			}
			outList.add(outputBean);
		}
		output.setPage(pageList.getCurrent());
		output.setRecords(pageList.getTotal());
		output.setTotal(pageList.getPages());
		output.setRows(outList);
		return ResultResponseBeanUtil.success(output);
	}

	/********
	 * 查询列表和导出统一处理查询条件
	 * 
	 * @param input
	 * @return
	 */
	private Set<Long> QueryMaterCiteria(TaskInfoUserUploadPageInput input) {
		Set<Long> taskIds = new HashSet<Long>();
		List<String> matterNames = input.getMatterNames();
		for (String matterName : matterNames) {
			QueryWrapper<TaskInfo> wrapperTaskScenes = WrapperUtils.getWrapper(false);
			wrapperTaskScenes.like("name", matterName);
			wrapperTaskScenes.eq("district_id", input.getDistrictId());
			if (StringUtil.isNotEmpty(input.getCreateTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getCreateTimeStart());
				wrapperTaskScenes.le("create_time", input.getCreateTimeEnd());
			}
			if (StringUtil.isNotEmpty(input.getSendTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getSendTimeStart());
				wrapperTaskScenes.le("create_time", input.getSendTimeEnd());
			}
			if (StringUtil.isNotEmpty(input.getConfirmTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getConfirmTimeStart());
				wrapperTaskScenes.le("create_time", input.getConfirmTimeEnd());
			}
			if (StringUtil.isNotEmpty(input.getActionTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getActionTimeStart());
				wrapperTaskScenes.le("create_time", input.getActionTimeEnd());
			}
			if (StringUtil.isNotEmpty(input.getFinishTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getFinishTimeStart());
				wrapperTaskScenes.le("create_time", input.getFinishTimeEnd());
			}
			List<TaskInfo> taskScenes = taskInfoService.list(wrapperTaskScenes);
			taskIds.addAll(taskScenes.stream().map(taskScen -> taskScen.getId()).collect(Collectors.toSet()));
		}
		return taskIds;
	}

	private Set<Long> GetQueryCiteria(TaskInfoUserUploadPageInput input) {
		Set<Long> taskIds = new HashSet<Long>();
		List<GridAndSceneInput> gridAndScenes = input.getGridAndScenes();
		for (GridAndSceneInput gridAndSceneInput : gridAndScenes) {
			QueryWrapper<TaskScenes> wrapperTaskScenes = WrapperUtils.getWrapper(false);
			wrapperTaskScenes.eq("district_id", input.getDistrictId());
//				@ApiModelProperty("网格或场景级别")
			Integer level = gridAndSceneInput.getLevel();
//				@ApiModelProperty(value = "网格ID")
			Long gridId = gridAndSceneInput.getGridId();
//				@ApiModelProperty(value = "场景ID")
			Long sceneId = gridAndSceneInput.getSceneId();
//				@ApiModelProperty(value = "是否场景，0:否;1:是")
			Integer isscene = gridAndSceneInput.getIsscene();
			if (isscene == 0) {
//				    @TableField("building_id")
//				    @TableField("floor_id")
//				    @TableField("multistage_grid_id")
				switch (level) {
				case 1:
					wrapperTaskScenes.eq("building_id", gridId);
					break;
				case 2:
					wrapperTaskScenes.eq("floor_id", gridId);
					break;
				default:
					wrapperTaskScenes.eq("multistage_grid_id", gridId);
					break;
				}
				wrapperTaskScenes.eq("grid_level", level);
			} else {
				switch (level) {
				case 1:
					wrapperTaskScenes.eq("scenes_id", sceneId);
					break;
				case 2:
					wrapperTaskScenes.eq("scenes_id", sceneId);
					break;
				}
				wrapperTaskScenes.eq("scenes_level", level);
			}
//			@ApiModelProperty(value = "任务生成时间")
//			private String createTimeStart;
			if (StringUtil.isNotEmpty(input.getCreateTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getCreateTimeStart());
				wrapperTaskScenes.le("create_time", input.getCreateTimeEnd());
			}
//				@ApiModelProperty(value = "任务派发时间")
//				private String sendTimeStart;
			if (StringUtil.isNotEmpty(input.getSendTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getSendTimeStart());
				wrapperTaskScenes.le("create_time", input.getSendTimeEnd());
			}
//				@ApiModelProperty(value = "任务响应时间")
//				private String confirmTimeStart;
			if (StringUtil.isNotEmpty(input.getConfirmTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getConfirmTimeStart());
				wrapperTaskScenes.le("create_time", input.getConfirmTimeEnd());
			}
//				@ApiModelProperty(value = "任务作业时间")
//				private String actionTimeStart;
			if (StringUtil.isNotEmpty(input.getActionTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getActionTimeStart());
				wrapperTaskScenes.le("create_time", input.getActionTimeEnd());
			}
//				@ApiModelProperty(value = "任务完成时间")
//				private String finishTimeStart;
			if (StringUtil.isNotEmpty(input.getFinishTimeStart())) {
				wrapperTaskScenes.ge("create_time", input.getFinishTimeStart());
				wrapperTaskScenes.le("create_time", input.getFinishTimeEnd());
			}
			List<TaskScenes> taskScenes = taskScenesService.list(wrapperTaskScenes);
			taskIds.addAll(taskScenes.stream().map(taskScen -> taskScen.getTaskId()).collect(Collectors.toSet()));
		}
		return taskIds;
	}

	@ApiOperation(value = "任务导出")
	@PostMapping("/export")
	public void export(HttpServletRequest request, HttpServletResponse response, @RequestBody TaskInfoUserUploadPageInput input) throws IOException {
		if (StringUtil.isNull(input.getDistrictId())) {
			return;
		}
		List<TaskInfoUserUploadExport> outList = new ArrayList<TaskInfoUserUploadExport>();
		User user = UserUtils.getUser(request);
		QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
		if (input.getTaskType() == null) {
			wrapper.in("task_type", 3, 4);
		}
		if (StringUtil.isNotEmpty(input.getGridAndScenes())) {
			Set<Long> taskIds = GetQueryCiteria(input);
			if (taskIds.size() == 0) {
				return;
			} else {
				wrapper.in("id", taskIds);
			}
			input.setGridAndScenes(null);
		}
		if (StringUtil.isNotEmpty(input.getMatterNames())) {
			Set<Long> taskIds = QueryMaterCiteria(input);
			if (taskIds.size() == 0) {
				return;
			} else {
				wrapper.in("id", taskIds);
			}
			input.setMatterNames(null);
		}
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		List<TaskInfo> taskInfoList = taskInfoService.list(wrapper);
		Map<Long, String> wageToName = new HashMap<Long, String>();
		QueryWrapper<Wage> wagWrapper = WrapperUtils.getWrapper(false);
		wagWrapper.eq("district_id", input.getDistrictId());
		List<Wage> wages = wageService.list(wagWrapper);
		if (wages.size() > 0) {
			for (Wage wage : wages) {
				wageToName.put(wage.getId(), wage.getName());
			}
		}
		for (TaskInfo taskInfo : taskInfoList) {
			TaskInfoUserUploadExport taskInfoExport = new TaskInfoUserUploadExport();
			BeanUtils.copyProperties(taskInfo, taskInfoExport);
			// 查下上报人手机号
			if (taskInfo.getCreateId() != null && taskInfo.getCreateId() != 0) {
				User tUser = userService.getById(taskInfo.getCreateId());
				taskInfoExport.setCreateNameMobile(tUser.getMobile());
			}
			if (taskInfo.getWageId() != 0 && wageToName.size() > 0) {
				taskInfoExport.setWageType(wageToName.get(taskInfo.getWageId()));
			}
			outList.add(taskInfoExport);
		}

		List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getPropertyAuthorityId());
		String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
		ExcelUtil<TaskInfoUserUploadExport> util = new ExcelUtil<TaskInfoUserUploadExport>(TaskInfoUserUploadExport.class);
		util.exportExcelByColumns(response, outList, "任务明细", exportColumns);
	}

	@Autowired
	private RedisService4 redisService4;
	@Autowired
	private RedisTemplate<String, String> redisTemplate5;

	@ApiOperation(value = "时薪设置")
	@AvoidRepeatSubmit
	@RequestMapping(value = "/wageSet", method = RequestMethod.POST)
	public BaseOutput<String> wageSet(HttpServletRequest request, @RequestBody TaskWageSet input) {
		if (StringUtil.isNull(input.getId())) {
			return ResultResponseBeanUtil.fail("请输入任务ID");
		}
		TaskInfo taskInfo = taskInfoService.getById(input.getId());
		if (StringUtil.isNull(taskInfo)) {
			return ResultResponseBeanUtil.fail("任务不存在");
		}
		Wage wage = wageService.getById(input.getWageId());
		taskInfo.setWageId(input.getWageId());
		taskInfo.setWageName(wage.getName());
		taskInfo.setWageHour(input.getWageHour());
		// 计算积分
		taskInfo.setPoint(NumberUtil.round(NumberUtil.mul(new BigDecimal(input.getWageHour() + ""), new BigDecimal(wage.getPoint() + "")), 2).doubleValue());
		taskInfo.setSalary(NumberUtil.round(NumberUtil.mul(new BigDecimal(input.getWageHour() + ""), new BigDecimal(wage.getAmount() + "")), 2).doubleValue());
		taskInfoService.updateById(taskInfo);
		if (taskInfo.getStatus().intValue()==5 || taskInfo.getStatus().intValue()==6) {
			QueryWrapper<TaskUser> qWrapper = WrapperUtils.getWrapper(false);
			qWrapper.eq("task_id", input.getId());
			List<TaskUser> tempList = taskUserService.list(qWrapper);
			int userSize = tempList.size();
			for (TaskUser taskUser : tempList) {
				double point = NumberUtil.div(new BigDecimal(taskInfo.getPoint() + ""), new BigDecimal(userSize + ""), 2).doubleValue();
				double salary = NumberUtil.div(new BigDecimal(taskInfo.getSalary() + ""), new BigDecimal(userSize + ""), 2).doubleValue();
				taskUser.setPoint(point);
				taskUser.setSalary(salary);
				taskUserService.updateById(taskUser);
			}
		}
		return ResultResponseBeanUtil.success("设置成功");

	}

	@ApiOperation(value = "取消任务")
	@AvoidRepeatSubmit
	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
	public BaseOutput<String> cancel(HttpServletRequest request, @RequestBody TaskCancel input) {
		User user = UserUtils.getUser(request);
		if (StringUtil.isNull(input.getId())) {
			return ResultResponseBeanUtil.fail("请输入任务ID");
		}
		TaskInfo taskInfo = taskInfoService.getById(input.getId());
		if (StringUtil.isNull(taskInfo)) {
			return ResultResponseBeanUtil.fail("任务不存在");
		}
//		待抢单 已接单 作业中
//		任务状态(0:已生成;1:待派发;2:待抢单;3:已接单;4:作业中;5:按时完成;6:超时完成;7:人工取消;8:系统取消;9:未完成)")
//		待派发 待抢单 已接单  作业中
		if (taskInfo.getStatus() == 1 || taskInfo.getStatus() == 2 || taskInfo.getStatus() == 3 || taskInfo.getStatus() == 4) {
			taskInfo.setStatus(7);
			taskInfo.setModifyTime(new Date());
			taskInfoService.updateById(taskInfo);
			TaskStatusLog taskStatusLog = new TaskStatusLog();
			BeanUtils.copyProperties(taskInfo, taskStatusLog, "id");
			taskStatusLog.setCreateTime(new Date());
			taskStatusLog.setTaskId(taskInfo.getId());
			taskStatusLog.setTaskStatus(7);
			taskStatusLog.setCancelReason(input.getCancelReason());
			taskStatusLog.setDetailReason(input.getDetailReason());
			taskStatusLog.setContent(user.getName() + " " + user.getMobile());
			taskStatusLogService.save(taskStatusLog);
			// 同时取消taskuser表的数据
			UpdateWrapper<TaskUser> taskUserWrapper = WrapperUtils.getUpdateWrapper(false);
			taskUserWrapper.eq("task_id", input.getId());
			taskUserWrapper.set("status", 7);
			taskUserWrapper.set("modify_time", new Date());
			taskUserService.update(taskUserWrapper);
			// 个推推送
			MessageNotification messageNotification = new MessageNotification();
			messageNotification.setDistrictId(taskInfo.getDistrictId());
			messageNotification.setStatus(taskInfo.getStatus());
			messageNotification.setTaskId(taskInfo.getId());
			messageNotification.setTaskName(taskInfo.getName());
			redisService4.lPush("taskPushQueue", JSON.toJSONString(messageNotification));
			/*** 通知匹配算法重新算 ***/
			redisTemplate5.opsForSet().add("RepeatTaskQueue", taskInfo.getDistrictId() + "");
			/****** 任务结束通知 ******/
			redisTemplate5.opsForList().leftPush("FinishTaskQueue", taskInfo.getId() + "");
			// 修改人员状态
			QueryWrapper<TaskUser> qWrapper = WrapperUtils.getWrapper(false);
			qWrapper.eq("task_id", input.getId());
			List<TaskUser> tempList = taskUserService.list(qWrapper);
			if (tempList.size() > 0) {
				for (TaskUser taskUser : tempList) {
					if (taskUser.getUserId() != null && taskUser.getUserId().intValue() != 0) {
						User temUser = userService.getById(taskUser.getUserId());
						if (temUser.getWorkAtStatus() != 3) {// 工作中状态0:作业;1:转场;2:空闲;3:休息
							// 查询有没有待完成的任务
							QueryWrapper<TaskUser> taskUserQuery = WrapperUtils.getWrapper(false);
							taskUserQuery.eq("status", 4);// 3:已接单;4:作业中;
							taskUserQuery.eq("user_id", temUser.getId());
							int countTemp = taskUserService.count(taskUserQuery);
							if (countTemp > 0) {
								temUser.setWorkAtStatus(0);// 工作中状态0:作业;1:转场;2:空闲;3:休息
								temUser.setModifyTime(new Date());
								userService.updateById(temUser);
							} else {
								QueryWrapper<TaskUser> taskUserQuery1 = WrapperUtils.getWrapper(false);
								taskUserQuery1.eq("status", 3);// 3:已接单;4:作业中;
								taskUserQuery1.eq("user_id", temUser.getId());
								countTemp = taskUserService.count(taskUserQuery1);
								if (countTemp > 0) {
									temUser.setWorkAtStatus(1);// 工作中状态0:作业;1:转场;2:空闲;3:休息
									temUser.setModifyTime(new Date());
									userService.updateById(temUser);
								} else {
									temUser.setWorkAtStatus(2);// 工作中状态0:作业;1:转场;2:空闲;3:休息
									temUser.setModifyTime(new Date());
									userService.updateById(temUser);
								}
							}
						}
					}

				}

			}
			return ResultResponseBeanUtil.success("取消成功");
		} else {
			return ResultResponseBeanUtil.fail("当前状态不支持此操作");
		}
	}

	@ApiOperation(value = "删除任务")
	@AvoidRepeatSubmit
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public BaseOutput<String> delete(HttpServletRequest request, @RequestBody IdEntity input) {
		User user = UserUtils.getUser(request);
		if (StringUtil.isNull(input.getId())) {
			return ResultResponseBeanUtil.fail("请输入任务ID");
		}
		TaskInfo taskInfo = taskInfoService.getById(input.getId());
		if (StringUtil.isNull(taskInfo)) {
			return ResultResponseBeanUtil.fail("任务不存在");
		}
//		任务状态(7:人工取消)")
		if (taskInfo.getStatus() == 7 || taskInfo.getStatus() == 1) {
			taskInfo.setSoftDelete(1);
			taskInfo.setModifyTime(new Date());
			taskInfoService.updateById(taskInfo);
			TaskStatusLog taskStatusLog = new TaskStatusLog();
			BeanUtils.copyProperties(taskInfo, taskStatusLog, "id");
			taskStatusLog.setCreateTime(new Date());
			taskStatusLog.setTaskId(taskInfo.getId());
			taskStatusLog.setTaskStatus(99);
			taskStatusLog.setContent(user.getName() + " " + user.getMobile());
			taskStatusLogService.save(taskStatusLog);
			// 同时取消taskuser表的数据
			UpdateWrapper<TaskUser> taskUserWrapper = WrapperUtils.getUpdateWrapper(false);
			taskUserWrapper.eq("task_id", input.getId());
			taskUserWrapper.set("soft_delete", 1);
			taskUserWrapper.set("modify_time", new Date());
			taskUserService.update(taskUserWrapper);

			/*** 通知匹配算法重新算 ***/
			redisTemplate5.opsForSet().add("RepeatTaskQueue", taskInfo.getDistrictId() + "");
			/****** 任务结束通知 ******/
			redisTemplate5.opsForList().leftPush("FinishTaskQueue", taskInfo.getId() + "");
			// 修改人员状态
			QueryWrapper<TaskUser> qWrapper = WrapperUtils.getWrapper(false);
			qWrapper.eq("task_id", input.getId());
			List<TaskUser> tempList = taskUserService.list(qWrapper);
			if (tempList.size() > 0) {
				for (TaskUser taskUser : tempList) {
					if (taskUser.getUserId() != null && taskUser.getUserId().intValue() != 0) {
						User temUser = userService.getById(taskUser.getUserId());
						if (temUser.getWorkAtStatus() != 3) {// 工作中状态0:作业;1:转场;2:空闲;3:休息
							// 查询有没有待完成的任务
							QueryWrapper<TaskUser> taskUserQuery = WrapperUtils.getWrapper(false);
							taskUserQuery.eq("status", 4);// 3:已接单;4:作业中;
							taskUserQuery.eq("user_id", temUser.getId());
							int countTemp = taskUserService.count(taskUserQuery);
							if (countTemp > 0) {
								temUser.setWorkAtStatus(0);// 工作中状态0:作业;1:转场;2:空闲;3:休息
								temUser.setModifyTime(new Date());
								userService.updateById(temUser);
							} else {
								QueryWrapper<TaskUser> taskUserQuery1 = WrapperUtils.getWrapper(false);
								taskUserQuery1.eq("status", 3);// 3:已接单;4:作业中;
								taskUserQuery1.eq("user_id", temUser.getId());
								countTemp = taskUserService.count(taskUserQuery1);
								if (countTemp > 0) {
									temUser.setWorkAtStatus(1);// 工作中状态0:作业;1:转场;2:空闲;3:休息
									temUser.setModifyTime(new Date());
									userService.updateById(temUser);
								} else {
									temUser.setWorkAtStatus(2);// 工作中状态0:作业;1:转场;2:空闲;3:休息
									temUser.setModifyTime(new Date());
									userService.updateById(temUser);
								}
							}
						}
					}

				}

			}
			return ResultResponseBeanUtil.success("取消成功");
		} else {
			return ResultResponseBeanUtil.fail("当前状态不支持此操作");
		}
	}

	@Autowired
	TaskPicService taskPicService;

	@ApiOperation(value = "查看用户上报任务")
	@AvoidRepeatSubmit
	@RequestMapping(value = "/look", method = RequestMethod.POST)
	public BaseOutput<TaskInfoUserUploadDetailOutput> look(HttpServletRequest request, @RequestBody IdEntity input) {
		TaskInfoUserUploadDetailOutput taskInfoDetailOutput = new TaskInfoUserUploadDetailOutput();
		if (StringUtil.isNull(input.getId())) {
			return ResultResponseBeanUtil.fail("请输入任务ID");
		}
		TaskInfo taskInfo = taskInfoService.getById(input.getId());
		if (StringUtil.isNull(taskInfo)) {
			return ResultResponseBeanUtil.fail("任务不存在");
		}
		BeanUtils.copyProperties(taskInfo, taskInfoDetailOutput);
		// 查下上报人手机号
		if (taskInfo.getCreateId() != null && taskInfo.getCreateId() != 0) {
			User tUser = userService.getById(taskInfo.getCreateId());
			taskInfoDetailOutput.setCreateNameMobile(tUser.getMobile());
		}
		// 查图片
		QueryWrapper<TaskPic> queryWrapper1 = WrapperUtils.getWrapper(false);
		queryWrapper1.eq("task_id", taskInfo.getId());
		queryWrapper1.eq("task_status", 0);
		taskInfoDetailOutput.setUploadPic(taskPicService.list(queryWrapper1));
		// 查语音
		QueryWrapper<TaskPic> queryWrapper2 = WrapperUtils.getWrapper(false);
		queryWrapper2.eq("task_id", taskInfo.getId());
		queryWrapper2.eq("task_status", 11);
		taskInfoDetailOutput.setVoice(taskPicService.list(queryWrapper2));
//		任务进度
		QueryWrapper<TaskStatusLog> taskStatusLogWrapper = WrapperUtils.getWrapper(false);
		taskStatusLogWrapper.eq("task_id", taskInfo.getId());
		if (taskInfo.getTaskType() == 3 || taskInfo.getTaskType() == 4) {
			taskStatusLogWrapper.ge("task_status", 0);
			taskStatusLogWrapper.ne("task_status", 2);
		}
		taskStatusLogWrapper.orderByAsc("task_status");
		List<TaskStatusLog> taskStatusLogs = taskStatusLogService.list(taskStatusLogWrapper);
		if (StringUtil.isNotEmpty(taskStatusLogs)) {
			List<TaskProgress> taskProgresss = new ArrayList<TaskProgress>();

			for (TaskStatusLog taskStatusLog : taskStatusLogs) {
				TaskProgress taskProgress = new TaskProgress();
				BeanUtils.copyProperties(taskStatusLog, taskProgress);
				// 数据脱敏处理
				if (taskInfo.getTaskType() == 3 || taskInfo.getTaskType() == 4) {
					String numbers = getPhoneNum(taskProgress.getContent());
					if (!numbers.equals("")) {
						String[] mobiles = numbers.split(",");
						for (String mobile : mobiles) {
							String temNum = DesensitizedUtil.mobilePhone(mobile);
							taskProgress.setContent(taskProgress.getContent().replace(mobile, temNum));
						}
					}
				}
				if (taskStatusLog.getTaskStatus() == 10 || taskStatusLog.getTaskStatus() == 5) {// 按时完成 上传记录 查下图片
					// 查图片
					QueryWrapper<TaskPic> queryWrapper3 = WrapperUtils.getWrapper(false);
					queryWrapper3.eq("task_id", taskInfo.getId());
					queryWrapper3.eq("task_status", taskStatusLog.getTaskStatus());
					queryWrapper3.eq("task_status_log_id", taskStatusLog.getId());
					taskProgress.setUploadPic(taskPicService.list(queryWrapper3));
				}
				taskProgresss.add(taskProgress);
			}
			taskInfoDetailOutput.setTaskProgresss(taskProgresss);
		}
//		任务积分
		TaskPoint taskPoint = new TaskPoint();
		if (taskInfo.getPoint()>0) {
			if (taskInfo.getFinishTime() != null) {
				if (taskInfo.getStatus() == 5 || taskInfo.getStatus() == 6) {
					taskPoint.setCreateTime(taskInfo.getFinishTime());
					taskPoint.setPoint(taskInfo.getPoint());
					taskInfoDetailOutput.setTaskPoint(taskPoint);
				}
			}else {
				taskPoint.setCreateTime(null);
				taskPoint.setPoint(taskInfo.getPoint());
				taskInfoDetailOutput.setTaskPoint(taskPoint);
			}
		}
		return ResultResponseBeanUtil.success(taskInfoDetailOutput);
	}

	// 提取文本手机号
	public static String getPhoneNum(String text) {
		String regex = "[\\d]{11}";
		Pattern compile = Pattern.compile(regex);
		Matcher matcher = compile.matcher(text);
		String result = "";
		while (matcher.find()) {
			result += "," + matcher.group();
		}
		return result.replaceFirst(",", "");
	}

//	@ApiOperation(value = "统计查询")
//	@RequestMapping(value = "/statistics", method = RequestMethod.POST)
//	public BaseOutput<TaskInfoStatisticsOutput> statistics(HttpServletRequest request, @RequestBody TaskInfoPageInput input) {
//		Long districtId = input.getDistrictId();
//		if (StringUtil.isNull(districtId)) {
//			return ResultResponseBeanUtil.fail("请选择站点");
//		}
//		TaskInfoStatisticsOutput taskInfoStatisticsOutput = new TaskInfoStatisticsOutput();
//		QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
//		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
////		@ApiModelProperty(value = "任务总数")
//		int allTask = taskInfoService.count(wrapper);
//		taskInfoStatisticsOutput.setAllTask(allTask);
////		@ApiModelProperty(value = "待抢单任务数")
//		int waitFortask = 0;
////		@ApiModelProperty(value = "已接单任务数")
//		int acceptTask = 0;
////		@ApiModelProperty(value = "作业中任务数")
//		int operationTask = 0;
////		@ApiModelProperty(value = "按时完成任务数")
//		int onTimeTask = 0;
////		@ApiModelProperty(value = "超时完成任务数")
//		int onTimeOutTask = 0;
////		@ApiModelProperty(value = "未完成任务数")
//		int incompleteTask = 0;
////		@ApiModelProperty(value = "人工取消任务数")
//		int manualCancelTask = 0;
////		@ApiModelProperty(value = "系统取消任务数")
//		int systemCancelTask = 0;
//		return ResultResponseBeanUtil.success(taskInfoStatisticsOutput);
//	}

}
