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.WarehouseOperPerf;
import com.ruoyi.web.stock.service.IRoleOrgService;
import com.ruoyi.web.stock.service.IWarehouseOperPerfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stock/operPerf")
public class WarehouseOperPerfController extends BaseController {

    @Autowired
    private IWarehouseOperPerfService operPerfService;
    @Autowired
    private IRoleOrgService roleOrgService;

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

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

    @PreAuthorize("@ss.hasPermi('erp:stock:operPerf:add')")
    @Log(title = "相经营业绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseOperPerf entity) {
        entity.setOrgId(currentOrgId());
        return toAjax(operPerfService.add(entity, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('erp:stock:operPerf:edit')")
    @Log(title = "相经营业绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseOperPerf entity) {
        WarehouseOperPerf old = operPerfService.selectById(entity.getId());
        if (old == null || !currentOrgId().equals(old.getOrgId())) {
            return error("无权修改该数据");
        }
        entity.setOrgId(currentOrgId());
        return toAjax(operPerfService.edit(entity, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('erp:stock:operPerf:remove')")
    @Log(title = "相经营业绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") String ids) {
        int rows = 0;
        for (Long id : Convert.toLongArray(ids)) {
            WarehouseOperPerf old = operPerfService.selectById(id);
            if (old == null || !currentOrgId().equals(old.getOrgId())) {
                continue;
            }
            rows += operPerfService.removeById(id, old.getOrgId(), getUsername());
        }
        return toAjax(rows);
    }

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

