package com.bonus.canteen.core.common.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bonus.canteen.core.order.deprecated.model.OrdDetail;
import com.bonus.canteen.core.order.deprecated.vo.OrdDetailMachineVO;

import java.util.List;

public class CustomOrdDetailListConverter implements Converter<List> {
   public Class supportJavaTypeKey() {
      return List.class;
   }

   public CellDataTypeEnum supportExcelTypeKey() {
      return CellDataTypeEnum.STRING;
   }

   public List convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
      return (List)JSONUtil.toBean(cellData.getStringValue(), List.class);
   }

   public WriteCellData convertToExcelData(List<OrdDetailMachineVO> s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
      StringBuffer resultData = new StringBuffer();
      if (CollUtil.isNotEmpty(s)) {
         s.forEach((ordDetail) -> {
            resultData.append(ordDetail.getGoodsName()).append("*").append(ordDetail.getQuantity()).append(ordDetail.getUnit()).append("；");
         });
      }

      return new WriteCellData(resultData.toString());
   }
}
