package com.aegis.web.controller.resource.warehouse;

import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.common.exception.ServiceException;
import com.aegis.resource.warehouse.domain.Warehouse;
import com.aegis.resource.warehouse.service.IResourceWarehouseService;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/resources/warehouses")
public class ResourceWarehouseController extends BaseController {

    @Autowired
    private IResourceWarehouseService warehouseService;

    @PreAuthorize("@ss.hasPermi('resource:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(Warehouse warehouse) {
        startPage();
        List<Warehouse> list = warehouseService.listByCondition(warehouse);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('resource:warehouse:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(warehouseService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('resource:warehouse:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Warehouse warehouse) {
        try {
            return toAjax(warehouseService.save(warehouse));
        } catch (ServiceException e) {
            return error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('resource:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Warehouse warehouse) {
        try {
            return toAjax(warehouseService.updateById(warehouse));
        } catch (ServiceException e) {
            return error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('resource:warehouse:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        boolean removed = warehouseService.removeByIds(Arrays.asList(ids));
        return removed ? success() : error("删除失败");
    }

    @PreAuthorize("@ss.hasPermi('resource:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/enable")
    public AjaxResult enable(@PathVariable Long id) {
        try {
            return toAjax(warehouseService.updateStatus(id, "0"));
        } catch (ServiceException e) {
            return error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('resource:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/disable")
    public AjaxResult disable(@PathVariable Long id) {
        try {
            return toAjax(warehouseService.updateStatus(id, "1"));
        } catch (ServiceException e) {
            return error(e.getMessage());
        }
    }
}