package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerSecurityRecordService;
import com.gxwebsoft.tower.entity.TowerSecurityRecord;
import com.gxwebsoft.tower.param.TowerSecurityRecordParam;
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 2024-02-22 13:44:23
 */
@Api(tags = "保养记录管理")
@RestController
@RequestMapping("/api/tower/tower-security-record")
public class TowerSecurityRecordController extends BaseController {
    @Resource
    private TowerSecurityRecordService towerSecurityRecordService;

    @PreAuthorize("hasAuthority('tower:towerSecurity:list')")
    @OperationLog
    @ApiOperation("分页查询保养记录")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerSecurityRecord>> page(TowerSecurityRecordParam param) {
//        PageParam<TowerSecurityRecord, TowerSecurityRecordParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
//        return success(towerSecurityRecordService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(towerSecurityRecordService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:list')")
    @OperationLog
    @ApiOperation("查询全部保养记录")
    @GetMapping()
    public ApiResult<List<TowerSecurityRecord>> list(TowerSecurityRecordParam param) {
        PageParam<TowerSecurityRecord, TowerSecurityRecordParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerSecurityRecordService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerSecurityRecordService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:list')")
    @OperationLog
    @ApiOperation("根据id查询保养记录")
    @GetMapping("/{id}")
    public ApiResult<TowerSecurityRecord> get(@PathVariable("id") Integer id) {
        return success(towerSecurityRecordService.getById(id));
        // 使用关联查询
        //return success(towerSecurityRecordService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:save')")
    @OperationLog
    @ApiOperation("添加保养记录")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerSecurityRecord towerSecurityRecord) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerSecurityRecord.setUserId(loginUser.getUserId());
        }
        if (towerSecurityRecordService.save(towerSecurityRecord)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:update')")
    @OperationLog
    @ApiOperation("修改保养记录")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerSecurityRecord towerSecurityRecord) {
        if (towerSecurityRecordService.updateById(towerSecurityRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:remove')")
    @OperationLog
    @ApiOperation("删除保养记录")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerSecurityRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:save')")
    @OperationLog
    @ApiOperation("批量添加保养记录")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerSecurityRecord> list) {
        if (towerSecurityRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:update')")
    @OperationLog
    @ApiOperation("批量修改保养记录")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerSecurityRecord> batchParam) {
        if (batchParam.update(towerSecurityRecordService, "security_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSecurity:remove')")
    @OperationLog
    @ApiOperation("批量删除保养记录")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerSecurityRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
