package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopStoreUserService;
import com.gxwebsoft.shop.entity.ShopStoreUser;
import com.gxwebsoft.shop.param.ShopStoreUserParam;
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:00:25
 */
@Tag(name = "店员管理")
@RestController
@RequestMapping("/api/shop/shop-store-user")
public class ShopStoreUserController extends BaseController {
    @Resource
    private ShopStoreUserService shopStoreUserService;

    @Operation(summary = "分页查询店员")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopStoreUser>> page(ShopStoreUserParam param) {
        // 使用关联查询
        return success(shopStoreUserService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:shopStoreUser:list')")
    @Operation(summary = "查询全部店员")
    @GetMapping()
    public ApiResult<List<ShopStoreUser>> list(ShopStoreUserParam param) {
        // 使用关联查询
        return success(shopStoreUserService.listRel(param));
    }

    @Operation(summary = "根据id查询店员")
    @GetMapping("/{id}")
    public ApiResult<ShopStoreUser> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopStoreUserService.getByIdRel(id));
    }

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

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

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

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

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

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

}
