package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopExpressTemplateService;
import com.gxwebsoft.shop.entity.ShopExpressTemplate;
import com.gxwebsoft.shop.param.ShopExpressTemplateParam;
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-template")
public class ShopExpressTemplateController extends BaseController {
    @Resource
    private ShopExpressTemplateService shopExpressTemplateService;

    @Operation(summary = "分页查询运费模板")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopExpressTemplate>> page(ShopExpressTemplateParam param) {
        // 使用关联查询
        return success(shopExpressTemplateService.pageRel(param));
    }

    @Operation(summary = "查询全部运费模板")
    @GetMapping()
    public ApiResult<List<ShopExpressTemplate>> list(ShopExpressTemplateParam param) {
        // 使用关联查询
        return success(shopExpressTemplateService.listRel(param));
    }

    @Operation(summary = "根据id查询运费模板")
    @GetMapping("/{id}")
    public ApiResult<ShopExpressTemplate> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopExpressTemplateService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopExpressTemplate:save')")
    @OperationLog
    @Operation(summary = "添加运费模板")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopExpressTemplate shopExpressTemplate) {
        if (shopExpressTemplateService.save(shopExpressTemplate)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpressTemplate:update')")
    @OperationLog
    @Operation(summary = "修改运费模板")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopExpressTemplate shopExpressTemplate) {
        if (shopExpressTemplateService.updateById(shopExpressTemplate)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpressTemplate:remove')")
    @OperationLog
    @Operation(summary = "删除运费模板")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopExpressTemplateService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpressTemplate:save')")
    @OperationLog
    @Operation(summary = "批量添加运费模板")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopExpressTemplate> list) {
        if (shopExpressTemplateService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopExpressTemplate:update')")
    @OperationLog
    @Operation(summary = "批量修改运费模板")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopExpressTemplate> batchParam) {
        if (batchParam.update(shopExpressTemplateService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
