package com.cim.idm.controller;

import com.alibaba.fastjson.JSONObject;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.CommonUtils;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工单-生产订单
 * @author ZXYGY17
 *
 */
@RestController
@RequestMapping("/api/WorkOrder")
@EnableAutoConfiguration
public class WorkOrderController {
	
	CommonUtils untils=new CommonUtils();
	
	@RequestMapping(value = "/Add", method = RequestMethod.POST)
	public AjaxResult AddWorkOrder(@RequestBody JSONObject in ) throws Exception  {
		String userId = in.get("userId").toString();
		String workOrder = in.get("workOrder").toString();
		
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("WORKORDER", workOrder);
		String qySql = "SELECT 1 FROM WORKORDER w WHERE WORKORDER = :WORKORDER";
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(qySql, hashMap);
		if(queryForList != null && queryForList.size() > 0) {
			throw new CustomException("工单号已经存在！");
		}
		
		hashMap.put("CREATENAME", userId);
		String addSql = "INSERT INTO WORKORDER (WORKORDER, CREATETIME, CREATENAME)\r\n" + 
				"VALUES (:WORKORDER, SYSDATE, :CREATENAME)";
		try {
			IDMFrameServiceProxy.getSqlTemplate().update(addSql, hashMap);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("保存错误："  + e);
		}
		return AjaxResult.me().setSuccess(true).setResultObj(null);
	}
	
	@RequestMapping(value = "/Del", method = RequestMethod.POST)
	public AjaxResult DeleteWorkOrder(@RequestBody JSONObject in ) throws Exception  {
		String workOrder = in.get("workOrder").toString();
		
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("WORKORDER", workOrder);
		String qySql = "DELETE FROM workOrder WHERE WORKORDER = :WORKORDER";
		IDMFrameServiceProxy.getSqlTemplate().update(qySql, hashMap);
		return AjaxResult.me().setSuccess(true).setResultObj(null);
	}
	
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value="userId",defaultValue="") String userId) throws Exception  {

		try {
		    Workbook workbook = WorkbookFactory.create(file.getInputStream());
		    Sheet sheet = workbook.getSheetAt(0);
		    int index = 0;
		    for (Row row : sheet) {
		    	
		    	System.out.println(row.getCell(row.getRowNum()));
		    	index++;
		       if(row.getCell(row.getRowNum()) != null) {
		    	   for (Cell cell : row) {
			        	if(cell != null ) {
			        		// 类型都转为字符串
			        		cell.setCellType(Cell.CELL_TYPE_STRING);  
			        		String cellValue = cell.getStringCellValue();
//			        		if(index == 1 && !"工单".equals(cellValue)  ) {
//			        			throw new RuntimeException("非工单信息！");
//			        		}
			        		if(!cellValue.isEmpty() && index > 1) {
//			        			System.out.println(cellValue);
//			        			System.out.println(index);
			        			Map<String, Object> hashMap = new HashMap<String,Object> ();
			        			hashMap.put("WORKORDER", cellValue);
			        			hashMap.put("CREATENAME", userId);
			        			String addSql = "INSERT INTO WORKORDER (WORKORDER, CREATETIME, CREATENAME)\r\n" + 
			        					"VALUES (:WORKORDER, SYSDATE, :CREATENAME)";
			        			IDMFrameServiceProxy.getSqlTemplate().update(addSql, hashMap);
			        		}
			        	}
			        }
		       }
		    }
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException("导入错误(工单不能重复)："  + e);
		}
		return AjaxResult.me().setSuccess(true).setResultObj(null);
	}
}
