package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerWorksheetPartsService;
import com.gxwebsoft.tower.entity.TowerWorksheetParts;
import com.gxwebsoft.tower.param.TowerWorksheetPartsParam;
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-parts")
public class TowerWorksheetPartsController extends BaseController {
    @Resource
    private TowerWorksheetPartsService towerWorksheetPartsService;

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:list')")
    @OperationLog
    @ApiOperation("分页查询派单配件")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerWorksheetParts>> page(TowerWorksheetPartsParam param) {
        PageParam<TowerWorksheetParts, TowerWorksheetPartsParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetPartsService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerWorksheetPartsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:list')")
    @OperationLog
    @ApiOperation("查询全部派单配件")
    @GetMapping()
    public ApiResult<List<TowerWorksheetParts>> list(TowerWorksheetPartsParam param) {
        PageParam<TowerWorksheetParts, TowerWorksheetPartsParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetPartsService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerWorksheetPartsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:list')")
    @OperationLog
    @ApiOperation("根据id查询派单配件")
    @GetMapping("/{id}")
    public ApiResult<TowerWorksheetParts> get(@PathVariable("id") Integer id) {
        return success(towerWorksheetPartsService.getById(id));
        // 使用关联查询
        //return success(towerWorksheetPartsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:save')")
    @OperationLog
    @ApiOperation("添加派单配件")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerWorksheetParts towerWorksheetParts) {
        if (towerWorksheetPartsService.save(towerWorksheetParts)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:update')")
    @OperationLog
    @ApiOperation("修改派单配件")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerWorksheetParts towerWorksheetParts) {
        if (towerWorksheetPartsService.updateById(towerWorksheetParts)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:remove')")
    @OperationLog
    @ApiOperation("删除派单配件")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerWorksheetPartsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:save')")
    @OperationLog
    @ApiOperation("批量添加派单配件")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerWorksheetParts> list) {
        if (towerWorksheetPartsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetParts:update')")
    @OperationLog
    @ApiOperation("批量修改派单配件")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerWorksheetParts> batchParam) {
        if (batchParam.update(towerWorksheetPartsService, "parts_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
