package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.admin.domain.param.ShippingTemplateParam;
import com.qs.mp.admin.domain.vo.ShippingTemplateVO;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.domain.ShippingTemplate;
import com.qs.mp.common.service.IShippingTemplateService;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 运费模板相关接口
 *
 * @author Cup
 * @date 2022/5/5
 */
@Api(tags = "运费模板相关接口")
@RestController
@RequestMapping("/api/v1/mp/admin/shipping/template/*")
public class ShippingTemplateController extends BaseApiController {

    @Autowired
    private IShippingTemplateService shippingTemplateService;

    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('setting:template:add')")
    @ApiOperation("新增运费模板")
    public AjaxResult create(@RequestBody ShippingTemplateParam shippingTemplateParam) {
        // 新增运费模板
        shippingTemplateService.create(shippingTemplateParam);

        return AjaxResult.success();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('setting:template:edit')")
    @ApiOperation("更新运费模板")
    public AjaxResult update(@RequestBody ShippingTemplateParam shippingTemplateParam){
        // 修改运费模板
        shippingTemplateService.updateShippingTemplate(shippingTemplateParam);
        return AjaxResult.success();
    }


    @PostMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPermi('setting:template:query')")
    @ApiOperation("运费模板详情")
    @ApiResponses(
            @ApiResponse(code = 200, message = "返回运费模板详情",response = ShippingTemplateVO.class)
    )
    public AjaxResult detail(@PathVariable("id") Long id){
        // 运费模板详情
        return AjaxResult.success(shippingTemplateService.detail(id));
    }

    @PostMapping("list")
    @PreAuthorize("@ss.hasPermi('setting:template:list')")
    @ApiOperation("运费模板列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "返回运费模板列表",response = ShippingTemplate.class)
    )
    public TableDataInfo list(){
        startPage();
        List<ShippingTemplate> list = shippingTemplateService.list(new LambdaQueryWrapper<ShippingTemplate>()
                .orderByDesc(ShippingTemplate::getCreatedTime));
        // 运费模板列表
        return getDataTable(list);
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('setting:template:remove')")
    @ApiOperation("删除运费模板")
    public AjaxResult delete(@PathVariable("id") Long id){
        // 删除运费模板
       boolean falg = shippingTemplateService.deleteShippingTemplate(id);
        return AjaxResult.success();
    }




}
