package org.jeecg.modules.flowpath.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flowpath.dto.*;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPathStep;
import org.jeecg.modules.flowpath.service.IItdmFlowPathStepService;
import org.jeecg.modules.flowpath.service.IItdmRunFlowPathStepService;
import org.jeecg.modules.flowpath.vo.ItdmBuohuiRunFlowPathStepVO;
import org.jeecg.modules.flowpath.vo.NextStepUserVO;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 运行流程步骤表
 * @Author: jeecg-boot
 * @Date:   2023-07-20
 * @Version: V1.0
 */
@Api(tags="运行流程步骤表")
@RestController
@RequestMapping("/flowpath/itdmRunFlowPathStep")
@Slf4j
public class ItdmRunFlowPathStepController extends JeecgController<ItdmRunFlowPathStep, IItdmRunFlowPathStepService> {
	@Autowired
	private IItdmRunFlowPathStepService itdmRunFlowPathStepService;
	@Autowired
	private IItdmFlowPathStepService flowPathStepService;
	@Autowired
	private ISysUserService sysUserService;

	 /**
      * 分页列表查询
      *
      * @param itdmRunFlowPathStep
      * @param pageNo
      * @param pageSize
      * @param req
      * @return
      */
	//@AutoLog(value = "运行流程步骤表-分页列表查询")
	@ApiOperation(value="运行流程步骤表-分页列表查询", notes="运行流程步骤表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ItdmRunFlowPathStep>> queryPageList(ItdmRunFlowPathStep itdmRunFlowPathStep,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ItdmRunFlowPathStep> queryWrapper = QueryGenerator.initQueryWrapper(itdmRunFlowPathStep, req.getParameterMap());
		Page<ItdmRunFlowPathStep> page = new Page<ItdmRunFlowPathStep>(pageNo, pageSize);
		IPage<ItdmRunFlowPathStep> pageList = itdmRunFlowPathStepService.page(page, queryWrapper);
		return Result.OK(pageList);
	}



	/**
	 *  编辑
	 *
	 * @param itdmRunFlowPathStep
	 * @return
	 */
	@AutoLog(value = "运行流程步骤表-保存")
	@ApiOperation(value="运行流程步骤表-保存", notes="运行流程步骤表-保存")
	//@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path_step:edit")
	@PostMapping(value = "/save")
	public Result<String> saveData(@RequestBody ItdmSaveRunFlowPath itdmRunFlowPathStep) {
		itdmRunFlowPathStepService.saveData(itdmRunFlowPathStep);
		return Result.OK("编辑成功!");
	}


	 /**
	  *  编辑
	  *
	  * @param itdmRunFlowPathStep
	  * @return
	  */
	 @AutoLog(value = "运行流程步骤表-通过")
	 @ApiOperation(value="运行流程步骤表-通过", notes="运行流程步骤表-通过")
	 //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path_step:edit")
	 @PostMapping(value = "/pass")
	 public Result<String> pass(@RequestBody ItdmSaveRunFlowPath itdmRunFlowPathStep) {
		 itdmRunFlowPathStepService.pass(itdmRunFlowPathStep);
		 return Result.OK("编辑成功!");
	 }

	 /**新增运行流程——选择第一步操作的人*/
	 @AutoLog(value = "新增运行流程——选择第一步操作的人")
	 @ApiOperation(value="新增运行流程——选择第一步操作的人", notes="新增运行流程——选择第一步操作的人")
	 @GetMapping(value = "/AddSelectNextStepUser")
	 public List<NextStepUserVO> AddSelectNextStepUser(){
		 List<String>  list = flowPathStepService.AddSelectNextStepUser();
		 List<NextStepUserVO> list1 = list.stream().map(i->{
			 NextStepUserVO vo = new NextStepUserVO();
			 vo.setUserName(i);
			 SysUser sysUser = sysUserService.getUserByName(i);
			 if(sysUser != null) vo.setRealName(sysUser.getRealname());
			 return vo;
		 }).collect(Collectors.toList());
		 return list1;
	 }

