package com.palmnest.dao.airconditioner.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.airconditioner.ConditionerAddTimeDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.airconditioner.AirConditionerAddTime;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

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


@Repository("conditionerAddTimeDao")
public class ConditionerAddTimeDaoImpl extends GenericDaoHibernate<AirConditionerAddTime, Long> implements ConditionerAddTimeDao {

	private Logger LOG = Logger.getLogger(ConditionerAddTimeDaoImpl.class);

	public ConditionerAddTimeDaoImpl() {
		super(AirConditionerAddTime.class);
	}

	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		LOG.info("ConditionerAddTimeDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select").append("\n");
		sql.append("bill.type,org.name orgName,bill.roleId,permConfig.name auditRoleName,bill.sort,bill.id billId,bill.code code,vir.name virtualName,bill.roomNames,bill.applyArea,date_format(bill.create_time, '%Y-%m-%d %H:%i') applyTime,bill.useDisplayTime,bill.modification_disabled modificationDisabled,").append("\n");
		sql.append("useLengthTime,bill.priceDisplay,bill.freeTotal,bill.apply_user applyUser,bill.apply_tel applyTel,").append("\n");
		sql.append("bill.billSource billSource,bill.status billStatus,bill.switchUser,bill.switchTel ").append("\n");
		sql.append("from tb_airconditioner_addtime bill").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.virtualId=vir.id").append("\n");
		sql.append("left join tb_vir_perm_config permConfig on permConfig.id =  bill.roleId").append("\n");
		sql.append("left JOIN tb_organization org on org.id = bill.organizationId").append("\n");

		sql.append(" where 1=1 and bill.billSource=1 and bill.status<>1 ");//空调加时才显示 保存的数据不显示

		/*if(params.get("releaseCode") != null && !params.get("releaseCode").equals("")) {
			sql.append(" and t.code = '"+params.get("releaseCode")+"'").append("\n");
		}*/


		//审批人对应楼层过滤 A
		//if (params.get("roleToFloorIds") != null && !params.get("roleToFloorIds").equals("")) {
		//	sql.append("  and bill.floorId in (" + params.get("roleToFloorIds") + ")").append("\n");
		//}

		//app楼层过滤 B A&&B 的关系
		if (params.get("floorId") != null && !params.get("floorId").equals("")) {
			sql.append(" and bill.floorId = '" + params.get("floorId") + "'").append("\n");
		}


		if (params.get("virtualId") != null && !params.get("virtualId").equals("")) {
			sql.append(" and bill.virtualId = '" + params.get("virtualId") + "'").append("\n");
		}

		//项目过滤
		if (params.get("organizationId") != null && !params.get("organizationId").equals("")) {
			sql.append(" and bill.organizationId = '" + params.get("organizationId") + "'").append("\n");
		}


		if (params.get("creatorBillId") != null && !params.get("creatorBillId").equals("")) {
			if (params.get("userMobile") != null && !params.get("userMobile").equals("")) {
				sql.append(" and (bill.creatorBillId = '" + params.get("creatorBillId") + "'  or  bill.apply_tel = '" + params.get("userMobile") + "') ").append("\n");
			} else {
				sql.append(" and bill.creatorBillId = '" + params.get("creatorBillId") + "'").append("\n");
			}
		}
		
		if (params.get("airSwitchUserId") != null && !params.get("airSwitchUserId").equals("")) {
			sql.append(" and bill.switchUserId = '" + params.get("airSwitchUserId") + "'").append("\n");
		}

		if (params.get("preBookUserId") != null && !params.get("preBookUserId").equals("")) {
			sql.append(" and (bill.isAppManageBook = 1 and bill.creatorBillId='" + params.get("preBookUserId") + "') ").append("\n");
		}


