package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.EquipmentFaultService;
import com.gxwebsoft.apps.entity.EquipmentFault;
import com.gxwebsoft.apps.param.EquipmentFaultParam;
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 2022-12-01 18:40:25
 */
@Api(tags = "故障电池管理")
@RestController
@RequestMapping("/api/apps/equipment-fault")
public class EquipmentFaultController extends BaseController {
    @Resource
    private EquipmentFaultService equipmentFaultService;

    @PreAuthorize("hasAuthority('apps:equipmentFault:list')")
    @OperationLog
    @ApiOperation("分页查询故障电池")
    @GetMapping("/page")
    public ApiResult<PageResult<EquipmentFault>> page(EquipmentFaultParam param) {
        PageParam<EquipmentFault, EquipmentFaultParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(equipmentFaultService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(equipmentFaultService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:list')")
    @OperationLog
    @ApiOperation("查询全部故障电池")
    @GetMapping()
    public ApiResult<List<EquipmentFault>> list(EquipmentFaultParam param) {
        PageParam<EquipmentFault, EquipmentFaultParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(equipmentFaultService.list(page.getOrderWrapper()));
        // 使用关联查询
//        return success(equipmentFaultService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:list')")
    @OperationLog
    @ApiOperation("根据id查询故障电池")
    @GetMapping("/{id}")
    public ApiResult<EquipmentFault> get(@PathVariable("id") Integer id) {
        return success(equipmentFaultService.getById(id));
        // 使用关联查询
        //return success(equipmentFaultService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:save')")
    @OperationLog
    @ApiOperation("添加故障电池")
    @PostMapping()
    public ApiResult<?> save(@RequestBody EquipmentFault equipmentFault) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          equipmentFault.setUserId(loginUser.getUserId());
          equipmentFault.setMerchantCode(getMerchantCode());
        }
        if (equipmentFaultService.save(equipmentFault)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:update')")
    @OperationLog
    @ApiOperation("修改故障电池")
    @PutMapping()
    public ApiResult<?> update(@RequestBody EquipmentFault equipmentFault) {
        if (equipmentFaultService.updateById(equipmentFault)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:remove')")
    @OperationLog
    @ApiOperation("删除故障电池")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (equipmentFaultService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:save')")
    @OperationLog
    @ApiOperation("批量添加故障电池")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<EquipmentFault> list) {
        if (equipmentFaultService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentFault:update')")
    @OperationLog
    @ApiOperation("批量修改故障电池")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<EquipmentFault> batchParam) {
        if (batchParam.update(equipmentFaultService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
