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.entity.ItdmFlowPathStep;
import org.jeecg.modules.flowpath.service.IItdmFlowPathStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 流程步骤表
 * @Author: jeecg-boot
 * @Date:   2023-07-20
 * @Version: V1.0
 */
@Api(tags=".流程步骤表")
@RestController
@RequestMapping("/flowpath/itdmFlowPathStep")
@Slf4j
public class ItdmFlowPathStepController extends JeecgController<ItdmFlowPathStep, IItdmFlowPathStepService> {
	@Autowired
	private IItdmFlowPathStepService itdmFlowPathStepService;

	 /**根据运行流程表的流程步骤id查对应流程步骤表的步骤顺序*/
	 @ApiOperation("判断有无下一步，0无，1有")
	 @GetMapping("/getSortByFlowPathId")
	 public Integer getSortByFlowPathStepId(String dqStep){
		 String sort = itdmFlowPathStepService.getSortByFlowPathStepId(dqStep);
		 if("12".equals(sort))  return 0; //当前步骤是最后一步，无需选择下一步的人
		 else return 1;
	 }

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

	/**
	 *   添加
	 *
	 * @param itdmFlowPathStep
	 * @return
	 */
	@AutoLog(value = "流程步骤表-添加")
	@ApiOperation(value="流程步骤表-添加", notes="流程步骤表-添加")
	//@RequiresPermissions("org.jeecg.modules:itdm_flow_path_step:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ItdmFlowPathStep itdmFlowPathStep) {
		itdmFlowPathStepService.save(itdmFlowPathStep);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param itdmFlowPathStep
	 * @return
	 */
	@AutoLog(value = "流程步骤表-编辑")
	@ApiOperation(value="流程步骤表-编辑", notes="流程步骤表-编辑")
	//@RequiresPermissions("org.jeecg.modules:itdm_flow_path_step:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ItdmFlowPathStep itdmFlowPathStep) {
		itdmFlowPathStepService.updateById(itdmFlowPathStep);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "流程步骤表-通过id删除")
	@ApiOperation(value="流程步骤表-通过id删除", notes="流程步骤表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_flow_path_step:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		itdmFlowPathStepService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "流程步骤表-批量删除")
	@ApiOperation(value="流程步骤表-批量删除", notes="流程步骤表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_flow_path_step:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.itdmFlowPathStepService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "流程步骤表-通过id查询")
	@ApiOperation(value="流程步骤表-通过id查询", notes="流程步骤表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ItdmFlowPathStep> queryById(@RequestParam(name="id",required=true) String id) {
		ItdmFlowPathStep itdmFlowPathStep = itdmFlowPathStepService.getById(id);
		if(itdmFlowPathStep==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(itdmFlowPathStep);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param itdmFlowPathStep
    */
    //@RequiresPermissions("org.jeecg.modules:itdm_flow_path_step:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ItdmFlowPathStep itdmFlowPathStep) {
        return super.exportXls(request, itdmFlowPathStep, ItdmFlowPathStep.class, "流程步骤表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("itdm_flow_path_step:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ItdmFlowPathStep.class);
    }

}
