package com.palmnest.dao.hibernate;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.dao.ElementGroupDao;
import com.palmnest.dao.OrgElementDao;
import com.palmnest.model.Element;
import com.palmnest.model.ElementGroup;
import com.palmnest.model.OrgElement;
import com.palmnest.model.dinner.Department;
import com.palmnest.model.estateserve.AppUser;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

/**
 * 服务之窗1L- ElemenetOrgItemDaoHibernate
 *
 * @Description TODO
 * @author: jiangyong
 * @since: 2020/8/12 15:28
 */
@Repository("orgElementDao")
public class OrgElementDaoHibernate extends GenericDaoHibernate<OrgElement, Long> implements OrgElementDao {


    public OrgElementDaoHibernate() {
        super(OrgElement.class);
    }


    @Override
    public Page<OrgElement,String> getElementListByAllOrgAndGroupAsPage(int pageNo, int pageSize, Map<String, String> map) {
        StringBuilder sql = new StringBuilder("  SELECT doi.* FROM tb_dictionary_org_item doi  ");
        sql.append("  left join tb_dictionary_group dg on dg.id=doi.groupid   ");
        sql.append("WHERE 1=1");
        if(StringUtils.isNotEmpty(map.get("groupCode"))){
            sql.append("  AND  dg.groupcode='" + map.get("groupCode") + "'   ");
        }
        if(StringUtils.isNotEmpty(map.get("orgId"))){
            sql.append("  AND  doi.orgid=" + Integer.parseInt(map.get("orgId")) + "   ");
        }
        sql.append("  ORDER BY doi.orderno  ");

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


	@Override
	public OrgElement findByCodeAndOrgId(String code, Integer orgId) {
		StringBuilder sql=new StringBuilder("from OrgElement where code=? and orgid=?");
		List<OrgElement> list = getSession().createQuery(sql.toString())
				.setString(0, code)
				.setInteger(1, orgId).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public OrgElement findByCodeAndOrgIdAndId(String code, Integer orgId,Long id) {
		StringBuilder sql=new StringBuilder("from OrgElement where code=? and orgid=? ");
		if(null!=id){
			sql.append(" and id<>"+id+" ");
		}
		List<OrgElement> list = getSession().createQuery(sql.toString())
				.setString(0, code)
				.setInteger(1, orgId).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Element findByCodeAndId(String code, Integer orgId,Long id) {
		StringBuilder sql=new StringBuilder("SELECT d.* FROM tb_dictionary_item d, tb_dictionary_org_item o ");
		sql.append("  WHERE d.groupid = o.groupid AND d.itemcode = o.`code` ");
		sql.append("  AND d.isenabled = 1  AND d.itemcode ='"+code+"'  ");
		sql.append("  AND o.orgid ="+orgId);
		if(null!=id){
			sql.append(" and id<>"+id+" ");
		}
		List<Element> list = getSession().createSQLQuery(sql.toString()).addEntity(Element.class).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
}
