package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.entity.TowerProjectOrder;
import com.gxwebsoft.tower.param.TowerProjectOrderParam;
import com.gxwebsoft.tower.service.TowerProjectOrderService;
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-06-07 18:48:56
 */
@Api(tags = "合同签订设备清单管理")
@RestController
@RequestMapping("/api/tower/tower-project-order")
public class TowerProjectOrderController extends BaseController {
    @Resource
    private TowerProjectOrderService towerProjectOrderService;

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:list')")
    @OperationLog
    @ApiOperation("分页查询合同签订设备清单")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerProjectOrder>> page(TowerProjectOrderParam param) {
        // 使用关联查询
        return success(towerProjectOrderService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:list')")
    @OperationLog
    @ApiOperation("查询全部合同签订设备清单")
    @GetMapping()
    public ApiResult<List<TowerProjectOrder>> list(TowerProjectOrderParam param) {
        // 使用关联查询
        return success(towerProjectOrderService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:list')")
    @OperationLog
    @ApiOperation("根据id查询合同签订设备清单")
    @GetMapping("/{id}")
    public ApiResult<TowerProjectOrder> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(towerProjectOrderService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:save')")
    @OperationLog
    @ApiOperation("添加合同签订设备清单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerProjectOrder towerProjectOrder) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerProjectOrder.setUserId(loginUser.getUserId());
        }
        if (towerProjectOrderService.save(towerProjectOrder)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:update')")
    @OperationLog
    @ApiOperation("修改合同签订设备清单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerProjectOrder towerProjectOrder) {
        if (towerProjectOrderService.updateById(towerProjectOrder)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:remove')")
    @OperationLog
    @ApiOperation("删除合同签订设备清单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerProjectOrderService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:save')")
    @OperationLog
    @ApiOperation("批量添加合同签订设备清单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerProjectOrder> list) {
        if (towerProjectOrderService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:update')")
    @OperationLog
    @ApiOperation("批量修改合同签订设备清单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerProjectOrder> batchParam) {
        if (batchParam.update(towerProjectOrderService, "project_order_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectOrder:remove')")
    @OperationLog
    @ApiOperation("批量删除合同签订设备清单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerProjectOrderService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