	 /**通过时(下一步)选择下一步操作的人*/
	 @AutoLog(value = "运行流程步骤表-通过1——选择下一步操作的人")
	 @ApiOperation(value="运行流程步骤表-通过1——选择下一步操作的人", notes="运行流程步骤表-通过1——选择下一步操作的人")
	 @GetMapping(value = "/selectNextStepUser")
	 public List<NextStepUserVO> selectNextStepUser(ItdmPassSelectNextStepUserRunFlowPath runFlowPath){
		 List<String>  list = itdmRunFlowPathStepService.selectNextStepUser(runFlowPath);
		 List<NextStepUserVO> list1 = list.stream().map(i->{
			 NextStepUserVO vo = new NextStepUserVO();
			 vo.setUserName(i);
			 SysUser sysUser = sysUserService.getUserByName(i);
			 if(sysUser != null) vo.setRealName(sysUser.getRealname());
			 return vo;
		 }).collect(Collectors.toList());
		 return list1;
	 }

	 /**
	  *  下一步
	  *
	  * @param itdmRunFlowPathStep
	  * @return
	  */
	 @AutoLog(value = "运行流程步骤表-通过1")
	 @ApiOperation(value="运行流程步骤表-通过1", notes="运行流程步骤表-通过1")
	 //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path_step:edit")
	 @PostMapping(value = "/pass1")
	 public Result<String> pass1(@RequestBody ItdmPassRunFlowPath itdmRunFlowPathStep) {
		 itdmRunFlowPathStepService.pass1(itdmRunFlowPathStep);
		 return Result.OK("编辑成功!");
	 }




	 /**
	  *  驳回到上一步
	  *
	  * @param itdmRunFlowPathStep
	  * @return
	  */
	 @AutoLog(value = "运行流程步骤表-驳回到上一节点")
	 @ApiOperation(value="运行流程步骤表-驳回到上一节点", notes="运行流程步骤表-驳回到上一节点")
	 //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path_step:edit")
	 @PostMapping(value = "/rejectDQ")
	 public Result<String> RejectDQ(@RequestBody ItdmRejectDQRunFlowPath itdmRunFlowPathStep) {
		 itdmRunFlowPathStepService.rejectDQ(itdmRunFlowPathStep);
		 return Result.OK("编辑成功!");
	 }




	 @AutoLog(value = "运行流程步骤表-当前流程当前执行中之前的节点")
	 @ApiOperation(value="运行流程步骤表-当前流程当前执行中之前的节点", notes="运行流程步骤表-当前流程当前执行中之前的节点")
	 @GetMapping("yrunList")
	 public  Result<List<ItdmBuohuiRunFlowPathStepVO>> yrunList(String runFlowPath) {

		 return Result.OK( itdmRunFlowPathStepService.yrunList(runFlowPath));
	 }



	 /**
	  *  驳回到指定节点
	  *
	  * @param itdmRunFlowPathStep
	  * @return
	  */
	 @AutoLog(value = "运行流程步骤表-驳回到指定节点")
	 @ApiOperation(value="运行流程步骤表-驳回到指定节点", notes="运行流程步骤表-驳回到指定节点")
	 //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path_step:edit")
	 @PostMapping(value = "/rejectzd")
	 public Result<String> rejectzd(@RequestBody ItdmRejectZDRunFlowPath itdmRunFlowPathStep) {
		 itdmRunFlowPathStepService.rejectzd(itdmRunFlowPathStep);
		 return Result.OK("编辑成功!");
	 }


	 /**
	  * 编辑
	  *
	  * @return
	  */
	 @AutoLog(value = "运行流程步骤表-流程结束")
	 @ApiOperation(value = "运行流程步骤表-流程结束", notes = "运行流程步骤表-流程结束")
	 //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path_step:edit")
	 @GetMapping(value = "/jieshu")
	 public Result<String> jieshu(String runFlowPath) {

		 itdmRunFlowPathStepService.jieshu(runFlowPath);
		 return Result.OK("编辑成功!");
	 }


 }
