package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.EquipmentOrderGoodsService;
import com.gxwebsoft.apps.entity.EquipmentOrderGoods;
import com.gxwebsoft.apps.param.EquipmentOrderGoodsParam;
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-05-11 19:10:13
 */
@Api(tags = "电池管理记录表管理")
@RestController
@RequestMapping("/api/apps/equipment-order-goods")
public class EquipmentOrderGoodsController extends BaseController {
    @Resource
    private EquipmentOrderGoodsService equipmentOrderGoodsService;

    @PreAuthorize("hasAuthority('apps:equipmentOrderGoods:list')")
    @OperationLog
    @ApiOperation("分页查询电池管理记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<EquipmentOrderGoods>> page(EquipmentOrderGoodsParam param) {
        PageParam<EquipmentOrderGoods, EquipmentOrderGoodsParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(equipmentOrderGoodsService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(equipmentOrderGoodsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentOrderGoods:list')")
    @OperationLog
    @ApiOperation("查询全部电池管理记录表")
    @GetMapping()
    public ApiResult<List<EquipmentOrderGoods>> list(EquipmentOrderGoodsParam param) {
//        PageParam<EquipmentOrderGoods, EquipmentOrderGoodsParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
//        System.out.println("param = " + param);
//        final EquipmentOrderGoods eog = equipmentOrderGoodsService.getById(param.getGoodsId());
//      System.out.println("eog = " + eog);
//        eog.setOrderId(param.getOrderId());
//        equipmentOrderGoodsService.updateById(eog);
//
//        final List<EquipmentOrderGoods> list = equipmentOrderGoodsService.list(page.getOrderWrapper());
//
//        return success(equipmentOrderGoodsService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(equipmentOrderGoodsService.listRel(param));
    }

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

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

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

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

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

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

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

}
