package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemOrganization;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.DataGridResult;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

/**
 * Created by hzp on 2016/1/11.
 */
@Repository("OrganizationDao")
public class OrganizationDao extends SQLGeneralDAO  {

    public static final String BEAN_ID = "OrganizationDao";

    /**
     * 获取组织列表
     * @return
     */
    public DataGridResult getOrgList(Map<String, Object> conditionMap, Integer page, Integer pageSize) throws Exception
    {
        StringBuilder sb = new StringBuilder();

        sb.append("from SystemOrganization t where 1=1 ");
        if (!StringUtils.isEmpty(conditionMap.get("pid"))) //父级ID
        {
            sb.append(" and t.pid = '" + conditionMap.get("pid") + "'");
        }
        Object name = conditionMap.get("name");
        if (!StringUtils.isEmpty(name)) {
            sb.append(" and (t.code like '%" + name + "%' or t.fullName like '%" + name + "%' or t.shortName like '%" + name + "%' or t.pyCode like '%" + name + "%')");
        }
        if (!StringUtils.isEmpty(conditionMap.get("activityFlag"))) //是否有效
        {
            sb.append(" and t.activityFlag = '" + conditionMap.get("activityFlag") + "'");
        }

        return getDataGridResult(sb.toString(),page,pageSize);
    }

    /**
     * 获取获取组织信息ByCodeAndName
     * @return
     */
    public List getOrgByCodeOrName(String code, String name) throws Exception
    {
        String sql = "select * from system_organization where code = '"+code+"'or full_name = '"+name+"'";
        List<SystemOrganization> list = super.queryListBySql(sql,SystemOrganization.class);

        return list;
    }


    /**
     * 修改组织状态
     */
    public void activityOrg(String orgId,String activityFlag) throws Exception
    {
        String sql = "update System_Organization set activity_Flag='"+activityFlag+"' where Id='"+orgId+"'";
        super.execute(sql);
    }

    public SystemOrganization getOrgByIde(String orgId) throws Exception
    {
        String sql = "select * from system_organization where id = '"+orgId+"'and activity_flag = '1'";
        List<SystemOrganization> list = super.queryListBySql(sql, SystemOrganization.class);
        if (list!=null && list.size()>0){
            return list.get(0);
        }else {
            return null;
        }

    }

}
