package cn.yw.machinemanage.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 cn.base.bean.BaseService;
import cn.base.util.CommUtils;
import cn.yw.machinemanage.dao.YwmachineinfoDao;

@Service("ywmachineinfoService")
public class YwmachineinfoService extends BaseService {
	
	private static Log log = LogFactory.getLog(YwmachineinfoService.class);
	
	@Autowired
	private YwmachineinfoDao ywmachineinfoDao;
	
	/**
	 * 保存记录
	 */
	
	public void insert_Ywmachineinfo(Map map) throws SQLException{
		CommUtils.addParams(map, false);
		ywmachineinfoDao.insert_Ywmachineinfo(map);
	}
	

	
	/**
	 * 修改记录
	 */
	
	public Integer update_Ywmachineinfo(Map map) throws SQLException{
		CommUtils.validateUpdateParams(map);
		CommUtils.addParams(map, true);
		return ywmachineinfoDao.update_Ywmachineinfo(map);
	}
	
	
	/**
	 * 删除记录
	 */
	
	public Integer delete_Ywmachineinfo(Map map) throws SQLException{
		CommUtils.validateDeleteParams(map);
		return ywmachineinfoDao.delete_Ywmachineinfo(map);
	}
	
	
	/**
	 * 批处理删除记录
	 */
	
	public Integer batch_delete_Ywmachineinfo(Map map) throws SQLException{
		CommUtils.validateDeleteParams(map);
		return ywmachineinfoDao.batch_delete_Ywmachineinfo(map);
	}
	
	
	/**
	 * 查询总数
	 */
	
	public Integer count_Ywmachineinfo(Map map) throws SQLException{
		return ywmachineinfoDao.count_Ywmachineinfo(map);
	}
	
	
	/**
	 * 查询列表记录SELECT
	 */
	
	public List<Map> query_Ywmachineinfo(Map map) throws SQLException{
		return ywmachineinfoDao.query_Ywmachineinfo(map);
	}

	/**
	 * 查询所有列表记录SELECT
	 */

	public List<Map> query_ALLYwmachineinfo(Map map) throws SQLException{
		return ywmachineinfoDao.ALLquery_Ywmachineinfo(map);
	}

	
	/**
	 * 查询一条
	 */
	 
	
	public Map get_Ywmachineinfo(Map map) throws SQLException{
		return ywmachineinfoDao.get_Ywmachineinfo(map);
	}
	
	
	/**
	 * 导出EXCEL
	 */
	 

	public Map exportYwmachineinfo(Map map) throws SQLException{
		return this.listYwmachineinfoByPage(map);
	}

	/**
	 * 导入EXCEL
	 */


	public String  batch_insert_Ywmachineinfo(List list) throws SQLException{
		try {
			int ListSubSize = 1000;
			int subCount = list.size();
			int subPageTotal = (subCount / ListSubSize) + ((subCount % ListSubSize > 0) ? 1 : 0);
			//分页计算，根据页码取数据
			for (int i = 0, len = subPageTotal - 1; i <= len; i++) {
				int fromIndex = i * ListSubSize;
				int toIndex = ((i == len) ? subCount : ((i + 1) * ListSubSize));
				List subList = list.subList(fromIndex, toIndex);
				ywmachineinfoDao.batch_insert_Ywmachineinfo(subList);
			}
			return "1";
		}catch (SQLException ex){
			return "0";
		}
	}


	
	/**
	 * Ywmachineinfo 分页
	 */
	
	public Map listYwmachineinfoByPage(Map map) throws SQLException {
		List<Map> list = ywmachineinfoDao.query_Ywmachineinfo(map);
		Integer totalCount = ywmachineinfoDao.count_Ywmachineinfo(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", list);
		return map;
	}
	
	// 更新
	public Integer edit_Ywmachineinfo(Map map) throws SQLException{
		return ywmachineinfoDao.edit_Ywmachineinfo(map);
	}
	
//  首页    运维数据看板 下数字统计
	public Map count_HomepageDeviceCount(Map map) throws SQLException{
		List<Map> list = ywmachineinfoDao.count_HomepageDeviceCount(map);
		Map res = new HashMap();
		if(list != null && list.size()>0) {
			for(Map m : list) {
				String name = (String) m.get("name");
				res.put(name, m.get("zs"));
			}
		}
		return res;
	}

	//根据备注信息更新维修状态信息
	public Integer update_YwmachinecurrentstateRepairstate(Map map) throws SQLException{
		CommUtils.validateUpdateParams(map);
		CommUtils.addParams(map,true);
		return ywmachineinfoDao.update_YwmachinecurrentstateRepairstate(map);
	}
	//更改备注信息，可为空
	public Integer update_YwmachineinfoRemarks(Map map) throws SQLException{
		CommUtils.validateUpdateParams(map);
		CommUtils.addParams(map,true);
		return ywmachineinfoDao.update_YwmachineinfoRemarks(map);
	}
}
