package com.unis.detectionnum.server.domain;

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

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

import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.common.vo.DKdxxsjyVO;
import com.unis.detectionnum.server.dao.DKdxxsjyDao;
import com.unis.detectionnum.server.entity.DKdxxsjyDO;

/**
 * @author huangxing
 * @version 1.0
 * @Title DKdxxsjyDomain
 * @ProjectName 持久化
 * @Description  TODO
 * @date 2019/07/22 17点26分
 */
@Component
public class DKdxxsjyDomain extends BaseDomain {
	
	@Autowired
	private DKdxxsjyDao dKdxxsjyDao;

	/**
	 * 根据参数获取库点信息
	 * @param dKdxxsjyVO
	 * @return
	 */
	public List<DKdxxsjyVO> getDKdxxsjyList(DKdxxsjyVO dKdxxsjyVO) {
		DKdxxsjyDO dKdxxsjyDO = new DKdxxsjyDO();
		LsBeanUtils.copyAllProperties(dKdxxsjyVO,dKdxxsjyDO);
		return dKdxxsjyDao.getDKdxxsjyLists(dKdxxsjyDO);
	}

	/**
	 * 根据库点编号获取库点信息
	 * @param kddm
	 * @return
	 */
	public DKdxxsjyVO getDKdxxsjyInfoBykddm(String kddm) {
		DKdxxsjyVO dKdxxsjyVO = new DKdxxsjyVO();
		DKdxxsjyDO dKdxxsjyDO = dKdxxsjyDao.getDKdxxsjyInfoBykddm(kddm);
		LsBeanUtils.copyAllProperties(dKdxxsjyDO, dKdxxsjyVO);
		return dKdxxsjyVO;
	}

	/**
	 * 新增库点信息
	 * @param dKdxxsjyVO
	 * @return
	 */
	public int insert(DKdxxsjyVO dKdxxsjyVO) {
		
		//根据统一社会信用代码获取企业信息
		/*TzSlqyDO slqyDO = tzSlqyDao.getCompanyBytyshxydm(dKdxxsjyVO.getTyshxydm());
		if(slqyDO == null) {
			return -1;
		}*/
		
		//进行库点信息copy
		DKdxxsjyDO dKdxxsjyDO = new DKdxxsjyDO();
		//dKdxxsjyVO.setQymc(slqyDO.getQymc());//设置数据库企业名称
		//根据统一社会信用代码生成库点id（库点id规则：统一社会信用代码+001）
		String kddm = getMaxCodeById(dKdxxsjyDao.getMaxById(dKdxxsjyVO.getTyshxydm()),dKdxxsjyVO.getTyshxydm(),3);
		dKdxxsjyVO.setKddm(kddm);
		dKdxxsjyVO.setStatus(1);
		LsBeanUtils.copyAllProperties(dKdxxsjyVO,dKdxxsjyDO);
		dKdxxsjyDO.setCreateDate(new Date());
		dKdxxsjyDO.setTyshxydm("911202236794497949");
		dKdxxsjyDO.setQymc("商丘直属库");
		return dKdxxsjyDao.insert(dKdxxsjyDO);
	}

	/**
	 * 获取库点名称
	 * @param kdmc
	 * @return
	 */
	public DKdxxsjyDO getDKdxxsjyInfoBykdmc(String kdmc) {
		return dKdxxsjyDao.getDKdxxsjyInfoBykdmc(kdmc);
	}

	/**
	 * 更新库点信息
	 * @param dKdxxsjyVO
	 * @return
	 */
	public int update(DKdxxsjyVO dKdxxsjyVO) {
		//进行库点信息copy
		DKdxxsjyDO dKdxxsjyDO = new DKdxxsjyDO();
		LsBeanUtils.copyAllProperties(dKdxxsjyVO,dKdxxsjyDO);
		return dKdxxsjyDao.update(dKdxxsjyDO);
	}

	/**
	 * 根据统一社会信用代码获取库点信息列表
	 * @param tyshxydm
	 * @return
	 */
    public List<DKdxxsjyVO> getDKdxxsjyInfoBytyshxydm(String tyshxydm) {
    	List<DKdxxsjyDO> kddos = dKdxxsjyDao.getDKdxxsjyInfoBytyshxydm(tyshxydm);
    	List<DKdxxsjyVO> kdvos = new ArrayList<DKdxxsjyVO>();
    	LsBeanUtils.copyAllProperties(kddos, kdvos);
    	return kdvos;
    }

	/**
	 * 根据统一社会信用代码删除库点信息
	 * @param tyshxydm
	 * @return
	 */
	public int deleteList(String tyshxydm) {

		List<DKdxxsjyDO> dKdxxsjyVOList = dKdxxsjyDao.getDKdxxsjyInfoBytyshxydm(tyshxydm);

		if (dKdxxsjyVOList == null){
			return 1;
		}
		return dKdxxsjyDao.deleteList(tyshxydm);
	}

	public List<DKdxxsjyDO> getDKdxxsjyAll(String userCode) {
		if(userCode.equals("U0000777")){
			userCode = "";
			return dKdxxsjyDao.getDKdxxsjyAlls(userCode);
		}
		return dKdxxsjyDao.getDKdxxsjyAll(userCode);
	}
}
