/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Delivery;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.DeliveryService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import com.yami.shop.common.util.R;

/**
 * 物流公司信息管理实现
 *
 * @author LGH
 * @version 1.0.0
 * @since 2019-10-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/delivery" )
public class DeliveryController {

    private final DeliveryService deliveryService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param delivery 物流公司
     * @return 分页数据
     */
    @GetMapping("/page" )
    public R<IPage<Delivery>> getDeliveryPage(PageParam<Delivery> page, Delivery delivery) {
        return R.SUCCESS(deliveryService.page(page, new LambdaQueryWrapper<Delivery>()));
    }


    /**
     * 通过id查询物流公司
     * @param dvyId id
     * @return 单个数据
     */
    @GetMapping("/info/{dvyId}" )
    public R<Delivery> getById(@PathVariable("dvyId") Long dvyId) {
        return R.SUCCESS(deliveryService.getById(dvyId));
    }

    /**
     * 新增物流公司
     * @param delivery 物流公司
     * @return 是否新增成功
     */
    @SysLog("新增物流公司" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('platform:delivery:save')" )
    public R<Boolean> save(@RequestBody @Valid Delivery delivery) {
        delivery.setRecTime(new Date());
        delivery.setModifyTime(new Date());
        return R.SUCCESS(deliveryService.save(delivery));
    }

    /**
     * 修改物流公司
     * @param delivery 物流公司
     * @return 是否修改成功
     */
    @SysLog("修改物流公司" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('platform:delivery:update')" )
    public R<Boolean> updateById(@RequestBody @Valid Delivery delivery) {
        return R.SUCCESS(deliveryService.updateById(delivery));
    }

    /**
     * 通过id删除物流公司
     * @param dvyId id
     * @return 是否删除成功
     */
    @SysLog("删除物流公司" )
    @DeleteMapping("/{dvyId}" )
//    @PreAuthorize("@pms.hasPermission('platform:delivery:delete')" )
    public R<Boolean> removeById(@PathVariable Long dvyId) {
        return R.SUCCESS(deliveryService.removeById(dvyId));
    }

    /**
     * 分页获取
     */
    @GetMapping("/list")
    public R<List<Delivery>> page(){

        List<Delivery> list = deliveryService.list();
        return R.SUCCESS(list);
    }
}
