package org.dromara.zhishu.controller;

import java.util.List;

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.zhishu.domain.bo.PrinterBo;
import org.dromara.zhishu.domain.bo.TLogisticsBo;
import org.dromara.zhishu.domain.vo.PrinterVo;
import org.dromara.zhishu.domain.vo.TDepotVo;
import org.dromara.zhishu.domain.vo.TLogisticsVo;
import org.dromara.zhishu.service.ITLogisticsService;
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-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/logistics")
public class TLogisticsController extends BaseController {

    private final ITLogisticsService tLogisticsService;

    /**
     * 查询物流管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<TLogisticsVo> list(TLogisticsBo bo, PageQuery pageQuery) {
        return tLogisticsService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询全部打印机设置
     * @param bo
     * @return
     */
    @GetMapping("/listNoPage")
    public List<TLogisticsVo> listNoPage(TLogisticsBo bo){
        return tLogisticsService.queryList(bo);
    }

    /**
     * 导出物流管理列表
     */
    @SaCheckPermission("zhishu:logistics:export")
    @Log(title = "物流管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TLogisticsBo bo, HttpServletResponse response) {
        List<TLogisticsVo> list = tLogisticsService.queryList(bo);
        ExcelUtil.exportExcel(list, "物流管理", TLogisticsVo.class, response);
    }

    /**
     * 获取物流管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:logistics:query")
    @GetMapping("/{id}")
    public R<TLogisticsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tLogisticsService.queryById(id));
    }

    /**
     * 新增物流管理
     */
    @Log(title = "物流管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @SaIgnore
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TLogisticsBo bo) {
        return toAjax(tLogisticsService.insertByBo(bo));
    }

    /**
     * 修改物流管理
     */
    @Log(title = "物流管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @SaIgnore
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TLogisticsBo bo) {
        return toAjax(tLogisticsService.updateByBo(bo));
    }

    /**
     * 删除物流管理
     *
     * @param ids 主键串
     */
    @Log(title = "物流管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @SaIgnore
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tLogisticsService.deleteWithValidByIds(List.of(ids), true));
    }

//    @SaCheckPermission("zhishu:logistics:namelist")
    @GetMapping("/namelist")
    @SaIgnore
    public TableDataInfo<TLogisticsVo> list1() {
        List<TLogisticsVo> list=  tLogisticsService.queryNameList();
        return TableDataInfo.build(list);
    }

    @GetMapping("/getList")
    @SaIgnore
    public TableDataInfo<TLogisticsVo> getList(Long userId) {
        List<TLogisticsVo> list=  tLogisticsService.getList(userId);
        return TableDataInfo.build(list);
    }

    /**
     * 小程序获取运费模板
     */
    @GetMapping("/xcx/list")
    @SaIgnore
    public TableDataInfo<TLogisticsVo> xcxList(@RequestParam Long userId) {
        List<TLogisticsVo> list=  tLogisticsService.queryNameListByXcx(userId);
        return TableDataInfo.build(list);
    }
}
