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

import com.ruoyi.basicData.domain.TWarehouseArea;
import com.ruoyi.basicData.service.ITWarehouseAreaService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
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.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库区Controller
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@RestController
@RequestMapping("/basicdata/area")
public class TWarehouseAreaController extends BaseController {
    @Autowired
    private ITWarehouseAreaService tWarehouseAreaService;

    /**
     * 查询库区列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseArea tWarehouseArea) {
        startPage();
        List<TWarehouseArea> list = tWarehouseAreaService.selectTWarehouseAreaList(tWarehouseArea);
        return getDataTable(list);
    }

    /**
     * 导出库区列表
     */
    @Log(title = "库区", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWarehouseArea tWarehouseArea) {
        List<TWarehouseArea> list = tWarehouseAreaService.selectTWarehouseAreaList(tWarehouseArea);
        ExcelUtil<TWarehouseArea> util = new ExcelUtil<TWarehouseArea>(TWarehouseArea.class);
        return util.exportExcel(list, "area");
    }

    /**
     * 获取库区详细信息
     */
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tWarehouseAreaService.selectTWarehouseAreaById(fId));
    }

    /**
     * 新增库区
     */
    @Log(title = "库区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWarehouseArea tWarehouseArea) {
        // 校验是否为库区
        if (UserConstants.NOT_UNIQUE.equals(tWarehouseAreaService.checkUFTWarehouseUnique(tWarehouseArea))){
            return AjaxResult.error("新增仓库库位'" + tWarehouseArea.getfName() + "'失败，请选择库区");
        }
        tWarehouseArea.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tWarehouseAreaService.insertTWarehouseArea(tWarehouseArea));
    }

    /**
     * 修改库区
     */
    @Log(title = "库区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseArea tWarehouseArea) {
        tWarehouseArea.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tWarehouseAreaService.updateTWarehouseArea(tWarehouseArea));
    }

    /**
     * 删除库区
     */
    @Log(title = "库区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return tWarehouseAreaService.deleteTWarehouseAreaByIds(fIds);
    }
}
