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.WarehouseIn;
import com.ruoyi.web.stock.service.IRoleOrgService;
import com.ruoyi.web.stock.service.IWarehouseInboundService;
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/inbound")
public class WarehouseInboundController extends BaseController {

    @Autowired
    private IWarehouseInboundService warehouseInboundService;
    @Autowired
    private IRoleOrgService roleOrgService;

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

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

    @PreAuthorize("@ss.hasPermi('erp:stock:inbound:add')")
    @Log(title = "生产入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseIn entity) {
        entity.setOrgId(currentOrgId());
        return toAjax(warehouseInboundService.submitInbound(entity, getUsername()));
    }

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

    @PreAuthorize("@ss.hasPermi('erp:stock:inbound:query')")
    @GetMapping("/materials/options")
    public AjaxResult materialOptions() {
        return success(warehouseInboundService.listMaterialOptions(currentOrgId()));
    }

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