package org.dromara.zhishu.controller;

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

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.TFreight;
import org.dromara.zhishu.domain.bo.TFreightBo;
import org.dromara.zhishu.domain.vo.TFreightVo;
import org.dromara.zhishu.domain.vo.TShelvesVo;
import org.dromara.zhishu.service.ITFreightService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 三级货区管理
 *
 * @author Lion Li
 * @date 2025-04-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/freight")
public class TFreightController extends BaseController {

    private final ITFreightService tFreightService;


    @GetMapping("/getDepotTree")
    public Map getDepotTree(){
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",tFreightService.selectDepotTree());
        return map;
    }

    /**
     * 查询三级货区管理列表
     */
    @SaCheckPermission("zhishu:freight:list")
    @GetMapping("/list")
    public TableDataInfo<TFreightVo> list(TFreightBo bo, PageQuery pageQuery) {
        return tFreightService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出三级货区管理列表
     */
    @SaCheckPermission("zhishu:freight:export")
    @Log(title = "三级货区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TFreightBo bo, HttpServletResponse response) {
        List<TFreightVo> list = tFreightService.queryList(bo);
        ExcelUtil.exportExcel(list, "三级货区管理", TFreightVo.class, response);
    }

    /**
     * 获取三级货区管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:freight:query")
    @GetMapping("/{id}")
    public R<TFreightVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tFreightService.queryById(id));
    }

    /**
     * 新增三级货区管理
     */
    @SaCheckPermission("zhishu:freight:add")
    @Log(title = "三级货区管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @SaIgnore
    @PostMapping()
    public R<TFreightBo> add(@Validated(AddGroup.class) @RequestBody TFreightBo bo) {
        try{
            tFreightService.insertByBo(bo);
            return R.ok(bo);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 修改三级货区管理
     */
    @SaCheckPermission("zhishu:freight:edit")
    @Log(title = "三级货区管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @SaIgnore
    public R<TFreightBo> edit(@Validated(EditGroup.class) @RequestBody TFreightBo bo) {
        try{
            tFreightService.updateByBo(bo);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return R.ok(bo);
    }

    /**
     * 删除三级货区管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:freight: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(tFreightService.deleteWithValidByIds(List.of(ids), true,LocalUserId));
    }

    /**
     * 查询货架id,code列表
     */
//    @SaIgnore
    @SaCheckPermission("shelves:shelves:sheList")
    @GetMapping("/sheList")
    @SaIgnore
    public TableDataInfo<TShelvesVo> listShe() {
        List<TShelvesVo> list=  tFreightService.querySheNameList();
        return TableDataInfo.build(list);
    }

}
