package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopStoreRiderService;
import com.gxwebsoft.shop.entity.ShopStoreRider;
import com.gxwebsoft.shop.param.ShopStoreRiderParam;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 配送员控制器
 *
 * @author 科技小王子
 * @since 2026-01-30 15:14:16
 */
@Tag(name = "配送员管理")
@RestController
@RequestMapping("/api/shop/shop-store-rider")
public class ShopStoreRiderController extends BaseController {
    @Resource
    private ShopStoreRiderService shopStoreRiderService;

    @Operation(summary = "分页查询配送员")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopStoreRider>> page(ShopStoreRiderParam param) {
        // 使用关联查询
        return success(shopStoreRiderService.pageRel(param));
    }

    @Operation(summary = "查询全部配送员")
    @GetMapping()
    public ApiResult<List<ShopStoreRider>> list(ShopStoreRiderParam param) {
        // 使用关联查询
        return success(shopStoreRiderService.listRel(param));
    }

    @Operation(summary = "根据id查询配送员")
    @GetMapping("/{id}")
    public ApiResult<ShopStoreRider> get(@PathVariable("id") Long id) {
        // 使用关联查询
        return success(shopStoreRiderService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopStoreRider:save')")
    @OperationLog
    @Operation(summary = "添加配送员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopStoreRider shopStoreRider) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        // shopStoreRider.setUserId(loginUser.getUserId());
        // }
        if (shopStoreRiderService.save(shopStoreRider)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopStoreRider:update')")
    @OperationLog
    @Operation(summary = "修改配送员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopStoreRider shopStoreRider) {
        if (shopStoreRiderService.updateById(shopStoreRider)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopStoreRider:remove')")
    @OperationLog
    @Operation(summary = "删除配送员")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopStoreRiderService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopStoreRider:save')")
    @OperationLog
    @Operation(summary = "批量添加配送员")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopStoreRider> list) {
        if (shopStoreRiderService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopStoreRider:update')")
    @OperationLog
    @Operation(summary = "批量修改配送员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopStoreRider> batchParam) {
        if (batchParam.update(shopStoreRiderService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopStoreRider:remove')")
    @OperationLog
    @Operation(summary = "批量删除配送员")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopStoreRiderService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
