package com.zbf.web.controller.flowable;

import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.domain.entity.SysUser;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.Leaveapply;
import com.zbf.system.domain.OutBound;
import com.zbf.system.service.IOutBoundService;
import com.zbf.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库申请单Controller
 *
 * @author tony
 * @date 2024-08-07
 */
@RestController
@RequestMapping("/outbound")
public class OutBoundController extends BaseController
{
    private String prefix = "flowable/outbound";

    @Autowired
    private IOutBoundService outBoundService;

    @Autowired
    private ISysUserService userService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    @GetMapping()
    public String outbound()
    {
        return prefix + "/outbound";
    }

/**
 * 查询出库申请单列表
 */
@GetMapping("/list")
    public TableDataInfo list(OutBound outBound)
    {
        startPage();
        List<OutBound> list = outBoundService.selectOutBoundList(outBound);
        return getDataTable(list);
    }

    /**
     * 导出出库申请单列表
     */
    @Log(title = "出库申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OutBound outBound)
    {
        List<OutBound> list = outBoundService.selectOutBoundList(outBound);
        ExcelUtil<OutBound> util = new ExcelUtil<OutBound>(OutBound.class);
        util.exportExcel(response, list, "出库申请单数据");
    }

    /**
     * 获取出库申请单详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(outBoundService.selectOutBoundById(id));
    }

    /**
     * 新增出库申请单
     */

    @Log(title = "出库申请单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OutBound outBound)
    {
        return toAjax(outBoundService.insertOutBound(outBound));
    }

    /**
     * 修改出库申请单
     */

    @Log(title = "出库申请单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OutBound outBound)
    {
        return toAjax(outBoundService.updateOutBound(outBound));
    }

    /**
     * 删除出库申请单
     */
    @Log(title = "出库申请单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(String ids)
    {
        return toAjax(outBoundService.deleteOutBoundByIds(ids));
    }

    /**
     * 启用
     */
    @Log(title = "出库申请单", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids)
    {
        return toAjax(outBoundService.disabledOutBoundByIds(ids));
    }

    /**
     * 禁用
     */
    @Log(title = "出库申请单", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids)
    {
        return toAjax(outBoundService.enableOutBoundByIds(ids));
    }

    /**
     * 修改状态
     */
    @Log(title = "出库申请单", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody OutBound outBound) {
        return toAjax(outBoundService.updateStatus(outBound));
    }

    /**
     * 第一审批人
     */
    @ApiOperation("第一审批人")
    @GetMapping("/deptmanager")
    @ResponseBody
    public AjaxResult deptmanager(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutBound apply = outBoundService.selectOutBoundById(Long.parseLong(p.getBusinessKey()));
            SysUser sysUser = userService.selectUserByUserName(apply.getApplyer());
            if(sysUser != null){
                apply.setApplyer(sysUser.getNickName());
            }

            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }


    /**
     * 第二审批人
     */
    @ApiOperation("第二审批人")
    @GetMapping("/managerName")
    @ResponseBody
    public AjaxResult manager(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutBound apply = outBoundService.selectOutBoundById(Long.parseLong(p.getBusinessKey()));
            SysUser sysUser = userService.selectUserByUserName(apply.getApplyer());
            if(sysUser != null){
                apply.setApplyer(sysUser.getNickName());
            }

            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }
    /**
     * 第三审批人
     */
    @ApiOperation("第三审批人")
    @GetMapping("/crossDeptManager")
    @ResponseBody
    public AjaxResult crossDeptManager(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutBound apply = outBoundService.selectOutBoundById(Long.parseLong(p.getBusinessKey()));
            SysUser sysUser = userService.selectUserByUserName(apply.getApplyer());
            if(sysUser != null){
                apply.setApplyer(sysUser.getNickName());
            }
            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }
    /**
     *
     * 驳回后使用
     * @return
     */
    @ApiOperation("驳回")
    @GetMapping("/reject")
    @ResponseBody
    public AjaxResult reject(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutBound apply = outBoundService.selectOutBoundById(Long.parseLong(p.getBusinessKey()));
            SysUser sysUser = userService.selectUserByUserName(apply.getApplyer());
            if(sysUser != null){
                apply.setApplyer(sysUser.getNickName());
            }
            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }
    /**
     * 发起出库申请
     * @return
     */
    @ApiOperation("驳回到发起人")
    @GetMapping("/addOutbound")
    @ResponseBody
    public AjaxResult addLeave(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutBound apply = outBoundService.selectOutBoundById(Long.parseLong(p.getBusinessKey()));
            SysUser sysUser = userService.selectUserByUserName(apply.getApplyer());
            if(sysUser != null){
                apply.setApplyer(sysUser.getNickName());
            }
            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }
    /**
     * 调整申请
     * @return
     */
    @ApiOperation("调整申请")
    @GetMapping("/modifyapply")
    @ResponseBody
    public AjaxResult modifyapply(String taskid)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutBound apply = outBoundService.selectOutBoundById(Long.parseLong(p.getBusinessKey()));
            SysUser sysUser = userService.selectUserByUserName(apply.getApplyer());
            if(sysUser != null){
                apply.setApplyer(sysUser.getNickName());
            };
            return AjaxResult.success(apply);
        }
        return AjaxResult.error("流程不存在");
    }

}

