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.WarehouseCheck;
import com.ruoyi.web.stock.service.IRoleOrgService;
import com.ruoyi.web.stock.service.IWarehouseCheckService;
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/check")
public class WarehouseCheckController extends BaseController {

    @Autowired
    private IWarehouseCheckService warehouseCheckService;
    @Autowired
    private IRoleOrgService roleOrgService;

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

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

    @PreAuthorize("@ss.hasPermi('erp:stock:check:add')")
    @Log(title = "仓库盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseCheck entity) {
        entity.setOrgId(currentOrgId());
        return toAjax(warehouseCheckService.submitCheck(entity, getUsername()));
    }

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

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

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