package com.sw.mes.sap.service.impl;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.sap.dao.LogSapStockDao;
import com.sw.mes.sap.dto.InventoryDto;
import com.sw.mes.sap.dto.WarehouseStatisticsDto;
import com.sw.mes.sap.entity.LogSapStock;
import com.sw.mes.sap.service.LogSapStockService;
import com.sw.mes.sap.service.SAPOutputServiceService;

/**
 * SAP库存数据Service业务层处理
 * 
 * @author liru
 * @date 2024-03-04
 */
@Service
@Transactional
@Jinko(value="晶科SAP下发生产工单通信记录Service业务层处理",author = "liru")
public class LogSapStockServiceImpl extends BaseServiceImpl<LogSapStock> implements LogSapStockService
{
    private LogSapStockDao dao;
    
    @Autowired
    private SAPOutputServiceService sAPOutputServiceService;

    @Autowired
    public void setDao(LogSapStockDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

	@Override
	public void deleteAll() {
		dao.deleteAll();
	}

	@Override
	public LogSapStock findByLgortAndMatnrAndCharg(String lgort, String matnr, String charg) {
		List<LogSapStock> list = dao.findByLgortAndMatnrAndCharg(lgort,matnr,charg);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public List<LogSapStock> findByLgortAndMatnr(String lgort, String matnr) {
		List<LogSapStock> list = dao.findByLgortAndMatnr(lgort,matnr);
		return list;
	}

	@Override
	public LogSapStock synSapStock(String lgort, String matnr, String charg) {
		try {
			sAPOutputServiceService.getSapStock(lgort, matnr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return findByLgortAndMatnrAndCharg(lgort,matnr,charg);
	}

	@Override
	public void deleteByMatnrAndLgort(String matnr, String lgort) {
		dao.deleteByMatnrAndLgort(matnr,lgort);
	}


	@Override
	public void deleteByLgort(String lgort) {
		dao.deleteByLgort(lgort);
	}

	@Override
	public List<LogSapStock> findByLgort(String lgort) {
		return dao.findByLgort(lgort);
	}

	@Override
	public List realTimeReconciliationOfInventory(WarehouseStatisticsDto dto) {
		return dao.realTimeReconciliationOfInventory(dto);
	}
	
	@Override
	public List realTimeReconciliationOfInventoryLgorts() {
		return dao.realTimeReconciliationOfInventoryLgorts();
	}
	
	@Override
	public List realTimeReconciliationOfInventoryChargs() {
		return dao.realTimeReconciliationOfInventoryChargs();
	}

	@Override
	public void export(HttpServletResponse response, String sheetName, String fileName, List list) throws IOException {
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // xlsx
		response.setCharacterEncoding("utf-8");
		fileName = URLEncoder.encode(fileName + DateUtil.formatDate(new Date(), DateUtil.longFormats), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

		List<InventoryDto> data = new ArrayList<InventoryDto>();
		for (int i = 0; i < list.size(); i++) {
			Map<String,Object> row = (Map<String,Object>)list.get(i);
			
			for (Iterator iterator = row.keySet().iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				System.out.println(key+" " + (row.get(key)==null?"":row.get(key).toString()));
			}
			
			String wlmc = row.get("物料名称")==null?"":row.get("物料名称").toString();
			String wlz = row.get("物料组")==null?"":row.get("物料组").toString();
			String wllx = row.get("物料类型")==null?"":row.get("物料类型").toString();
			
			
			InventoryDto dto = new InventoryDto();
			
			dto.setField1(row.get("工厂").toString());
			dto.setField2(row.get("工厂名称").toString());
			dto.setField3(row.get("库位").toString());
			dto.setField4(row.get("库位名称").toString());
			dto.setField5(row.get("料号").toString());
			dto.setField6("物料名称:"+wlmc +" 物料组:"+wlz+" 物料类型:"+wllx);
			dto.setField7(row.get("批次").toString());
			dto.setField8(row.get("线边仓库存数").toString());
			dto.setField9(row.get("SAP库存数").toString());
			dto.setField10(row.get("库存差异").toString());
			dto.setField11(row.get("线边仓未收货库存数").toString());
			dto.setField12(row.get("已报工101数").toString());
			dto.setField13(row.get("未报工101数").toString());
			dto.setField14(row.get("已报工261数").toString());
			dto.setField15(row.get("未报工261数").toString());
			dto.setField16(row.get("已报工311调拨出库数").toString());
			dto.setField17(row.get("未报工311调拨出库数").toString());
			dto.setField18(row.get("待调入库位")==null?"":row.get("待调入库位").toString());
			
			data.add(dto);
		}
		
		
		EasyExcel.write(response.getOutputStream(), InventoryDto.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet(sheetName).doWrite(data);
	}

}