package com.prolog.rdc.tms.controller;

import java.util.List;

import com.prolog.rdc.tms.model.vo.basicConfigure.ApprovalOpenVO;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.rdc.tms.model.dto.ApplyApprovalQueryDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.PendingApprovalQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ApprovalLogDTO;
import com.prolog.rdc.tms.model.dto.basicData.ApprovalNodeSelDTO;
import com.prolog.rdc.tms.model.vo.ApplyApprovalVO;
import com.prolog.rdc.tms.model.vo.ApprovalBusLogSaveVO;
import com.prolog.rdc.tms.model.vo.ApprovalBusinessNodeVO;
import com.prolog.rdc.tms.service.ApprovalBusinessService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 审批流业务控制层
 *
 * @author :lhb
 * @version :1.0.0
 * @className :ApprovalBusinessController
 * @creationTime :2022/05/07
 * @description :
 * @modifier :
 * @modifyTime :
 * @reviseRemarks:
 */
@RestController
@Api(tags = "基础配置-审批流业务控制层")
@RequestMapping("/approval_business")
public class ApprovalBusinessController {

	@Autowired
	private ApprovalBusinessService approvalBusinessService;

	@UserLog("是否开启审批流程")
	@ApiOperation(httpMethod = "POST", value = "审批配置查询")
	@PostMapping(value = "/open")
	public RestMessage<Boolean> openExecute(@RequestBody ApprovalOpenVO approvalOpenVO) {
		try {
			Boolean rs = approvalBusinessService.approvalOpen(approvalOpenVO);
			return RestMessage.newInstance(true, "审批配置查询成功", rs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("审批流程节点审核")
	@ApiOperation(httpMethod = "POST", value = "审批流程节点审核")
	@PostMapping(value = "/approval")
	public RestMessage<Boolean> nodeApprovalExecute(@RequestBody ApprovalBusinessNodeVO approvalBusinessNodeVO) {
		try {
			Boolean isFinish = approvalBusinessService.nodeApproval(approvalBusinessNodeVO);
			return RestMessage.newInstance(true, "节点审核成功", isFinish);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("首页 我发起的审批流程查询")
	@ApiOperation(httpMethod = "POST", value = "首页 我发起的审批流程查询")
	@PostMapping(value = "/apply")
	public RestMessage<Page<ApplyApprovalQueryDTO>> applyApprovalExecute(@RequestBody ApplyApprovalVO vo) {
		try {
			Page<ApplyApprovalQueryDTO> rs = approvalBusinessService.applyApproval(vo);
			return RestMessage.newInstance(true, "我发起的审批流程查询成功", rs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("首页 待我审批的审批流程查询")
	@ApiOperation(httpMethod = "POST", value = "首页 待我审批的审批流程查询")
	@PostMapping(value = "/pending_approval")
	public RestMessage<Page<PendingApprovalQueryDTO>> pendingApprovalExecute(@RequestBody ApplyApprovalVO vo) {
		try {
			Page<PendingApprovalQueryDTO> rs = approvalBusinessService.pendingApproval(vo);
			return RestMessage.newInstance(true, "待我审批查询成功", rs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("首页 我已处理的审批流程查询")
	@ApiOperation(httpMethod = "POST", value = "首页 我已处理的审批流程查询")
	@PostMapping(value = "/processed")
	public RestMessage<Page<ApplyApprovalQueryDTO>> processedApprovalExecute(@RequestBody ApplyApprovalVO vo) {
		try {
			Page<ApplyApprovalQueryDTO> rs = approvalBusinessService.processedApproval(vo);
			return RestMessage.newInstance(true, "我已处理的审批流程查询成功", rs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("审核业务日志新增")
	@ApiOperation(httpMethod = "POST", value = "审核业务日志新增")
	@PostMapping(value = "/save_log")
	public RestMessage<Object> saveLogExecute(@RequestBody ApprovalBusLogSaveVO approvalBusLogSaveVO) {
		try {
			approvalBusinessService.saveLogData(approvalBusLogSaveVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("审核作废")
	@ApiOperation(httpMethod = "POST", value = "审核作废")
	@PostMapping(value = "/invalid")
	public RestMessage<Object> approvalInvalidExecute(@RequestParam("approvalBusinessId") Long approvalBusinessId) {
		try {
			approvalBusinessService.approvalInvalidData(approvalBusinessId);
			return RestMessage.newInstance(true, "作废成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("审核撤回")
	@ApiOperation(httpMethod = "POST", value = "审核撤回")
	@PostMapping(value = "/retract")
	public RestMessage<Object> approvalRetractExecute(@RequestParam("approvalBusinessId") Long approvalBusinessId) {
		try {
			approvalBusinessService.approvalRetractData(approvalBusinessId);
			return RestMessage.newInstance(true, "撤回成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("审核提交")
	@ApiOperation(httpMethod = "POST", value = "审核提交")
	@PostMapping(value = "/submit")
	public RestMessage<Object> approvalSubmitExecute(@RequestParam("approvalBusinessId") Long approvalBusinessId) {
		try {
			approvalBusinessService.approvalSubmitData(approvalBusinessId);
			return RestMessage.newInstance(true, "撤回成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("查询审核历史")
	@ApiOperation(httpMethod = "POST", value = "查询审核历史")
	@PostMapping(value = "/get_bus_log")
	public RestMessage<List<ApprovalLogDTO>> getBusLogExecute(@RequestParam("approvalBusinessId") String approvalBusinessId) {
		try {
			List<ApprovalLogDTO> list = approvalBusinessService.getBusLogData(approvalBusinessId);
			return RestMessage.newInstance(true, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("查询审核节点")
	@ApiOperation(httpMethod = "POST", value = "查询审核节点")
	@PostMapping(value = "/get_bus_detail")
	public RestMessage<List<ApprovalNodeSelDTO>> getBusDetailExecute(@RequestParam("approvalBusinessId") String approvalBusinessId) {
		try {
			List<ApprovalNodeSelDTO> list = approvalBusinessService.getBusDetailData(approvalBusinessId);
			return RestMessage.newInstance(true, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
