
package com.tmerclub.cloud.delivery.controller.app;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.order.vo.TransfeeVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.delivery.dto.TransfeeDTO;
import com.tmerclub.cloud.delivery.model.Transfee;
import com.tmerclub.cloud.delivery.service.TransfeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 运费项
 *
 * @author tmerclub
 * @date 2020-12-07 15:10:01
 */
@RestController("appTransfeeController")
@RequestMapping("/transfee")
@Tag(name = "运费项")
public class TransfeeController {

    @Autowired
    private TransfeeService transfeeService;


    @GetMapping("/page")
    @Operation(summary = "获取运费项列表", description = "分页获取运费项列表")
    public ServerResponseEntity<PageVO<TransfeeVO>> page(@Valid PageDTO pageDTO) {
        PageVO<TransfeeVO> transfeePage = transfeeService.page(pageDTO);
        return ServerResponseEntity.success(transfeePage);
    }

    @GetMapping
    @Operation(summary = "获取运费项", description = "根据transfeeId获取运费项")
    public ServerResponseEntity<TransfeeVO> getByTransfeeId(@RequestParam Long transfeeId) {
        return ServerResponseEntity.success(transfeeService.getByTransfeeId(transfeeId));
    }

    @PostMapping
    @Operation(summary = "保存运费项", description = "保存运费项")
    public ServerResponseEntity<Void> save(@Valid @RequestBody TransfeeDTO transfeeDTO) {
        Transfee transfee = BeanUtil.map(transfeeDTO, Transfee.class);
        transfee.setTransfeeId(null);
        transfeeService.save(transfee);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新运费项", description = "更新运费项")
    public ServerResponseEntity<Void> update(@Valid @RequestBody TransfeeDTO transfeeDTO) {
        Transfee transfee = BeanUtil.map(transfeeDTO, Transfee.class);
        transfeeService.update(transfee);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除运费项", description = "根据运费项id删除运费项")
    public ServerResponseEntity<Void> delete(@RequestParam Long transfeeId) {
        transfeeService.deleteById(transfeeId);
        return ServerResponseEntity.success();
    }
}
