package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.tower.entity.TowerProjectPlace;
import com.gxwebsoft.tower.param.TowerProjectPlaceParam;
import com.gxwebsoft.tower.service.TowerProjectPlaceService;
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-08 17:22:55
 */
@Api(tags = "检查/保养部位配置表管理")
@RestController
@RequestMapping("/api/tower/tower-project-place")
public class TowerProjectPlaceController extends BaseController {
    @Resource
    private TowerProjectPlaceService towerProjectPlaceService;

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:list')")
    @OperationLog
    @ApiOperation("分页查询检查/保养部位配置表")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerProjectPlace>> page(TowerProjectPlaceParam param) {
        // 使用关联查询
        return success(towerProjectPlaceService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:list')")
    @OperationLog
    @ApiOperation("查询全部检查/保养部位配置表")
    @GetMapping()
    public ApiResult<List<TowerProjectPlace>> list(TowerProjectPlaceParam param) {
        // 使用关联查询
        return success(towerProjectPlaceService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:list')")
    @OperationLog
    @ApiOperation("根据id查询检查/保养部位配置表")
    @GetMapping("/{id}")
    public ApiResult<TowerProjectPlace> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(towerProjectPlaceService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:save')")
    @OperationLog
    @ApiOperation("添加检查/保养部位配置表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerProjectPlace towerProjectPlace) {
      if (towerProjectPlaceService.count(new LambdaQueryWrapper<TowerProjectPlace>()
        .eq(TowerProjectPlace::getProjectId, towerProjectPlace.getProjectId()).eq(TowerProjectPlace::getPlaceId,towerProjectPlace.getPlaceId())) > 0) {
        return fail("该部位已存在");
      }
      if (towerProjectPlaceService.save(towerProjectPlace)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:update')")
    @OperationLog
    @ApiOperation("修改检查/保养部位配置表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerProjectPlace towerProjectPlace) {
        if (towerProjectPlaceService.updateById(towerProjectPlace)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:remove')")
    @OperationLog
    @ApiOperation("删除检查/保养部位配置表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerProjectPlaceService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:save')")
    @OperationLog
    @ApiOperation("批量添加检查/保养部位配置表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerProjectPlace> list) {
        if (towerProjectPlaceService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:update')")
    @OperationLog
    @ApiOperation("批量修改检查/保养部位配置表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerProjectPlace> batchParam) {
        if (batchParam.update(towerProjectPlaceService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectPlace:remove')")
    @OperationLog
    @ApiOperation("批量删除检查/保养部位配置表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerProjectPlaceService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
