package com.unis.detectionnum.server.domain;

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

import com.unis.detectionnum.server.entity.DCfxxsjyDO;
import org.springframework.beans.factory.annotation.Autowired;

import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.common.vo.DCfxxsjyVO;
import com.unis.detectionnum.common.vo.MenuVO;
import com.unis.detectionnum.server.dao.DCfxxsjyDao;
import com.unis.detectionnum.server.dao.DKdxxsjyDao;

import org.springframework.stereotype.Component;

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

	@Autowired
	private DCfxxsjyDao dCfxxsjyDao;
	@Autowired
	private DKdxxsjyDao dKdxxsjyDao;
	//@Autowired
	//private TzSlqyDao tzSlqyDao;

	@Autowired
	private WarehouseDoorDomain warehouseDoorDomain;

	/**
	 * 根据参数获取仓房信息
	 * @param dCfxxsjyVO
	 * @return
	 */
	public List<DCfxxsjyVO> getDCfxxxsjyList(DCfxxsjyVO dCfxxsjyVO) {
		DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
		LsBeanUtils.copyAllProperties(dCfxxsjyVO,dCfxxsjyDO);
//		String kddm = dCfxxsjyVO.getKddm();
		List<DCfxxsjyVO> dCfxxxsjyVOList = new ArrayList<>();

		List<DCfxxsjyDO> dCfxxxsjyDOList = dCfxxsjyDao.getDCfxxxsjyList(dCfxxsjyDO);
		for (DCfxxsjyDO cfxxsjyDO: dCfxxxsjyDOList){
			DCfxxsjyVO cfxxsjyVO = new DCfxxsjyVO();
			LsBeanUtils.copyAllProperties(cfxxsjyDO,cfxxsjyVO);
			dCfxxxsjyVOList.add(cfxxsjyVO);
		}

		/*//获取总库容和总仓房数
		List<DKdxxsjyDO> dKdxxsjyDOs = dKdxxsjyDao.getKdyxcrAndCfsByKddm(kddm);
		if (dCfxxxsjyVOList != null && dCfxxxsjyVOList.size() > 0) {
			if (dKdxxsjyDOs != null && dKdxxsjyDOs.size() > 0) {
				dCfxxxsjyVOList.get(0).setKdyxcr(dKdxxsjyDOs.get(0).getKdyxcr());
				dCfxxxsjyVOList.get(0).setCfs(dKdxxsjyDOs.get(0).getCfs());
			}
		}*/

		//获取单位等
		/*TzSlqyDO tzSlqyDO = tzSlqyDao.getqyxxBytyshxydm(kddm);
		if (dCfxxxsjyVOList != null && dCfxxxsjyVOList.size() > 0) {
			if (tzSlqyDO != null) {
				dCfxxxsjyVOList.get(0).setQymc(tzSlqyDO.getQymc());
				dCfxxxsjyVOList.get(0).setQytxdz(tzSlqyDO.getQytxdz());
				dCfxxxsjyVOList.get(0).setFddbr(tzSlqyDO.getFddbr());
				dCfxxxsjyVOList.get(0).setCreateDate(tzSlqyDO.getCreateDate());
			}
		}*/
		return dCfxxxsjyVOList;
	}

	/**
	 * 根据仓房编号获取仓房信息
	 * @param cfbh
	 * @return
	 */
	public DCfxxsjyVO getDCfxxxsjyInfo(String cfbh) {
        DCfxxsjyDO dCfxxxsjyInfo = dCfxxsjyDao.getDCfxxxsjyInfo(cfbh);
        if (dCfxxxsjyInfo == null){
            return null;
        }
        DCfxxsjyVO dCfxxsjyVO = new DCfxxsjyVO();
        LsBeanUtils.copyAllProperties(dCfxxxsjyInfo, dCfxxsjyVO);
        return dCfxxsjyVO;
       // return this.getWarehouse(dCfxxsjyDao.getDCfxxxsjyInfo(cfbh));
	}

    /**
     * 根据仓房编号获取仓房信息-任何状态
     * @param cfbh
     * @return
     */
    public DCfxxsjyVO getWarehouseInfo(String cfbh) {
        return this.getWarehouse(dCfxxsjyDao.getWarehouseInfo(cfbh));
    }

	/**
	 * 获取仓房信息
	 * @param dCfxxsjyDO
	 * @return
	 */
	public DCfxxsjyVO getWarehouse(DCfxxsjyDO dCfxxsjyDO){
		if (dCfxxsjyDO == null){
			return null;
		}
		DCfxxsjyVO dCfxxsjyVO = new DCfxxsjyVO();
		LsBeanUtils.copyAllProperties(dCfxxsjyDO, dCfxxsjyVO);
		dCfxxsjyVO.setWarehouseDoorVOList(warehouseDoorDomain.getWarehouseDoorListByCfbh(dCfxxsjyDO.getCfbh()));
		return dCfxxsjyVO;
	}

	/**
	 * 根据仓房编号更新仓房信息
	 * @param dCfxxsjyVO
	 * @return
	 */
	public int update(DCfxxsjyVO dCfxxsjyVO) {
		DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
		LsBeanUtils.copyAllProperties(dCfxxsjyVO,dCfxxsjyDO);
		//warehouseDoorDomain.updateWarehouseDoorList(dCfxxsjyVO.getWarehouseDoorVOList(),dCfxxsjyVO.getCfbh());
		return dCfxxsjyDao.update(dCfxxsjyDO);
	}

	/**
	 * 新增仓房信息
	 * @param dAjxxsjyVO
	 * @return
	 */
	public int insert(DCfxxsjyVO dAjxxsjyVO) {
		//进行库点信息copy
		DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
		//根据统一社会信用代码生成库点id（库点id规则：统一社会信用代码+001）
		String cfbh = getMaxCodeById(dCfxxsjyDao.getMaxById(dAjxxsjyVO.getKddm()),dAjxxsjyVO.getKddm(),3);
		dAjxxsjyVO.setCfbh(cfbh);
		LsBeanUtils.copyAllProperties(dAjxxsjyVO,dCfxxsjyDO);
		dCfxxsjyDO.setCreateDate(new Date());
		dCfxxsjyDO.setStatus(1);
		//warehouseDoorDomain.insertWarehouseDoorList(dAjxxsjyVO.getWarehouseDoorVOList(),dAjxxsjyVO.getCfbh());
		return dCfxxsjyDao.insert(dCfxxsjyDO);
	}

	/**
	 * 根据库点代码删除仓房信息列表
	 * @param kddm
	 * @return
	 */
	public int deleteList(String kddm) {
		List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDao.getWarehouseListByKddm(kddm);
		if (dCfxxsjyVOList != null && dCfxxsjyVOList.size() > 0){
			for (DCfxxsjyVO dCfxxsjyVO : dCfxxsjyVOList) {
				warehouseDoorDomain.deleteList(dCfxxsjyVO.getCfbh());
			}
		}
		return dCfxxsjyDao.deleteList(kddm);
	}

	/**
	 * 根据库点代码获取仓房列表
	 * @param kddm
	 * @return
	 */
	public List<DCfxxsjyVO> getWarehouseListByKddm(String kddm) {
		List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDao.getWarehouseListByKddm(kddm);
		if (dCfxxsjyVOList != null && dCfxxsjyVOList.size() > 0){
			for (DCfxxsjyVO dCfxxsjyVO : dCfxxsjyVOList) {
				dCfxxsjyVO.setWarehouseDoorVOList(warehouseDoorDomain.getWarehouseDoorListByCfbh(dCfxxsjyVO.getCfbh()));
			}
		}
		return dCfxxsjyVOList;
	}

	/**
	 * 备案主表状态
	 * @param dCfxxsjyVO
	 * @return
	 */
	public int updateFiling(DCfxxsjyVO dCfxxsjyVO) {
		return dCfxxsjyDao.updateFiling(dCfxxsjyVO);
	}

	/** 
	 * @Title: getMenuList 
	 * @Description: TODO
	 * @param userCode
	 * @return List<MenuVO>
	 * @throws 
	 */
	public List<MenuVO> getMenuList(String userCode) {

		return dCfxxsjyDao.selectMenuList(userCode);
	}

	public String getCfbhs(String userCode) {

		return dCfxxsjyDao.selectCfbhs(userCode);
	}

	/**
	 * @Title: getAdminMenuList
	 * @Description: TODO
	 * @param userCode
	 * @return List<MenuVO>
	 * @throws
	 */
	public List<MenuVO> getAdminMenuList(String userCode) {
		userCode = "";
		return dCfxxsjyDao.selectAdminMenuList(userCode);
	}

    public double getZlxGd(String sbCode) {
	    return dCfxxsjyDao.getZlxGd(sbCode);
    }

	public int deleteBycfbh(String cfbh) {
		dCfxxsjyDao.deleteBycfbh(cfbh);
		return 1;
	}
}
