package org.security.trans.app.service.impl;

import lombok.AllArgsConstructor;
import org.security.common.Constant;
import org.security.common.eum.TaskBusinessTypeEnum;
import org.security.trans.app.service.IAppService;
import org.security.trans.app.vo.BacklogVO;
import org.security.trans.basic.service.IDeviceService;
import org.security.trans.dailyDuties.service.IDeviceInspectionLineService;
import org.security.trans.dailyDuties.service.IDeviceInspectionService;
import org.security.trans.task.service.ITaskHandinService;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Create by wzf
 * @date 2022/4/20 9:52
 */
@AllArgsConstructor
@Service
public class AppServiceImpl implements IAppService {
	private final ITaskHandinService taskHandinService;
	private final IDeviceInspectionService deviceInspectionService;
	private final IDeviceInspectionLineService deviceInspectionLineService;
	private final IDeviceService deviceService;

	@Override
	public List<BacklogVO> backlog(String type,String dataType) {
		BladeUser user = AuthUtil.getUser();
		//模块标识符
		String moduleType = "";
		//设备待办状态值list
		ArrayList<String> list = new ArrayList<>();
		//获取教育培训待办信息
		if(Constant.APP_BACKLOG_TYPE_1.equals(dataType)){
			moduleType = "edu";
			List<BacklogVO> taskHandinBacklog = taskHandinService.getAppBacklog(user,moduleType);
			return setBacklogVOS(type, taskHandinBacklog);
		}
		//获取应急演练待办信息
		if(Constant.APP_BACKLOG_TYPE_2.equals(dataType)){
			moduleType = "drill";
			List<BacklogVO> taskHandinBacklog = taskHandinService.getAppBacklog(user,moduleType);
			return setBacklogVOS(type, taskHandinBacklog);
		}
		//获取设备检查待办信息
		if (Constant.APP_BACKLOG_TYPE_3.equals(dataType)){
			List<BacklogVO> deviceCheckBacklogs = getDeviceCheckData(user, list);
			return setBacklogVOS(type, deviceCheckBacklogs);
		}
		//获取设备整改待办信息
		if (Constant.APP_BACKLOG_TYPE_4.equals(dataType)){
			List<BacklogVO> deviceCheckBacklogs = getDeviceRectifyData(user, list);
			return setBacklogVOS(type, deviceCheckBacklogs);
		}
		//获取设备持续整改代办信息
		if (Constant.APP_BACKLOG_TYPE_5.equals(dataType)){
			List<BacklogVO> deviceContinuedBacklogs = getDeviceContinuedRectifyData(user);
			return setBacklogVOS(type, deviceContinuedBacklogs);
		}
		//获取所有待办数据
		if (!Func.isNotBlank(dataType)){
			List<BacklogVO> taskHandInBacklog = getAllBacklogData(user, moduleType, list);
			return setBacklogVOS(type, taskHandInBacklog);
		}
		return null;
	}

	/**
	 *获取设备持续整改信息
	 * @return
	 */
	private List<BacklogVO> getDeviceContinuedRectifyData(BladeUser user) {
		List<BacklogVO> deviceContinuedData = deviceInspectionLineService.getData(user,Constant.DEVICE_STATE_RECTIFY,Constant.PROBLEM_STATE_CONTINUED);
		if (Func.isNotEmpty(deviceContinuedData)){
			deviceContinuedData.forEach(item ->{
				item.setDeviceNum(deviceService.getById(item.getDeviceId()).getDeviceNum());
				item.setBusinessType("device_continued_rectify");
				item.setModule("deviceContinuedRectify");
			});
		}
		return deviceContinuedData;
	}

	/**
	 *获取app端设备整改待办数据
	 */
	private List<BacklogVO> getDeviceRectifyData(BladeUser user, ArrayList<String> list) {
		list.add(Constant.DEVICE_STATE_RECTIFY);
		List<BacklogVO> deviceCheckBacklogs = deviceInspectionService.getAppBacklog(user,list);
		if (Func.isNotEmpty(deviceCheckBacklogs)){
			deviceCheckBacklogs.forEach(item->{
				item.setDeviceNum(deviceService.getById(item.getDeviceId()).getDeviceNum());
				item.setBusinessType("device_rectify");
				item.setModule("deviceRectify");
			});
		}
		return deviceCheckBacklogs;
	}

	/**
	 *获取app端所有待办数据
	 */
	private List<BacklogVO> getAllBacklogData(BladeUser user, String moduleType, ArrayList<String> list) {
		list.add(Constant.DEVICE_STATE_INSPECTION);
		list.add(Constant.DEVICE_STATE_OVERTIME_INSPECTION);
		list.add(Constant.DEVICE_STATE_RECTIFY);
//		List<BacklogVO> taskHandInBacklog = taskHandinService.getAppBacklog(user, moduleType);
		List<BacklogVO> deviceCheckBacklogs = deviceInspectionService.getAppBacklog(user, list);
		if (Func.isNotEmpty(deviceCheckBacklogs)){
			deviceCheckBacklogs.forEach(item->{
				item.setDeviceNum(deviceService.getById(item.getDeviceId()).getDeviceNum());
				if (Constant.DEVICE_STATE_INSPECTION.equals(item.getStatus()) || Constant.DEVICE_STATE_OVERTIME_INSPECTION.equals(item.getStatus())){
					item.setBusinessType("device_check");
					item.setModule("deviceCheck");
				}
				if (Constant.DEVICE_STATE_RECTIFY.equals(item.getStatus())){
					item.setBusinessType("device_rectify");
					item.setModule("deviceRectify");
				}
			});
		}
		return deviceCheckBacklogs;
	}

	/**
	 *获取设备检查待办数据
	 */
	private List<BacklogVO> getDeviceCheckData(BladeUser user, ArrayList<String> list) {
		list.add(Constant.DEVICE_STATE_INSPECTION);
		list.add(Constant.DEVICE_STATE_OVERTIME_INSPECTION);
		List<BacklogVO> deviceCheckBacklogs = deviceInspectionService.getAppBacklog(user, list);
		if (Func.isNotEmpty(deviceCheckBacklogs)){
			deviceCheckBacklogs.forEach(item->{
				item.setDeviceNum(deviceService.getById(item.getDeviceId()).getDeviceNum());
				item.setBusinessType("device_check");
				item.setModule("deviceCheck");
			});
		}
		return deviceCheckBacklogs;
	}

	/**
	 *设置app待办返回值
	 */
	private List<BacklogVO> setBacklogVOS(String type, List<BacklogVO> taskHandInBacklog) {
		for (BacklogVO vo : taskHandInBacklog) {
			vo.setOverTime(String.valueOf(vo.getDeadline().getTime() - DateUtil.now().getTime()));
			vo.setBusinessType(TaskBusinessTypeEnum.convertToName(vo.getBusinessType()));
		}
		if (StringUtil.equals("all", type)) {
			return taskHandInBacklog;
		}
		return taskHandInBacklog.stream().limit(10).collect(Collectors.toList());
	}









}
