package cn.com.usky.iot.company.dao;

import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.com.usky.iot.entity.YtiotTCompany;
import cn.com.usky.iot.entity.YtiotTCompanyVer;
import cn.com.usky.iot.order.dao.YtiotTOrderDao;
import cn.com.usky.utils.DaoQuery;
import cn.com.usky.utils.DaoResult;

public class YtiotTCompanyDaoImpl extends HibernateDaoSupport implements YtiotTCompanyDao {

	private DaoQuery daoQuery;
	private YtiotTCompanyDao ytiotTCompanyDao;
	private YtiotTCompanyVerDao ytiotTCompanyVerDao;
	private YtiotTOrderDao ytiotTOrderDao;
	public DaoQuery getDaoQuery() {
		return daoQuery;
	}

	public void setDaoQuery(DaoQuery daoQuery) {
		this.daoQuery = daoQuery;
	}

	public YtiotTCompanyDao getYtiotTCompanyDao() {
		return ytiotTCompanyDao;
	}

	public void setYtiotTCompanyDao(YtiotTCompanyDao ytiotTCompanyDao) {
		this.ytiotTCompanyDao = ytiotTCompanyDao;
	}

	public YtiotTCompanyVerDao getYtiotTCompanyVerDao() {
		return ytiotTCompanyVerDao;
	}

	public void setYtiotTCompanyVerDao(YtiotTCompanyVerDao ytiotTCompanyVerDao) {
		this.ytiotTCompanyVerDao = ytiotTCompanyVerDao;
	}

	public YtiotTOrderDao getYtiotTOrderDao() {
		return ytiotTOrderDao;
	}

	public void setYtiotTOrderDao(YtiotTOrderDao ytiotTOrderDao) {
		this.ytiotTOrderDao = ytiotTOrderDao;
	}

	@Override
	public DaoResult add(YtiotTCompany ytiotTCompany,String comments) {
		// TODO Auto-generated method stub
		if(ytiotTCompany.getVCompanyName()==null)
			return new DaoResult("单位名称不能为空",false);
		if(ytiotTCompany.getUqAdminId()==null)
			return new DaoResult("录入人不能为空",false);
		long idx = ytiotTOrderDao.getMaxOrderIdx("ytiot_t_company");
		ytiotTCompany.setIOrderIdx(idx);
		ytiotTCompany.setIVersion(1);
		ytiotTCompany.setUqParentCompanyId("13654");
//		ytiotTCompany.setIStoretime(new Date());
		this.getHibernateTemplate().save(ytiotTCompany);
		ytiotTCompany.setUqParentCompanyId(ytiotTCompany.getUqCompanyId());
		this.getHibernateTemplate().update(ytiotTCompany);
		YtiotTCompanyVer ver = new YtiotTCompanyVer();
		
		//ver.setICompanyverId(ytiotTCompany.getUqCompanyId());//单位/部门记录版本ID
		
		ver.setUqCompanyId(ytiotTCompany.getUqCompanyId());//单位/部门ID		
		ver.setIOrderIdx(ytiotTCompany.getIOrderIdx());//默认序号
		ver.setUqParentId(ytiotTCompany. getUqParentId());//上级部门ID，当本字段值为“ffffffffffffffffffffffffffffffff”时，该记录为入网单位，其余都是入网单位的下属部门
		ver.setUqParentCompanyId(ytiotTCompany.getUqParentCompanyId());//所属单位的ID，当本记录为入网单位时，本字段与主键字段值相同
		ver.setVCompanyName(ytiotTCompany.getVCompanyName());//单位名称
		ver.setVCompanyAddress0(ytiotTCompany.getVCompanyAddress0());//单位地址_1
		ver.setVCompanyAddress1(ytiotTCompany.getVCompanyAddress1());//单位地址_2
		ver.setVCompanyAddress2(ytiotTCompany.getVCompanyAddress2());//单位地址_3
		ver.setEStatus(ytiotTCompany.getEStatus());//当前状态，0为待开通服务，1为已开通服务，2为服务锁定，3为已注销，4、5留用
		ver.setVComment("新增说明："+comments);//注册/修改说明		
		ver.setUqAdminId(ytiotTCompany.getUqAdminId());//信息录入人ID
		ver.setIVersion(ytiotTCompany.getIVersion());//当前记录版本		
		ver.setDStoretime(ytiotTCompany.getDStoretime());//记录存储时间
		ytiotTCompanyVerDao.add(ver);
		return new DaoResult(ytiotTCompany.getUqCompanyId(),true);
	}

