package org.dromara.zhishu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.zhishu.domain.bo.TShelvesBo;
import org.dromara.zhishu.domain.vo.TDepotVo;
import org.dromara.zhishu.domain.vo.TFreightVo;
import org.dromara.zhishu.domain.vo.TShelvesVo;
import org.dromara.zhishu.service.ITDepotService;
import org.dromara.zhishu.service.ITShelvesService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 货架信息
 *
 * @author Lion Li
 * @date 2025-03-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shelves/shelves")
public class TShelvesController extends BaseController {

    private final ITShelvesService tShelvesService;
    private final ITDepotService tDepotService;

    /**
     * 查询货架信息列表
     */
    @SaCheckPermission("shelves:shelves:list")
    @GetMapping("/list")
    public TableDataInfo<TShelvesVo> list(TShelvesBo bo, PageQuery pageQuery) {
        return tShelvesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出货架信息列表
     */
    @SaCheckPermission("shelves:shelves:export")
    @Log(title = "货架信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TShelvesBo bo, HttpServletResponse response) {
        List<TShelvesVo> list = tShelvesService.queryList1();
        ExcelUtil.exportExcel(list, "货架信息", TShelvesVo.class, response);
    }

    /**
     * 获取货架信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shelves:shelves:query")
    @GetMapping("/{id}")
    public R<TShelvesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tShelvesService.queryById(id));
    }

    /**
     * 前台本地回显用查询函数
     */
    @SaCheckPermission("shelves:shelves:query")
    @GetMapping("/forLocal/{depotId}/{code}")
    public R<TShelvesVo> getInfo(@PathVariable("depotId") Long depotId, @PathVariable("code") String code) {
        return R.ok(tShelvesService.queryFoundId(depotId, code));
    }

    /**
     * 新增货架信息
     */
    @SaCheckPermission("shelves:shelves:add")
    @Log(title = "货架信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @SaIgnore
    public R<TShelvesBo> add(@Validated(AddGroup.class) @RequestBody TShelvesBo bo) {
        try{
            tShelvesService.insertByBo(bo);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return R.ok(bo);
    }

    /**
     * 修改货架信息
     */
    @SaCheckPermission("shelves:shelves:edit")
    @Log(title = "货架信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @SaIgnore
    public R<TShelvesBo> edit(@Validated(EditGroup.class) @RequestBody TShelvesBo bo) {
        try{
            tShelvesService.updateByBo(bo);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return R.ok(bo);
    }

    /**
     * 删除货架信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shelves:shelves:remove")
    @Log(title = "货架信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @SaIgnore
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids,
                          @RequestParam(required = false) Long LocalUserId) {
        return toAjax(tShelvesService.deleteWithValidByIds(List.of(ids), true,LocalUserId));
    }

    /**
     * 查询仓库信息列表
     */
//    @SaIgnore
    @SaCheckPermission("shelves:shelves:namelist")
    @GetMapping("/namelist")
    @SaIgnore
    public TableDataInfo<TDepotVo> list1(String phoneNumber) {
         List<TDepotVo> list=  tDepotService.queryNameList(phoneNumber);
       return TableDataInfo.build(list);
    }

    /**
     * 查询货架id,code列表
     */
    @GetMapping("/sheNamelist")
    @SaIgnore
    public TableDataInfo<TShelvesVo> listShe(String depotId) {
        List<TShelvesVo> list=  tDepotService.querySheNameList(depotId);
        return TableDataInfo.build(list);
    }
    /**
     * 查询货位id,code列表
     */

    @GetMapping("/freNamelist")
    @SaIgnore
    public TableDataInfo<TFreightVo> listFre(String sheId) {
        List<TFreightVo> list=  tDepotService.queryFreNameList(sheId);
        return TableDataInfo.build(list);
    }

    @GetMapping("/sheNamelist/{depotId}")
    @SaIgnore
    public TableDataInfo<TShelvesVo> listShe1(@PathVariable String depotId) {
        List<TShelvesVo> list=  tDepotService.querySheNameList(depotId);
        return TableDataInfo.build(list);
    }
    @GetMapping("/freNamelist/{sheId}")
    @SaIgnore
    public TableDataInfo<TFreightVo> listFre1(@PathVariable String sheId) {
        List<TFreightVo> list=  tDepotService.queryFreNameList(sheId);
        return TableDataInfo.build(list);
    }

    @GetMapping("/getShelvesId")
    @SaIgnore
    public TableDataInfo<TShelvesVo> getShelvesId(String depotId,String code) {
        List<TShelvesVo> list=  tShelvesService.selectShelvesIdByDepotIdAndCode(depotId,code);
        return TableDataInfo.build(list);
    }


}
