package com.yami.shop.platform.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.UpdateShannerProdDTO;
import com.yami.shop.bean.model.ChannelProd;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.ChannelProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 渠道商品管理控制器
 *
 * @author lgh on 2024/01/01.
 */
@RestController
@RequestMapping("/platform/channelProd")
@Api(tags = "渠道商品管理接口")
@AllArgsConstructor
public class ChannelProdController {

    private final ChannelProdService channelProdService;

    @GetMapping("/channel/{channelId}")
    @ApiOperation(value = "根据渠道ID获取商品列表")
    public R<List<ChannelProd>> getChannelProdsBychannelId(@PathVariable Long channelId) {
        List<ChannelProd> channelProds = channelProdService.getChannelProdsBychannelId(channelId);
        return R.SUCCESS(channelProds);
    }


    @GetMapping("/sku/{skuId}")
    @ApiOperation(value = "根据SKU ID获取渠道商品列表")
    public R<List<ChannelProd>> getChannelProdsBySkuId(@PathVariable Long skuId) {
        List<ChannelProd> channelProds = channelProdService.getChannelProdsBySkuId(skuId);
        return R.SUCCESS(channelProds);
    }

    @GetMapping("/channel/{channelId}/sku/{skuId}")
    @ApiOperation(value = "根据渠道ID和SKU ID获取渠道商品")
    public R<ChannelProd> getChannelProdBychannelIdAndSkuId(
            @PathVariable Long channelId,
            @PathVariable Long skuId) {
        ChannelProd channelProd = channelProdService.getChannelProdBychannelIdAndSkuId(channelId, skuId);
        return R.SUCCESS(channelProd);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询渠道商品")
    public R<IPage<ChannelProd>> getChannelProdPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            ChannelProd channelProd) {

        PageParam page = new PageParam();
        page.setCurrent(current);
        page.setSize(size);
        IPage<ChannelProd> channelProdPage = channelProdService.page(page);
        return R.SUCCESS(channelProdPage);
    }

    @PostMapping
    @ApiOperation(value = "保存渠道商品")
    public R saveChannelProd(@RequestBody @Valid ChannelProd channelProd) {
        channelProdService.saveChannelProd(channelProd);
        return R.SUCCESS();
    }

    @PutMapping("/setUpChannels")
    @ApiOperation(value = "设置销售渠道及价格")
    public R setUpChannels(@RequestBody UpdateShannerProdDTO updateShannerProdDTO) {
        channelProdService.setUpChannels(updateShannerProdDTO);
        return R.SUCCESS();
    }

    @PutMapping
    @ApiOperation(value = "更新渠道商品")
    public R updateChannelProd(@RequestBody @Valid ChannelProd channelProd) {
        channelProdService.updateChannelProd(channelProd);
        return R.SUCCESS();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除渠道商品")
    public R deleteChannelProdById(@PathVariable Long id) {
        channelProdService.deleteChannelProdById(id);
        return R.SUCCESS();
    }

    @DeleteMapping("/channel/{channelId}")
    @ApiOperation(value = "根据渠道ID删除渠道商品")
    public R deleteChannelProdsBychannelId(@PathVariable Long channelId) {
        channelProdService.deleteChannelProdsBychannelId(channelId);
        return R.SUCCESS();
    }

    @DeleteMapping("/sku/{skuId}")
    @ApiOperation(value = "根据SKU ID删除渠道商品")
    public R deleteChannelProdsBySkuId(@PathVariable Long skuId) {
        channelProdService.deleteChannelProdsBySkuId(skuId);
        return R.SUCCESS();
    }

    @PutMapping("/batch/prices")
    @ApiOperation(value = "批量更新渠道商品价格")
    public R updateBatchPrices(@RequestBody List<ChannelProd> channelProds) {
        channelProdService.updateBatchPrices(channelProds);
        return R.SUCCESS();
    }

    @GetMapping("/channel/{channelId}/count")
    @ApiOperation(value = "根据渠道ID统计商品数量")
    public R<Integer> countBychannelId(@PathVariable Long channelId) {
        Integer count = channelProdService.countByChannelId(channelId);
        return R.SUCCESS(count);
    }

    @GetMapping("/sku/{skuId}/count")
    @ApiOperation(value = "根据SKU ID统计渠道数量")
    public R<Integer> countBySkuId(@PathVariable Long skuId) {
        Integer count = channelProdService.countBySkuId(skuId);
        return R.SUCCESS(count);
    }

    @GetMapping("/exists/channel/{channelId}/sku/{skuId}")
    @ApiOperation(value = "检查渠道商品是否存在")
    public R<Boolean> existsBychannelIdAndSkuId(
            @PathVariable Long channelId,
            @PathVariable Long skuId) {
        Boolean exists = channelProdService.existsByChannelIdAndSkuId(channelId, skuId);
        return R.SUCCESS(exists);
    }


    @GetMapping("/template/download")
    @ApiOperation(value = "下载渠道商品导入模板")
    public void downloadTemplate(HttpServletResponse response) {
        channelProdService.downloadTemplate(response);
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入渠道商品价格数据")
    public R<String> importChannelProds(@RequestParam("file") MultipartFile file) {
        String result = channelProdService.importChannelProds(file);
        return R.SUCCESS(result);
    }



}
