package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerWorksheetDetailService;
import com.gxwebsoft.tower.entity.TowerWorksheetDetail;
import com.gxwebsoft.tower.param.TowerWorksheetDetailParam;
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:56:04
 */
@Api(tags = "派单详情管理")
@RestController
@RequestMapping("/api/tower/tower-worksheet-detail")
public class TowerWorksheetDetailController extends BaseController {
    @Resource
    private TowerWorksheetDetailService towerWorksheetDetailService;

    @PreAuthorize("hasAuthority('tower:towerWorksheetDetail:list')")
    @OperationLog
    @ApiOperation("分页查询派单详情")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerWorksheetDetail>> page(TowerWorksheetDetailParam param) {
        PageParam<TowerWorksheetDetail, TowerWorksheetDetailParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetDetailService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerWorksheetDetailService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetDetail:list')")
    @OperationLog
    @ApiOperation("查询全部派单详情")
    @GetMapping()
    public ApiResult<List<TowerWorksheetDetail>> list(TowerWorksheetDetailParam param) {
        PageParam<TowerWorksheetDetail, TowerWorksheetDetailParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetDetailService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerWorksheetDetailService.listRel(param));
    }

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

    @PreAuthorize("hasAuthority('tower:towerWorksheetDetail:save')")
    @OperationLog
    @ApiOperation("添加派单详情")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerWorksheetDetail towerWorksheetDetail) {
        if (towerWorksheetDetailService.save(towerWorksheetDetail)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetDetail:update')")
    @OperationLog
    @ApiOperation("修改派单详情")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerWorksheetDetail towerWorksheetDetail) {
        if (towerWorksheetDetailService.updateById(towerWorksheetDetail)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('tower:towerWorksheetDetail:update')")
    @OperationLog
    @ApiOperation("批量修改派单详情")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerWorksheetDetail> batchParam) {
        if (batchParam.update(towerWorksheetDetailService, "detail_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
