package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.EquipmentOrderService;
import com.gxwebsoft.apps.entity.EquipmentOrder;
import com.gxwebsoft.apps.param.EquipmentOrderParam;
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-04-14 21:24:31
 */
@Api(tags = "订单记录表管理")
@RestController
@RequestMapping("/api/apps/equipment-order")
public class EquipmentOrderController extends BaseController {
    @Resource
    private EquipmentOrderService equipmentOrderService;

    @PreAuthorize("hasAuthority('apps:equipmentOrder:list')")
    @OperationLog
    @ApiOperation("分页查询订单记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<EquipmentOrder>> page(EquipmentOrderParam param) {
//        PageParam<EquipmentOrder, EquipmentOrderParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
//        return success(equipmentOrderService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(equipmentOrderService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentOrder:list')")
    @OperationLog
    @ApiOperation("查询全部订单记录表")
    @GetMapping()
    public ApiResult<List<EquipmentOrder>> list(EquipmentOrderParam param) {
        PageParam<EquipmentOrder, EquipmentOrderParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(equipmentOrderService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(equipmentOrderService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:equipmentOrder:list')")
    @OperationLog
    @ApiOperation("根据id查询订单记录表")
    @GetMapping("/{id}")
    public ApiResult<EquipmentOrder> get(@PathVariable("id") Integer id) {
        return success(equipmentOrderService.getById(id));
        // 使用关联查询
        //return success(equipmentOrderService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('apps:equipmentOrder:update')")
    @OperationLog
    @ApiOperation("修改订单记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody EquipmentOrder equipmentOrder) {
        if (equipmentOrderService.updateById(equipmentOrder)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('apps:equipmentOrder:save')")
    @OperationLog
    @ApiOperation("批量添加订单记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<EquipmentOrder> list) {
        if (equipmentOrderService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:equipmentOrder:update')")
    @OperationLog
    @ApiOperation("批量修改订单记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<EquipmentOrder> batchParam) {
        if (batchParam.update(equipmentOrderService, "order_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
