package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopExpressTemplateDetailService;
import com.gxwebsoft.shop.entity.ShopExpressTemplateDetail;
import com.gxwebsoft.shop.param.ShopExpressTemplateDetailParam;
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-detail")
public class ShopExpressTemplateDetailController extends BaseController {
    @Resource
    private ShopExpressTemplateDetailService shopExpressTemplateDetailService;

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

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

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

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

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

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

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

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

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

}
