package com.zhyc.module.dairyProducts.service.impl;

import com.zhyc.module.dairyProducts.domain.NpMilkInOutStore;
import com.zhyc.module.dairyProducts.mapper.NpMilkInOutStoreMapper;
import com.zhyc.module.dairyProducts.service.INpMilkInOutStoreService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

@Service
public class NpMilkInOutStoreServiceImpl implements INpMilkInOutStoreService {

    @Autowired
    private NpMilkInOutStoreMapper mapper;

    @Override
    public List<Map<String, Object>> selectWithDynamicColumns(Date start, Date end) {
        List<String> feed = mapper.selectFeedSources();
        List<String> sale = mapper.selectSaleDestinations();
        return mapper.selectWithColumns(start, end, feed, sale);
    }

    @Override
    public List<Map<String, Object>> getAllColumnOptions() {
        Map<String,Object> m = new HashMap<>();
        m.put("feed", mapper.selectFeedSources());
        m.put("sale", mapper.selectSaleDestinations());
        return Collections.singletonList(m);
    }
    @Override
    public List<Map<String, Object>> parseImportExcel(MultipartFile file) throws Exception {
        // 用 Apache POI 解析 Excel 第一行标题，动态映射列名跟 unit cells
        InputStream in = file.getInputStream();
        Workbook wb = WorkbookFactory.create(in);
        Sheet sheet = wb.getSheetAt(0);
        Row header = sheet.getRow(0);
        int cols = header.getLastCellNum();
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < cols; i++) titles.add(header.getCell(i).getStringCellValue());
        List<Map<String,Object>> rows = new ArrayList<>();
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            Map<String,Object> map = new LinkedHashMap<>();
            for (int c = 0; c < cols; c++) {
                Cell cell = row.getCell(c);
                map.put(titles.get(c), cell == null ? null : cell.toString());
            }
            rows.add(map);
        }
        return rows;
    }

    @Override
    public void batchInsertFromRows(List<Map<String, Object>> rows) throws Exception {
        for (Map<String,Object> row : rows) {
            // 提取主表字段
            NpMilkInOutStore store = new NpMilkInOutStore();
            store.setDatetime(java.sql.Date.valueOf(row.get("日期").toString()));
            store.setNum(Integer.valueOf(row.get("羊数").toString()));
            // ... 设置其它固定字段 ...
            mapper.insertStore(store);
            Integer sid = store.getId();
            // 其余列为动态饲喂或销售，根据字典决定分类：
            for (Map.Entry<String,Object> ent: row.entrySet()) {
                String col = ent.getKey();
                BigDecimal amt = new BigDecimal(ent.getValue().toString());
                if (mapper.selectFeedSources().contains(col)) {
                    mapper.insertFeedRecord(sid, col, amt);
                } else if (mapper.selectSaleDestinations().contains(col)) {
                    mapper.insertSaleRecord(sid, col, amt);
                }
            }
        }
    }
}

