package com.unis.base.server.domain;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.unis.base.common.vo.GranaryVO;
import com.unis.base.server.dao.DAjxxsjyDao;
import com.unis.base.server.dao.DCfxxsjyDao;
import com.unis.base.server.entity.DAjxxsjyDO;
import com.unis.base.server.entity.DCfxxsjyDO;
import com.unis.common.utils.LsBeanUtils;

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

	@Autowired
	private DAjxxsjyDao dAjxxsjyDao;

	@Autowired
	private DCfxxsjyDao dCfxxsjyDao;

	/**
	 * 获取廒间列表
	 * @param granaryVO
	 * @return
	 */
	public List<GranaryVO> getGranaryList(GranaryVO granaryVO) {
		//进行传递参数copy
		DAjxxsjyDO dCfxxsjyDO = new DAjxxsjyDO();
		LsBeanUtils.copyAllProperties(granaryVO, dCfxxsjyDO);
		return dAjxxsjyDao.getDAjxxsjyList(dCfxxsjyDO);
	}

	/**
	 * 添加廒间信息
	 * @param granaryVO
	 * @return
	 */
	public int insert(GranaryVO granaryVO) {

		//根据仓房编号获取仓房信息
		DCfxxsjyDO dCfxxsjyDO = dCfxxsjyDao.getDCfxxxsjyInfo(granaryVO.getCfbh());
		if(dCfxxsjyDO==null) {
			return -1;
		}
		//进行传递参数copy
		DAjxxsjyDO dAjxxsjyDO = new DAjxxsjyDO();
		//根据统一社会信用代码生成库点id（库点id规则：统一社会信用代码+001）
		String ajbh = getMaxCodeById(dAjxxsjyDao.getMaxCodeById(),granaryVO.getCfbh(),3);
		granaryVO.setAjbh(ajbh);
		LsBeanUtils.copyAllProperties(granaryVO,dAjxxsjyDO);
		return dAjxxsjyDao.insert(dAjxxsjyDO);
	}

	/**
	 * 根据廒间编号获取廒间信息
	 * @param ajbh
	 * @return
	 */
	public GranaryVO getGranaryByajbh(String ajbh) {
		GranaryVO granaryVO = new GranaryVO();
		DAjxxsjyDO ajxxsjyDO = dAjxxsjyDao.getGranaryByajbh(ajbh);
		LsBeanUtils.copyAllProperties(ajxxsjyDO,granaryVO);
		return granaryVO;
	}

	/**
	 * 更新廒间信息
	 * @param granaryVO
	 * @return
	 */
	public int update(GranaryVO granaryVO) {
		//根据廒间编号获取廒间信息
		DAjxxsjyDO dAjxxsjyDO = dAjxxsjyDao.getGranaryByajbh(granaryVO.getAjbh());
		if(dAjxxsjyDO == null) {//廒间不存在
			return 0;
		}
		LsBeanUtils.copyAllProperties(granaryVO,dAjxxsjyDO);
		return dAjxxsjyDao.update(dAjxxsjyDO);
	}

}
