package com.zbf.web.controller.system;

import com.zbf.common.annotation.DataScope;
import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.TBasePallet;
import com.zbf.system.domain.TBaseStorage;
import com.zbf.system.domain.TBaseStorageArea;
import com.zbf.system.domain.TBaseStorageAreaLocation;
import com.zbf.system.service.ITBaseStorageAreaLocationService;
import com.zbf.system.service.ITBaseStorageAreaService;
import com.zbf.system.service.ITBaseStorageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库位 Controller
 *
 * @author tony
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/system/location")
public class TBaseStorageAreaLocationController extends BaseController {

    private ITBaseStorageAreaLocationService tBaseStorageAreaLocationService;
    private ITBaseStorageAreaService baseStorageAreaService;

    public TBaseStorageAreaLocationController(ITBaseStorageAreaLocationService tBaseStorageAreaLocationService, ITBaseStorageAreaService baseStorageAreaService) {
        this.tBaseStorageAreaLocationService = tBaseStorageAreaLocationService;
        this.baseStorageAreaService = baseStorageAreaService;
    }

    /**
     * 根据当前用的数据权限，查询所管理的仓库信息
     * @param tBaseStorageArea
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:location:list')")
    @GetMapping("/getStorageAreaList")
    public AjaxResult getStorageAreaList(TBaseStorageArea tBaseStorageArea) {
        return success(baseStorageAreaService.getBaseStorageAreaList(tBaseStorageArea));
    }

    /**
     * 查询库位 列表
     */
    @PreAuthorize("@ss.hasPermi('system:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBaseStorageAreaLocation tBaseStorageAreaLocation) {
        startPage();
        List<TBaseStorageAreaLocation> list = tBaseStorageAreaLocationService.selectTBaseStorageAreaLocationList(tBaseStorageAreaLocation);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setColumns(tBaseStorageAreaLocation.getColumnsJson());
        return dataTable;
    }

    /**
     * 查询库位占用
     */
    @PreAuthorize("@ss.hasPermi('system:location:list')")
    @GetMapping("/occupy")
    public AjaxResult occupy() {
        logger.info("调用库位占用接口");
        return success(tBaseStorageAreaLocationService.occupy());
    }
    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:provider:remove')")
    @Log(title = "库位管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids) {
        return toAjax(tBaseStorageAreaLocationService.disabledTBaseStorageAreaLocationByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:provider:edit')")
    @Log(title = "库位管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids) {
        return toAjax(tBaseStorageAreaLocationService.enableTBaseStorageAreaLocationByIds(ids));
    }


    /**
     * 更改禁用状态
     */
    @Log(title = "库位管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{ids}")
    public AjaxResult changeStatus(@PathVariable Long[] ids,@RequestBody  TBaseStorageAreaLocation tBaseStorageAreaLocation) {
        return toAjax(tBaseStorageAreaLocationService.changeStatus(ids,tBaseStorageAreaLocation));
    }
    /**
     * 导出库位 列表
     */
    @PreAuthorize("@ss.hasPermi('system:location:export')")
    @Log(title = "库位 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBaseStorageAreaLocation tBaseStorageAreaLocation) {
        List<TBaseStorageAreaLocation> list = tBaseStorageAreaLocationService.selectTBaseStorageAreaLocationList(tBaseStorageAreaLocation);
        ExcelUtil<TBaseStorageAreaLocation> util = new ExcelUtil<TBaseStorageAreaLocation>(TBaseStorageAreaLocation.class);
        util.exportExcel(response, list, "库位 数据");
    }

    /**
     * 获取库位 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tBaseStorageAreaLocationService.selectTBaseStorageAreaLocationById(id));
    }

    /**
     * 新增库位
     */
    @PreAuthorize("@ss.hasPermi('system:location:add')")
    @Log(title = "库位 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBaseStorageAreaLocation tBaseStorageAreaLocation) {
        return toAjax(tBaseStorageAreaLocationService.insertTBaseStorageAreaLocation(tBaseStorageAreaLocation));
    }

    /**
     * 修改库位
     */
    @PreAuthorize("@ss.hasPermi('system:location:edit')")
    @Log(title = "库位 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBaseStorageAreaLocation tBaseStorageAreaLocation) {
        return toAjax(tBaseStorageAreaLocationService.updateTBaseStorageAreaLocation(tBaseStorageAreaLocation));
    }

    /**
     * 删除库位
     */
    @PreAuthorize("@ss.hasPermi('system:location:remove')")
    @Log(title = "库位 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBaseStorageAreaLocationService.deleteTBaseStorageAreaLocationByIds(ids));
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TBaseStorageAreaLocation> util = new ExcelUtil<TBaseStorageAreaLocation>(TBaseStorageAreaLocation.class);
        util.importTemplateExcel(response, "库位数据");
    }

    @Log(title = "库位", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:location:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TBaseStorageAreaLocation> util = new ExcelUtil<TBaseStorageAreaLocation>(TBaseStorageAreaLocation.class);
        List<TBaseStorageAreaLocation> importList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBaseStorageAreaLocationService.importData(importList, updateSupport, operName);
        return success(message);
    }
}

