/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2020-03-06
 * 描述            ：null
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;
import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.transaction.annotation.Transactional;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Util.ImportUtil;
import market.Qms.qcm.mapper.QcmChemExamMapper;
import market.Qms.qcm.service.QcmChemExamService;
import market.Qms.qcm.vo.QcmChemExam;
import market.Slm.ContractManagement.impl.SlmContractLineInfoServiceImpl;
import market.Slm.ContractManagement.vo.SlmContractHeatInfo;
import market.Slm.ContractManagement.vo.SlmContractLineInfo;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="QcmChemExamService")
public class QcmChemExamServiceImpl extends BaseServiceImpl<QcmChemExam,Object> implements QcmChemExamService {
	
	/**
	 * 保存
	 * @param listChemical
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doSaveQcmChemExam(CommonPage<QcmChemExam> cp) throws Exception{
		ResultModel rm = new ResultModel();
		List<QcmChemExam> qceList = cp.getObjectList();
		if (qceList==null || qceList.size()<=0) {
			throw new Exception("请传入参数");
		}
		
		String now = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String order = (String) this.getSession().getAttribute(HomeService.WRK_ORD);
		String team = (String) this.getSession().getAttribute(HomeService.WRK_GRP);
		for (QcmChemExam qce : qceList) {
			QcmChemExam qce1 = this.getMapper(QcmChemExamMapper.class).findById(qce.getEid());
			if (qce1!=null) {
				//修改
				qce.setEid(qce1.getEid());
				qce.setCreate_time(now);
				qce.setCreate_name(username);
				qce.setWrk_ord(order);
				qce.setWrk_grp(team);
				this.getMapper(QcmChemExamMapper.class).update(qce);
			}else {
				//新增	
				String condition = "eid like '%"+now+"%'";
				List<QcmChemExam> maxQ = this.getMapper(QcmChemExamMapper.class).findWithCondition(null, condition, "eid desc", 0, 1);
				if (maxQ!=null && maxQ.size()>0) {
					String maxCode = maxQ.get(0).getEid();
					String seq = maxCode.substring(maxCode.length()-3);
					Integer iseq = Integer.parseInt(seq) + 1;
					qce.setEid(now + String.format("%03d", iseq));					 
				}else {
					qce.setEid(now + "001");
				}				
				qce.setCreate_time(now);
				qce.setCreate_name(username);
				qce.setWrk_ord(order);
				qce.setWrk_grp(team);
				this.getMapper(QcmChemExamMapper.class).insert(qce);
			}
		}
		rm.setState("200");
		rm.setMsgInfo("保存成功");
		return rm;
	}
	
	/**
	 * 删除
	 * @param listChemical
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doDeleteQcmChemExam(CommonPage<QcmChemExam> cp) throws Exception{
		ResultModel rm = new ResultModel();
		rm.setState("200");
		rm.setMsgInfo("删除成功！");
		List<QcmChemExam> exams = cp.getObjectList();
		if (exams==null || exams.size()<=0) {
			throw new Exception("请勾选数据进行删除操作");
		}
		
		for (QcmChemExam qce : exams) {
			if (qce.getEid() != null) {
				this.getMapper(QcmChemExamMapper.class).delete(qce.getEid());
			}
		}
		
		return rm;
	}
	
	public ResultModel doQuaryQcmChemExam(CommonPage<QcmChemExam> cp) throws Exception{
		ResultModel rm = new ResultModel();
		QcmChemExam qce = cp.getObject();		
		String swhere = "1=1";
		if (qce.getMachine_code()!=null && !"".equals(qce.getMachine_code())) {
			swhere += " and machine_code like '%"+qce.getMachine_code()+"%'";
		}
		if (qce.getWrk_date()!=null && !"".equals(qce.getWrk_date())) {
			swhere += " and wrk_date like '%"+qce.getWrk_date()+"%'";
		}
		List<QcmChemExam> list = this.getMapper(QcmChemExamMapper.class).findWithCondition(null, swhere, "wrk_date desc,eid desc", 0, 0);
		rm.setData(list);
		rm.setState("200");
		rm.setMsgInfo("查询成功");
		return rm;
	}
	
	/**
	 * 各机组故障数据查询：
	 */
	public ResultModel doQuaryQcmChemExamGZ(CommonPage<QcmChemExam> cp) throws Exception{
		ResultModel rm = new ResultModel();
		QcmChemExam qce = cp.getObject();		
		String swhere = "1=1";
		if (qce.getMemo() != null && !"".equals(qce.getMemo())) {
			swhere += " and memo like '%"+qce.getMemo()+"%'";
		}
		if (qce.getWrk_date()!=null && !"".equals(qce.getWrk_date())) {
			swhere += " and wrk_date like '%"+qce.getWrk_date()+"%'";
		}
		// 查询sql得改：
		List<QcmChemExam> list = this.getMapper(QcmChemExamMapper.class).doQuaryGZTime(qce.getWrk_date());
		rm.setData(list);
		rm.setState("200");
		rm.setMsgInfo("查询成功");
		return rm;
	}
	
