package com.palmnest.dao.dinner.impl;

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

import com.palmnest.application.Constants;
import lombok.extern.slf4j.Slf4j;
import org.openxmlformats.schemas.drawingml.x2006.chart.STScatterStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import cn.jiguang.common.utils.StringUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.input.OrderDinnerInputDto;
import com.palmnest.dao.dinner.OrderDinnerDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dinner.OrderDinner;

@Slf4j
@Repository("orderDinnerDao")
public class OrderDinnerDaoImpl extends GenericDaoHibernate<OrderDinner, Long> implements OrderDinnerDao {
	private static Logger LOG = LoggerFactory.getLogger(OrderDinnerDaoImpl.class);

	public OrderDinnerDaoImpl() {
		super(OrderDinner.class);
	}

	/**
	 * 获取该用户创建的订单
	 *
	 * @param consId
	 * @param orgId
	 * @return
	 */
	@Override
	public List<OrderDinner> findByCons(String consId, String orgId, String status) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" from OrderDinner where  orgId=? and isEnabled='1'  ");
		sql.append("   and date_format(createTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d')  ");
		if (StringUtils.isNotEmpty(consId)) {
			sql.append(" and creator='" + consId + "'  ");
		}
		if (StringUtils.isNotEmpty(status)) {
			sql.append(" and status in (" + status + ")  ");
		}
		return getSession().createQuery(sql.toString()).setString(0, orgId).list();
	}

	/**
	 * PC/WECHAT端获取订餐列表
	 *
	 * @param dto
	 * @return
	 */
	@Override
	public Page findList(OrderDinnerInputDto dto, int pageSize, int pageNo) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select distinct o.orgId,o.id orderId,o.star,o.number,o.`status`,c.userName,ifnull(dol.itemname,dl.itemname) address,c.userMobile mobile,di.name dept ");
		sql.append(" ,date_format(o.createTime,'%Y-%m-%d %T') createTime,date_format(o.dinnerTime,'%Y-%m-%d') dinnerTime,o.integral  ");
		sql.append(" ,(case when o.isTempOrder=1 then '临时工作餐' when od.isMeetFood=1 then '会议餐' else '普通订餐' end ) typeName ");
		sql.append(" from tb_order_dinner  o ");
		sql.append(" left join tb_order_dinner_info od on o.id=od.orderId ");
		sql.append(" left join tb_user c on c.id=o.consId ");
		sql.append(" left join tb_dictionary_item dl on dl.id=c.address  ");
		sql.append(" left join tb_dictionary_org_item dol on dol.id=c.address  ");
		sql.append(" left join tb_department di on di.id=c.departmentId  ");
		sql.append(" where o.isEnabled='1' and c.isEnabled=1  ");
		//项目
		if (StringUtil.isNotEmpty(dto.getOrgId())) {
			sql.append(" and o.orgId in (" + dto.getOrgId() + ") ");
		}
		//订餐人
		if (StringUtil.isNotEmpty(dto.getConsId())) {
			sql.append(" and o.consId='" + dto.getConsId() + "' ");
		}
		//部门
		if (StringUtil.isNotEmpty(dto.getDept())) {
			sql.append(" and c.departmentId=" + dto.getDept() + " ");
		}
		//企业
		if (StringUtil.isNotEmpty(dto.getVirId())) {
			sql.append(" and c.virtualArchId=" + dto.getVirId() + " ");
		}
		//预订人
		if (StringUtil.isNotEmpty(dto.getUserName())) {
			sql.append(" and c.userName like '%" + dto.getUserName() + "%' ");
		}
		//订餐时间
		if (StringUtil.isNotEmpty(dto.getOrderTime())) {
			String beginTime = dto.getOrderTime() + " 00:00:00 ";
			if (StringUtils.isNotEmpty(dto.getEndOrderTime())) {
				String endTime = dto.getEndOrderTime() + " 23:59:59 ";
				sql.append(" and (o.dinnerTime>='" + beginTime + "' and o.dinnerTime<='" + endTime + "' ) ");
			} else {
				sql.append(" and  o.dinnerTime>='" + beginTime + "' ");
			}
		} else if (StringUtil.isNotEmpty(dto.getEndOrderTime())) {
			String endTime = dto.getEndOrderTime() + " 23:59:59 ";
			sql.append(" and o.dinnerTime<='" + endTime + "'  ");
		}

		//下单时间
		if (StringUtil.isNotEmpty(dto.getBeginTime())) {
			String beginTime = dto.getBeginTime() + " 00:00:00 ";
			if (StringUtil.isNotEmpty(dto.getEndTime())) {
				String endTime = dto.getEndTime() + " 23:59:59 ";
				sql.append(" and (o.createTime>='" + beginTime + "' and o.createTime<='" + endTime + "' ) ");
			} else {
				sql.append(" and  o.createTime>='" + beginTime + "' ");
			}
		} else if (StringUtil.isNotEmpty(dto.getEndTime())) {
			String endTime = dto.getEndTime() + " 23:59:59 ";
			sql.append(" and o.createTime<='" + endTime + "'  ");
		}
		//状态
		if (StringUtil.isNotEmpty(dto.getStatus()) && !"0".equals(dto.getStatus())) {
			sql.append(" and o.status='" + dto.getStatus() + "'  ");
		}
		//星级
		if (StringUtil.isNotEmpty(dto.getStar()) && !"0".equals(dto.getStar())) {
			sql.append(" and o.star=" + dto.getStar() + " ");
		}
		if (StringUtil.isNotEmpty(dto.getType()) && !"0".equals(dto.getType())) {
			if ("1".equals(dto.getType())) {//普通餐
				sql.append(" and o.isTempOrder=0 ");
			} else if ("2".equals(dto.getType())) {//会议餐
				sql.append(" and od.isMeetFood=1 ");
			} else if ("3".equals(dto.getType())) {//临时工作餐
				sql.append(" and o.isTempOrder=1 ");
			}
		}
		sql.append(" order by o.dinnerTime desc,o.createTime desc ");
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	/**
	 * 确认送达后将当天所有有效的订单的状态改成待评价状态
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public int updateOrderStatus(String orgId, String consId) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" update OrderDinner set  status='2',modifier='" + consId + "',modifyTime=SYSDATE()    where  orgId='" + orgId + "' and isEnabled='1'  ");
		sql.append("   and date_format(dinnerTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d') and status='1'  ");
		return getSession().createQuery(sql.toString()).executeUpdate();
	}

	@Override
	public List<OrderDinner> findByConsId(String consId, String orgId) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" from OrderDinner where  orgId=? and isEnabled='1'  ");
		if (StringUtils.isNotEmpty(consId)) {
			sql.append(" and consId='" + consId + "'  ");
		}
		return getSession().createQuery(sql.toString()).setString(0, orgId).list();
	}

	/**
	 * 获取该项目下第一条数据创建的时间
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public String getTopCreateTime(String orgId) {
		String time = "";
		if (StringUtils.isNotEmpty(orgId)) {
			String sql = "select date_format(createTime,'%Y-%m-%d') createTime from tb_order_dinner where orgId in (" + orgId + ")  order by createTime LIMIT 1";
			List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql);
			if (list != null && list.size() > 0) {
				Map<String, Object> map = list.get(0);
				if (map != null) {
					time = map.get("createTime") == null ? "" : map.get("createTime").toString();
				}
			}
		}
		return time;
	}

	/**
	 * 获取个人订单统计
	 *
	 * @param orgId    项目
	 * @param dateType 1年2季度3月
	 * @param year     年份
	 * @param time     时间【dateType=1时，该字段不需要填，dateType=2时，time的值有1-4个季度，
	 *                 dateType=3时，time的值有1-12个月】
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getConsumerOrderList(String orgId,
														  int dateType, String year, String time) {
		StringBuilder sql = new StringBuilder("");
		if (StringUtils.isEmpty(orgId)) {
			return null;
		}
		sql.append(" select vir.name companyName,sum(od.foodCount) sumCount,c.id,c.user_Name userName,dl.name dept,c.user_Mobile mobile ");
		if (dateType == 1) {//年度
			sql.append(" ,(select ifnull(sum(odi.foodCount),0) from tb_order_dinner tod  ");
			sql.append(" 	left join tb_order_dinner_info odi on tod.id=odi.orderId  ");
			sql.append(" 	where odi.consId=c.id and o.isEnabled='1'  ");
			sql.append(" 	and od.isEnabled='1' and (tod.status='2' or tod.status='3')    ");
			sql.append(" 	and year(od.createTime)=" + year + "    and odi.isMeetFood=1) fruitFood ");
			sql.append(" 	,(select ifnull(sum(odi.foodCount),0) from tb_order_dinner tod  ");
			sql.append(" 	left join tb_order_dinner_info odi on tod.id=odi.orderId  ");
			sql.append(" 	where odi.consId=c.id and o.isEnabled='1'  ");
			sql.append(" 	and od.isEnabled='1' and (tod.status='2' or tod.status='3')    ");
			sql.append(" 	and  year(od.createTime)=" + year + "    and tod.isTempOrder=1) tempWorkFood ");
		} else if (dateType == 2) {//季度
			sql.append(" ,(select ifnull(sum(odi.foodCount),0) from tb_order_dinner tod  ");
			sql.append(" 	left join tb_order_dinner_info odi on tod.id=odi.orderId  ");
			sql.append(" 	where odi.consId=c.id and o.isEnabled='1'  ");
			sql.append(" 	and od.isEnabled='1' and (tod.status='2' or tod.status='3')    ");
			sql.append(" 	and  year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  and odi.isMeetFood=1) fruitFood ");
			sql.append(" 	,(select ifnull(sum(odi.foodCount),0) from tb_order_dinner tod  ");
			sql.append(" 	left join tb_order_dinner_info odi on tod.id=odi.orderId  ");
			sql.append(" 	where odi.consId=c.id and o.isEnabled='1'  ");
			sql.append(" 	and od.isEnabled='1' and (tod.status='2' or tod.status='3')    ");
			sql.append(" 	and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "    and tod.isTempOrder=1) tempWorkFood ");
		} else if (dateType == 3) {//月
			sql.append(" ,(select ifnull(sum(odi.foodCount),0) from tb_order_dinner tod  ");
			sql.append(" 	left join tb_order_dinner_info odi on tod.id=odi.orderId  ");
			sql.append(" 	where odi.consId=c.id and o.isEnabled='1'  ");
			sql.append(" 	and od.isEnabled='1' and (tod.status='2' or tod.status='3')    ");
			sql.append(" 	and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "    and odi.isMeetFood=1) fruitFood ");
			sql.append(" 	,(select ifnull(sum(odi.foodCount),0) from tb_order_dinner tod  ");
			sql.append(" 	left join tb_order_dinner_info odi on tod.id=odi.orderId  ");
			sql.append(" 	where odi.consId=c.id and o.isEnabled='1'  ");
			sql.append(" 	and od.isEnabled='1' and (tod.status='2' or tod.status='3')    ");
			sql.append(" 	and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "     and tod.isTempOrder=1) tempWorkFood ");
		}
		sql.append(" from tb_order_dinner_info od ");
		sql.append(" inner join " + Constants.DATABASE + "tb_user c on od.consId=c.id ");
		sql.append(" left join " + Constants.DATABASE + "tb_department dl on dl.id=c.department_Id ");
		sql.append(" left join tb_order_dinner o on o.id=od.orderId ");
		sql.append(" left join tb_virtualarchitecture vir on vir.id=c.virt_id ");
		sql.append(" where c.org_id in (" + orgId + ") and o.isEnabled='1' and od.isEnabled='1' and (o.status='2' or o.status='3') ");
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		sql.append(" group by c.id ");
		sql.append(" order by dl.id  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 规格数量
	 *
	 * @param orgId    项目
	 * @param dateType 1年2季度3月
	 * @param year     年份
	 * @param time     时间【dateType=1时，该字段不需要填，dateType=2时，time的值有1-4个季度，
	 *                 dateType=3时，time的值有1-12个月】
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getSpecOrderList(String orgId,
													  int dateType, String year, String time) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return null;
		}
		StringBuilder sql = new StringBuilder("");
		sql.append("  select sum(od.foodCount) sumCount,m.spec");
		sql.append("  from tb_order_dinner_info od   ");
		sql.append("  left join tb_order_dinner o on o.id=od.orderId   ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append("  where 1=1  ");
		sql.append("  and od.orgId in (" + orgId + ")  ");
		sql.append(" and o.isEnabled='1' and od.isEnabled='1' and (o.status='2' or o.status='3')   ");
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		sql.append("  group by m.spec  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 主食数量
	 *
	 * @param orgId    项目
	 * @param dateType 1年2季度3月
	 * @param year     年份
	 * @param time     时间【dateType=1时，该字段不需要填，dateType=2时，time的值有1-4个季度，
	 *                 dateType=3时，time的值有1-12个月】
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getStapleFoodOrderList(String orgId,
															int dateType, String year, String time) {
		if (StringUtils.isEmpty(orgId)) {
			return null;
		}
		StringBuilder sql = new StringBuilder("");
		sql.append(" select sum(od.foodCount) sumCount,od.stapleFood,od.stapleFoodStr  ");
		sql.append(" from tb_order_dinner_info od  ");
		sql.append(" left join tb_order_dinner o on o.id=od.orderId  ");
		sql.append(" where od.orgId in (" + orgId + ") and o.isEnabled='1' and od.isEnabled='1' and (o.status='2' or o.status='3')  ");
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		sql.append(" group by od.stapleFood  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public int getMeetCount(String orgId, int dateType, String year, String time, int type) {
		if (StringUtils.isEmpty(orgId)) {
			return 0;
		}
		StringBuilder sql = new StringBuilder("");
		sql.append("  select sum(od.foodCount) sumCount from tb_order_dinner o  ");
		sql.append("  left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append("  where o.orgId in (" + orgId + ") and o.isEnabled='1' and od.isEnabled='1'  ");
		sql.append("  and (o.status='2' or o.status='3')  ");
		if (type == 1) {//会议餐
			sql.append("  and od.isMeetFood=1  ");
		} else if (type == 2) {//工作餐
			sql.append(" and m.spec>1 and od.isMeetFood=0   ");
		} else if (type == 3) {//单品
			sql.append(" and m.spec=1   ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	@Override
	public int getMeetCountByDept(String orgId, int dateType, String year,
								  String time, int type, String dept) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return 0;
		}
		if (!StringUtil.isNotEmpty(dept)) {
			return 0;
		}
		StringBuilder sql = new StringBuilder("");
		sql.append("  select sum(od.foodCount) sumCount from tb_order_dinner o  ");
		sql.append("  left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append("  left join tb_user c on c.id=od.consId  ");
//			sql.append(" left join tb_dictionary_item dl on dl.id=c.departmentId ");
		sql.append("  where o.orgId in (" + orgId + ") and o.isEnabled='1' and od.isEnabled='1'  ");
		sql.append("  and (o.status='2' or o.status='3')  ");
		if (type == 1) {//会议餐
			sql.append("  and od.isMeetFood=1  ");
		} else if (type == 2) {//工作餐
			sql.append(" and m.spec>1 and od.isMeetFood=0 and o.isTempOrder=0");
		} else if (type == 3) {//单品
			sql.append(" and m.spec=1   ");
		} else if (type == 4) {//临时工作餐
			sql.append("  and o.isTempOrder=1   ");
		} else if (type == 5) {//组合
			sql.append("  and m.spec>1   ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		sql.append(" and c.departmentId=" + dept + " ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	@Override
	public int getTempFoodCountByDept(String orgId, int dateType, String year,
									  String time, int type, String deptOrComp, int isDept) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return 0;
		}
		if (!StringUtil.isNotEmpty(deptOrComp)) {
			return 0;
		}
		StringBuilder sql = new StringBuilder("");
		sql.append("  select sum(od.foodCount) sumCount from tb_order_dinner o  ");
		sql.append("  left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append("  left join tb_user c on c.id=od.consId  ");
//			sql.append(" left join tb_dictionary_item dl on dl.id=c.departmentId ");
		sql.append("  where o.orgId in (" + orgId + ") and o.isEnabled='1' and od.isEnabled='1'  ");
		sql.append("  and (o.status='2' or o.status='3')  ");
		if (type == 1) {//会议餐
			sql.append("  and od.isMeetFood=1  ");
		} else if (type == 2) {//工作餐
			sql.append(" and m.spec>1 and od.isMeetFood=0 and o.is   ");
		} else if (type == 3) {//单品
			sql.append(" and m.spec=1   ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		if (isDept == 1) {
			sql.append(" and c.departmentId=" + deptOrComp + " ");
		} else if (isDept == 2) {
			sql.append(" and c.virtualArchId=" + deptOrComp + " ");
		}

		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	/**
	 * 根据所有部门汇总主食
	 *
	 * @param orgId
	 * @param dateType
	 * @param year
	 * @param time
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getFoodListByDept(String orgId,
													   int dateType, String year, String time) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select vir.name companyName,dl.name dept,sum(od.foodCount) sumCount,od.stapleFood,od.stapleFoodStr  ");
		sql.append("  from tb_order_dinner_info od  ");
		sql.append("  LEFT join tb_order_dinner o on o.id=od.orderId  ");
		sql.append("  left join " + Constants.DATABASE + "tb_user c on c.id=od.consId  ");
		sql.append(" left join " + Constants.DATABASE + "tb_department dl on dl.id=c.department_Id ");
		sql.append(" left join tb_virtualarchitecture vir on vir.id=c.virt_id ");
//			sql.append("  left join tb_consumer c on c.id=od.creator  ");
		sql.append("  where 1=1  ");
		if (StringUtils.isNotEmpty(orgId)) {
			sql.append("  and o.orgId in (" + orgId + ")  ");
		}
		sql.append("   and o.isEnabled='1' and od.isEnabled='1' and (o.status='2' or o.status='3')  ");
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		sql.append("  group by c.virt_id,dl.id,od.stapleFood  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getSpecListByDept(String orgId,
													   int dateType, String year, String time) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select c.dept,sum(od.foodCount) sumCount,m.spec  ");
		sql.append("  from tb_order_dinner_info od  ");
		sql.append("  LEFT join tb_order_dinner o on o.id=od.orderId  ");
		sql.append("  left join tb_consumer c on c.id=od.creator  ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append("  where 1=1  ");
		if (StringUtils.isNotEmpty(orgId)) {
			sql.append("  and o.orgId in (" + orgId + ")  ");
		}
		sql.append("   and o.isEnabled='1' and od.isEnabled='1' and (o.status='2' or o.status='3')  ");
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)=" + year + "  and QUARTER(od.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(od.createTime)=" + year + "  and month(od.createTime)=" + time + "   ");
		}
		sql.append("  group by c.dept,m.spec  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 该项目下所有有效的订单
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public List<OrderDinner> getOrderDinnerByOrgId(String orgId, String status) {
		StringBuilder sql = new StringBuilder("from OrderDinner where orgId=? and isEnabled='1' ");
		if (StringUtils.isNotEmpty(status)) {
			sql.append("  and status in (" + status + ") ");
		}
		return getSession().createQuery(sql.toString()).setString(0, orgId).list();
	}

	@Override
	public List<Map<String, Object>> getStarOrderList(String orgId,
													  int dateType, String year, String time) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select o.star,count(o.id) sumCount from tb_order_dinner o ");
		sql.append(" where o.isEnabled='1' and o.status='3' ");
		if (StringUtils.isNotEmpty(orgId)) {
			sql.append("  and o.orgId in (" + orgId + ")  ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(o.createTime)=" + year + "   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(o.createTime)=" + year + "  and QUARTER(o.createTime)=" + time + "  ");
		} else if (dateType == 3) {//月
			sql.append("  and year(o.createTime)=" + year + "  and month(o.createTime)=" + time + "   ");
		}
		sql.append(" group by o.star ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<OrderDinner> getOrderInfoByParentOrderId(String orderId, String status) {
		StringBuilder sql = new StringBuilder("from OrderDinner where parentOrderId=? ");
		if (StringUtils.isNotEmpty(status)) {
			sql.append(" and status=" + status + "  ");
		}
		return getSession().createQuery(sql.toString()).setString(0, orderId).list();
	}

	@Override
	public int updateOrderDinnerByTime(String orgIds, String time) {
		StringBuilder sql = new StringBuilder("update tb_order_dinner set status='2',modifyTime=SYSDATE() where isEnabled='1' ");
		if (StringUtil.isNotEmpty(orgIds)) {
			sql.append("  and orgId in (" + orgIds + ") ");
		}
		sql.append(" and status='1' ");
		sql.append(" and date_format(dinnerTime,'%Y-%m-%d')='" + time + "' ");
		return getSession().createSQLQuery(sql.toString()).executeUpdate();
	}

	@Override
	public List<Map<String, Object>> getAllMealName(String orgId) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select DISTINCT ifnull(diot.id,dit.id) itemId,ifnull(diot.itemname,dit.itemname) mealname from tb_dictionary_group dg  ");
		sql.append(" left join tb_dictionary_item dit on dg.id=dit.groupid  ");
		sql.append(" left join tb_dictionary_org_item diot on dg.id=diot.groupid and orgId=" + orgId + "  ");
		sql.append(" where dg.groupcode='mealName' or dg.groupcode='mealNameBySingle'  ");
		sql.append(" ORDER BY dg.id,ifnull(diot.orderno,dit.orderno)  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getUserByAddress(String orgId,
													  String addressId) {
		StringBuilder sql = new StringBuilder();
		sql.append("   select ifnull(ador.itemname,adr.itemname) address,u.id,u.userName deptname,ifnull(b.chuan,0) chuan,ifnull(b.yue,0) yue,ifnull(b.su,0) su,ifnull(b.meet,0) meet  ");
		sql.append("   ,ifnull(b.dao,0) dao,ifnull(b.you,0) you,ifnull(b.za,0) za,ifnull(b.he,0) he,ifnull(b.te,0) te,ifnull(b.xian,0) xian  ");
		sql.append("   ,ifnull(b.ka,0) ka,ifnull(b.suan,0) suan,ifnull(b.egg,0) egg,ifnull(b.workBuns,0) workBuns  ");
		sql.append("   ,ifnull(b.workRoll,0) workRoll ,ifnull(k.rolls,0) rolls ,ifnull(k.buns,0) buns  ");
		sql.append("   ,(ifnull(b.chuan,0)+ifnull(b.yue,0)+ifnull(b.su,0)   ");
		sql.append("   +ifnull(b.meet,0)+ifnull(b.dao,0)+ifnull(b.you,0)+ifnull(b.za,0)+ifnull(b.he,0)+ifnull(b.te,0)+ifnull(b.xian,0)  ");
		sql.append("   +ifnull(b.ka,0)+ifnull(b.suan,0)+ifnull(b.egg,0)+ifnull(b.workBuns,0)+ifnull(b.workRoll,0)) heji  ");
		sql.append("   from tb_user u  ");
		sql.append("   left join tb_dictionary_item adr on adr.id=u.address  ");
		sql.append("   left join tb_dictionary_org_item ador on ador.id=u.address  ");
		sql.append("  left join (select c.address,od.consId consId,sum(case ifnull(doi.itemname,di.itemname) when '素餐' then foodCount else 0 end ) su,sum(case ifnull(doi.itemname,di.itemname) when '川菜' then foodCount else 0 end ) chuan,sum(case ifnull(doi.itemname,di.itemname) when '粤菜' then foodCount else 0 end ) yue  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '会议餐' then foodCount else 0 end ) meet,sum(case ifnull(doi.itemname,di.itemname) when '刀削面' then foodCount else 0 end ) dao  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname) when '油泼面' then foodCount else 0 end ) you  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '杂酱面' then foodCount else 0 end ) za,sum(case ifnull(doi.itemname,di.itemname)  when '河粉' then foodCount else 0 end ) he  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '水果餐' then foodCount else 0 end ) te,sum(case ifnull(doi.itemname,di.itemname)  when '鲜奶' then foodCount else 0 end ) xian  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '卡士' then foodCount else 0 end ) ka,sum(case ifnull(doi.itemname,di.itemname)  when '酸奶' then foodCount else 0 end ) suan  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '咸鸭蛋' then foodCount else 0 end ) egg,sum(case ifnull(doi.itemname,di.itemname)  when '纯馒头' then foodCount else 0 end ) workBuns  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '纯花卷' then foodCount else 0 end ) workRoll  ");
		sql.append("   from tb_order_dinner_info od    ");
		sql.append("   left join  tb_order_dinner o on o.id=od.orderId  ");
		sql.append("   left join tb_user  c on c.id=o.consId   ");
		sql.append("   left join tb_meal m on m.id=od.mealId  ");
		sql.append("   left join tb_dictionary_item di on di.id=m.mealCodeId   ");
		sql.append("   left join tb_dictionary_org_item doi on doi.id=m.mealCodeId   ");
		sql.append("   where o.orgId=" + orgId + "   and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d')  ");
		sql.append("   and o.status='1'  ");
		sql.append("   group by c.id) b on b.consId=u.id  and  b.address=adr.id    ");
		sql.append("   left join (select c.address,od.consId consId,sum(case ifnull(doi.itemname,di.itemname) when '花卷' then foodCount else 0 end ) rolls,sum(case ifnull(doi.itemname,di.itemname) when '馒头' then foodCount else 0 end ) buns  ");
		sql.append("   from tb_order_dinner_info od    ");
		sql.append("   left join  tb_order_dinner o on o.id=od.orderId  ");
		sql.append("   left join tb_user  c on c.id=o.consId   ");
		sql.append("   left join tb_dictionary_item di on di.id=od.stapleFood  ");
		sql.append("   left join tb_dictionary_org_item doi on doi.id=od.stapleFood  ");
		sql.append("   where o.orgId=" + orgId + "   and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d')   ");
		sql.append("   and o.status='1'  ");
		sql.append("   group by c.id) k on k.consId=u.id  and  k.address=adr.id    ");
		sql.append("   where u.organizationId=" + orgId + " and u.isLeader=1 and u.isEnabled=1  and u.address=" + addressId + " ");
		sql.append("   order by u.orderNum  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getDeptByAddress(String orgId,
													  String addressId) {
		StringBuilder sql = new StringBuilder();
		sql.append("   select distinct ifnull(ador.itemname,adr.itemname) address,d.id,IF(IFNULL(d.shortName,'')='',d.name,d.shortName) deptname   ");
		sql.append("   ,ifnull(b.chuan,0) chuan,ifnull(b.yue,0) yue,ifnull(b.su,0) su,ifnull(b.meet,0) meet  ");
		sql.append("   ,ifnull(b.dao,0) dao,ifnull(b.you,0) you,ifnull(b.za,0) za,ifnull(b.he,0) he,ifnull(b.te,0) te,ifnull(b.xian,0) xian  ");
		sql.append("   ,ifnull(b.ka,0) ka,ifnull(b.suan,0) suan,ifnull(b.egg,0) egg,ifnull(b.workBuns,0) workBuns,ifnull(b.workRoll,0) workRoll  ");
		sql.append("   ,ifnull(k.rolls,0) rolls ,ifnull(k.buns,0) buns ,(ifnull(b.chuan,0)+ifnull(b.yue,0)+ifnull(b.su,0)  ");
		sql.append("   +ifnull(b.meet,0)+ifnull(b.dao,0)+ifnull(b.you,0)+ifnull(b.za,0)+ifnull(b.he,0)+ifnull(b.te,0)+ifnull(b.xian,0)  ");
		sql.append("   +ifnull(b.ka,0)+ifnull(b.suan,0)+ifnull(b.egg,0)+ifnull(b.workBuns,0)+ifnull(b.workRoll,0)) heji  ");
		sql.append("   from tb_department d  ");
		sql.append("   left join tb_user u on d.id=u.departmentId  ");
		sql.append("   left join tb_dictionary_item adr on adr.id=u.address  ");
		sql.append("   left join tb_dictionary_org_item ador on ador.id=u.address  ");
		sql.append("   left join (select c.address,IF(IFNULL(dept.shortName,'')='',dept.name,dept.shortName) deptname,c.departmentId deptId,sum(case ifnull(doi.itemname,di.itemname) when '素餐' then foodCount else 0 end ) su,sum(case ifnull(doi.itemname,di.itemname) when '川菜' then foodCount else 0 end ) chuan,sum(case ifnull(doi.itemname,di.itemname) when '粤菜' then foodCount else 0 end ) yue  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '会议餐' then foodCount else 0 end ) meet,sum(case ifnull(doi.itemname,di.itemname) when '刀削面' then foodCount else 0 end ) dao  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname) when '油泼面' then foodCount else 0 end ) you  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '杂酱面' then foodCount else 0 end ) za,sum(case ifnull(doi.itemname,di.itemname)  when '河粉' then foodCount else 0 end ) he  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '水果餐' then foodCount else 0 end ) te,sum(case ifnull(doi.itemname,di.itemname)  when '鲜奶' then foodCount else 0 end ) xian  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '卡士' then foodCount else 0 end ) ka,sum(case ifnull(doi.itemname,di.itemname)  when '酸奶' then foodCount else 0 end ) suan  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '咸鸭蛋' then foodCount else 0 end ) egg,sum(case ifnull(doi.itemname,di.itemname)  when '纯馒头' then foodCount else 0 end ) workBuns  ");
		sql.append("   ,sum(case ifnull(doi.itemname,di.itemname)  when '纯花卷' then foodCount else 0 end ) workRoll  ");
		sql.append("   from tb_order_dinner_info od    ");
		sql.append("   left join  tb_order_dinner o on o.id=od.orderId  ");
		sql.append("   left join tb_user  c on c.id=o.consId   ");
		sql.append("   left join tb_meal m on m.id=od.mealId  ");
		sql.append("   left join tb_department dept on dept.id=c.departmentId    ");
		sql.append("   left join tb_dictionary_item di on di.id=m.mealCodeId   ");
		sql.append("   left join tb_dictionary_org_item doi on doi.id=m.mealCodeId   ");
		sql.append("   where o.orgId=" + orgId + "  and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d')   ");
		sql.append("   and o.status='1'   ");
		sql.append("   and c.id not in (select u.id from tb_user u   ");
		sql.append("   where u.organizationId=" + orgId + " and u.isLeader=1)  ");
		sql.append("   group by c.departmentId) b on b.deptId=d.id  and  b.address=adr.id     ");
		sql.append("   left join (select c.address,c.departmentId deptId,sum(case ifnull(doi.itemname,di.itemname) when '花卷' then foodCount else 0 end ) rolls,sum(case ifnull(doi.itemname,di.itemname) when '馒头' then foodCount else 0 end ) buns  ");
		sql.append("   from tb_order_dinner_info od    ");
		sql.append("   left join  tb_order_dinner o on o.id=od.orderId  ");
		sql.append("   left join tb_user  c on c.id=o.consId   ");
		sql.append("   left join tb_dictionary_item di on di.id=od.stapleFood  ");
		sql.append("   left join tb_dictionary_org_item doi on doi.id=od.stapleFood  ");
		sql.append("   where o.orgId=" + orgId + "   and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d')   ");
		sql.append("   and o.status='1'   ");
		sql.append("   and c.id not in (select u.id from tb_user u   ");
		sql.append("   where u.organizationId=" + orgId + " and u.isLeader=1)  ");
		sql.append("   group by c.departmentId) k on k.deptId=d.id  and  k.address=adr.id    where   adr.id=" + addressId + " and u.isEnabled='1' and  u.organizationId=" + orgId + " ");
		sql.append("   and (ifnull(b.chuan,0)+ifnull(b.yue,0)+ifnull(b.su,0)+ifnull(b.meet,0)+ifnull(b.dao,0)+ifnull(b.you,0)+ifnull(b.za,0)+ifnull(b.he,0)+ifnull(b.te,0)");
		sql.append("+ifnull(b.xian,0)+ifnull(b.ka,0)+ifnull(b.suan,0)+ifnull(b.egg,0)+ifnull(b.workBuns,0)+ifnull(b.workRoll,0))>0 ");
		sql.append("   group by u.address,d.name  ORDER BY d.orderNum ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 获取餐品的排行榜
	 */
	@Override
	public List<Map<String, Object>> getMealRanking(String orgId) {
		StringBuilder sql = new StringBuilder();
		sql.append("  select ifnull(doi.itemname,di.itemname) mealName,sum(od.foodCount) sumCount from tb_order_dinner o    ");
		sql.append("  left join tb_order_dinner_info od on o.id=od.orderId     ");
		sql.append("  left join tb_meal m on m.id=od.mealId      ");
		sql.append("  LEFT JOIN tb_dictionary_item di on di.id=m.mealCodeId  ");
		sql.append("  LEFT JOIN tb_dictionary_org_item doi on doi.id=m.mealCodeId  ");
		sql.append("  where o.orgId='" + orgId + "'  and o.isEnabled='1' and od.isEnabled='1'     ");
		sql.append("  and (o.status='2' or o.status='3')     ");
		sql.append("  and date_format(o.dinnerTime, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')   ");
		sql.append("  GROUP BY ifnull(doi.itemname,di.itemname)  ");
		sql.append("  ORDER BY sum(od.foodCount) desc  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getUserByOrgAddress(String orgId,
														 String addressId, String addressOrgId) {
		StringBuilder sql = new StringBuilder("  select ifnull(ador.itemname,adr.itemname) address,u.id,u.userName deptname,1 type  ");
		sql.append(" from tb_user u   ");
		sql.append(" left join tb_dictionary_item adr on adr.id=u.address   ");
		sql.append("  left join tb_dictionary_org_item ador on ador.id=u.address  ");
		sql.append("  where u.organizationId=" + orgId + " and u.isLeader=1 and u.isEnabled=1  and (adr.id= '" + addressId + "' or ador.id= '" + addressOrgId + "' )    ");
		sql.append("  order by u.orderNum    ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public int getCountByUserAndMeal(String userId, String itemId, String itemOrgId) {
		StringBuilder sql = new StringBuilder(" select SUM(od.foodCount) sumCount  from tb_order_dinner d  ");
		sql.append("  left join tb_order_dinner_info od on d.id=od.orderId   ");
		sql.append("  left join tb_meal m on m.id=od.mealId   ");
		sql.append("  LEFT JOIN tb_dictionary_item di on di.id=m.mealCodeId  ");
		sql.append("  LEFT JOIN tb_dictionary_org_item doi on doi.id=m.mealCodeId  ");
		sql.append("  where  d.status=1 and d.consId=" + userId + " and (di.id='" + itemId + "' or doi.id='" + itemOrgId + "'  )  ");
		sql.append("  and date_format(d.dinnerTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d')   ");
		sql.append("  GROUP BY d.consId   ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	@Override
	public int getCountByUserAndMealFood(String userId, String foodId, String foodOrgId,String date) {
		StringBuilder sql = new StringBuilder(" select SUM(od.foodCount) sumCount  from tb_order_dinner d  ");
		sql.append("  left join tb_order_dinner_info od on d.id=od.orderId   ");
		sql.append("  left join tb_meal m on m.id=od.mealId   ");
		sql.append("  where  d.status=1 and d.consId=" + userId + "  ");
		sql.append("  and (od.stapleFood='" + foodId + "' or (od.stapleFood='" + foodOrgId + "' and m.spec>1  )  )   ");
		sql.append("  and date_format(d.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')   ");
		sql.append("  GROUP BY d.consId   ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	@Override
	public List<Map<String, Object>> getDeptByOrgAddress(String orgId,
														 String addressId, String addressOrgId,String date) {
		StringBuilder sql = new StringBuilder(" SELECT DISTINCT ifnull(doi.itemname, di.itemname) AS address,d.id,  ");
		sql.append("  d. NAME AS deptname,2 type  ");
		sql.append(" FROM tb_department d,   ");
		sql.append("  ( SELECT DISTINCT us.departmentId FROM tb_order_dinner o   ");
		sql.append("    LEFT JOIN tb_order_dinner_info od ON o.id = od.orderId AND od.isEnabled = '1'  ");
		sql.append("    AND date_format(od.dinnerTime, '%Y-%m-%d') = date_format('"+date+"', '%Y-%m-%d')  ");
		sql.append("    LEFT JOIN tb_user us ON us.id = od.consId  ");
		sql.append("    WHERE o.`status` = '1' AND od.orgId = "+orgId+"  ");
		sql.append("    AND ( us.address = '"+addressId+"' OR us.address = '"+addressOrgId+"' )  ");
		sql.append("  ) b,tb_user u  ");
		sql.append(" LEFT JOIN tb_dictionary_item di ON di.id = u.address   ");
		sql.append(" LEFT JOIN tb_dictionary_org_item doi ON doi.id = u.address   ");
		sql.append(" WHERE b.departmentId = d.id AND u.departmentId = d.id and u.organizationId="+orgId+"   ");
		sql.append(" AND ( u.address = '"+addressId+"' OR u.address = '"+addressOrgId+"')   ");
		sql.append(" ORDER BY d.orderNum   ");
		log.info("getDeptByOrgAddress-sql:{}",sql);
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}

	@Override
	public int getCountByDeptAndMeal(String deptId, String itemId, String itemOrgId,String date) {
		StringBuilder sql = new StringBuilder(" select SUM(od.foodCount) sumCount  from tb_order_dinner d  ");
		sql.append("  left join tb_order_dinner_info od on d.id=od.orderId   ");
		sql.append("  left join tb_user u on u.id=d.consId  ");
		sql.append("  left join tb_department dept on dept.id=u.departmentId   ");
		sql.append("  left join tb_meal m on m.id=od.mealId   ");
		sql.append("  where d.status=1  and u.departmentId=" + deptId + " and (m.mealCodeId='" + itemId + "' or m.mealCodeId='" + itemOrgId + "'  )  ");
		sql.append("  and date_format(d.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')   ");
		sql.append("  and u.id not in (select us.id from tb_user us where us.organizationId=u.organizationId and us.isLeader=1)  ");
		sql.append("  GROUP BY u.departmentId,m.mealCodeId   ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	@Override
	public int getCountByDeptAndMealFood(String addressId, String addressOrgId, String deptId, String foodId, String foodOrgId,String date) {
		StringBuilder sql = new StringBuilder(" select SUM(od.foodCount) sumCount  from tb_order_dinner d  ");
		sql.append("  left join tb_order_dinner_info od on d.id=od.orderId   ");
		sql.append("  left join tb_user u on u.id=d.consId  ");
		sql.append("  left join tb_department dept on dept.id=u.departmentId   ");
		sql.append("  left join tb_meal m on m.id=od.mealId   ");
		sql.append("  left join tb_dictionary_item dl on dl.id=u.address ");
		sql.append("  left join tb_dictionary_org_item dol on dol.id=u.address  ");
		sql.append("  where d.status=1  and u.departmentId=" + deptId + "  ");
		sql.append("  and (dl.id='" + addressId + "' or dol.id='" + addressOrgId + "')  ");
		sql.append("  and (od.stapleFood='" + foodId + "' or (od.stapleFood='" + foodOrgId + "' and m.spec>1  )  )   ");
		sql.append("  and date_format(d.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')   ");
		sql.append("  and u.id not in (select us.id from tb_user us where us.organizationId=u.organizationId and us.isLeader=1)  ");
		sql.append("  GROUP BY u.departmentId   ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		int count = 0;
		if (list != null && list.size() > 0) {
			if (list.get(0) == null) {
				return count;
			}
			count = Integer.parseInt(list.get(0).get("sumCount") == null ? "0" : list.get(0).get("sumCount").toString());
		}
		return count;
	}

	@Override
	public List<Map<String, Object>> getCountByUserOrDept(String orgId,
														  String userId, String addressId, String addressOrgId, String deptId,String date) {
		StringBuilder sql = new StringBuilder();
		sql.append("  select m.mealCodeId,ifnull(di.itemname,doi.itemname) mealName,sum(od.foodCount) sumCount from tb_order_dinner_info od  ");
		sql.append("  left join tb_order_dinner o on o.id=od.orderId  ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append("  left join tb_dictionary_item di on di.id=m.mealCodeId ");
		sql.append("  left join tb_dictionary_org_item doi on doi.id=m.mealCodeId  ");
		sql.append("  left join tb_user u on u.id=od.consId  ");
		sql.append("  left join tb_dictionary_item dl on dl.id=u.address ");
		sql.append("  left join tb_dictionary_org_item dol on dol.id=u.address  ");
		sql.append("  left join tb_department d on d.id=u.departmentId  ");
		sql.append("  where o.isEnabled=1 and date_format(o.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')   ");
		sql.append("  and o.status=1  and u.organizationId=" + orgId + "  ");
		if (StringUtil.isNotEmpty(userId)) {
			sql.append("  and u.id=" + userId + "  ");
		}
		if (StringUtil.isNotEmpty(deptId)) {
			sql.append("  and (dl.id='" + addressId + "' or dol.id='" + addressOrgId + "')  ");
			sql.append("  and d.id=" + deptId + " and u.id not in (select us.id from tb_user us where us.organizationId=u.organizationId and u.isLeader=1)   ");
		}
		sql.append("  group by m.mealCodeId  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getCountByAddress(String orgId,
													   String addressId, String addressOrgId,String date) {
		StringBuilder sql = new StringBuilder();
		sql.append("  select ifnull(doi.itemname,di.itemname) mealName,sum(od.foodCount) sumCount from tb_order_dinner_info od   ");
		sql.append("  left join tb_order_dinner o on o.id=od.orderId  ");
		sql.append("  left join tb_meal m on m.id=od.mealId  ");
		sql.append(" left join tb_dictionary_item di on di.id=m.mealCodeId ");
		sql.append("  left join tb_dictionary_org_item doi on doi.id=m.mealCodeId  ");
		sql.append("  left join tb_user u on u.id=od.consId  ");
		sql.append("  where o.isEnabled=1 and date_format(o.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')  ");
		sql.append("  and o.status=1  and o.orgId=" + orgId + " and (u.address='" + addressId + "' or u.address='" + addressOrgId + "' )  ");
		sql.append("  group by ifnull(doi.itemname,di.itemname)  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 获取订餐统计总数、会议餐、普通餐、临时工作餐
	 *
	 * @param dateType 1年2季度3月4周\日
	 * @param date
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getDinnerDataByInfo(String orgId, int dateType, String date, String endDate) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select od.orgId orgId,org.name orgName,ifnull(sum(od.foodCount),0) count  ");
		sql.append("  ,ifnull(sum(b.sumCount),0) meetCount,ifnull(sum(e.sumCount),0) tempCount  ");
		sql.append("  ,ifnull(sum(f.sumCount),0) ordinaryCount from tb_order_dinner o  ");
		sql.append("  left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append("  left join tb_organization org on org.id=od.orgId  ");
		sql.append("  left join (select c.id,c.orgId,c.foodCount sumCount   ");
		sql.append(" from tb_order_dinner_info c    ");
		sql.append("  where c.isMeetFood=1   ");
		sql.append("  group by c.id,c.orgId) b   ");
		sql.append(" on b.id=od.id    ");
		sql.append("   left join (select c.id,d.orgId,c.foodCount sumCount  ");
		sql.append("   from tb_order_dinner d  ");
		sql.append("   left join tb_order_dinner_info c on d.id=c.orderId ");
		sql.append("   where d.isTempOrder=1 ");
		sql.append("   group by c.id,d.orgId) e  ");
		sql.append("   on e.id=od.id ");
		sql.append("   left join (select c.id,d.orgId,c.foodCount sumCount  ");
		sql.append("   from tb_order_dinner d  ");
		sql.append("   left join tb_order_dinner_info c on d.id=c.orderId ");
		sql.append("   where (d.isTempOrder=0 and c.isMeetFood=0) ");
		sql.append("   group by c.id,d.orgId) f ");
		sql.append("   on f.id=od.id ");
		sql.append("   where org.isEnabled=1 and (o.status=1 or o.status=2 or o.status=3) ");
		if (StringUtil.isNotEmpty(orgId)) {
			sql.append(" and od.orgId='" + orgId + "'  ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)='" + date + "'  and QUARTER(od.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(od.createTime)='"+date+"'  and month(od.createTime)='"+endDate+"'   ");
			sql.append("  and date_format(od.createTime,'%Y-%m')='" + date + "'    ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(od.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(od.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append(" group by od.orgId ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}

	/**
	 * 获取访客统计 (通行次数、员工通行次数、访客通行次数、二维码通行次数、实体卡通行次数)
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getVisitorDataByInfo(String orgId,
														  int dateType, String date, String endDate) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select ld.orgId orgId,org.name orgName,count(ll.id) count,count(b.id) userCount   ");
		sql.append("  ,count(c.id) visitorCount,count(d.id) QRcodeCount,count(e.id) entityCardCount  ");
		sql.append(" from tb_lingling_log ll   ");
		sql.append("  left join tb_lingling_device ld on ll.deviceId=ld.deviceId  ");
		sql.append("  left join tb_organization org on org.id=ld.orgId  ");
		sql.append("  left join (select llo.id from tb_lingling_log llo where llo.type=1 or llo.type=3) b on b.id=ll.id  ");
		sql.append("  left join (select llo.id from tb_lingling_log llo where llo.type=2) c on c.id=ll.id  ");
		sql.append("  left join (select llo.id from tb_lingling_log llo where llo.type=1 or llo.type=2) d on d.id=ll.id  ");
		sql.append(" left join (select llo.id from tb_lingling_log llo where llo.type=3) e on e.id=ll.id   ");
		sql.append("  where 1=1 and ll.type in (1,2,3)  ");
		sql.append(" and  ld.isEnable=1 ");
		if (StringUtil.isNotEmpty(orgId)) {
			sql.append(" and ld.orgId='" + orgId + "'  ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(ll.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(ll.createTime)='" + date + "'  and QUARTER(ll.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(ll.createTime)='"+date+"'  and month(ll.createTime)='"+endDate+"'   ");
			sql.append("  and date_format(ll.createTime,'%Y-%m')='" + date + "'    ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(ll.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(ll.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append(" group by ld.orgId ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}

	@Override
	public List<Map<String, Object>> getPlatformDataByInfo(String orgId,
														   int dateType, String date, String endDate) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select uac.serviceCode serviceCode,s.serviceName serviceName,sum(uac.count) serviceCount,uac.orgId  orgId,(case when uac.serviceCode='total' then 'total' else s.serviceCode end)  code  ");
		sql.append("  from tb_user_active_count uac  ");
		sql.append("  left join tb_servicetypeinfo s on s.code=uac.serviceCode  ");
		sql.append("  where (s.serviceCode in ('DCFW','HYS','ZHFK','DZFXT','KTJS','QYRZ','QYQC','GGZX','GCWX') or uac.serviceCode='total')  ");
		if (StringUtil.isNotEmpty(orgId)) {
			sql.append(" and uac.orgId='" + orgId + "'  ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(uac.date)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(uac.date)='" + date + "'  and QUARTER(uac.date)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(uac.date)='"+date+"'  and month(uac.date)='"+endDate+"'   ");
			sql.append("  and date_format(uac.date,'%Y-%m')='" + date + "'    ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(uac.date,'%Y-%m-%d')>='" + date + "'  and date_format(uac.date,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("  group by uac.orgId,uac.serviceCode  ");
		sql.append("  order by uac.orgId  ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}

	@Override
	public List<Map<String, Object>> getOrgUser(String orgId, int dateType, String date, String endDate) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select * from ( ");
		sql.append("   select distinct u.organizationId orgId,org.name orgName,org.accountType,ifnull(count(u.id),0) userSumCount   ");
		sql.append("   ,ifnull(ues.count,0) userCount,ifnull(dinner.count,0) dinnerCount   ");
		sql.append("   ,ifnull(ling.count,0) lingCount,ifnull(goods.count,0) goodsCount   ");
		sql.append("   ,ifnull(airAddTime.count,0) airAddTimeCount,ifnull(enter.count,0) enterCount   ");
		sql.append("   ,ifnull(emig.count,0) emigCount  ,ifnull(user.count,0) enUserCount  ");
		sql.append("   ,ifnull(virt.count,0) virtSumCount,ifnull(newVirt.count,0) virtCount,ifnull(rep.count,0) repairUseCount    ");
		sql.append("   from tb_user u    ");
		sql.append("   RIGHT join tb_organization org on org.id=u.organizationId   ");
		//企业数
		sql.append("   left join (select vir.organizationId orgId,orgs.name,COUNT(vir.id) count from tb_virtualarchitecture vir      ");
		sql.append("   inner join tb_organization orgs on orgs.id=vir.organizationId  and orgs.isEnabled=1   ");
		sql.append("   where vir.type='1' and vir.isEnabled='1' and orgs.isEnabled='1' group by vir.organizationId) virt on virt.orgId=org.id   ");
		//企业新增数
		sql.append("   left join ( select v.organizationId id,ifnull(count(v.id),0) count from tb_virtualarchitecture v    ");
		sql.append("   left join tb_organization o on o.id=v.organizationId     ");
		sql.append("   where 1=1   and v.type=1 and v.isEnabled=1 and o.isEnabled=1 ");

		if (dateType == 1) {//年度
			sql.append("  and year(v.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(v.createTime)='" + date + "'  and QUARTER(v.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
			sql.append("  and date_format(v.createTime,'%Y-%m')='" + date + "'   ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(v.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(v.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("  group by v.organizationId ) newVirt on newVirt.id=org.id  ");
		//有效用户数
		sql.append("   left join (select ue.organizationId orgId,count(ue.id) count from tb_user ue    ");
		sql.append("     where 1=1     and ue.isEnabled=1      group by ue.organizationId) user on user.orgId=org.id    ");
		//新增用户数
		sql.append("   left join (select ue.organizationId orgId,count(ue.id) count from tb_user ue   ");
		sql.append("   where 1=1   ");
		if (dateType == 1) {//年度
			sql.append("  and year(ue.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(ue.createTime)='" + date + "'  and QUARTER(ue.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(ue.createTime)='"+date+"'  and month(ue.createTime)='"+endDate+"'   ");
			sql.append("  and date_format(ue.createTime,'%Y-%m')='" + date + "'   ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(ue.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(ue.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by ue.organizationId) ues on ues.orgId=org.id   ");
		//订餐
		sql.append("   left join (select od.orgId,COUNT(od.id) count from tb_order_dinner od   ");
		sql.append("   where 1=1   ");
		if (dateType == 1) {//年度
			sql.append("  and year(od.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(od.createTime)='" + date + "'  and QUARTER(od.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(od.createTime)='"+date+"'  and month(od.createTime)='"+endDate+"'   ");
			sql.append("  and date_format(od.createTime,'%Y-%m')='" + date + "'   ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(od.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(od.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by od.orgId ) dinner on dinner.orgId=org.id   ");
		//令令开门
		sql.append("   left join (select ld.orgId,COUNT(ll.id) count from tb_lingling_log ll   ");
		sql.append("   left join tb_lingling_device ld on ll.deviceId=ld.deviceId   ");
		sql.append("   where ld.isEnable=1 and (ll.type=1 or ll.type=2 or ll.type=3)   ");
		if (dateType == 1) {//年度
			sql.append("  and year(ll.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(ll.createTime)='" + date + "'  and QUARTER(ll.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//				sql.append("  and year(ll.createTime)='"+date+"'  and month(ll.createTime)='"+endDate+"'   ");
			sql.append("  and date_format(ll.createTime,'%Y-%m')='" + date + "'   ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(ll.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(ll.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by ld.orgId) ling on ling.orgId=org.id   ");
		//物品放行
		sql.append("   left join (select grl.orgId,count(grl.id) count from tb_goods_release grl   ");
		sql.append("   where 1=1   ");
		if (dateType == 1) {//年度
			sql.append("  and year(grl.create_time)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(grl.create_time)='" + date + "'  and QUARTER(grl.create_time)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(grl.create_time)='"+date+"'  and month(grl.create_time)='"+endDate+"'   ");
			sql.append("  and date_format(grl.create_time,'%Y-%m')='" + date + "'  ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(grl.create_time,'%Y-%m-%d')>='" + date + "'  and date_format(grl.create_time,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by grl.orgId) goods on goods.orgId=org.id   ");
		//空调加时
		sql.append("   left join (select aa.organizationId orgId,count(aa.id) count from tb_airconditioner_addtime aa   ");
		sql.append("   where 1=1   ");
		if (dateType == 1) {//年度
			sql.append("  and year(aa.create_time)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(aa.create_time)='" + date + "'  and QUARTER(aa.create_time)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(aa.create_time)='"+date+"'  and month(aa.create_time)='"+endDate+"'   ");
			sql.append("  and date_format(aa.create_time,'%Y-%m')='" + date + "'  ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(aa.create_time,'%Y-%m-%d')>='" + date + "'  and date_format(aa.create_time,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by aa.organizationId) airAddTime on airAddTime.orgId=org.id   ");
		//企业入驻，企业迁出
		sql.append("   left join (select orgs.id orgId,count(ent.id) count from tb_enterprise ent   ");
		sql.append("   left join tb_build build on build.id=ent.orgId   ");
		sql.append("   left join tb_organization orgs on orgs.id=build.organizationId  ");
		sql.append("   where ent.state=1   ");
		if (dateType == 1) {//年度
			sql.append("  and year(ent.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(ent.createTime)='" + date + "'  and QUARTER(ent.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(ent.createTime)='"+date+"'  and month(ent.createTime)='"+endDate+"'   ");
			sql.append(" and  date_format(ent.createTime,'%Y-%m')='" + date + "'  ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(ent.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(ent.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by orgs.id) enter on enter.orgId=org.ID   ");
		//迁出
		sql.append("   left join (select orgs.id orgId,count(ent.id) count from tb_enterprise ent   ");
		sql.append("   left join tb_build build on build.id=ent.orgId   ");
		sql.append("   left join tb_organization orgs on orgs.id=build.organizationId  ");
		sql.append("   where ent.state=2   ");
		if (dateType == 1) {//年度
			sql.append("  and year(ent.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(ent.createTime)='" + date + "'  and QUARTER(ent.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(ent.createTime)='"+date+"'  and month(ent.createTime)='"+endDate+"'   ");
			sql.append(" and  date_format(ent.createTime,'%Y-%m')='" + date + "'  ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(ent.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(ent.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("   group by orgs.id) emig on emig.orgId=org.id   ");

		//工程维修
		sql.append(" left join ( select r.orgId,count(r.id) count from tb_repair_bills r where 1=1 ");
		if (dateType == 1) {//年度
			sql.append("  and year(r.create_time)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(r.create_time)='" + date + "'  and QUARTER(r.create_time)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(r.create_time)='"+date+"'  and month(r.create_time)='"+endDate+"'   ");
			sql.append(" and  date_format(r.create_time,'%Y-%m')='" + date + "'  ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(r.create_time,'%Y-%m-%d')>='" + date + "'  and date_format(r.create_time,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("  group by r.orgId ) rep on rep.orgId=org.id ");


		sql.append("   where  org.isEnabled=1  ");
		if (StringUtil.isNotEmpty(orgId)) {
			sql.append(" and u.organizationId='" + orgId + "'  ");
		}
		sql.append("   group by u.organizationId   ");
		sql.append("   order by org.createTime desc   ");

		sql.append(" ) platform where  1=1 and (platform.userSumCount>0 or platform.virtSumCount>0) ");
		LOG.info("getOrgUser------sql:{}", sql.toString());
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}

	@Override
	public List<Map<String, Object>> getUserDataByInfo(String orgId,
													   int dateType, String date, String endDate) {
		StringBuilder sql = new StringBuilder("");
		sql.append("   select u.organizationId,org.name orgName,count(u.id) userCount from tb_user u   ");
		sql.append("  left join tb_organization org on org.id=u.organizationId   ");
		sql.append("   where 1=1  ");
		if (StringUtil.isNotEmpty(orgId)) {
			sql.append(" and u.organizationId='" + orgId + "'  ");
		}
		if (dateType == 1) {//年度
			sql.append("  and year(u.createTime)='" + date + "'   ");
		} else if (dateType == 2) {//季度
			sql.append("  and year(u.createTime)='" + date + "'  and QUARTER(u.createTime)='" + endDate + "'  ");
		} else if (dateType == 3) {//月
//					sql.append("  and year(u.createTime)='"+date+"'  and month(u.createTime)='"+endDate+"'   ");
			sql.append(" and  date_format(u.createTime,'%Y-%m')='" + date + "'  ");
		} else if (dateType == 4) {//周、日
			sql.append("  and date_format(u.createTime,'%Y-%m-%d')>='" + date + "'  and date_format(u.createTime,'%Y-%m-%d')<='" + endDate + "'   ");
		}
		sql.append("  group by u.organizationId   ");
		sql.append("  order by u.organizationId   ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}
}