	@Override
	public DaoResult update(YtiotTCompany ytiotTCompany,String comments) {
		// TODO Auto-generated method stub
		if(ytiotTCompany.getVCompanyName()==null)
			return new DaoResult("单位名称不能为空",false);
		if(ytiotTCompany.getUqAdminId()==null)
			return new DaoResult("录入人不能为空",false);
		//long idx = ytiotTOrderDao.getMaxOrderIdx("ytiot_t_company");
		ytiotTCompany.setIVersion(ytiotTCompany.getIVersion()+1);
		ytiotTCompany.setDStoretime(new Date());
		this.getHibernateTemplate().update(ytiotTCompany);
		YtiotTCompanyVer ver = new YtiotTCompanyVer();
		ver.setUqCompanyId(ytiotTCompany.getUqCompanyId());
		ver.setUqParentId(ytiotTCompany. getUqParentId());//上级部门ID，当本字段值为“ffffffffffffffffffffffffffffffff”时，该记录为入网单位，其余都是入网单位的下属部门
		ver.setUqParentCompanyId(ytiotTCompany.getUqParentCompanyId());//所属单位的ID，当本记录为入网单位时，本字段与主键字段值相同
		ver.setVCompanyName(ytiotTCompany.getVCompanyName());//单位名称
		ver.setVCompanyAddress0(ytiotTCompany.getVCompanyAddress0());//单位地址_1
		ver.setVCompanyAddress1(ytiotTCompany.getVCompanyAddress1());//单位地址_2
		ver.setVCompanyAddress2(ytiotTCompany.getVCompanyAddress2());//单位地址_3
		ver.setVComment("修改说明："+comments);//注册/修改说明
		ver.setEStatus(ytiotTCompany.getEStatus());//当前状态，0为待开通服务，1为已开通服务，2为服务锁定，3为已注销，4、5留用
		ver.setUqAdminId(ytiotTCompany.getUqAdminId());//信息录入人ID
		ver.setIVersion(ytiotTCompany.getIVersion());//当前记录版本
		ver.setDStoretime(ytiotTCompany.getDStoretime());//记录存储时间
		ytiotTCompanyVerDao.add(ver);
		return new DaoResult(ytiotTCompany.getUqCompanyId(),true);
	}

	@Override 
	public DaoResult delect(YtiotTCompany ytiotTCompany,String comments) {
		// TODO Auto-generated method stub
		//long idx = ytiotTOrderDao.getMaxOrderIdx("ytiot_t_company");
		ytiotTCompany.setIVersion(ytiotTCompany.getIVersion()+1);
		ytiotTCompany.setDStoretime(new Date());
		this.getHibernateTemplate().update(ytiotTCompany);
		YtiotTCompanyVer ver = new YtiotTCompanyVer();
		ver.setUqCompanyId(ytiotTCompany.getUqCompanyId());
		ver.setUqParentId(ytiotTCompany. getUqParentId());//上级部门ID，当本字段值为“ffffffffffffffffffffffffffffffff”时，该记录为入网单位，其余都是入网单位的下属部门
		ver.setUqParentCompanyId(ytiotTCompany.getUqParentCompanyId());//所属单位的ID，当本记录为入网单位时，本字段与主键字段值相同
		ver.setVCompanyName(ytiotTCompany.getVCompanyName());//单位名称
		ver.setVCompanyAddress0(ytiotTCompany.getVCompanyAddress0());//单位地址_1
		ver.setVCompanyAddress1(ytiotTCompany.getVCompanyAddress1());//单位地址_2
		ver.setVCompanyAddress2(ytiotTCompany.getVCompanyAddress2());//单位地址_3
		ver.setVComment("注销说明："+comments);//注册/修改说明
		ver.setEStatus(ytiotTCompany.getEStatus());//当前状态，0为待开通服务，1为已开通服务，2为服务锁定，3为已注销，4、5留用
		ver.setUqAdminId(ytiotTCompany.getUqAdminId());//信息录入人ID
		ver.setIVersion(ytiotTCompany.getIVersion());//当前记录版本
		ver.setDStoretime(ytiotTCompany.getDStoretime());//记录存储时间
		ytiotTCompanyVerDao.add(ver);
		return new DaoResult(ytiotTCompany.getUqCompanyId(),true);
	}

	@Override
	public int getCount(String sqlwhere) {
		// TODO Auto-generated method stub
		return daoQuery.getCount("YtiotTCompany", sqlwhere);
	}

	@Override
	public List query(String sqlwhere, String order, String dir, String start, String limit) {
		// TODO Auto-generated method stub
		return daoQuery.query("YtiotTCompany", sqlwhere, order, dir, start, limit);
	}

}
