package com.unis.base.server.domain;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.DSbxxsjyVO;
import com.unis.base.common.vo.EquipmentTypeVO;
import com.unis.base.common.vo.EquipmentTypequeryVO;
import com.unis.base.server.dao.DSbxxsjyDao;
import com.unis.base.server.dao.EquipmentTypeDao;
import com.unis.base.server.entity.DSbxxsjyDO;
import com.unis.base.server.entity.EquipmentTypeDO;
import com.unis.common.utils.DateUtil;
import com.unis.common.utils.LsBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 设备类型EquipmentTypeDomain
 * @author liuweilong
 *
 */
@Component
public class EquipmentTypeDomain extends BaseDomain{

	
	
	@Autowired
    private EquipmentTypeDao equipmentTypeDao;
	
	@Autowired
	private DSbxxsjyDao dSbxxsjyDao;

    
    
    
    
    /**
     * 增加EquipmentTypeDO
     * @return
     */
    public int insert(EquipmentTypeVO equipmentTypeVO){
		/*
		 * EquipmentTypequeryVO equipmentTypequeryVO = new EquipmentTypequeryVO();
		 * LsBeanUtils.copyAllProperties(equipmentTypeVO, equipmentTypequeryVO);
		 * List<EquipmentTypeVO> equipmentTypeList =
		 * equipmentTypeDao.getEquipmentTypeList(equipmentTypequeryVO); if
		 * (equipmentTypeList != null && equipmentTypeList.size() > 0) { return -2; }
		 */
    	EquipmentTypeDO equipmentTypeDO = new EquipmentTypeDO();
    	//设置主键
		String id = getMaxCodeById(equipmentTypeDao.getMaxCodeById("SBLX" + DateUtil.getDate()),
				"SBLX" + DateUtil.getDate(), 3);
		equipmentTypeVO.setSblxbh(id);
    	LsBeanUtils.copyAllProperties(equipmentTypeVO, equipmentTypeDO);
    	return equipmentTypeDao.insertSelective(equipmentTypeDO);
    	
    }
    
    /**
     * 删除EquipmentTypeDO
     * @param sblxbh
     * @return
     */
    public int delete(String sblxbh){
    	dSbxxsjyDao.deleteBySblxbh(sblxbh);
    	return equipmentTypeDao.deleteByPrimaryKey(sblxbh);
    }
    
    
    
    /**
     * 更新EquipmentTypeDO
     * @param sblxbh
     * @return
     */
    public int update(EquipmentTypeVO equipmentTypeVO){
    	
    	EquipmentTypeDO equipmentTypeDO = new EquipmentTypeDO();
		LsBeanUtils.copyAllProperties(equipmentTypeVO, equipmentTypeDO);
    	int updateByPrimaryKeySelective = equipmentTypeDao.updateByPrimaryKeySelective(equipmentTypeDO);
    
    	return updateByPrimaryKeySelective;
    }
    
    
    
    /**
     * 根据主键查询EquipmentTypeDO
     */
    public EquipmentTypeDO geEquipmentTypeDO(String sblxbh){
    	EquipmentTypeDO selectByPrimaryKey = equipmentTypeDao.selectByPrimaryKey(sblxbh);
    
    	return selectByPrimaryKey;
    }

    
    
    /**
     * 分页查询
     */
    public PageInfo<EquipmentTypeVO> getList(EquipmentTypequeryVO equipmentTypequeryVO){
    	
    	List<EquipmentTypeDO> list = equipmentTypeDao.selectAll();
    	PageInfo<EquipmentTypeDO> info = new PageInfo<EquipmentTypeDO>(list);

    	PageInfo<EquipmentTypeVO> pageInfo = new PageInfo<EquipmentTypeVO>();
    	LsBeanUtils.copyAllProperties(info, pageInfo);
    	
    	
		return pageInfo;
    	
    	
    }

	/**
	 * 查询列表
	 * @param equipmentTypequeryVO
	 * @return
	 */
	public List<EquipmentTypeVO> getEquipmentTypeList(EquipmentTypequeryVO equipmentTypequeryVO) {
		
		 List<EquipmentTypeVO> equipmentTypeList = equipmentTypeDao.getEquipmentTypeList(equipmentTypequeryVO);
		 
		 
		 
		 if(equipmentTypeList!=null) {
			 
			 equipmentTypeList = JSONObject.parseArray(JSONObject.toJSONString(equipmentTypeList), EquipmentTypeVO.class);
			 
			 for (EquipmentTypeVO equipmentTypeVO : equipmentTypeList) {
				
				 String sblxbh = equipmentTypeVO.getSblxbh();
				 
				 DSbxxsjyDO dSbxxsjyDO = new DSbxxsjyDO();
				 
				 dSbxxsjyDO.setSblxbh(sblxbh);
				 
				 List<DSbxxsjyVO> dSbxxsjyList = dSbxxsjyDao.getDSbxxsjyList(dSbxxsjyDO);
				 
				 if(dSbxxsjyList!=null) {
					 
					 equipmentTypeVO.setKcsl(dSbxxsjyList.size());
					 
				 }
			}
		 }
		return equipmentTypeList;
	}
}
