package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerWorksheetSetUpService;
import com.gxwebsoft.tower.entity.TowerWorksheetSetUp;
import com.gxwebsoft.tower.param.TowerWorksheetSetUpParam;
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-07-03 10:18:57
 */
@Api(tags = "派单安装信息管理")
@RestController
@RequestMapping("/api/tower/tower-worksheet-set-up")
public class TowerWorksheetSetUpController extends BaseController {
    @Resource
    private TowerWorksheetSetUpService towerWorksheetSetUpService;

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:list')")
    @OperationLog
    @ApiOperation("分页查询派单安装信息")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerWorksheetSetUp>> page(TowerWorksheetSetUpParam param) {
        PageParam<TowerWorksheetSetUp, TowerWorksheetSetUpParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetSetUpService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerWorksheetSetUpService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:list')")
    @OperationLog
    @ApiOperation("查询全部派单安装信息")
    @GetMapping()
    public ApiResult<List<TowerWorksheetSetUp>> list(TowerWorksheetSetUpParam param) {
        PageParam<TowerWorksheetSetUp, TowerWorksheetSetUpParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetSetUpService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerWorksheetSetUpService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:list')")
    @OperationLog
    @ApiOperation("根据id查询派单安装信息")
    @GetMapping("/{id}")
    public ApiResult<TowerWorksheetSetUp> get(@PathVariable("id") Integer id) {
        return success(towerWorksheetSetUpService.getById(id));
        // 使用关联查询
        //return success(towerWorksheetSetUpService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:save')")
    @OperationLog
    @ApiOperation("添加派单安装信息")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerWorksheetSetUp towerWorksheetSetUp) {
        if (towerWorksheetSetUpService.save(towerWorksheetSetUp)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:update')")
    @OperationLog
    @ApiOperation("修改派单安装信息")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerWorksheetSetUp towerWorksheetSetUp) {
        if (towerWorksheetSetUpService.updateById(towerWorksheetSetUp)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:remove')")
    @OperationLog
    @ApiOperation("删除派单安装信息")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerWorksheetSetUpService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:save')")
    @OperationLog
    @ApiOperation("批量添加派单安装信息")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerWorksheetSetUp> list) {
        if (towerWorksheetSetUpService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:update')")
    @OperationLog
    @ApiOperation("批量修改派单安装信息")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerWorksheetSetUp> batchParam) {
        if (batchParam.update(towerWorksheetSetUpService, "set_up_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetSetUp:remove')")
    @OperationLog
    @ApiOperation("批量删除派单安装信息")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerWorksheetSetUpService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
