package org.dromara.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.TblCarInfoBo;
import org.dromara.system.domain.vo.TblCarInfoVo;
import org.dromara.system.service.ISysDataScopeService;
import org.dromara.system.service.ITblCarInfoService;
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.system.domain.vo.TblCarScheduleVo;
import org.dromara.system.domain.bo.TblCarScheduleBo;
import org.dromara.system.service.ITblCarScheduleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 车及人员排班
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/carSchedule")
public class TblCarScheduleController extends TblBaseController {

    private final ITblCarScheduleService tblCarScheduleService;

    private final ITblCarInfoService tblCarInfoService;

    private final ISysDataScopeService dataScopeService;

    /**
     * 查询车及人员排班列表
     */
    //@SaCheckPermission("system:carSchedule:list")
    @GetMapping("/list")
    public TableDataInfo<TblCarScheduleVo> list(TblCarScheduleBo bo, PageQuery pageQuery) {
        getDepts(dataScopeService,bo);
        TblCarInfoBo bo1 = new TblCarInfoBo();
        bo1.setParams(bo.getParams());
        List<TblCarInfoVo> cars = tblCarInfoService.queryList(bo1);
        bo.getParams().put("cars", cars.stream().map(TblCarInfoVo::getCarNum).collect(Collectors.joining(",")));
        return tblCarScheduleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出车及人员排班列表
     */
    //@SaCheckPermission("system:carSchedule:export")
    @Log(title = "车及人员排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblCarScheduleBo bo, HttpServletResponse response) {
        List<TblCarScheduleVo> list = tblCarScheduleService.queryList(bo);
        ExcelUtil.exportExcel(list, "车及人员排班", TblCarScheduleVo.class, response);
    }

    /**
     * 获取车及人员排班详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("system:carSchedule:query")
    @GetMapping("/{id}")
    public R<TblCarScheduleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tblCarScheduleService.queryById(id));
    }

    /**
     * 新增车及人员排班
     */
    //@SaCheckPermission("system:carSchedule:add")
    @Log(title = "车及人员排班", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TblCarScheduleBo bo) {
        return toAjax(tblCarScheduleService.insertByBo(bo));
    }

    /**
     * 修改车及人员排班
     */
    //@SaCheckPermission("system:carSchedule:edit")
    @Log(title = "车及人员排班", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblCarScheduleBo bo) {
        return toAjax(tblCarScheduleService.updateByBo(bo));
    }

    /**
     * 删除车及人员排班
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("system:carSchedule:remove")
    @Log(title = "车及人员排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tblCarScheduleService.deleteWithValidByIds(List.of(ids), true));
    }
}
