package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.EquipmentRecordService;
import com.gxwebsoft.apps.entity.EquipmentRecord;
import com.gxwebsoft.apps.param.EquipmentRecordParam;
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-03 01:23:53
 */
@Api(tags = "前世今生管理")
@RestController
@RequestMapping("/api/apps/equipment-record")
public class EquipmentRecordController extends BaseController {
    @Resource
    private EquipmentRecordService equipmentRecordService;

    @PreAuthorize("hasAuthority('apps:equipmentRecord:list')")
    @OperationLog
    @ApiOperation("分页查询前世今生")
    @GetMapping("/page")
    public ApiResult<PageResult<EquipmentRecord>> page(EquipmentRecordParam param) {
        PageParam<EquipmentRecord, EquipmentRecordParam> page = new PageParam<>(param);
        // 搜索条件
        if (getMerchantCode() != null) {
          param.setMerchantCode(getMerchantCode());
        }
//        page.setDefaultOrder("create_time desc");
//        return success(equipmentRecordService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(equipmentRecordService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:list')")
    @OperationLog
    @ApiOperation("查询全部前世今生")
    @GetMapping()
    public ApiResult<List<EquipmentRecord>> list(EquipmentRecordParam param) {
        PageParam<EquipmentRecord, EquipmentRecordParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(equipmentRecordService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(equipmentRecordService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:list')")
    @OperationLog
    @ApiOperation("根据id查询前世今生")
    @GetMapping("/{id}")
    public ApiResult<EquipmentRecord> get(@PathVariable("id") Integer id) {
//        return success(equipmentRecordService.getById(id));
        // 使用关联查询
        return success(equipmentRecordService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:save')")
    @OperationLog
    @ApiOperation("添加前世今生")
    @PostMapping()
    public ApiResult<?> save(@RequestBody EquipmentRecord equipmentRecord) {
        // 记录当前登录用户id、租户id、商户编号
        User loginUser = getLoginUser();
        if (loginUser != null) {
          equipmentRecord.setUserId(loginUser.getUserId());
          equipmentRecord.setMerchantCode(getMerchantCode());
        }
        if (equipmentRecordService.save(equipmentRecord)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:update')")
    @OperationLog
    @ApiOperation("修改前世今生")
    @PutMapping()
    public ApiResult<?> update(@RequestBody EquipmentRecord equipmentRecord) {
        if (equipmentRecordService.updateById(equipmentRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:remove')")
    @OperationLog
    @ApiOperation("删除前世今生")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (equipmentRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:save')")
    @OperationLog
    @ApiOperation("批量添加前世今生")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<EquipmentRecord> list) {
        if (equipmentRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:update')")
    @OperationLog
    @ApiOperation("批量修改前世今生")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<EquipmentRecord> batchParam) {
        if (batchParam.update(equipmentRecordService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentRecord:remove')")
    @OperationLog
    @ApiOperation("批量删除前世今生")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (equipmentRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
