package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.fms.domain.WmsSupplier;
import com.yunfeiyun.agmp.fms.domain.reqvo.WmsSupplierAddReqVo;
import com.yunfeiyun.agmp.fms.service.IWmsSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 供应商Controller
 *
 * @author 杨晓辉
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/wms/supplier")
public class WmsSupplierController extends BaseController
{
    @Autowired
    private IWmsSupplierService wmsSupplierService;
    @Autowired
    private ResManager resManager;

    /**
     * 查询供应商列表
     */
    @PreAuthorize("@ss.hasPermi('wms:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsSupplier wmsSupplier)
    {
        startPage();
        List<WmsSupplier> list = wmsSupplierService.selectWmsSupplierList(wmsSupplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @PreAuthorize("@ss.hasPermi('wms:supplier:export')")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsSupplier wmsSupplier)
    {
        List<WmsSupplier> list = wmsSupplierService.selectWmsSupplierList(wmsSupplier);
        ExcelUtil<WmsSupplier> util = new ExcelUtil<WmsSupplier>(WmsSupplier.class);
        util.exportExcel(response, list, "供应商数据");
    }

    @PreAuthorize("@ss.hasPermi('wms:supplier:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("supplierId") String supplierId)
    {
        List<SysRes> sysRes = resManager.queryRes(supplierId, ResConstants.BizType.SUPPLY.getKey());
        String supplierPreview = "";
        for (SysRes sysRe : sysRes) {
            supplierPreview += sysRe.getResUrl();
        }
        WmsSupplier wmsSupplier = wmsSupplierService.selectWmsSupplierBySupplierId(supplierId);
        wmsSupplier.setSupplierPreview(supplierPreview);
        return success(wmsSupplier);
    }

    /**
     * 新增供应商
     */
    @PreAuthorize("@ss.hasPermi('wms:supplier:add')")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WmsSupplierAddReqVo wmsSupplierAddReqVo)
    {
        return toAjax(wmsSupplierService.insertWmsSupplier(wmsSupplierAddReqVo));
    }

    /**
     * 修改供应商
     */
    @PreAuthorize("@ss.hasPermi('wms:supplier:edit')")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody WmsSupplierAddReqVo  wmsSupplierAddReqVo)
    {
        return toAjax(wmsSupplierService.updateWmsSupplier(wmsSupplierAddReqVo));
    }

    /**
     * 删除供应商
     */
    @PreAuthorize("@ss.hasPermi('wms:supplier:delete')")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(String[] supplierIds)
    {
        return toAjax(wmsSupplierService.deleteWmsSupplierBySupplierIds(supplierIds));
    }

    /**
     * 供应商资质上传
     */
    @PreAuthorize("@ss.hasPermi('wms:supplier:upload')")
    @Log(title = "供应商资质上传", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file)
    {
        return success(wmsSupplierService.upload(file));
    }
}
