package com.gxwebsoft.apps.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.apps.entity.Equipment;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.EquipmentGoodsService;
import com.gxwebsoft.apps.entity.EquipmentGoods;
import com.gxwebsoft.apps.param.EquipmentGoodsParam;
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 com.gxwebsoft.common.system.entity.User;
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-02-28 22:40:50
 */
@Api(tags = "电池管理记录表管理")
@RestController
@RequestMapping("/api/apps/equipment-goods")
public class EquipmentGoodsController extends BaseController {
    @Resource
    private EquipmentGoodsService equipmentGoodsService;

    @PreAuthorize("hasAuthority('apps:equipmentGoods:list')")
    @OperationLog
    @ApiOperation("分页查询电池管理记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<EquipmentGoods>> page(EquipmentGoodsParam param) {
        // 使用关联查询
        return success(equipmentGoodsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:list')")
    @OperationLog
    @ApiOperation("查询全部电池管理记录表")
    @GetMapping()
    public ApiResult<List<EquipmentGoods>> list(EquipmentGoodsParam param) {
        // 使用关联查询
        return success(equipmentGoodsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:list')")
    @OperationLog
    @ApiOperation("根据id查询电池管理记录表")
    @GetMapping("/{id}")
    public ApiResult<EquipmentGoods> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(equipmentGoodsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:save')")
    @OperationLog
    @ApiOperation("添加电池管理记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody EquipmentGoods equipmentGoods) {
      System.out.println("equipmentGoods = " + equipmentGoods);
        if (equipmentGoodsService.save(equipmentGoods)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:update')")
    @OperationLog
    @ApiOperation("修改电池管理记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody EquipmentGoods equipmentGoods) {
        if (equipmentGoodsService.updateById(equipmentGoods)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:remove')")
    @OperationLog
    @ApiOperation("删除电池管理记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (equipmentGoodsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:save')")
    @OperationLog
    @ApiOperation("批量添加电池管理记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<EquipmentGoods> list) {
        if (equipmentGoodsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:update')")
    @OperationLog
    @ApiOperation("批量修改电池管理记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<EquipmentGoods> batchParam) {
        if (batchParam.update(equipmentGoodsService, "goods_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentGoods:remove')")
    @OperationLog
    @ApiOperation("批量删除电池管理记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (equipmentGoodsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