		if (params.get("isQueryMarchInside") != null && !params.get("isQueryMarchInside").equals("") && params.get("isQueryMarchInside").equals("1")) {//申请时间
			sql.append(" and (date_format(bill.useStartTime, '%Y-%m-%d')>=DATE_SUB(CURDATE(), INTERVAL 3 MONTH)   or  date_format(bill.useStartTime, '%Y-%m-%d')<=DATE_ADD(CURDATE(), INTERVAL 3 MONTH)) ").append("\n");
		} else {
			if (params.get("applyTime") != null && !params.get("applyTime").equals("")) {//申请时间
				sql.append(" and date_format(bill.useStartTime, '%Y-%m-%d') = '" + params.get("applyTime") + "'").append("\n");
			}
		}


		boolean isAll = false;
		if (params.get("status") != null && !params.get("status").equals("")) {
			if (((String) params.get("status")).indexOf(",") != -1) {
				sql.append("  and bill.status in (" + params.get("status") + ")").append("\n");
			} else if (((String) params.get("status")).equals("0")) {
				isAll = true;
				//不过滤状态

				//查询全部时 暂不权限过滤
				/*sql.append("  and ( bill.status in (31,32,33,34,35) ").append("\n");//不等于保存

				if(params.get("airAuditPermission") != null && !params.get("airAuditPermission").equals("")) {
					sql.append(" or bill.status = '"+params.get("airAuditPermission")+"' ) ").append("\n");
				}*/

				sql.append("  and bill.status in (24,31,32,33,34,35) ").append("\n");

			} else {
				sql.append("  and bill.status =" + params.get("status")).append("\n");
			}
		}

		//if (params.get("status") != null && !params.get("status").equals("") && params.get("status").equals("21,22,23")) {//app查待审批时 过滤权限
		//	//app端 空调加时审批权限 待审批的数据
		//	if (params.get("airAuditPermission") != null && !params.get("airAuditPermission").equals("")) {
		//		sql.append(" and bill.status = '" + params.get("airAuditPermission") + "'").append("\n");
		//	}
		//}

		//PC角色 待审批的数据
		//21 22 33 查询自己的能审批数据
		/*if (params.get("billFrom") != null && !params.get("billFrom").equals("") && params.get("billFrom").equals(CodeConstants.PC)) {

			if (params.get("status") != null && !params.get("status").equals("")) {
				if (params.get("status").equals("21") || params.get("status").equals("22") || params.get("status").equals("23")) {
					if (params.get("airAuditPermission") != null && !params.get("airAuditPermission").equals("")) {
						sql.append(" and bill.status = '" + params.get("airAuditPermission") + "'").append("\n");
					}
				}
			} else {
				if (params.get("airAuditPermission") != null && !params.get("airAuditPermission").equals("")) {

					//查询全部时
					sql.append("  and ( bill.status in (31,32,33,34,35) ").append("\n");//不等于保存					
					sql.append(" or bill.status = '" + params.get("airAuditPermission") + "' ) ").append("\n");

				}
			}
		}*/

