package com.unis.base.server.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import com.unis.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.unis.base.common.vo.DKdxxsjyVO;
import com.unis.base.server.domain.DKdxxsjyDomain;
import com.unis.base.server.domain.CompanyDomain;
import com.unis.base.server.entity.DKdxxsjyDO;
import com.unis.base.server.service.DKdxxsjyService;

@Service
public class DKdxxsjyServiceImpl implements DKdxxsjyService {

	@Autowired
	private DKdxxsjyDomain dKdxxsjyDomain;

	@Autowired
	private CompanyDomain tzSlqyDomain;

	/**
	 * 根据库点获取库点信息列表
	 * @param dKdxxsjyVO
	 * @return
	 */
	@Override
	public List<DKdxxsjyVO> getDKdxxsjyList(DKdxxsjyVO dKdxxsjyVO) {
		return dKdxxsjyDomain.getDKdxxsjyList(dKdxxsjyVO);
	}

	/**
	 * 获取库点列表,带组织机构id
	 * @param dKdxxsjyVO
	 * @return
	 */
	public List<DKdxxsjyVO> selectKdxx(DKdxxsjyVO dKdxxsjyVO){
		DKdxxsjyDO dKdxxsjyDO=JsonUtils.parse(JsonUtils.serialize(dKdxxsjyVO), DKdxxsjyDO.class);
		List<DKdxxsjyVO> tzQyjgList = tzSlqyDomain.selectKdxx(dKdxxsjyDO);
		return  tzQyjgList;
	}

	public List<DKdxxsjyVO> selectKdxxLgd(DKdxxsjyVO dKdxxsjyVO){
		DKdxxsjyDO dKdxxsjyDO=JsonUtils.parse(JsonUtils.serialize(dKdxxsjyVO), DKdxxsjyDO.class);
		List<DKdxxsjyVO> tzQyjgList = tzSlqyDomain.selectKdxxLgd(dKdxxsjyDO);
		return  tzQyjgList;
	}
	public List<DKdxxsjyVO> selectKdxxLgdForQc(DKdxxsjyVO dKdxxsjyVO){
		DKdxxsjyDO dKdxxsjyDO=JsonUtils.parse(JsonUtils.serialize(dKdxxsjyVO), DKdxxsjyDO.class);
		List<DKdxxsjyVO> tzQyjgList = tzSlqyDomain.selectKdxxLgdForQc(dKdxxsjyDO);
		return  tzQyjgList;
	}

	@Override
	public List<DKdxxsjyVO> sinkpointListJQ(DKdxxsjyVO dKdxxsjyVO) {
		return dKdxxsjyDomain.sinkpointListJQ(dKdxxsjyVO);
	}

	@Override
	public List<DKdxxsjyVO> sinkpointListLDQ(DKdxxsjyVO dKdxxsjyVO) {
		return dKdxxsjyDomain.sinkpointListLDQ(dKdxxsjyVO);
	}

	/**
	 * 根据库点获取库点信息树
	 * @param dKdxxsjyVO
	 * @return
	 */
	public List<DKdxxsjyVO> getDKdxxsjyTree(DKdxxsjyVO dKdxxsjyVO){
		DKdxxsjyDO dKdxxsjyDO=new DKdxxsjyDO();
		List<DKdxxsjyVO> tzQyjgList = tzSlqyDomain.selectKdxx(dKdxxsjyDO);
		Iterator<DKdxxsjyVO> it = tzQyjgList.iterator();
		String qyjgdm=null;
		DKdxxsjyVO kdxxVO=null;
		while(it.hasNext()){
			kdxxVO=it.next();
			qyjgdm=kdxxVO.getQyjgdm();
			for(DKdxxsjyVO vo : tzQyjgList){
				if(!qyjgdm.equals(vo.getQyjgdm()) && qyjgdm.contains(vo.getQyjgdm())){
                    List<DKdxxsjyVO> childList = Optional.ofNullable(vo.getChildList()).orElse(new ArrayList<>()) ;
                    childList.add(kdxxVO);
                    vo.setChildList(childList);
					it.remove();
					break;
				}
			}
		}
		return tzQyjgList;
	}

	/**
	 * 根据库点获取库点信息树
	 * @param dKdxxsjyVO
	 * @return
	 */
	public List<DKdxxsjyVO> getDKdxxsjyTree1(DKdxxsjyVO dKdxxsjyVO){
		DKdxxsjyDO dKdxxsjyDO=new DKdxxsjyDO();
		List<DKdxxsjyVO> tzQyjgList = tzSlqyDomain.selectKdxx(dKdxxsjyDO);
		Iterator<DKdxxsjyVO> it = tzQyjgList.iterator();
		String qyjgdm=null;
		DKdxxsjyVO kdxxVO=null;
		while(it.hasNext()){
			kdxxVO=it.next();
			qyjgdm=kdxxVO.getQyjgdm();
			for(DKdxxsjyVO vo : tzQyjgList){
				if(!qyjgdm.equals(vo.getQyjgdm()) && vo.getQyjgdm().contains(qyjgdm)){
					List<DKdxxsjyVO> list=vo.getChildList();
					if(list==null){
						list=new ArrayList<>();
						vo.setChildList(list);
					}
					list.add(kdxxVO);
					it.remove();
					break;
				}
			}
		}
		return tzQyjgList;
	}

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

	/**
	 * 分页获取库点分页信息
	 * @param dKdxxsjyVO 库点参数，库点编码，库点名称，企业名称，库点类型
	 * @param pageNum 分页页码
	 * @param pageSize 分页大小
	 * @return
	 */
	@Override
	public List<DKdxxsjyVO> getDKdxxsjyPage(DKdxxsjyVO dKdxxsjyVO, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return dKdxxsjyDomain.getDKdxxsjyList(dKdxxsjyVO);
	}

	/**
	 * 添加库点信息
	 * @param dKdxxsjyVO
	 * @return
	 */
	@Override
	public int insert(DKdxxsjyVO dKdxxsjyVO) {

		return dKdxxsjyDomain.insert(dKdxxsjyVO);
	}

	/**
	 * 更新库点信息
	 * @param dKdxxsjyVO 库点信息实体类
	 * @return
	 */
	@Override
	public int update(DKdxxsjyVO dKdxxsjyVO) {
		//查询该库点名称是否已经存在
		DKdxxsjyDO dKdxxsjyDO = dKdxxsjyDomain.getDKdxxsjyInfoBykddm(dKdxxsjyVO.getKddm());
		if(dKdxxsjyDO == null) {
			return 0;
		}
		return dKdxxsjyDomain.update(dKdxxsjyVO);
	}

	/**
	 * 删除库点信息
	 * @param kddm
	 * @return
	 */
	@Override
	public int delete(String kddm) {
		DKdxxsjyDO vo = dKdxxsjyDomain.getDKdxxsjyInfoBykddm(kddm);
		if(vo==null) {
			return -1;
		}
		DKdxxsjyVO dKdxxsjyVO = new DKdxxsjyVO();
		dKdxxsjyVO.setKddm(kddm);
		dKdxxsjyVO.setStatus(0);
		return dKdxxsjyDomain.update(dKdxxsjyVO);
	}

	/**
	 * 根据统一社会信用代码获取库点信息列表
	 * @param tyshxydm
	 * @return
	 */
	@Override
	public List<DKdxxsjyVO> getDKdxxsjyInfoBytyshxydm(String tyshxydm) {
		return dKdxxsjyDomain.getDKdxxsjyInfoBytyshxydm(tyshxydm);
	}

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

}
