/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.managementcenter.materialplan.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.materialplan.entity.InventoryTable;
import com.jeeplus.modules.sg.managementcenter.materialplan.entity.MaterialPlan;
import com.jeeplus.modules.sg.managementcenter.materialplan.mapper.MaterialPlanMapper;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class MaterialPlanService extends CrudService<MaterialPlanMapper, MaterialPlan> {
    @Autowired
	private MaterialPlanMapper materialPlanMapper;

	public MaterialPlan get(String uniqueCode) {
		return super.get(uniqueCode);
	}
	
	public List<MaterialPlan> findList(MaterialPlan materialPlan) {
		return super.findList(materialPlan);
	}
	
	public Page<MaterialPlan> findPage(Page<MaterialPlan> page, MaterialPlan materialPlan) {
		return super.findPage(page, materialPlan);
	}
	
	@Transactional(readOnly = false)
	public void save(MaterialPlan materialPlan) {
		super.save(materialPlan);
	}
	
	@Transactional(readOnly = false)
	public void delete(MaterialPlan materialPlan) {
		super.delete(materialPlan);
	}

	@Transactional(readOnly = false)
	public void update(MaterialPlan materialPlan){
		materialPlan.preUpdate();
		materialPlanMapper.update(materialPlan);
	}
	@Transactional(readOnly = false)
	public void saveList(List<MaterialPlan> list) {
		for(MaterialPlan materialPlan : list){
			materialPlan.preInsert();
		}
		materialPlanMapper.insertList(list);
	}

	@Transactional(readOnly = false)
	public void deleteData(String uniqueCode){materialPlanMapper.deleteData(uniqueCode);
	}

	@Transactional(readOnly = false)
	public void updateTime(MaterialPlan materialPlan){
		materialPlan.preUpdate();
		materialPlanMapper.updateTime(materialPlan);
	}
	@Transactional(readOnly = false)
	public void updateInventory(InventoryTable inventoryTable){
		inventoryTable.preUpdate();
		materialPlanMapper.updateInventory(inventoryTable);
	}

	public void exportHeard( HttpServletResponse response){
		InputStream inputStream = null;
		XSSFWorkbook workbook = null;
		try {
			String path = (getSispPath() + "excelmodel/flagstorage.xlsx");
			File file = new File(path);
			inputStream = new FileInputStream(file);// 将excel文件转为输入流
			workbook = new XSSFWorkbook(inputStream);// 创建个workbook，
			String str = URLEncoder.encode("是否入库模板.xlsx", "UTF8");
			OutputStream outputStream = response.getOutputStream();
			response.reset();
			response.setHeader("Content-Disposition", "attachment;filename="+str);
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			workbook.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}


	public  String getSispPath() {
		String sispPath = this.getClass().getResource("/").getPath()+ "freemarker/";
		return sispPath;
	}
}