package cn.bsystem.ptuserrole.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.base.util.StringUtil;
import cn.bsystem.ptuserrole.dao.PtyhDao;
import cn.bsystem.ptuserrole.dao.PtyhbmDao;
import cn.bsystem.ptuserrole.dao.PtyhbmscbfDao;
import cn.bsystem.ptuserrole.dao.PtyhcdjsDao;
import cn.bsystem.ptuserrole.dao.PtyhhmdlDao;
import cn.bsystem.ptuserrole.dao.PtyhscbfDao;
import cn.bsystem.ptuserrole.dao.SbtdjqqxDao;
import cn.bsystem.ptuserrole.dao.SbtdtpqxDao;

@Service("ptyhbmService")
public class PtyhbmService extends BaseService {
	
	private static Log log = LogFactory.getLog(PtyhbmService.class);
	
	@Autowired
	private PtyhbmDao ptyhbmDao;
	
	@Autowired
	private PtyhbmscbfDao ptyhbmscbfDao;
	
	@Autowired
	private PtyhDao ptyhDao;
	
	@Autowired
	private PtyhscbfDao ptyhscbfDao;
	
	@Autowired
	private PtyhhmdlDao ptyhhmdlDao;
	
	@Autowired
	private PtyhcdjsDao ptyhcdjsDao;
	
	@Autowired
	private SbtdjqqxDao sbtdjqqxDao;
	
	@Autowired
	private SbtdtpqxDao sbtdtpqxDao;
	
	
	
	/**
	 * 保存记录
	 * 
	 * @param
	 * @return
	 */
	public void insert_Ptyhbm(Map map) throws SQLException{
		CommUtils.addParams(map, false);
		ptyhbmDao.insert_Ptyhbm(map);
	}

	
	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 */
	public Integer update_Ptyhbm(Map map) throws SQLException{
		CommUtils.validateUpdateParams(map);
		CommUtils.addParams(map, true);
		return ptyhbmDao.update_Ptyhbm(map);
	}
	
	/**
	 * 删除记录
	 * 
	 * @param
	 * @return
	 */
	public Integer delete_Ptyhbm(Map map) throws SQLException{
		CommUtils.validateDeleteParams(map);
		Map mm = new HashMap();
		if(!(StringUtil.checkEmpty(map.get("khbh"))||StringUtil.checkEmpty(map.get("bmbh")))){
			mm.put("khbh", map.get("khbh"));
			mm.put("bmbh", map.get("bmbh"));
			Map ptyhbmMap = get_Ptyhbm(mm);
			if(StringUtil.checkEmpty(ptyhbmMap.get("xh"))){
				ptyhbmMap.put("xh", StringUtil.getUUID());
			}
			//备份
			//1.平台用户部门删除备份	ptyhbmscbf
			ptyhbmscbfDao.insert_Ptyhbmscbf(ptyhbmMap);
			List ptyhList = ptyhDao.query_Ptyh(mm);
			for(int i=0;i<ptyhList.size();i++){
				Map ptyhMap = (Map)ptyhList.get(i);
				if(StringUtil.checkEmpty(ptyhMap.get("xh"))){
					ptyhMap.put("xh", StringUtil.getUUID());
					//备份
					//1.平台用户删除备份	ptyhscbf
					ptyhscbfDao.insert_Ptyhscbf(ptyhMap);
				}
				if(!StringUtil.checkEmpty(ptyhMap.get("yhzh"))){
					Map delMap = new  HashMap();
					delMap.put("yhzh", ptyhMap.get("yhzh"));
					//2。平台用户菜单角色	ptyhcdjs
					ptyhhmdlDao.delete_Ptyhhmdl(delMap);
				}
				if(!StringUtil.checkEmpty(ptyhMap.get("yhbh"))){
					Map delMap = new  HashMap();
					delMap.put("yhbh", ptyhMap.get("yhbh"));
					//3.平台用户虹膜登录	ptyhhmdl	
					ptyhcdjsDao.delete_Ptyhcdjs(delMap);
					//1.平台用户			ptyh
					ptyhDao.delete_Ptyh(delMap);
				}
			}	
			//2.设备通道精确权限	sbtdjqqx
			sbtdjqqxDao.delete_Sbtdjqqx(mm);			
			//3.设备通道通配权限	sbtdtpqx
			sbtdtpqxDao.delete_Sbtdtpqx(mm);
		}
		return ptyhbmDao.delete_Ptyhbm(map);
	}
	
	/**
	 * 批处理删除记录
	 * 
	 * @param
	 */
	public Integer batch_delete_Ptyhbm(Map map) throws SQLException{
		return ptyhbmDao.batch_delete_Ptyhbm(map);
	}

	
	/**
	 * 查询总数
	 * 
	 * @param
	 * @return
	 */
	public Integer count_Ptyhbm(Map map) throws SQLException{
		return ptyhbmDao.count_Ptyhbm(map);
	}
	
	/**
	 * 查询列表记录SELECT
	 * 
	 * @param
	 * @return
	 */
	public List<Map> query_Ptyhbm(Map map) throws SQLException{
		return ptyhbmDao.query_Ptyhbm(map);
	}
	
	/**
	 * 查询一条
	 * 
	 * @param
	 * @return
	 */
	public Map get_Ptyhbm(Map map) throws SQLException{
		return ptyhbmDao.get_Ptyhbm(map);
	}
	
	/**
	 * 导出EXCEL
	 * 
	 * @param
	 * @return
	 */
	public Map exportPtyhbm(Map map) throws SQLException{
		return this.listPtyhbmByPage(map);
	}
	
	/**
	 * Ptyhbm 分页
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	public Map listPtyhbmByPage(Map map) throws SQLException {
		List<Map> list = ptyhbmDao.query_Ptyhbm(map);
		Integer totalCount = ptyhbmDao.count_Ptyhbm(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * Ptyhbm 分页
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	public Map query_ptyhbm_ext(Map map) throws SQLException {
		List<Map> list = ptyhbmDao.query_ptyhbm_ext(map);
		Integer totalCount = ptyhbmDao.count_ptyhbm_ext(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", list);
		return map;
	}
}
