/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.delivery.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.bean.model.Transport2;
import com.yami.shop.delivery.comment.param.ShopTransport;
import com.yami.shop.delivery.comment.service.Transport2Service;
import com.yami.shop.security.multishop.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2018/11/16.
 */
@RestController
@RequestMapping("/shop/transport2")
public class Transport2Controller {

    @Autowired
    private Transport2Service transportService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Transport2>> page(Transport2 transport, PageParam<Transport2> page) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        IPage<Transport2> transports = transportService.page(page,
                new LambdaQueryWrapper<Transport2>()
                        .eq(Transport2::getShopId, shopId)
        );
        return ResponseEntity.ok(transports);
    }

    /**
     * 分页获取
     */
    @GetMapping("/shopPage")
    public ResponseEntity<ShopTransport> shopPage() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        ShopTransport shopTransports = transportService.findShopTransports(shopId);
        return ResponseEntity.ok(shopTransports);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    public ResponseEntity<Transport2> info(@PathVariable("id") Long id) {
        Transport2 transport = transportService.getTransportAndAllItems(id);
        return ResponseEntity.ok(transport);
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody Transport2 transport) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Date createTime = new Date();
        transport.setCreateTime(createTime);
        transport.setShopId(shopId);
        transportService.insertTransportAndTransfee(transport);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody Transport2 transport) {
        transportService.updateTransportAndTransfee(transport);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestBody Long[] ids) {
        transportService.deleteTransportAndTransfeeAndTranscity(ids);
        // 删除运费模板的缓存
        for (Long id : ids) {
            transportService.removeTransportAndAllItemsCache(id);
        }
        return ResponseEntity.ok().build();
    }


    /**
     * 获取运费模板列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<Transport2>> list() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<Transport2> list = transportService.list(new LambdaQueryWrapper<Transport2>().eq(Transport2::getShopId, shopId));
        return ResponseEntity.ok(list);
    }

}
