package org.dromara.system.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
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.system.domain.bo.TblDzbcMultiSitePathBo;
import org.dromara.system.domain.vo.TblDzbcMultiSitePathVo;
import org.dromara.system.service.ITblDzbcMultiSitePathService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dzbc/multiSitePath")
public class TblDzbcMultiSitePathController extends BaseController {

    private final ITblDzbcMultiSitePathService tblDzbcMultiSitePathService;

    @GetMapping("/list")
    public TableDataInfo<TblDzbcMultiSitePathVo> list(TblDzbcMultiSitePathBo bo, PageQuery pageQuery) {
        return tblDzbcMultiSitePathService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据站点ID查询包含该站点的多站点路线列表
     * @param siteId 站点ID
     * @param hasSchedul 是否只返回有未过期排班的路线 1-是 0-否(默认)
     */
    @GetMapping("/listBySiteId/{siteId}")
    public R<List<TblDzbcMultiSitePathVo>> listBySiteId(@PathVariable Long siteId,
                                                        @RequestParam(required = false, defaultValue = "0") Integer hasSchedul) {
        return R.ok(tblDzbcMultiSitePathService.queryBySiteId(siteId, hasSchedul));
    }

    @Log(title = "多站点路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblDzbcMultiSitePathBo bo, HttpServletResponse response) {
        List<TblDzbcMultiSitePathVo> list = tblDzbcMultiSitePathService.queryList(bo);
        ExcelUtil.exportExcel(list, "多站点路线", TblDzbcMultiSitePathVo.class, response);
    }

    @GetMapping("/{id}")
    public R<TblDzbcMultiSitePathVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(tblDzbcMultiSitePathService.queryByIdWithDetails(id));
    }

    @Log(title = "多站点路线", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TblDzbcMultiSitePathBo bo) {
        return toAjax(tblDzbcMultiSitePathService.insertByBo(bo));
    }

    @Log(title = "多站点路线", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblDzbcMultiSitePathBo bo) {
        return toAjax(tblDzbcMultiSitePathService.updateByBo(bo));
    }

    @Log(title = "多站点路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(tblDzbcMultiSitePathService.deleteWithValidByIds(List.of(ids), true));
    }
}
