package com.zhongdao.jlr.business.dao.impl;

import com.dubh.common.dao.hibernate.BaseDaoHibernate;
import com.util.Vo;
import com.zhongdao.jlr.business.dao.EnterpriseDao;
import com.zhongdao.jlr.business.vo.PageInfo;
import com.zhongdao.jlr.pojo.BaseUser;
import com.zhongdao.jlr.pojo.EnterpriseJlr;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/***
 * 经销商维护
 */

@Repository(value = "enterpriseDaoImpl")
public class EnterpriseDaoImpl extends BaseDaoHibernate implements EnterpriseDao {

    @Override
    public Vo searchEnterprise(String name,String abbrCode, Integer page, Integer rows) {
        Vo vo=new Vo();
        //声明一个List集合用于接收查询的数据
        List list=null;
        //声明一个分页的实体类分页数据放入实体类中
        PageInfo pageInfo=new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setRows(rows);
        //声明一个字符串对象并将所需要的sql语句拼接到字符串中
        StringBuffer hqlSb=new StringBuffer("select name,abbr_code,address,contact,id from jlr_enterprise where 1=1");
        if(!TextUtils.isEmpty(name)){
            hqlSb.append(" and name like '%"+name+"%'");
        }
        if(!TextUtils.isEmpty(abbrCode)){
            hqlSb.append(" and abbr_code like '%"+abbrCode+"%'");
        }
        //声明一个字符串用于查询经销商数据总数
        String sql=hqlSb.toString();
        String hql=hqlSb.append(" limit "+pageInfo.getCurrentIndex()+","+pageInfo.getRows()).toString();
        //声明一个List集合用于存放Enterprise对象
        List<EnterpriseJlr> lists=new ArrayList<>();
        try {
            list=getRsBySql(hql);
            if(list != null && list.size()>0){
                //遍历查询到数据并将其放入Enterprise对象中
                for (int i=0;i<list.size();i++){
                    Object[] objects= (Object[]) list.get(i);
                    EnterpriseJlr enterpriseJlr=new EnterpriseJlr();
                    enterpriseJlr.setName(objects[0] == null ? "" : objects[0].toString());
                    enterpriseJlr.setAbbr_code(objects[1] == null ? "" : objects[1].toString());
                    enterpriseJlr.setAddress(objects[2] == null ? "" : objects[2].toString());
                    enterpriseJlr.setContact(objects[3] == null ? "" : objects[3].toString());
                    enterpriseJlr.setId(Integer.valueOf(objects[4].toString()));
                    //将Enterprise对象放入集合中
                    lists.add(enterpriseJlr);
                }
            }
            vo.setRows(lists);
            //获取查询经销商信息的总数
            int size=getRsCountsBySelectSql(sql);
            vo.setTotal(size);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return vo;
    }

    @Override
    public boolean deleteById(String id) {
        try {
            deleteHql("delete EnterpriseJlr where id="+id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean putEnterprise(EnterpriseJlr enterprise) {
        try {
            saveOrUpdate(enterprise);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean putSystemUser(BaseUser systemUser) {
        try {
            updateSql("update jlr_user set password = '"+systemUser.getPassword() +"' where id="+systemUser.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public EnterpriseJlr getById(String id) {
        EnterpriseJlr enterprisejlr=null;
        try {
            enterprisejlr=(EnterpriseJlr) getObjectHql("from EnterpriseJlr where id ="+id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return enterprisejlr;
    }
}
