package com.unis.base.server.domain;

import java.util.List;

import com.unis.base.server.dao.CommonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.unis.base.common.vo.DKdxxsjyVO;
import com.unis.base.server.dao.DKdxxsjyDao;
import com.unis.base.server.entity.DKdxxsjyDO;
import com.unis.base.server.entity.TzSlqyDO;
import com.unis.common.utils.LsBeanUtils;
import com.unis.base.server.dao.TzSlqyDao;
/**
 * @author huangxing
 * @version 1.0
 * @Title DKdxxsjyDomain
 * @ProjectName 持久化
 * @Description
 * @date 2019/07/22 17点26分
 */
@Component
public class DKdxxsjyDomain extends BaseDomain {

	@Autowired
	private DKdxxsjyDao dKdxxsjyDao;

	@Autowired
	private TzSlqyDao tzSlqyDao;

	@Autowired
	private CommonDao commonDao;

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

	/**
	 * 根据库点编号获取库点信息
	 * @param kddm
	 * @return
	 */
	public DKdxxsjyDO getDKdxxsjyInfoBykddm(String kddm) {
		return dKdxxsjyDao.getDKdxxsjyInfoBykddm(kddm);
	}

	/**
	 * 新增库点信息
	 * @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.setSfba(0);

		// 生成标准库区代码
		String tyshxydm = dKdxxsjyVO.getTyshxydm();
		if(otherProvGrDepots.contains(tyshxydm)) {
			tyshxydm = "911200007128680350";
		}
		String kqdm = getMaxCodeById(commonDao.getMaxCode(null,null,null,null,"substr(kqdm,19,21)","d_kdxxsjy"),tyshxydm,3);
		dKdxxsjyVO.setKqdm(kqdm);
		LsBeanUtils.copyAllProperties(dKdxxsjyVO,dKdxxsjyDO);

		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) {
    	return dKdxxsjyDao.getDKdxxsjyInfoBytyshxydm(tyshxydm);
    }

	/**
	 * 根据统一社会信用代码删除库点信息
	 * @param tyshxydm
	 * @return
	 */
	public int deleteList(String tyshxydm) {
		List<DKdxxsjyVO> dKdxxsjyVOList = dKdxxsjyDao.getDKdxxsjyInfoBytyshxydm(tyshxydm);
		if (dKdxxsjyVOList == null){
			return 1;
		}
		return dKdxxsjyDao.deleteList(tyshxydm);
	}

	public List<DKdxxsjyVO> sinkpointListJQ(DKdxxsjyVO dKdxxsjyVO) {
		DKdxxsjyDO dKdxxsjyDO = new DKdxxsjyDO();
		LsBeanUtils.copyAllProperties(dKdxxsjyVO,dKdxxsjyDO);
		//
		List<DKdxxsjyVO> list = dKdxxsjyDao.getSlqyList(dKdxxsjyDO);
		//List<DKdxxsjyVO> list = dKdxxsjyDao.getDKdxxsjyList(dKdxxsjyDO);
		DKdxxsjyVO kdxxsjy = new DKdxxsjyVO();
		kdxxsjy.setCgbh("LS001019");
		kdxxsjy.setKdmc("天津市粮食储备有限公司");
		kdxxsjy.setTyshxydm("911200007128680350");
		list.add(kdxxsjy);
		return list;
	}

	public List<DKdxxsjyVO> sinkpointListLDQ(DKdxxsjyVO dKdxxsjyVO) {
		DKdxxsjyDO dKdxxsjyDO = new DKdxxsjyDO();
		LsBeanUtils.copyAllProperties(dKdxxsjyVO,dKdxxsjyDO);
		List<DKdxxsjyVO> list = dKdxxsjyDao.sinkpointListLDQ(dKdxxsjyDO);
		return list;
	}
}
