package com.ruoyi.web.controller.warehouse.approvalFlow;

import java.util.List;

import com.ruoyi.approvalFlow.domain.AuditItems;
import com.ruoyi.approvalFlow.domain.AuditPaths;
import com.ruoyi.approvalFlow.service.IAuditPathsService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审批流配置主Controller
 *
 * @author ruoyi
 * @date 2021-01-21
 */
@RestController
@RequestMapping("/warehouse/paths")
public class AuditPathsController extends BaseController {
    @Autowired
    private IAuditPathsService auditPathsService;

    /**
     * 查询审批流配置主列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:paths:list')")
    @GetMapping("/list")
    public TableDataInfo list(AuditPaths auditPaths) {
        startPage();
        List<AuditPaths> list = auditPathsService.selectAuditPathsList(auditPaths);
        return getDataTable(list);
    }

    /**
     * 审批通过
     */
    @RepeatSubmit
    @PostMapping("/approved")
    public AjaxResult approved(@RequestBody AuditItems auditItems) {
        if (StringUtils.isNull(auditItems.getActId())) {
            return AjaxResult.error("操作失败 未找到活动号，请确认");
        }
        if (StringUtils.isNull(auditItems.getBillId())) {
            return AjaxResult.error("操作失败 未找到表单信息，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        auditItems.setSendName(loginUser.getUsername());
        auditItems.setSendUserId(loginUser.getUser().getUserId());
        return auditPathsService.approved(auditItems);
    }

    /**
     * 审批驳回
     */
    @RepeatSubmit
    @PostMapping("/approvalRejected")
    public AjaxResult approvalRejected(@RequestBody AuditItems auditItems) {
        if (StringUtils.isNull(auditItems.getActId())) {
            return AjaxResult.error("操作失败 未找到活动号，请确认");
        }
        if (StringUtils.isNull(auditItems.getBillId())) {
            return AjaxResult.error("操作失败 未找到表单信息，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        auditItems.setSendName(loginUser.getUsername());
        auditItems.setSendUserId(loginUser.getUser().getUserId());
        return auditPathsService.approvalRejected(auditItems);
    }

    /**
     * 审批撤销
     */
    @RepeatSubmit
    @PostMapping("/revoke")
    public AjaxResult revoke(@RequestBody AuditItems auditItems) {
        if (StringUtils.isNull(auditItems.getActId())) {
            return AjaxResult.error("操作失败 未找到活动号，请确认");
        }
        if (StringUtils.isNull(auditItems.getBillId())) {
            return AjaxResult.error("操作失败 未找到表单信息，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        auditItems.setSendName(loginUser.getUsername());
        auditItems.setSendUserId(loginUser.getUser().getUserId());
        return auditPathsService.revoke(auditItems);
    }

    /**
     * 导出审批流配置主列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:paths:export')")
    @Log(title = "审批流配置主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @RepeatSubmit
    public AjaxResult export(AuditPaths auditPaths) {
        List<AuditPaths> list = auditPathsService.selectAuditPathsList(auditPaths);
        ExcelUtil<AuditPaths> util = new ExcelUtil<AuditPaths>(AuditPaths.class);
        return util.exportExcel(list, "paths");
    }

    /**
     * 获取审批流配置主详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:paths:query')")
    @GetMapping(value = "/{id}")
    @RepeatSubmit
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (StringUtils.isNull(id)) {
            return AjaxResult.error("查询条件为空，请确认");
        }
        return AjaxResult.success(auditPathsService.selectAuditPathsById(id));
    }

    /**
     * 根据id、actId 查询审批流
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:paths:query')")
    @PostMapping(value = "/projectEndQueryPendingVal")
    public AjaxResult projectEndQueryPendingVal(@RequestBody AuditItems auditItems) {
        if (StringUtils.isNull(auditItems) || StringUtils.isNull(auditItems.getId()) || StringUtils.isNull(auditItems.getActId())) {
            return AjaxResult.error("找不到查询条件，请确认");
        }
        return auditPathsService.projectEndQueryPendingVal(auditItems);
    }

    /**
     * 首页查询待审审批流
     */
    // @PreAuthorize("@ss.hasPermi('warehouse:paths:query')")
    @PostMapping(value = "/selectAllAuditItems")
    public AjaxResult selectAllAuditItems(@RequestBody AuditItems auditItems) {
        /*if (StringUtils.isNull(auditItems)) {
            return AjaxResult.error("找不到查询条件，请确认");
        }*/
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        auditItems.setAuditUserId(loginUser.getUser().getUserId());
        return auditPathsService.selectAllAuditItems(auditItems);
    }

    /**
     * 新增审批流配置主
     */
    @PreAuthorize("@ss.hasPermi('warehouse:paths:add')")
    @Log(title = "审批流配置主", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("auditPaths") String auditPaths,
                          @RequestParam("auditPathsLevels") String auditPathsLevels) {
        if (StringUtils.isNull(auditPaths) || "{}".equals(auditPaths)) {
            return AjaxResult.error("未找到主表信息，请确认");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return auditPathsService.insertAuditPaths(auditPaths, auditPathsLevels, loginUser);
    }

    /**
     * 修改审批流配置主
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('warehouse:paths:edit')")
    @Log(title = "审批流配置主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuditPaths auditPaths) {
        return toAjax(auditPathsService.updateAuditPaths(auditPaths));
    }

    /**
     * 删除审批流配置主
     */
    @PreAuthorize("@ss.hasPermi('warehouse:paths:remove')")
    @Log(title = "审批流配置主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] ids) {
        return auditPathsService.deleteAuditPathsByIds(ids);
    }
}
