package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Sku;
import com.yami.shop.bean.po.FreightStatisticsPo;
import com.yami.shop.bean.po.SkuStatisticsPo;
import com.yami.shop.bean.vo.FreightStatisticsVo;
import com.yami.shop.bean.vo.IdNameVo;
import com.yami.shop.bean.vo.SkuStatisticsVo;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.ChannelService;
import com.yami.shop.service.SkuService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品SKU管理实现
 */
@RestController
@RequestMapping("/platform/sku")
@AllArgsConstructor
public class SkuController {

    private final SkuService skuService;
    private final ChannelService channelService;

    @GetMapping("/getAllSkuList")
    @PreAuthorize("@pms.hasPermission('plateform:sku:list')")
    public R<List<Sku>> getSkuListByProdId(Long prodId) {
        List<Sku> skus = skuService.listByProdId(prodId);
        return R.SUCCESS(skus);
    }

    @GetMapping("/skuStatisticsList")
    @ApiOperation(value = "对账单汇总表（商品）-列表")
    public R<IPage<SkuStatisticsVo>> skuStatisticsList(PageParam<SkuStatisticsPo> page, SkuStatisticsPo po) {
        if (po.getChannelIds().contains(0L)) {
            List<IdNameVo> idNameVos = channelService.queryMyChannel(SecurityUtils.getSysUser().getUserId());
            if (!idNameVos.isEmpty()) {
                List<Long> ids = idNameVos.stream().map(IdNameVo::getId).collect(Collectors.toList());
                po.setChannelIds(ids);
            }
        }
        if (po.getUserAttrType() == null) {
            po.setUserAttrType("1");
        }
        return R.SUCCESS(skuService.skuStatisticsList(page, po));
    }

    @GetMapping(value = "/skuStatisticsExcel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation("对账单汇总表（商品）-导出")
    public void skuStatisticsExcel(HttpServletResponse response, SkuStatisticsPo po) {
        if (po.getChannelIds().contains(0L)) {
            List<IdNameVo> idNameVos = channelService.queryMyChannel(SecurityUtils.getSysUser().getUserId());
            if (!idNameVos.isEmpty()) {
                List<Long> ids = idNameVos.stream().map(IdNameVo::getId).collect(Collectors.toList());
                po.setChannelIds(ids);
            }
        }
        if (po.getUserAttrType() == null) {
            po.setUserAttrType("1");
        }
        skuService.skuStatisticsExcel(response, po);
    }

    @GetMapping("/freightStatisticsList")
    @ApiOperation(value = "运费明细表-列表")
    public R<IPage<FreightStatisticsVo>> freightStatisticsList(PageParam<FreightStatisticsPo> page, FreightStatisticsPo po) {
        if (po.getChannelIds().contains(0L)) {
            List<IdNameVo> idNameVos = channelService.queryMyChannel(SecurityUtils.getSysUser().getUserId());
            if (!idNameVos.isEmpty()) {
                List<Long> ids = idNameVos.stream().map(IdNameVo::getId).collect(Collectors.toList());
                po.setChannelIds(ids);
            }
        }
        if (po.getUserAttrType() == null) {
            po.setUserAttrType("1");
        }
        return R.SUCCESS(skuService.freightStatisticsList(page, po));
    }

    @GetMapping("/freightStatisticsExcel")
    @ApiOperation("运费明细表-导出")
    public R<Void> freightStatisticsExcel(HttpServletResponse response, FreightStatisticsPo po) {
        if (po.getChannelIds().contains(0L)) {
            List<IdNameVo> idNameVos = channelService.queryMyChannel(SecurityUtils.getSysUser().getUserId());
            if (!idNameVos.isEmpty()) {
                List<Long> ids = idNameVos.stream().map(IdNameVo::getId).collect(Collectors.toList());
                po.setChannelIds(ids);
            }
        }
        if (po.getUserAttrType() == null) {
            po.setUserAttrType("1");
        }
        skuService.freightStatisticsExcel(response, po);
        return R.SUCCESS();
    }

    @GetMapping("/test")
    @ApiOperation("导出测试")
    public R<Void> test(HttpServletResponse response) {
        skuService.test(response);
        return R.SUCCESS();
    }

}
