package cn.yw.machinestatusmanage.service;


import cn.base.bean.BaseService;
import cn.yw.machinestatusmanage.dao.YwmachinestatusinfoDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ywmachinestatusinfoService")
public class YwmachinestatusinfoService extends BaseService {
	
	private static Log log = LogFactory.getLog(YwmachinestatusinfoService.class);
	
	@Autowired
	private YwmachinestatusinfoDao ywmachinestatusinfoDao;
	
	/**
	 * 保存记录
	 */
	/*
	public void insert_Ywmachinestatusinfo(Map map) throws SQLException{
		CommUtils.addParams(map, false);
		ywmachinestatusinfoDao.insert_Ywmachinestatusinfo(map);
	}
	*/

	
	/**
	 * 修改记录
	 */
	/*
	public Integer update_Ywmachinestatusinfo(Map map) throws SQLException{
		CommUtils.validateUpdateParams(map);
		CommUtils.addParams(map, true);
		return ywmachinestatusinfoDao.update_Ywmachinestatusinfo(map);
	}
	*/
	
	/**
	 * 删除记录
	 */
	/*
	public Integer delete_Ywmachinestatusinfo(Map map) throws SQLException{
		CommUtils.validateDeleteParams(map);
		return ywmachinestatusinfoDao.delete_Ywmachinestatusinfo(map);
	}
	*/
	
	/**
	 * 批处理删除记录
	 */
	/*
	public Integer batch_delete_Ywmachinestatusinfo(Map map) throws SQLException{
		return ywmachinestatusinfoDao.batch_delete_Ywmachinestatusinfo(map);
	}
	*/
	
	/**
	 * 查询总数
	 */
	
	public Integer count_Ywmachinestatusinfo(Map map) throws SQLException{
		return ywmachinestatusinfoDao.count_Ywmachinestatusinfo(map);
	}
	
	
	/**
	 * 查询列表记录SELECT
	 */
	
	public List<Map> query_Ywmachinestatusinfo(Map map) throws SQLException{
		return ywmachinestatusinfoDao.query_Ywmachinestatusinfo(map);
	}
	
	
	/**
	 * 查询一条
	 */
	 
	
	public Map get_Ywmachinestatusinfo(Map map) throws SQLException{
		return ywmachinestatusinfoDao.get_Ywmachinestatusinfo(map);
	}
	
	
	/**
	 * 导出EXCEL
	 */
	 
	/*
	public Map exportYwmachinestatusinfo(Map map) throws SQLException{
		return this.listYwmachinestatusinfoByPage(map);
	}
	*/
	
	/**
	 * Ywmachinestatusinfo 分页
	 */
	
