package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerWorksheetCarDispatchService;
import com.gxwebsoft.tower.entity.TowerWorksheetCarDispatch;
import com.gxwebsoft.tower.param.TowerWorksheetCarDispatchParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆调度控制器
 *
 * @author 科技小王子
 * @since 2023-10-31 12:05:49
 */
@Api(tags = "车辆调度管理")
@RestController
@RequestMapping("/api/tower/tower-worksheet-car-dispatch")
public class TowerWorksheetCarDispatchController extends BaseController {
    @Resource
    private TowerWorksheetCarDispatchService towerWorksheetCarDispatchService;

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:list')")
    @OperationLog
    @ApiOperation("分页查询车辆调度")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerWorksheetCarDispatch>> page(TowerWorksheetCarDispatchParam param) {
        PageParam<TowerWorksheetCarDispatch, TowerWorksheetCarDispatchParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetCarDispatchService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerWorksheetCarDispatchService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:list')")
    @OperationLog
    @ApiOperation("查询全部车辆调度")
    @GetMapping()
    public ApiResult<List<TowerWorksheetCarDispatch>> list(TowerWorksheetCarDispatchParam param) {
        PageParam<TowerWorksheetCarDispatch, TowerWorksheetCarDispatchParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetCarDispatchService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerWorksheetCarDispatchService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:list')")
    @OperationLog
    @ApiOperation("根据id查询车辆调度")
    @GetMapping("/{id}")
    public ApiResult<TowerWorksheetCarDispatch> get(@PathVariable("id") Integer id) {
        return success(towerWorksheetCarDispatchService.getById(id));
        // 使用关联查询
        //return success(towerWorksheetCarDispatchService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:save')")
    @OperationLog
    @ApiOperation("添加车辆调度")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerWorksheetCarDispatch towerWorksheetCarDispatch) {
        if (towerWorksheetCarDispatchService.save(towerWorksheetCarDispatch)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:update')")
    @OperationLog
    @ApiOperation("修改车辆调度")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerWorksheetCarDispatch towerWorksheetCarDispatch) {
        if (towerWorksheetCarDispatchService.updateById(towerWorksheetCarDispatch)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:remove')")
    @OperationLog
    @ApiOperation("删除车辆调度")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerWorksheetCarDispatchService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:save')")
    @OperationLog
    @ApiOperation("批量添加车辆调度")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerWorksheetCarDispatch> list) {
        if (towerWorksheetCarDispatchService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:update')")
    @OperationLog
    @ApiOperation("批量修改车辆调度")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerWorksheetCarDispatch> batchParam) {
        if (batchParam.update(towerWorksheetCarDispatchService, "car_dispatch_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCarDispatch:remove')")
    @OperationLog
    @ApiOperation("批量删除车辆调度")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerWorksheetCarDispatchService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