		if (params.get("companyName") != null && !params.get("companyName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("companyName") + "%'");
		}

		if (params.get("code") != null && !params.get("code").equals("")) {
			sql.append(" and bill.code like '%" + params.get("code") + "%'");
		}

		if (params.get("startTime") != null && !params.get("startTime").equals("")) {
			sql.append(" and bill.create_time >= str_to_date('" + params.get("startTime") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("endTime") != null && !params.get("endTime").equals("")) {
			sql.append(" and bill.create_time <= str_to_date('" + params.get("endTime") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}
		
		
		if (params.get("useStartTime") != null && !params.get("useStartTime").equals("")) {
			sql.append(" and bill.useStartTime >= str_to_date('" + params.get("useStartTime") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("useEndTime") != null && !params.get("useEndTime").equals("")) {
			sql.append(" and bill.useStartTime <= str_to_date('" + params.get("useEndTime") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}

		sql.append(" order by bill.create_time desc ").append("\n");

		LOG.info("ConditionerAddTimeDaoImpl findPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public Page reportFindPageBy(Map<String, Object> params, int pageSize,
								 int pageNo) {

		LOG.info("ConditionerAddTimeDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select").append("\n");
		sql.append("bill.virtualId,vir.name virtualName,vir.roomNames roomNames,org.name orgName,count(bill.id) billNo,").append("\n");
		sql.append("date_format(bill.useStartTime, '%Y-%m') applyTime,sum(bill.freeTotal) freeTotal").append("\n");
		sql.append("from tb_airconditioner_addtime bill").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.virtualId=vir.id").append("\n");
		sql.append("left JOIN tb_organization org on org.id = bill.organizationId").append("\n");
		sql.append("where bill.status=32 ").append("\n");


		if (params.get("reportTime") != null && !params.get("reportTime").equals("")) {
			//and date_format(bill.create_time, '%Y-%m')='2018-10'
			sql.append("and date_format(bill.useStartTime, '%Y-%m')='" + params.get("reportTime") + "'").append("\n");
		}

		if (params.get("companyName") != null && !params.get("companyName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("companyName") + "%'");
		}

		//项目过滤
		if (params.get("organizationId") != null && !params.get("organizationId").equals("")) {
			sql.append(" and bill.organizationId = '" + params.get("organizationId") + "'").append("\n");
		}

		sql.append("group by bill.virtualId").append("\n");

		LOG.info("ConditionerAddTimeDaoImpl findPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public List<Map<String, Object>> reportFind(Map<String, Object> params) {
		LOG.info("ConditionerAddTimeDaoImpl reportFind======");

		StringBuilder sql = new StringBuilder("select").append("\n");
		sql.append("bill.virtualId,vir.name virtualName,count(bill.id) billNo,").append("\n");
		sql.append("date_format(bill.create_time, '%Y-%m') applyTime,sum(bill.freeTotal) freeTotal").append("\n");
		sql.append("from tb_airconditioner_addtime bill").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.virtualId=vir.id").append("\n");
		sql.append("where bill.status=32 ").append("\n");


		if (params.get("reportTime") != null && !params.get("reportTime").equals("")) {
			//and date_format(bill.create_time, '%Y-%m')='2018-10'
			sql.append("and date_format(bill.create_time, '%Y-%m')='" + params.get("reportTime") + "'").append("\n");
		}

		if (params.get("companyName") != null && !params.get("companyName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("companyName") + "%'");
		}

		//项目过滤
		if (params.get("organizationId") != null && !params.get("organizationId").equals("")) {
			sql.append(" and bill.organizationId = '" + params.get("organizationId") + "'").append("\n");
		}

		sql.append("group by bill.virtualId").append("\n");

		LOG.info("ConditionerAddTimeDaoImpl reportFind sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public Page reportDetailFindPageBy(Map<String, Object> params,
									   int pageSize, int pageNo) {

		LOG.info("ConditionerAddTimeDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select").append("\n");
		sql.append("bill.code,bill.billSource, case when bill.billSource=1 then '空调加时' else '预定会议室' end billSourceName, bill.roomNames,bill.applyArea,").append("\n");
		sql.append("bill.useDisplayTime,bill.useLengthTime,bill.priceDisplay,bill.apply_user applyUser,bill.freeTotal").append("\n");
		sql.append("from tb_airconditioner_addtime bill").append("\n");
		sql.append("where bill.status=32 ").append("\n");


		if (params.get("reportTime") != null && !params.get("reportTime").equals("")) {
			//and date_format(bill.create_time, '%Y-%m')='2018-10'
			sql.append("and date_format(bill.useStartTime, '%Y-%m')='" + params.get("reportTime") + "'").append("\n");
		}

		if (params.get("virtualId") != null && !params.get("virtualId").equals("")) {
			sql.append(" and bill.virtualId = '" + params.get("virtualId") + "'").append("\n");
		}

		//项目过滤
		if (params.get("organizationId") != null && !params.get("organizationId").equals("")) {
			sql.append(" and bill.organizationId = '" + params.get("organizationId") + "'").append("\n");
		}

		LOG.info("ConditionerAddTimeDaoImpl findPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	/**
	 * 查找空 加时单 申请时间大于当前时间
	 * 条件:待使用状态
	 */
	public List<Map<String, Object>> findMeeting(){
		LOG.info("ConditionerAddTimeDaoImpl findMeeting");
		StringBuilder sql = new StringBuilder("select").append("\n");
		sql.append("bill.id id,apply_user user,apply_tel tel,roomNames roomNames,useDisplayTime useDisplayTime,creatorBillId creatorBillId,bill.useStartTime,org.name name ").append("\n");
		sql.append("from tb_airconditioner_addtime bill ").append("\n");
		sql.append("left join tb_organization org on bill.organizationId=org.id ").append("\n");
		sql.append("where bill.useEndTime<NOW() and bill.status=31").append("\n");
		LOG.info("ConditionerAddTimeDaoImpl findMeeting sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}
	/**
	 * 加时单 申请时间大于当前时间  把状态改为已完成
	 *
	 */
	@Override
	public void airAddTimeDoTimerTask(String id) {

		LOG.info("ConditionerAddTimeDaoImpl airAddTimeDoTimerTask");

		StringBuilder sql = new StringBuilder();
		sql.append("update tb_airconditioner_addtime set status=32").append("\n");
		sql.append("where id="+id).append("\n");

		LOG.info("ConditionerAddTimeDaoImpl airAddTimeDoTimerTask sql" + sql.toString());
		int count = getSession().createSQLQuery(sql.toString()).executeUpdate();
		LOG.info("ConditionerAddTimeDaoImpl airAddTimeDoTimerTask 执行结束count:" + count);

	}

	@Override
	public List<Map<String, Object>> airConditioningOrder(Map<String, Object> params) {
		LOG.info("ConditionerAddTimeDaoImpl airConditioningOrder");
		StringBuilder sql=new StringBuilder("select billk.* from  ( select").append("\n");
		sql.append("bill.organizationId organizationId,bill.id id,apply_user user,roomNames roomNames,useDisplayTime useDisplayTime,org.name name,bill.switchTel,useStartTime ,useEndTime,").append("\n");
		sql.append("TIMESTAMPDIFF(MINUTE,date_add("+params.get("timeName")+",interval - 10 minute),now()) mt ").append("\n");
        sql.append("from tb_airconditioner_addtime bill left join tb_organization org on bill.organizationId=org.id ").append("\n");
		sql.append("where "+params.get("type")+"=0)billk  where billk.mt<=10 and billk.mt>=0" ).append("\n");
		LOG.info("ConditionerAddTimeDaoImpl airConditioningOrder sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());

	}

	@Override
	public void updateAirConditioningOrder(Map<String, Object> params) {
		LOG.info("ConditionerAddTimeDaoImpl updateAirConditioningOrder");

		StringBuilder sql = new StringBuilder();
		sql.append("update tb_airconditioner_addtime set "+params.get("type")+"=1 where id="+params.get("id")).append("\n");
		int count = getSession().createSQLQuery(sql.toString()).executeUpdate();
		LOG.info("ConditionerAddTimeDaoImpl airAddTimeDoTimerTask 执行结束count:" + count);
	}

	@Override
	public void airAddTimeAuditOverTimeTask() {

		LOG.info("ConditionerAddTimeDaoImpl airAddTimeAuditOverTimeTask");

		StringBuilder sql = new StringBuilder();
		sql.append("update tb_airconditioner_addtime ").append("\n");
		sql.append("set status=35,").append("\n");
		sql.append("cancelReason='管理员审批超时',").append("\n");
		sql.append("cancelUser='系统自动',").append("\n");
		sql.append("cancelTime=NOW()").append("\n");
		sql.append("where useStartTime<NOW() and status in (24)").append("\n");

		LOG.info("ConditionerAddTimeDaoImpl airAddTimeAuditOverTimeTask sql" + sql.toString());
		int count = getSession().createSQLQuery(sql.toString()).executeUpdate();
		LOG.info("ConditionerAddTimeDaoImpl airAddTimeAuditOverTimeTask 执行结束count:" + count);

	}
	
	
	@Override
	public List<Map<String, Object>> getAirAddTimeBusinessData(Map<String, Object> params) {
		LOG.info("ConditionerAddTimeDaoImpl getAirAddTimeBusinessData======");

		
		StringBuilder sql = new StringBuilder("select").append("\n");		
		sql.append(" org.company_id compId, ").append("\n");
		sql.append(" bill.organizationId orgId, ").append("\n");
		sql.append(" count(bill.id) sumCount ").append("\n");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append(" ,count(if( (bill.create_time >= '"+params.get("beginDate")+"' and bill.create_time <= '"+params.get("endDate")+"') ,1,null)) newCount ").append("\n");
		}

		sql.append(" FROM ").append("\n");
		sql.append(" tb_airconditioner_addtime bill ").append("\n");
		sql.append(" LEFT JOIN tr_company_org org ON org.org_id = bill.organizationId ").append("\n");
		sql.append(" WHERE 1=1 and org.company_id is not null ").append("\n");
		if(null != params && params.containsKey("compId") && null != params.get("compId") && StringUtils.isNotEmpty(params.get("compId").toString())){
			sql.append(" AND org.company_id ="+Integer.parseInt(params.get("compId").toString()));
		}
		sql.append(" GROUP BY 	bill.organizationId ").append("\n");			
		

		LOG.info("ConditionerAddTimeDaoImpl getAirAddTimeBusinessData sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}
	
	
	@Override
	public List<Map<String, Object>> getGoodsReleaseBusinessData(Map<String, Object> params) {
		LOG.info("ConditionerAddTimeDaoImpl getGoodsReleaseBusinessData======");

		
		StringBuilder sql = new StringBuilder("select").append("\n");		
		sql.append(" org.company_id compId, ").append("\n");
		sql.append(" bill.orgId orgId, ").append("\n");
		sql.append(" count(bill.id) sumCount ").append("\n");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append(" ,count(if( (bill.create_time >='"+params.get("beginDate")+"' and bill.create_time <= '"+params.get("endDate")+"') ,1,null)) newCount ").append("\n");
		}

		sql.append(" FROM ").append("\n");
		sql.append(" tb_goods_release bill ").append("\n");
		sql.append(" LEFT JOIN tr_company_org org ON org.org_id = bill.orgId ").append("\n");
		sql.append(" WHERE 1=1 and org.company_id is not null ").append("\n");
		if(null != params && params.containsKey("compId") && null != params.get("compId") && StringUtils.isNotEmpty(params.get("compId").toString())){
			sql.append(" AND org.company_id ="+Integer.parseInt(params.get("compId").toString()));
		}
		sql.append(" GROUP BY 	bill.orgId ").append("\n");			
		

		LOG.info("ConditionerAddTimeDaoImpl getGoodsReleaseBusinessData sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getNoticeBusinessData(Map<String, Object> params) {
		StringBuilder sql = new StringBuilder("   ");

		sql.append(" SELECT temp.compId, temp.orgId, sum( temp.sumCount) sumCount, sum( temp.newCount ) newCount FROM( ");

		sql.append(" SELECT oc.company_id compId, t.orgId, t.sumCount sumCount, t.newCount  newCount ");
		sql.append("  FROM(  ");
		sql.append("  SELECT n.orgId, COUNT( n.id ) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( n.createTime >= '"+params.get("beginDate").toString()+"' AND n.createTime <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("   FROM tb_app_notice n ");
		sql.append("  GROUP BY n.orgId  ");
		sql.append(" UNION  ");
		sql.append(" SELECT n.orgId, COUNT( n.id ) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( n.createTime >= '"+params.get("beginDate").toString()+"' AND n.createTime <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("  FROM tb_wxtemplate_message n  ");
		sql.append("  GROUP BY n.orgId  ");
		sql.append("  UNION ");
		sql.append(" SELECT n.orgId, COUNT( n.id ) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( n.createTime >= '"+params.get("beginDate").toString()+"' AND n.createTime <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("  FROM tb_phone_message n   ");
		sql.append("  GROUP BY n.orgId  ");
		sql.append("  UNION ");
		sql.append(" SELECT n.orgId, COUNT( n.id ) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( n.createTime >= '"+params.get("beginDate").toString()+"' AND n.createTime <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("  FROM tb_email_send n  ");
		sql.append("  GROUP BY n.orgId  ");
		sql.append("  ) t, tr_company_org oc  ");
		sql.append("  WHERE t.orgId = oc.org_id  ");
		if(null != params && params.containsKey("compId") && null != params.get("compId") && StringUtils.isNotEmpty(params.get("compId").toString())){
			sql.append(" AND oc.company_id ="+Integer.parseInt(params.get("compId").toString()));
		}
		sql.append(" ORDER BY oc.company_id, t.orgId  ");
		sql.append("  ) temp  ");


		sql.append("  GROUP BY temp.compId, temp.orgId ");
		LOG.info("ConditionerAddTimeDaoImpl getNoticeBusinessData sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getRepairBusinessData(Map<String, Object> params) {
		StringBuilder sql = new StringBuilder(" SELECT oc.company_id compId, oc.org_id orgId, count( v.id) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( v.create_time >= '"+params.get("beginDate").toString()+"' AND v.create_time <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("  FROM tb_repair_bills v LEFT JOIN tr_company_org oc ON oc.org_id = v.orgId   ");
		sql.append(" WHERE v.orgId IS NOT NULL AND oc.org_id IS NOT NULL   ");
		if(null != params && params.containsKey("compId") && null != params.get("compId") && StringUtils.isNotEmpty(params.get("compId").toString())){
			sql.append(" AND oc.company_id ="+Integer.parseInt(params.get("compId").toString()));
		}
		sql.append("  GROUP BY oc.company_id, oc.org_id ");
		sql.append(" ORDER BY oc.company_id, oc.org_id  ");
		LOG.info("ConditionerAddTimeDaoImpl getRepairBusinessData sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getVisitorBusinessData(Map<String, Object> params) {
		StringBuilder sql = new StringBuilder(" SELECT oc.company_id compId, t.orgId, SUM( t.sumCount) sumCount, sum( t.newCount ) newCount  ");
		sql.append("  FROM(  ");
		sql.append("  SELECT u.organizationId orgId, count( v.visitorOrderId ) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( v.visitorOrderCreateTime >= '"+params.get("beginDate").toString()+"' AND v.visitorOrderCreateTime <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("   FROM tb_visitororder v LEFT JOIN tb_user u ON u.id = v.visitorUserId   ");
		sql.append("   WHERE ( v.visitorOrderType = 1 OR v.visitorOrderType = 2 OR v.visitorOrderType = 3 )   ");
		sql.append("   GROUP BY u.organizationId    ");
		sql.append("   UNION    ");
		sql.append("  SELECT v.visitorToOrgId orgId, count( v.visitorOrderId ) sumCount  ");
		if(null != params && params.containsKey("beginDate") && params.containsKey("endDate") && null != params.get("beginDate") && null != params.get("endDate")){
			sql.append("  , count( IF ( ( v.visitorOrderCreateTime >= '"+params.get("beginDate").toString()+"' AND v.visitorOrderCreateTime <= '"+params.get("endDate")+"' ), 1, NULL )) newCount ");
		}
		sql.append("   FROM tb_visitororder v    ");
		sql.append("   WHERE ( v.visitorOrderType = 4 OR v.visitorOrderType = 5 )    ");
		sql.append("   GROUP BY v.visitorToOrgId   ");
		sql.append("  ) t, tr_company_org oc  ");
		sql.append("  WHERE t.orgId = oc.org_id  ");
		if(null != params && params.containsKey("compId") && null != params.get("compId") && StringUtils.isNotEmpty(params.get("compId").toString())){
			sql.append(" AND oc.company_id ="+Integer.parseInt(params.get("compId").toString()));
		}
		sql.append("  GROUP BY oc.company_id, t.orgId ");
		sql.append(" ORDER BY oc.company_id, t.orgId  ");
		LOG.info("ConditionerAddTimeDaoImpl getVisitorBusinessData sql" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

}
