package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.OrderExtractService;
import com.gxwebsoft.shop.entity.OrderExtract;
import com.gxwebsoft.shop.param.OrderExtractParam;
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 2024-09-24 01:39:23
 */
@Api(tags = "自提订单联系方式管理")
@RestController
@RequestMapping("/api/shop/order-extract")
public class OrderExtractController extends BaseController {
    @Resource
    private OrderExtractService orderExtractService;

    @ApiOperation("分页查询自提订单联系方式")
    @GetMapping("/page")
    public ApiResult<PageResult<OrderExtract>> page(OrderExtractParam param) {
        // 使用关联查询
        return success(orderExtractService.pageRel(param));
    }

    @ApiOperation("查询全部自提订单联系方式")
    @GetMapping()
    public ApiResult<List<OrderExtract>> list(OrderExtractParam param) {
        // 使用关联查询
        return success(orderExtractService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:orderExtract:list')")
    @ApiOperation("根据id查询自提订单联系方式")
    @GetMapping("/{id}")
    public ApiResult<OrderExtract> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(orderExtractService.getByIdRel(id));
    }

    @ApiOperation("添加自提订单联系方式")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderExtract orderExtract) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         orderExtract.setUserId(loginUser.getUserId());
        }
        if (orderExtractService.save(orderExtract)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改自提订单联系方式")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderExtract orderExtract) {
        if (orderExtractService.updateById(orderExtract)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除自提订单联系方式")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderExtractService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加自提订单联系方式")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderExtract> list) {
        if (orderExtractService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改自提订单联系方式")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OrderExtract> batchParam) {
        if (batchParam.update(orderExtractService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除自提订单联系方式")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderExtractService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
