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.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.zhishu.domain.bo.TDepotBo;
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.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库信息设置
 *
 * @author Lion Li
 * @date 2025-03-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/depot/depot")
public class TDepotController extends BaseController {

    private final ITDepotService tDepotService;


    /**
     * 查询仓库信息设置列表
     */
    @SaCheckPermission("depot:depot:list")
    @GetMapping("/list")
    public TableDataInfo<TDepotVo> list(TDepotBo bo, PageQuery pageQuery) {
        return tDepotService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出仓库信息设置列表
     */
    @SaCheckPermission("depot:depot:export")
    @Log(title = "仓库信息设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TDepotBo bo, HttpServletResponse response) {
        List<TDepotVo> list = tDepotService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库信息设置", TDepotVo.class, response);
    }

    /**
     * 获取仓库信息设置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("depot:depot:query")
    @GetMapping("/{id}")
    public R<TDepotVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tDepotService.queryById(id));
    }

    /**
     * 新增仓库信息设置
     */
    @SaCheckPermission("depot:depot:add")
    @Log(title = "仓库信息设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @SaIgnore
    public R<TDepotBo> add(@Validated(AddGroup.class) @RequestBody TDepotBo bo) {
      try{
          tDepotService.insertByBo(bo);
      } catch (Exception e) {
          throw new RuntimeException(e.getMessage());
      }
//        return toAjax(tDepotService.insertByBo(bo));
        return R.ok(bo);
    }

    /**
     * 修改仓库信息设置
     */
    @SaCheckPermission("depot:depot:edit")
    @Log(title = "仓库信息设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @SaIgnore
    public R<TDepotBo> edit(@Validated(EditGroup.class) @RequestBody TDepotBo bo) {
        try{
            tDepotService.updateByBo(bo);
        }catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return R.ok(bo);
    }

    /**
     * 删除仓库信息设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("depot:depot: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(tDepotService.deleteWithValidByIds(List.of(ids), true,LocalUserId));
    }

    @GetMapping("/getIds")
    public Map<String, Object> getIds(@RequestParam String depotName,
                                      @RequestParam String freightName,
                                      @RequestParam String shelvesName) {
        return tDepotService.getIds(depotName, freightName, shelvesName);
    }

    /**
     * 获取二级货区信息
     * @param id
     * @return
     */
    @GetMapping("/shelves/{id}")
    public R<List<TShelvesVo>> getSheInfo(@PathVariable Long id) {
        List<TShelvesVo> list =tDepotService.queryShelvesList(id);
        System.out.println(list);
        return R.ok(list);
    }

    /**
     * 获取三级货区信息
     * @param id
     * @return
     */
    @GetMapping("/freight/{id}")
    public R<List<TFreightVo>> getFreInfo(@PathVariable Long id) {
        List<TFreightVo> list =tDepotService.queryTFreightList(id);
        System.out.println(list);
        return R.ok(list);
    }

    @GetMapping("/getDepotAll")
    public TableDataInfo<TDepotVo> getDepotAll(){
        List<TDepotVo> list = tDepotService.queryListAll();
        return TableDataInfo.build(list);
    }

    @GetMapping("/getDepotId")
    @SaIgnore
    public TableDataInfo<TDepotVo> getDepotId(Long userId,String code){
        List<TDepotVo> list = tDepotService.selectDepotIdByUserIdAndCode(userId,code);
        return TableDataInfo.build(list);
    }

}