	/**
	 * 各机组故障时间数据保存
	 * @param listChemical
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doSaveQcmChemExamGZ(CommonPage<QcmChemExam> cp) throws Exception{
		ResultModel rm = new ResultModel();
		List<QcmChemExam> qceList = cp.getObjectList();
		if (qceList==null || qceList.size()<=0) {
			throw new Exception("请传入参数");
		}
		
		String now = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String order = (String) this.getSession().getAttribute(HomeService.WRK_ORD);
		String team = (String) this.getSession().getAttribute(HomeService.WRK_GRP);
		for (QcmChemExam qce : qceList) {
			QcmChemExam qce1 = this.getMapper(QcmChemExamMapper.class).findById(qce.getEid());
			if (qce1!=null) {
				//修改
				qce.setEid(qce1.getEid());
				qce.setCreate_time(now);
				qce.setCreate_name(username);
				qce.setWrk_ord(order);
				qce.setWrk_grp(team);
				this.getMapper(QcmChemExamMapper.class).update(qce);
			}else {
				//新增
				String condition = "eid like '%"+now+"%'";
				List<QcmChemExam> maxQ = this.getMapper(QcmChemExamMapper.class).findWithCondition(null, condition, "eid desc", 0, 1);
				if (maxQ!=null && maxQ.size()>0) {
					String maxCode = maxQ.get(0).getEid();
					String seq = maxCode.substring(maxCode.length()-3);
					Integer iseq = Integer.parseInt(seq) + 1;
					qce.setEid(now + String.format("%03d", iseq));					 
				}else {
					qce.setEid(now + "001");
				}				
				qce.setCreate_time(now);
				qce.setCreate_name(username);
				qce.setWrk_ord(order);
				qce.setWrk_grp(team);
				// 只有：日考核值 param1、param7, 日实际值 param2、param8
				// 只需要插入param1,param2,param7,param8的值：
				this.getMapper(QcmChemExamMapper.class).insert(qce);
			}
		}
		rm.setState("200");
		rm.setMsgInfo("保存成功");
		return rm;
	}
	
	/**
	 * 根据月份查询当月所有的日期 
	 * @param cp memo1：yyyy-MM
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQuaryDaysByMonth(CommonPage<QcmChemExam> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String work_month = cp.getMemo1();
		List<String> list = this.getMapper(QcmChemExamMapper.class).doQuaryDaysByMonth(work_month);
		rm.setData(list);
		rm.setState("200");
		rm.setMsgInfo("查询成功");
		return rm;
	}
	
	/**
	 * 检化验导入 20220411
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doImportJHY() throws Exception{
		ResultModel rm = new ResultModel();
		Workbook wb = null;
		URL url = this.getClass().getClassLoader().getResource("/");
        // 替换路径中的空格
        String filePath = url.getPath().replaceAll("%20", " ");
        File file = new File(filePath);
        //1.导入excel
        try {
			List<FileItem> items = ImportUtil.getUpload(QcmChemExamServiceImpl.this,file,getRequest());		
			for (FileItem item : items) {
				// 若是文件域则读取文件流
				if (!item.isFormField()) {
					wb = ImportUtil.getWB(item, file.length());
				}
			}
			rm.setState("200");
			rm.setMsgInfo("合同模板读取成功！");
		} catch (FileUploadException e) {
			rm.setState("500");
			rm.setMsgInfo("合同模板读取失败！");
		}
        //System.out.println("导入成功！");
        
        // 2.读取Excel数据
        int numRow = 0;//最后的行数
        Sheet sheet = null;
		try {
			//第一个表单
			sheet = wb.getSheetAt(0);
			//行数
			numRow = sheet.getLastRowNum();//最后的行数
		} catch (Exception e) {
			throw new Exception("Excel表格数据读取失败！" + e.getMessage());
		}
		// 表二：酸洗
		List<QcmChemExam> list2= new ArrayList<QcmChemExam>();
		// 表一：乳化液
		List<QcmChemExam> list1= new ArrayList<QcmChemExam>();
		// 表三：平整液
		List<QcmChemExam> list3= new ArrayList<QcmChemExam>();
		if (numRow >= 1) {
			for (int i = 1; i <= numRow; i++) {
				Row row = sheet.getRow(i);
				// ImportUtil.getCellValue(row.getCell(20))
				// 酸洗：
				QcmChemExam sxi = new QcmChemExam();
				sxi.setMachine_code("表二");
//				System.out.println(ImportUtil.getCellValue(row.getCell(0)));
				sxi.setWrk_date(ImportUtil.getCellValue(row.getCell(0)));
				sxi.setParam1(ImportUtil.getCellValue(row.getCell(1)));
				sxi.setParam2(ImportUtil.getCellValue(row.getCell(2)));
				sxi.setParam3(ImportUtil.getCellValue(row.getCell(3)));
				sxi.setParam4(ImportUtil.getCellValue(row.getCell(4)));
				sxi.setParam5(ImportUtil.getCellValue(row.getCell(5)));
				sxi.setParam6(ImportUtil.getCellValue(row.getCell(6)));
				sxi.setParam7(ImportUtil.getCellValue(row.getCell(7)));
				sxi.setParam8(ImportUtil.getCellValue(row.getCell(8)));
				list2.add(sxi);
				
				// 乳化液：
				QcmChemExam rhy = new QcmChemExam();
				rhy.setMachine_code("表一");
				rhy.setWrk_date(ImportUtil.getCellValue(row.getCell(0)));
				rhy.setParam1(ImportUtil.getCellValue(row.getCell(14)));
				rhy.setParam2(ImportUtil.getCellValue(row.getCell(15)));
				rhy.setParam4(ImportUtil.getCellValue(row.getCell(16)));
				rhy.setParam6(ImportUtil.getCellValue(row.getCell(17)));
				rhy.setParam7(ImportUtil.getCellValue(row.getCell(18)));
				rhy.setParam8(ImportUtil.getCellValue(row.getCell(19)));
				rhy.setParam9(ImportUtil.getCellValue(row.getCell(20)));
				rhy.setParam10(ImportUtil.getCellValue(row.getCell(21)));
				rhy.setParam12(ImportUtil.getCellValue(row.getCell(22)));
				rhy.setParam11(ImportUtil.getCellValue(row.getCell(23)));
				list1.add(rhy);
				
				// 平整液：
				QcmChemExam pzy = new QcmChemExam();
				pzy.setMachine_code("表三");
				pzy.setWrk_date(ImportUtil.getCellValue(row.getCell(0)));
				pzy.setParam1(ImportUtil.getCellValue(row.getCell(12)));
				list3.add(pzy);
			}
//			System.out.println(list1.toString());
//			System.out.println(list2.toString());
//			System.out.println(list3.toString());
		}
		
		CommonPage<QcmChemExam> cp1 = new CommonPage<>();
		cp1.setObjectList(list1);
		rm = this.doSaveQcmChemExam(cp1);
		
		if ("200".equals(rm.getState())) {
			CommonPage<QcmChemExam> cp2 = new CommonPage<>();
			cp2.setObjectList(list2);
			rm = this.doSaveQcmChemExam(cp2);
		}
		
		if ("200".equals(rm.getState())) {
			CommonPage<QcmChemExam> cp3 = new CommonPage<>();
			cp3.setObjectList(list3);
			rm = this.doSaveQcmChemExam(cp3);
		}
		
		
		return rm;
	}
	
}
