package com.ruoyi.web.stock.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.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.stock.domain.WarehouseStockTransfer;
import com.ruoyi.web.stock.service.IRoleOrgService;
import com.ruoyi.web.stock.service.IWarehouseStockTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 仓库调拨控制器。
 */
@RestController
@RequestMapping("/stock/transfer")
public class WarehouseStockTransferController extends BaseController {

    @Autowired
    private IWarehouseStockTransferService warehouseStockTransferService;
    @Autowired
    private IRoleOrgService roleOrgService;

    @PreAuthorize("@ss.hasPermi('erp:stock:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseStockTransfer query) {
        query.setOrgId(currentOrgId());
        startPage();
        List<WarehouseStockTransfer> list = warehouseStockTransferService.selectList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('erp:stock:transfer:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        WarehouseStockTransfer entity = warehouseStockTransferService.selectById(id);
        if (entity == null || !currentOrgId().equals(entity.getOrgId())) {
            return error("无权访问该数据");
        }
        return success(entity);
    }

    @PreAuthorize("@ss.hasPermi('erp:stock:transfer:add')")
    @Log(title = "仓库调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseStockTransfer entity) {
        entity.setOrgId(currentOrgId());
        return toAjax(warehouseStockTransferService.submitTransfer(entity, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('erp:stock:transfer:remove')")
    @Log(title = "仓库调拨", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") String ids) {
        int rows = 0;
        for (Long id : Convert.toLongArray(ids)) {
            WarehouseStockTransfer old = warehouseStockTransferService.selectById(id);
            if (old == null || !currentOrgId().equals(old.getOrgId())) {
                continue;
            }
            rows += warehouseStockTransferService.removeById(id, getUsername());
        }
        return toAjax(rows);
    }

    @PreAuthorize("@ss.hasPermi('erp:stock:transfer:query')")
    @GetMapping("/inventory/options")
    public AjaxResult inventoryOptions() {
        return success(warehouseStockTransferService.listInventoryLineOptions(currentOrgId()));
    }

    private String currentOrgId() {
        return roleOrgService.resolveOrgId(getUserId(), getDeptId());
    }
}
