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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.security.common.Constant;
import org.security.common.util.DataFormatUtil;
import org.security.trans.app.service.IAppEducateService;
import org.security.trans.app.vo.TaskHandInVO;
import org.security.trans.basic.service.IDepartmentService;
import org.security.trans.flow.dto.FlowStartDTO;
import org.security.trans.flow.service.IFlowHistoryService;
import org.security.trans.task.entity.Task;
import org.security.trans.task.entity.TaskHandin;
import org.security.trans.task.service.ITaskHandinService;
import org.security.trans.task.service.ITaskService;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;

/**
 * @author Create by wzf
 * @date 2022/4/28 11:39
 */
@AllArgsConstructor
@Service
public class AppEducateServiceImpl implements IAppEducateService {
	private final ITaskHandinService taskHandinService;
	private final ITaskService taskService;
	private final IDepartmentService departmentService;
	private final IFlowHistoryService flowHistoryService;


	@Override
	public TaskHandInVO getOtherDetail(String id) {
		TaskHandInVO taskHandInVO = new TaskHandInVO();
		TaskHandin taskHandin = taskHandinService.getOne(new QueryWrapper<TaskHandin>().lambda().eq(Func.isNotBlank(id), TaskHandin::getId, id)
			.and(item -> item.eq(TaskHandin::getState, Constant.HANDIN_STATE_DRAFT).or().eq(TaskHandin::getState, Constant.HANDIN_STATE_DRAFT)));
		if (Func.isNotEmpty(taskHandin)){
			//获取参加人数
			taskHandInVO.setJoinNumber(Func.isNotEmpty(taskHandin.getPeopleNumber())?Integer.parseInt(String.valueOf(taskHandin.getPeopleNumber())):0);
			//获取当前机构
			taskHandInVO.setDeptName(departmentService.getById(taskHandin.getDeptId()).getFullName());
			//获取待办标题和描述
			Task task = taskService.getById(taskHandin.getTaskId());
			taskHandInVO.setTitle(task.getName());
			taskHandInVO.setScript(task.getSummary());
		}
		return taskHandInVO;
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean handInDetail(String id, List<MultipartFile> imgUrl) throws ParseException {
		//获取提报期限
		TaskHandin taskHandin = taskHandinService.getById(id);
		taskHandin.setState(Constant.HANDIN_STATE_AUDIT);
		//判断提报是否超期
//		taskHandin.setStatus(taskHandin.getDeadline().before(DataFormatUtil.getLocalDate())?Constant.HANDIN_STATE_OVERTIME_SUBMIT:Constant.HANDIN_STATE_SUBMIT);
//		taskHandin.setPicIds(taskHandInDTO.getImgUrl());
		//加入流程
		FlowStartDTO flowStartDTO = new FlowStartDTO();
		flowStartDTO.setFlowBusinessCode(taskHandin.getBusinessType());
		flowStartDTO.setBusinessLinkId(taskHandin.getId());
		return taskHandinService.saveOrUpdate(taskHandin) && flowHistoryService.startFlow(flowStartDTO);
	}
}
