package com.unis.base.server.domain;

import com.unis.base.common.vo.WarehouseDoorVO;
import com.unis.base.server.dao.WarehouseDoorDao;
import com.unis.base.server.entity.WarehouseDoorDO;
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.Date;
import java.util.List;

/**
 * @author WangZL
 * @version 1.0
 * @Title WarehouseDoorDomain
 * @ProjectName 持久化
 * @Description
 * @date 2019/07/22 17点26分
 */
@Component
public class WarehouseDoorDomain extends BaseDomain {

	@Autowired
	private WarehouseDoorDao warehouseDoorDao;

	/**
	 * 新增仓门信息列表
	 * @param warehouseDoorVOList
	 * @param cfbh
	 * @return
	 */
	public void insertWarehouseDoorList(List<WarehouseDoorVO> warehouseDoorVOList, String cfbh) {
		if (warehouseDoorVOList != null && warehouseDoorVOList.size() > 0) {
			for (WarehouseDoorVO warehouseDoorVO : warehouseDoorVOList) {
				this.insertWarehouseDoor(warehouseDoorVO,cfbh);
			}
		}
	}

	/**
	 * 新增仓门信息
	 * @param warehouseDoorVO
	 * @param cfbh
	 * @return
	 */
	public void insertWarehouseDoor(WarehouseDoorVO warehouseDoorVO,String cfbh) {
		WarehouseDoorDO warehouseDoorDO = new WarehouseDoorDO();
		//仓门编号（仓房编号+001 三位序号）
		String cmbh = getMaxCodeById(warehouseDoorDao.getMaxCodeById(cfbh),cfbh,3);
		warehouseDoorVO.setCmbh(cmbh);
		warehouseDoorVO.setCfbh(cfbh);
		warehouseDoorVO.setStatus(1);
		warehouseDoorVO.setCreateDate(new Date());
		LsBeanUtils.copyAllProperties(warehouseDoorVO, warehouseDoorDO);
		warehouseDoorDao.insert(warehouseDoorDO);
	}

	/**
	 * 更新仓门信息
	 * @param warehouseDoorVOList
	 * @param cfbh
	 */
	public void updateWarehouseDoorList(List<WarehouseDoorVO> warehouseDoorVOList,String cfbh) {
		if (warehouseDoorVOList != null) {
			for (WarehouseDoorVO warehouseDoorVO : warehouseDoorVOList) {
				WarehouseDoorDO warehouseDoorDO = warehouseDoorDao.getWarehouseDoorInfo(warehouseDoorVO.getCmbh());
				if (warehouseDoorDO == null) {
					if (warehouseDoorVO.getCmmc() != null) {
						this.insertWarehouseDoor(warehouseDoorVO, cfbh);
					}
				} else {
					LsBeanUtils.copyAllProperties(warehouseDoorVO, warehouseDoorDO);
					warehouseDoorDao.update(warehouseDoorDO);
				}
			}
		}
	}

	/**
	 * 查询仓门列表
	 * @param cfbh
	 * @return
	 */
	public List<WarehouseDoorVO> getWarehouseDoorListByCfbh(String cfbh) {
		List<WarehouseDoorDO> warehouseDoorDOList = warehouseDoorDao.getWarehouseDoorListByCfbh(cfbh);
		List<WarehouseDoorVO> warehouseDoorVOList = new ArrayList<>();
		if (warehouseDoorDOList != null) {
			for (WarehouseDoorDO warehouseDoorDO : warehouseDoorDOList) {
				WarehouseDoorVO warehouseDoorVO = new WarehouseDoorVO();
				LsBeanUtils.copyAllProperties(warehouseDoorDO, warehouseDoorVO);
				warehouseDoorVOList.add(warehouseDoorVO);
			}
		}
		return warehouseDoorVOList;
	}


	/**
	 * 根据仓门ID获取仓门信息
	 * @param cmbh
	 * @return
	 */
	public WarehouseDoorVO getWarehouseDoorInfoById(String cmbh) {
		WarehouseDoorDO warehouseDoorDO = warehouseDoorDao.getWarehouseDoorInfo(cmbh);
		if(warehouseDoorDO == null) {
			return null;
		}
		WarehouseDoorVO warehouseDoorVO = new WarehouseDoorVO();
		LsBeanUtils.copyAllProperties(warehouseDoorDO, warehouseDoorVO);
		return warehouseDoorVO;
	}

	/**
	 * 更新仓门信息
	 * @param warehouseDoorVO
	 * @return
	 */
	public int update(WarehouseDoorVO warehouseDoorVO) {
		WarehouseDoorDO warehouseDoorDO = warehouseDoorDao.getWarehouseDoorInfo(warehouseDoorVO.getCmbh());
		if (warehouseDoorDO == null) {
			return 0;
		}
		//进行传递参数copy
		LsBeanUtils.copyAllProperties(warehouseDoorVO, warehouseDoorDO);
		return warehouseDoorDao.update(warehouseDoorDO);
	}

	/**
	 * 根据仓房代码删除仓门信息列表
	 * @param cfbh
	 * @return
	 */
	public int deleteList(String cfbh) {
		return warehouseDoorDao.deleteList(cfbh);
	}
}
