package com.palmnest.dao.dinner.impl;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.palmnest.dao.dinner.OrderDinnerInfoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dinner.OrderDinnerInfo;

@Repository("orderDinnerInfoDao")
public class OrderDinnerInfoDaoImpl extends GenericDaoHibernate<OrderDinnerInfo, Long> implements OrderDinnerInfoDao {
	private static Logger LOG = LoggerFactory.getLogger(OrderDinnerInfoDaoImpl.class);

	public OrderDinnerInfoDaoImpl() {
		super(OrderDinnerInfo.class);
	}

	/**
	 * 根据订单id获取订单的详情
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public List<OrderDinnerInfo> findByOrderId(Long orderId) {
		String sql = "from OrderDinnerInfo where orderId=? and isEnabled='1' ";
		return getSession().createQuery(sql).setLong(0, orderId).list();
	}

	/**
	 * 获取当天的订单详情列表
	 */
	@Override
	public List<Map<String, Object>> findBySysdate(String orgId) {
		StringBuilder sql = new StringBuilder("");
//			sql.append(" select od.id,od.mealName,od.mealId,od.stapleFood,od.stapleFoodStr,od.foodCount  from  tb_order_dinner o left join  tb_order_dinner_info od on o.id=od.orderId where 1=1 ");
//			sql.append("  and o.orgId ='"+orgId+"'  and  o.isEnabled='1'  and date_format(o.createTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d') ");
//			sql.append("  and  o.status ='1' ");
		sql.append(" select (case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN di.itemname else CONCAT(di.itemname,':',od.stapleFoodStr) end ) mName  ");
		sql.append(" ,di.itemname mealName,od.mealId,od.stapleFood,od.stapleFoodStr,sum(od.foodCount) foodCount  from  tb_order_dinner o 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("  where 1=1 ");
		sql.append(" and 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' and od.isEnabled='1' ");
		sql.append(" group by (case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN di.itemname else CONCAT(di.itemname,':',od.stapleFoodStr) end ) ");
//			String str="from OrderDinnerInfo where orgId =? and  isEnabled='1' and date_format(createTime,'%Y-%m-%d')=date_format(SYSDATE(),'%Y-%m-%d') ";
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 取消订单时将所有订单详情设置成无效
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public int updateByIsEnabled(Long orderId) {
		String sql = "update OrderDinnerInfo set isEnabled='0' where orderId=? ";
		return getSession().createQuery(sql).setLong(0, orderId).executeUpdate();
	}

	/**
	 * 获取导出excel的第一个sheet中的数据
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getOrderExcelListByMeal(String orgId,String date) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select sum(od.foodCount) other,ifnull((case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN doi.itemname else CONCAT(doi.itemname,':',od.stapleFoodStr) end ) ");
		sql.append("   , (case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN di.itemname else CONCAT(di.itemname,':',od.stapleFoodStr) end )) mName  ");
		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(" 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 in (" + orgId + ")  and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d') ");
		sql.append(" and o.status='1' ");
		sql.append(" group by ifnull((case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN doi.itemname else CONCAT(doi.itemname,':',od.stapleFoodStr) end ) ,(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN di.itemname else CONCAT(di.itemname,':',od.stapleFoodStr) end )) ");
		sql.append(" order by  (case when m.spec>1 then 2 else 1 end) desc  ");
		LOG.info("getOrderExcelListByMeal sql:" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 根据部门汇总
	 */
	@Override
	public List<Map<String, Object>> getOrderExcelListByDept(String orgId,String date) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select dl.name other,ifnull((case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN  CONCAT(doi.itemname,'*',sum(foodCount)) else CONCAT(CONCAT(doi.itemname,':',od.stapleFoodStr),'*',SUM(foodCount)) end ) ");
		sql.append(" ,(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN  CONCAT(di.itemname,'*',sum(foodCount)) else CONCAT(CONCAT(di.itemname,':',od.stapleFoodStr),'*',SUM(foodCount)) end )) mName   ");
		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 dl on dl.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  in (" + orgId + ")   and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')  ");
		sql.append(" and o.status='1' ");
		sql.append(" group by dl.name,ifnull((case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN doi.itemname else CONCAT(doi.itemname,':',od.stapleFoodStr) end )  ");
		sql.append(" ,(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN di.itemname else CONCAT(di.itemname,':',od.stapleFoodStr) end ) ) ");
		sql.append(" order by dl.orderNum,dl.name ");
		LOG.info("getOrderExcelListByDept sql:" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 根据用户名汇总
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getOrderExcelListByUserName(
			String orgId,String date) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select dl.name deptname,c.userName other,ifnull((case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN  CONCAT(doi.itemname,'*',sum(foodCount)) else CONCAT(CONCAT(doi.itemname,':',od.stapleFoodStr),'*',SUM(foodCount)) end )   ");
		sql.append("  ,(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN  CONCAT(di.itemname,'*',sum(foodCount)) else CONCAT(CONCAT(di.itemname,':',od.stapleFoodStr),'*',SUM(foodCount)) end ))  mName  ");
		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(" left join tb_department dl on dl.id=c.departmentId ");
		sql.append(" where o.orgId  in (" + orgId + ")   and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')  ");
		sql.append(" and o.status='1' ");
		sql.append(" group by c.userName,ifnull((case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN doi.itemname else CONCAT(doi.itemname,':',od.stapleFoodStr) end ) ");
		sql.append(" ,(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN di.itemname else CONCAT(di.itemname,':',od.stapleFoodStr) end )) ");
		sql.append(" order by dl.orderNum,dl.name,c.orderNum ");
		LOG.info("getOrderExcelListByUserName sql:" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 根据地址汇总
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getOrderExcelListByAddress(
			String orgId,String date) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select ifnull(dol.itemname,dl.itemname) other,(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN  CONCAT(ifnull(doi.itemname,di.itemname),'*',sum(foodCount)) else CONCAT(CONCAT(ifnull(doi.itemname,di.itemname),':',od.stapleFoodStr),'*',SUM(foodCount)) end ) mName  ");
		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 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_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  in (" + orgId + ")   and o.isEnabled='1' and date_format(o.dinnerTime,'%Y-%m-%d')=date_format('"+date+"','%Y-%m-%d')  ");
		sql.append(" and o.status='1' ");
		sql.append(" group by ifnull(dol.itemname,dl.itemname),(case when od.stapleFoodStr='' or od.stapleFoodStr is NULL THEN ifnull(doi.itemname,di.itemname) else CONCAT(ifnull(doi.itemname,di.itemname),':',od.stapleFoodStr) end ) ");
		sql.append(" order by ifnull(dol.orderno,dl.orderno)  ");
		LOG.info("getOrderExcelListByUserName sql:" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 根据下单人以及送餐时间获取该用户下大订餐数据
	 *
	 * @param consId
	 * @param time
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getOrderInfoByTime(String consId,
														String time) {
		StringBuilder sql = new StringBuilder("");//"select  from tb_order_dinner where consId=? and date_format(dinnerTime,'%Y-%m-%d')=? and isEnabled='1' ";
		sql.append(" select od.id    ");
		sql.append(" from  tb_order_dinner o   ");
		sql.append(" left join tb_order_dinner_info od on od.orderId=o.id   ");
		sql.append("  where o.isEnabled='1' and o.isTempOrder=0 and od.isEnabled='1' and (o.status='1' or o.status='2' or o.status='3') ");
		sql.append(" and od.consId=" + consId + " and date_format(od.dinnerTime,'%Y-%m-%d')='" + time + "'   ");
		sql.append(" and (od.isMeetFood=0 or od.isMeetFood is null )  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 汇总会议餐
	 *
	 * @param orgId
	 * @param time  年月
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getMeetFood(String orgId, String time) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select DATE_FORMAT(od.dinnerTime,'%Y-%m-%d') dinnerTime,sum(od.foodCount) foodCount from tb_order_dinner o  ");
		sql.append(" left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append(" where o.isEnabled='1' and od.isEnabled='1'   ");
		sql.append(" and o.orgId='" + orgId + "'   ");
		sql.append(" and o.status in ('2','3')  ");
		sql.append(" and od.isMeetFood=1  ");
		sql.append(" and DATE_FORMAT(od.dinnerTime,'%Y-%m')='" + time + "'  ");
		sql.append(" GROUP BY DATE_FORMAT(od.dinnerTime,'%Y-%m-%d')  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getTempOrder(String orgId, String time) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select DATE_FORMAT(od.dinnerTime,'%Y-%m-%d') dinnerTime,sum(od.foodCount) foodCount from tb_order_dinner o  ");
		sql.append(" left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append(" where o.isEnabled='1' and od.isEnabled='1'   ");
		sql.append(" and o.orgId='" + orgId + "'   ");
		sql.append(" and o.status in ('2','3')  ");
		sql.append(" and o.isTempOrder=1  ");
		sql.append(" and DATE_FORMAT(od.dinnerTime,'%Y-%m')='" + time + "'  ");
		sql.append(" GROUP BY DATE_FORMAT(od.dinnerTime,'%Y-%m-%d')  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getOrderInfoByParentOrderId(
			String orderId) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select od.id,od.isMeetFood,od.mealId,od.orderId,ifnull(doi.itemname,di.itemname) mealName,od.stapleFood,od.stapleFoodStr,od.foodCount,od.consId userId,u.userName userName,u.userMobile mobile,o.status,DATE_FORMAT(od.dinnerTime,'%Y-%m-%d') dinnerTime  ");
		sql.append(" from tb_order_dinner o left join tb_order_dinner_info od on od.orderId=o.id  ");
		sql.append(" left join tb_user u on u.id=od.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.parentOrderId='" + orderId + "'  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public int getOrderCount(String consId, String time) {
		StringBuilder sql = new StringBuilder("select ifnull(sum(od.foodCount),0) count from tb_order_dinner o");
		sql.append("  left join tb_order_dinner_info od on o.id=od.orderId  ");
		sql.append("  where (od.creator='" + consId + "' or od.consId='" + consId + "')  ");
		sql.append("  and DATE_FORMAT(od.dinnerTime,'%Y-%m-%d')='" + time + "'  ");
		sql.append("  and (o.`status`=1 or o.`status`=2 or o.`status`=3)  ");
		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("count") == null ? "0" : list.get(0).get("count").toString());
		}
		return count;
	}

	@Override
	public List<Map<String, Object>> getSysdateByOrgId(String orgId) {
		StringBuilder sql = new StringBuilder("");
		sql.append("  select 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 left join (select od.consId consId,sum(case di.itemname when '素餐' then foodCount else 0 end ) su,sum(case di.itemname when '川菜' then foodCount else 0 end ) chuan,sum(case di.itemname when '粤菜' then foodCount else 0 end ) yue ");
		sql.append("  ,sum(case di.itemname  when '会议餐' then foodCount else 0 end ) meet,sum(case di.itemname when '刀削面' then foodCount else 0 end ) dao ");
		sql.append("  ,sum(case di.itemname when '油泼面' then foodCount else 0 end ) you ");
		sql.append("  ,sum(case di.itemname  when '杂酱面' then foodCount else 0 end ) za,sum(case di.itemname  when '河粉' then foodCount else 0 end ) he ");
		sql.append("  ,sum(case di.itemname  when '特餐' then foodCount else 0 end ) te,sum(case di.itemname  when '鲜奶' then foodCount else 0 end ) xian ");
		sql.append("  ,sum(case di.itemname  when '卡士' then foodCount else 0 end ) ka,sum(case di.itemname  when '酸奶' then foodCount else 0 end ) suan ");
		sql.append("  ,sum(case di.itemname  when '咸鸭蛋' then foodCount else 0 end ) egg,sum(case di.itemname  when '纯馒头' then foodCount else 0 end ) workBuns ");
		sql.append("  ,sum(case 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_item dl on dl.id=c.departmentId ");
		sql.append("  left join tb_dictionary_group dg on dg.id=di.groupid ");
		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 ");
		sql.append("  left join (select od.consId consId,sum(case di.itemcode when 'roll' then foodCount else 0 end ) rolls,sum(case di.itemcode when 'buns' 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("  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 ");
		sql.append("  where u.organizationId=" + orgId + " and u.isLeader=1 and u.isEnabled=1 ");
		sql.append("  UNION ");
		sql.append("  select distinct dz.id,dz.itemname 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,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_dictionary_group dgr ");
		sql.append("  inner join tb_dictionary_item dz on dz.groupid=dgr.id ");
		sql.append("  left join (select c.departmentId deptId,sum(case di.itemname when '素餐' then foodCount else 0 end ) su,sum(case di.itemname when '川菜' then foodCount else 0 end ) chuan,sum(case di.itemname when '粤菜' then foodCount else 0 end ) yue ");
		sql.append("  ,sum(case di.itemname  when '会议餐' then foodCount else 0 end ) meet,sum(case di.itemname when '刀削面' then foodCount else 0 end ) dao ");
		sql.append("  ,sum(case di.itemname when '油泼面' then foodCount else 0 end ) you ");
		sql.append("  ,sum(case di.itemname  when '杂酱面' then foodCount else 0 end ) za,sum(case di.itemname  when '河粉' then foodCount else 0 end ) he ");
		sql.append("  ,sum(case di.itemname  when '特餐' then foodCount else 0 end ) te,sum(case di.itemname  when '鲜奶' then foodCount else 0 end ) xian ");
		sql.append("  ,sum(case di.itemname  when '卡士' then foodCount else 0 end ) ka,sum(case di.itemname  when '酸奶' then foodCount else 0 end ) suan ");
		sql.append("  ,sum(case di.itemname  when '咸鸭蛋' then foodCount else 0 end ) egg,sum(case di.itemname  when '纯馒头' then foodCount else 0 end ) workBuns ");
		sql.append("  ,sum(case 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_item dl on dl.id=c.departmentId ");
		sql.append("  left join tb_dictionary_group dg on dg.id=di.groupid ");
		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=dz.id  ");
		sql.append("  left join (select c.departmentId deptId,sum(case di.itemcode when 'roll' then foodCount else 0 end ) rolls,sum(case di.itemcode when 'buns' 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("  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=dz.id  ");
		sql.append("  where dgr.groupcode='dinnerDept'  ");
		sql.append("  UNION ");
		sql.append("  select 1,'合计',sum(sumAll.chuan),sum(sumAll.yue),sum(sumAll.su),sum(sumAll.meet),sum(sumAll.dao),sum(sumAll.you),sum(sumAll.za) ");
		sql.append("  ,sum(sumAll.he),sum(sumAll.te),sum(sumAll.xian),sum(sumAll.ka),sum(sumAll.suan),sum(sumAll.egg),sum(sumAll.workBuns),sum(sumAll.workRoll) ");
		sql.append("  ,sum(sumAll.rolls),sum(sumAll.buns),sum(sumAll.heji)  ");
		sql.append("  from (select u.id,u.userName name,ifnull(b.su,0) su,ifnull(b.chuan,0) chuan,ifnull(b.yue,0) yue,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 left join (select od.consId consId,sum(case di.itemname when '素餐' then foodCount else 0 end ) su,sum(case di.itemname when '川菜' then foodCount else 0 end ) chuan,sum(case di.itemname when '粤菜' then foodCount else 0 end ) yue ");
		sql.append("  ,sum(case di.itemname  when '会议餐' then foodCount else 0 end ) meet,sum(case di.itemname when '刀削面' then foodCount else 0 end ) dao ");
		sql.append("  ,sum(case di.itemname when '油泼面' then foodCount else 0 end ) you ");
		sql.append("  ,sum(case di.itemname  when '杂酱面' then foodCount else 0 end ) za,sum(case di.itemname  when '河粉' then foodCount else 0 end ) he ");
		sql.append("  ,sum(case di.itemname  when '特餐' then foodCount else 0 end ) te,sum(case di.itemname  when '鲜奶' then foodCount else 0 end ) xian ");
		sql.append("  ,sum(case di.itemname  when '卡士' then foodCount else 0 end ) ka,sum(case di.itemname  when '酸奶' then foodCount else 0 end ) suan ");
		sql.append("  ,sum(case di.itemname  when '咸鸭蛋' then foodCount else 0 end ) egg,sum(case di.itemname  when '纯馒头' then foodCount else 0 end ) workBuns ");
		sql.append("  ,sum(case 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_item dl on dl.id=c.departmentId ");
		sql.append("  left join tb_dictionary_group dg on dg.id=di.groupid ");
		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 ");
		sql.append("  left join (select od.consId consId,sum(case di.itemcode when 'roll' then foodCount else 0 end ) rolls,sum(case di.itemcode when 'buns' 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("  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 ");
		sql.append("  where u.organizationId=" + orgId + " and u.isLeader=1 and u.isEnabled=1 ");
		sql.append("  UNION ");
		sql.append("  select distinct dz.id,dz.itemname name,ifnull(b.su,0) su,ifnull(b.chuan,0) chuan,ifnull(b.yue,0) yue,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_dictionary_group dgr ");
		sql.append("  inner join tb_dictionary_item dz on dz.groupid=dgr.id ");
		sql.append("  left join (select c.departmentId deptId,sum(case di.itemname when '素餐' then foodCount else 0 end ) su,sum(case di.itemname when '川菜' then foodCount else 0 end ) chuan,sum(case di.itemname when '粤菜' then foodCount else 0 end ) yue ");
		sql.append("  ,sum(case di.itemname  when '会议餐' then foodCount else 0 end ) meet,sum(case di.itemname when '刀削面' then foodCount else 0 end ) dao ");
		sql.append("  ,sum(case di.itemname when '油泼面' then foodCount else 0 end ) you ");
		sql.append("  ,sum(case di.itemname  when '杂酱面' then foodCount else 0 end ) za,sum(case di.itemname  when '河粉' then foodCount else 0 end ) he ");
		sql.append("  ,sum(case di.itemname  when '特餐' then foodCount else 0 end ) te,sum(case di.itemname  when '鲜奶' then foodCount else 0 end ) xian ");
		sql.append("  ,sum(case di.itemname  when '卡士' then foodCount else 0 end ) ka,sum(case di.itemname  when '酸奶' then foodCount else 0 end ) suan ");
		sql.append("  ,sum(case di.itemname  when '咸鸭蛋' then foodCount else 0 end ) egg,sum(case di.itemname  when '纯馒头' then foodCount else 0 end ) workBuns ");
		sql.append("  ,sum(case 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_item dl on dl.id=c.departmentId ");
		sql.append("  left join tb_dictionary_group dg on dg.id=di.groupid ");
		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=dz.id  ");
		sql.append("  left join (select c.departmentId deptId,sum(case di.itemcode when 'roll' then foodCount else 0 end ) rolls,sum(case di.itemcode when 'buns' 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("  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=dz.id  ");
		sql.append("  where dgr.groupcode='dinnerDept' )  sumAll ");
		LOG.info("getSysdateByOrgId sql:" + sql.toString());
		return getJdbcTemplate().queryForList(sql.toString());
	}
}
