package com.yami.shop.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ChannelProd;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopSku;
import com.yami.shop.bean.po.ProductPageQuery;
import com.yami.shop.bean.vo.ChannelProdExportVO;
import com.yami.shop.bean.vo.ProdChannelVO;
import com.yami.shop.bean.vo.ProductPageVO;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.*;
import com.yami.shop.service.ShopSkuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class ShopSkuServiceImpl extends ServiceImpl<ShopSkuMapper, ShopSku> implements ShopSkuService {

    private final ShopSkuMapper shopProdMapper;
    private final ChannelProdMapper channelProdMapper;
    private final SkuMapper skuMapper;
    private final ShopDetailMapper shopDetailMapper;


    @Override
    public void insertSku(ShopSku shopSku) {
        shopProdMapper.insert(shopSku);
    }

    @Override
    public IPage<ProductPageVO> shopSkuPage(PageParam<ProductPageQuery> page, ProductPageQuery query) {
        IPage<ProductPageVO> page1 = shopProdMapper.selectShopSkuPage(page, query);
        //循环获取sku和渠道价格设置信息
        for (ProductPageVO record : page1.getRecords()) {
            record.setSku(skuMapper.selectById(record.getSkuId()));
            List<ProdChannelVO>  channelProds = channelProdMapper.selectListAll(record.getSkuId(), record.getShopId());
            record.setChannelVOS(channelProds);
            if (channelProds != null && channelProds.size() > 0){
                // 获取channelProds最大更新时间
                Date maxUpdateTime = channelProds.stream()
                        .map(ProdChannelVO::getUpdateTime)  // 假设ProdChannelVO有getUpdateTime方法
                        .filter(Objects::nonNull)           // 过滤掉空值
                        .max(Date::compareTo)               // 找出最大的日期
                        .orElse(null);                // 如果没有找到，返回null

                record.setUpdateTime(maxUpdateTime);
            }
        }
        return page1;
    }

    @Override
    public void exportChannelProds(ProductPageQuery shopSku, HttpServletResponse response) {
        // 查询数据
        List<ChannelProdExportVO> channelProdExportVOS = shopProdMapper.exportList(shopSku);
        try {
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");

            String fileName = URLEncoder.encode("渠道商品数据导出", "UTF-8").replaceAll("\\+", "%20");

            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 写入Excel
            EasyExcel.write(response.getOutputStream(), ChannelProdExportVO.class)
                    .sheet("渠道商品数据")
                    .doWrite(channelProdExportVOS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
