package com.unis.base.server.domain;

import com.unis.base.common.vo.SinkpointRecordsQueryVO;
import com.unis.base.common.vo.SinkpointRecordsVO;
import com.unis.base.server.dao.SinkpointRecordsDao;
import com.unis.base.server.entity.SinkpointRecordsDO;
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.Date;
import java.util.List;

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

	@Autowired
	private SinkpointRecordsDao sinkpointRecordsDao;

	/**
	 * 根据参数获取库点记录列表
	 * @param sinkpointRecordsQueryVO
	 * @return
	 */
	public List<SinkpointRecordsVO> getSinkpointRecordsList(SinkpointRecordsQueryVO sinkpointRecordsQueryVO) {
		return sinkpointRecordsDao.getSinkpointRecordsList(sinkpointRecordsQueryVO);
	}

	/**
	 * 获取库点记录详情
	 * @param kdbabh
	 * @return
	 */
	public SinkpointRecordsVO getSinkpointRecordsInfo(String kdbabh) {
		SinkpointRecordsDO sinkpointRecordsDO = sinkpointRecordsDao.getSinkpointRecordsInfo(kdbabh);
		if (sinkpointRecordsDO == null){
			return  null;
		}
		SinkpointRecordsVO sinkpointRecordsVO = new SinkpointRecordsVO();
		LsBeanUtils.copyAllProperties(sinkpointRecordsDO, sinkpointRecordsVO);
		return sinkpointRecordsVO;
	}

	/**
	 * 新增库点记录
	 * @param sinkpointRecordsVO
	 * @return
	 */
	public String insert(SinkpointRecordsVO sinkpointRecordsVO) {
		//进行信息copy
		SinkpointRecordsDO sinkpointRecordsDO = new SinkpointRecordsDO();
		//库点备案编号（yyyymmdd+0001）
		String prefix = DateUtil.getDate()+"";
		String kdbabh = getMaxCodeById(sinkpointRecordsDao.getMaxById(prefix), prefix, 4);
		sinkpointRecordsVO.setKdbabh(kdbabh);
		sinkpointRecordsVO.setStatus(1);
		sinkpointRecordsVO.setCreateDate(new Date());
		LsBeanUtils.copyAllProperties(sinkpointRecordsVO, sinkpointRecordsDO);
        int x = sinkpointRecordsDao.insert(sinkpointRecordsDO);
        if (x == 0){
            return null;
        }
		return kdbabh;
	}

	/**
	 * 更新库点记录
	 * @param sinkpointRecordsVO
	 * @return
	 */
	public int update(SinkpointRecordsVO sinkpointRecordsVO) {
		//进行信息copy
		SinkpointRecordsDO sinkpointRecordsDO = new SinkpointRecordsDO();
		LsBeanUtils.copyAllProperties(sinkpointRecordsVO, sinkpointRecordsDO);
		return sinkpointRecordsDao.updateByPrimaryKey(sinkpointRecordsDO);
	}
}
