package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.TBaseStorageAreaLocation;
import com.ruoyi.business.domain.TOngoodsshelf;
import com.ruoyi.business.domain.Task;
import com.ruoyi.business.service.ITBaseStorageAreaLocationService;
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.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/business/locationMonitor")
public class LocationController extends BaseController {
    private String prefix = "business/locationMonitor";
    @Autowired
    private ITBaseStorageAreaLocationService locationService;

    @RequiresPermissions("business:locationMonitor:view")
    @GetMapping()
    public String locationMonitor(ModelMap mmap)
    {
        mmap.put("locationMonitor", new TBaseStorageAreaLocation());
        return prefix + "/locationMonitor";
    }

    /**
     * 查询库位列表
     */
    @RequiresPermissions("business:locationMonitor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBaseStorageAreaLocation location)
    {
        startPage();
        return getDataTable(locationService.selectTBaseStorageAreaLocationList(location));
    }

    /**
     * 是否锁定库位
     */
    @Log(title = "是否锁定库位", businessType = BusinessType.UPDATE)
    @PostMapping("/lockLocation")
    @ResponseBody
    @RequiresPermissions("business:locationMonitor:update")
    public AjaxResult lockLocation(TBaseStorageAreaLocation location) {
        try {
            TBaseStorageAreaLocation currentLocation = locationService.selectTBaseStorageAreaLocationByLocationId(location.getLocationId());
            if (currentLocation != null && StringUtils.isNotEmpty(location.getIslock())) {
                currentLocation.setIslock(location.getIslock());
                locationService.updateTBaseStorageAreaLocation(currentLocation);
            }
        } catch (Exception e) {
            return error("-1");
        }
        return success("1");
    }

    /**
     * 是否占用库位
     */
    @Log(title = "是否占用库位", businessType = BusinessType.UPDATE)
    @PostMapping("/occupyLocation")
    @ResponseBody
    @RequiresPermissions("business:locationMonitor:update")
    public AjaxResult occupyLocation(TBaseStorageAreaLocation location) {
        try {
            TBaseStorageAreaLocation currentLocation = locationService.selectTBaseStorageAreaLocationByLocationId(location.getLocationId());
            if (currentLocation != null && StringUtils.isNotEmpty(location.getStatus())) {
                currentLocation.setStatus(location.getStatus());
                locationService.updateTBaseStorageAreaLocation(currentLocation);
            }
        } catch (Exception e) {
            return error("-1");
        }
        return success("1");
    }
}
