package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.work.domain.Packages;
import com.ruoyi.web.work.domain.dto.RejectDto;
import com.ruoyi.web.work.service.IPackagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.List;

/**
 * 接包管理
 *
 * @author lsw
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/work/packages")
public class PackagesController extends BaseController {
    @Autowired
    private IPackagesService packagesService;

    @PreAuthorize("@ss.hasPermi('work:packages:list')")
    @GetMapping("/list")
    public TableDataInfo list(Packages packages) {
        packages.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Packages> list = packagesService.selectList(packages);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:packages:list')")
    @Log(title = "接包凭证审核通过", businessType = BusinessType.UPDATE)
    @GetMapping("/success/{id}")
    public AjaxResult success(@PathVariable("id") Long id) throws ServerException {
        return packagesService.success(id);
    }
    @PreAuthorize("@ss.hasPermi('work:packages:list')")
    @Log(title = "接包凭证审核驳回", businessType = BusinessType.UPDATE)
    @PostMapping("reject")
    public AjaxResult reject(@Validated @RequestBody RejectDto dto) throws ServerException {
        return packagesService.reject(dto);
    }

    @PreAuthorize("@ss.hasPermi('work:packages:list')")
    @Log(title = "删除接包用户", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) throws ServerException {
        return packagesService.remove(id);
    }

    @Log(title = "导出接包用户信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('work:packages:list')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Packages packages) {
        packages.setCompanyId(getLoginUser().getUser().getCompanyId());
        PageUtils.orderBy("p.id desc");
        List<Packages> list = packagesService.selectList(packages);
        ExcelUtil<Packages> util = new ExcelUtil<Packages>(Packages.class);
        util.exportExcel(response, list, "用户信息");
    }
}
