package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopExpressService;
import com.gxwebsoft.shop.entity.ShopExpress;
import com.gxwebsoft.shop.param.ShopExpressParam;
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.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 2025-08-12 12:07:03
 */
@Tag(name = "物流公司管理")
@RestController
@RequestMapping("/api/shop/shop-express")
public class ShopExpressController extends BaseController {
    @Resource
    private ShopExpressService shopExpressService;

    @Operation(summary = "分页查询物流公司")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopExpress>> page(ShopExpressParam param) {
        // 使用关联查询
        return success(shopExpressService.pageRel(param));
    }

    @Operation(summary = "查询全部物流公司")
    @GetMapping()
    public ApiResult<List<ShopExpress>> list(ShopExpressParam param) {
        // 使用关联查询
        return success(shopExpressService.listRel(param));
    }

    @Operation(summary = "根据id查询物流公司")
    @GetMapping("/{id}")
    public ApiResult<ShopExpress> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopExpressService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopExpress:save')")
    @OperationLog
    @Operation(summary = "添加物流公司")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopExpress shopExpress) {
        if (shopExpressService.save(shopExpress)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpress:update')")
    @OperationLog
    @Operation(summary = "修改物流公司")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopExpress shopExpress) {
        if (shopExpressService.updateById(shopExpress)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpress:remove')")
    @OperationLog
    @Operation(summary = "删除物流公司")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopExpressService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpress:save')")
    @OperationLog
    @Operation(summary = "批量添加物流公司")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopExpress> list) {
        if (shopExpressService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpress:update')")
    @OperationLog
    @Operation(summary = "批量修改物流公司")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopExpress> batchParam) {
        if (batchParam.update(shopExpressService, "express_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
