package com.palmnest.dao.dinner.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.dao.dinner.OrderDinnerSetDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dinner.OrderDinner;
import com.palmnest.model.dinner.OrderDinnerSet;
import com.palmnest.model.dinner.RechargeSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

@Slf4j
@Repository("orderDinnerSetDao")
public class OrderDinnerSetDaoImpl extends GenericDaoHibernate<OrderDinnerSet, Integer> implements OrderDinnerSetDao {

    public OrderDinnerSetDaoImpl() {
        super(OrderDinnerSet.class);
    }

    @Override
    public OrderDinnerSet findByOrgId(Integer orgId) {
        String sql = "from OrderDinnerSet where orgId=? ";
        List<OrderDinnerSet> list = getSession().createQuery(sql).setInteger(0, orgId).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public OrderDinnerSet findByUserId(Integer userId) {
        String sql = "from OrderDinnerSet where userId=? ";
        List<OrderDinnerSet> list = getSession().createQuery(sql).setInteger(0, userId).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public String getOrgListByOrgIds(String orgIds) {
        return null;
    }

    @Override
    public List<Map<String, Object>> findUserByOrgIds(String orgIds,String openId) {
        StringBuilder sql = new StringBuilder("");
        sql.append(" SELECT t.orgId, t.id userId FROM (   ");
        sql.append(" SELECT u.organizationId orgId, u.id, ifnull( u.isPushState, 1 ) state ");
        sql.append("  FROM tb_user u");
        sql.append(" WHERE u.organizationId IN ( "+orgIds+" ) AND u.openId <> '' AND u.isEnabled = '1'  ");
        if(StringUtils.isNotEmpty(openId)){
            sql.append(" AND (u.openid in ("+openId+") or u.bind_openid in ("+openId+") )  ");
        }
        sql.append("   ) t WHERE t.state = 1 ");
        sql.append("  ORDER BY t.orgId, t.id ");
        return getJdbcTemplate().queryForList(sql.toString());
    }
}