	public Map listYwmachinestatusinfoByPage(Map map) throws SQLException {
		List<Map> list = ywmachinestatusinfoDao.query_Ywmachinestatusinfo(map);
		List<Map> resList = new ArrayList<Map>();
		for (Map map2 : list) {
			Map resMap = new HashMap();
			Map param = new HashMap();
			param.put("statusupbatch", map2.get("statusupbatch"));
			param.put("machineid", map2.get("machineid"));
			List<Map> listDetail = ywmachinestatusinfoDao.getYwmachinestatusinfoDetail(param);
			String statusupbatch = map2.get("statusupbatch").toString();
//			String machineid = map2.get("machineid").toString();
//			String machineip = map2.get("machineip").toString();
			String machineid = (String) map2.get("machineid");
			String machineip = (String) map2.get("machineip");
			String machinename = null;
			String statusuptime = null;
			String zjzt = null;
			String cpuzt = null;
			String nczt = null;
			String ypzt = null;
			String cpusy = null;
			String ncrl = null;
			String yprl = null;
			String ncsy = null;
			String ypsy = null;
			for (Map map3 : listDetail) {
				machinename = map3.get("machinename").toString();
				statusuptime = map3.get("statusuptime").toString();
				if (map3.get("machinedevicetype").toString().equals("0")) {
					if (map3.get("machinedevicestatus") != null && !map3.get("machinedevicestatus").toString().equals("1")) {
						zjzt = "0";
					} else {
						if (zjzt != null && !zjzt.equals("1")) {
							zjzt = "0";
						} else {
							zjzt = "1";
						}
					}
				} else if (map3.get("machinedevicetype").toString().equals("1")) {
					if (map3.get("machinedevicestatus") != null && !map3.get("machinedevicestatus").toString().equals("1")) {
						cpuzt = "0";
					} else {
						if (cpuzt != null && !cpuzt.equals("1")) {
							cpuzt = "0";
						} else {
							cpuzt = "1";
						}
					}
					cpusy = map3.get("currentusage") == null ? null : map3.get("currentusage").toString();
				} else if (map3.get("machinedevicetype").toString().equals("2")) {
					if (map3.get("machinedevicestatus") != null && !map3.get("machinedevicestatus").toString().equals("1")) {
						nczt = "0";
					} else {
						if (nczt != null && !"1".equals(nczt)) {
							nczt = "0";
						} else {
							nczt = "1";
						}
					}
					if (ncsy != null && !"".equals(ncsy)) {
						if (map3.get("currentusage") != null && !map3.get("currentusage").equals("")) {
							if (ncrl != null && !"".equals(ncrl) && map3.get("devicecapacity") != null && !map3.get("devicecapacity").equals("")) {
								if (ncrl.indexOf("KB") != -1) {
									BigDecimal rlA = new BigDecimal(ncrl.substring(0, ncrl.length()-2));
									BigDecimal rlB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length() -2));
									BigDecimal sylA = new BigDecimal(ncsy.substring(0, ncsy.length() -1));
									BigDecimal sylB = new BigDecimal(map3.get("currentusage").toString().substring(0, map3.get("currentusage").toString().length() -1));
									BigDecimal ysyA = sylA.divide(new BigDecimal("100")).multiply(rlA);
									BigDecimal ysyB = sylB.divide(new BigDecimal("100")).multiply(rlB);
							//		BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB))).setScale(2, BigDecimal.ROUND_HALF_UP);
									BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB)), 2, BigDecimal.ROUND_HALF_UP);
									ncsy = ncsyVal + "%";
								} else {
									BigDecimal rlA = new BigDecimal(ncrl.substring(0, ncrl.length()-1));
									BigDecimal rlB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length() -1));
									BigDecimal sylA = new BigDecimal(ncsy.substring(0, ncsy.length() -1));
									BigDecimal sylB = new BigDecimal(map3.get("currentusage").toString().substring(0, map3.get("currentusage").toString().length() -1));
									BigDecimal ysyA = sylA.divide(new BigDecimal("100")).multiply(rlA);
									BigDecimal ysyB = sylB.divide(new BigDecimal("100")).multiply(rlB);
		//							BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB))).setScale(2, BigDecimal.ROUND_HALF_UP);
									BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB)), 2, BigDecimal.ROUND_HALF_UP);

									ncsy = ncsyVal + "%";
								}
							}
						}
					} else {
						ncsy = map3.get("currentusage") == null ? null : map3.get("currentusage").toString();
					}
					if (ncrl != null && !"".equals(ncrl)) {
						ncrl = ncrl.toUpperCase();
						if (map3.get("devicecapacity") != null && !map3.get("devicecapacity").equals("")) {
							if (ncrl.indexOf("G") != -1) {
								BigDecimal ncrlValA = new BigDecimal(ncrl.substring(0, ncrl.length()-1));
								BigDecimal ncrlValB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length()-1));
								ncrl = ncrlValA.add(ncrlValB) + "";
							} else if (ncrl.indexOf("M") != -1) {
								BigDecimal ncrlValA = new BigDecimal(ncrl.substring(0, ncrl.length()-1));
								BigDecimal ncrlValB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length()-1));
								ncrl = ncrlValA.add(ncrlValB) + "";
							} else if (ncrl.indexOf("KB") != -1) {
								BigDecimal ncrlValA = new BigDecimal(ncrl.substring(0, ncrl.length()-2));
								BigDecimal ncrlValB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length()-2));
								ncrl = ncrlValA.add(ncrlValB) + "";
							}
						}
					} else {
						if (map3.get("devicecapacity") == null) {
							
							ncrl = null;
						} else {
							String ncrlstr = map3.get("devicecapacity").toString();
							if (ncrlstr.indexOf("G") != -1) {
								ncrlstr = ncrlstr.substring(0, ncrlstr.length() -1);
							} else if (ncrlstr.indexOf("M") != -1) {
								ncrlstr = ncrlstr.toString().substring(0, ncrlstr.length() -1);
							}  else if (ncrlstr.indexOf("KB") != -1) {
								ncrlstr = ncrlstr.substring(0, ncrlstr.length() -2);
							}
							ncrl = ncrlstr;
						}
					}
				} else if (map3.get("machinedevicetype").toString().equals("3")) {
					if (map3.get("machinedevicestatus") != null && !map3.get("machinedevicestatus").toString().equals("1")) {
						ypzt = "0";
					} else {
						if (ypzt != null && !"1".equals(nczt)) {
							ypzt = "0";
						} else {
							ypzt = "1";
						}
					}
					if (ypsy != null && !"".equals(ypsy)) {
						if (map3.get("currentusage") != null && !map3.get("currentusage").equals("")) {
							if (yprl != null && !"".equals(yprl) && map3.get("devicecapacity") != null && !map3.get("devicecapacity").equals("")) {
								if (yprl.indexOf("KB") != -1) {
									BigDecimal rlA = new BigDecimal(yprl.substring(0, yprl.length()-2));
									BigDecimal rlB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length() -2));
									BigDecimal sylA = new BigDecimal(ypsy.substring(0, ypsy.length() -1));
									BigDecimal sylB = new BigDecimal(map3.get("currentusage").toString().substring(0, map3.get("currentusage").toString().length() -1));
									BigDecimal ysyA = sylA.divide(new BigDecimal("100")).multiply(rlA);
									BigDecimal ysyB = sylB.divide(new BigDecimal("100")).multiply(rlB);
									BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB))).setScale(2, BigDecimal.ROUND_HALF_UP);
									ypsy = ncsyVal + "%";
								} else {
									BigDecimal rlA = new BigDecimal(yprl.substring(0, yprl.length()-1));
									BigDecimal rlB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length() -1));
									BigDecimal sylA = new BigDecimal(ypsy.substring(0, ypsy.length() -1));
									BigDecimal sylB = new BigDecimal(map3.get("currentusage").toString().substring(0, map3.get("currentusage").toString().length() -1));
									BigDecimal ysyA = sylA.divide(new BigDecimal("100")).multiply(rlA);
									BigDecimal ysyB = sylB.divide(new BigDecimal("100")).multiply(rlB);
//									BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB))).setScale(2, BigDecimal.ROUND_HALF_UP);
									BigDecimal ncsyVal = (ysyA.add(ysyB)).divide((rlA.add(rlB)),2, BigDecimal.ROUND_HALF_UP);
									ypsy = ncsyVal + "%";
								}
							}
						}
					} else {
						ypsy = map3.get("currentusage") == null ? null : map3.get("currentusage").toString();
					}
					if (yprl != null && !"".equals(yprl)) {
						yprl = yprl.toUpperCase();
						if (map3.get("devicecapacity") != null && !map3.get("devicecapacity").equals("")) {
							if (yprl.indexOf("G") != -1) {
								BigDecimal ncrlValA = new BigDecimal(yprl.substring(0, yprl.length()-1));
								BigDecimal ncrlValB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length()-1));
								yprl = ncrlValA.add(ncrlValB) + "";
							} else if (yprl.indexOf("M") != -1) {
								BigDecimal ncrlValA = new BigDecimal(yprl.substring(0, yprl.length()-1));
								BigDecimal ncrlValB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length()-1));
								yprl = ncrlValA.add(ncrlValB) + "";
							} else if (yprl.indexOf("KB") != -1) {
								BigDecimal ncrlValA = new BigDecimal(yprl.substring(0, yprl.length()-2));
								BigDecimal ncrlValB = new BigDecimal(map3.get("devicecapacity").toString().substring(0, map3.get("devicecapacity").toString().length()-2));
								yprl = ncrlValA.add(ncrlValB) + "";
							}
						}
					} else {
						if (map3.get("devicecapacity") == null) {
							yprl = null;
						} else {
							String yprlstr = map3.get("devicecapacity").toString();
							if (yprlstr.indexOf("G") != -1) {
								yprlstr = yprlstr.substring(0, yprlstr.length() -1);
							} else if (yprlstr.indexOf("M") != -1) {
								yprlstr = yprlstr.toString().substring(0, yprlstr.length() -1);
							}  else if (yprlstr.indexOf("KB") != -1) {
								yprlstr = yprlstr.substring(0, yprlstr.length() -2);
							}
							yprl = yprlstr;
						}
					}
				}
			}
			resMap.put("statusupbatch", statusupbatch);
			resMap.put("machineid", machineid);
			resMap.put("machineip", machineip);
			resMap.put("machinename", machinename);
			resMap.put("statusuptime", statusuptime);
			resMap.put("zjzt", zjzt);
			resMap.put("cpuzt", cpuzt);
			resMap.put("nczt", nczt);
			resMap.put("ypzt", ypzt);
			resMap.put("cpusy", cpusy);
			resMap.put("ncrl", ncrl);
			resMap.put("yprl", yprl);
			resMap.put("ncsy", ncsy);
			resMap.put("ypsy", ypsy);
			resList.add(resMap);
		}

		Integer totalCount = ywmachinestatusinfoDao.count_Ywmachinestatusinfo(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", resList);
		return map;
	}
	
	public List<Map> getYwmachinestatusinfoDetail(Map map) throws SQLException {
		List<Map> list = ywmachinestatusinfoDao.getYwmachinestatusinfoDetail(map);
		return list;
	}
	
	//查询
	public Map get_ywmachinestatusinfo(Map map) throws SQLException {
		List<Map> list = ywmachinestatusinfoDao.get_ywmachinestatusinfo(map);
		Integer totalCount = ywmachinestatusinfoDao.get_ywmachinestatusinfoCount(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", list);
		return map;
	}

	public Map  get_YwNoTaskmachinestatusinfo(Map  map)throws SQLException{
		List<Map> list = ywmachinestatusinfoDao.get_YwNoTaskmachinestatusinfo(map);
		Integer totalCount = ywmachinestatusinfoDao.get_YwNoTaskmachinestatusCount(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", list);
		return map;
	}
	
}
