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

import com.github.pagehelper.Page;
import javax.annotation.Resource;

import com.unis.base.common.vo.*;
import com.unis.base.server.dao.DCfxxsjyDao;
import com.unis.base.server.entity.DCfxxsjyDO;
import com.unis.common.utils.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.unis.base.server.domain.DCfxxsjyDomain;
import com.unis.base.server.domain.DKdxxsjyDomain;
import com.unis.base.server.entity.DKdxxsjyDO;
import com.unis.base.server.service.DCfxxsjyService;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author huangxing
 * @version 1.0
 * @Title DYgxxsjyServiceImpl
 * @ProjectName 仓房服务实现层
 * @Description
 * @date 2019/07/22 17点26分
 */
@Service
public class DCfxxsjyServiceImpl implements DCfxxsjyService {

    @Resource
    private DCfxxsjyDomain dCfxxsjyDomain;

    @Autowired
    private DKdxxsjyDomain dKdxxsjyDomain;

    @Autowired
    private DCfxxsjyDao dCfxxsjyDao;

    /**
     * 获取仓房信息列表
     *
     * @param tzSlqyVO
     * @return
     */
    @Override
    public List<DCfxxsjyVO> getDCfxxxsjyList(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> dCfxxxsjyList = dCfxxsjyDomain.getDCfxxxsjyList(dCfxxsjyVO);
        //return this.cfxxGroupByCfxhCommon(dCfxxxsjyList);
        return dCfxxxsjyList;
    }

    /**
     * 根据参数获取成品粮仓房列表
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public List<DCfxxsjyVO> getChengPinList(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> chengPinList = dCfxxsjyDomain.getChengPinList(dCfxxsjyVO);
        //return this.cfxxGroupByCfxhCommon(chengPinList);
        return chengPinList;
    }

    /**
     * 根据参数获取原粮仓房列表
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public List<BaseKcVO> getYuanLiangList(DCfxxsjyVO dCfxxsjyVO) {
        return dCfxxsjyDomain.getYuanLiangList(dCfxxsjyVO);
    }

    /**
     * 根据仓房编号获取仓房信息
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public ResponseWrapper selectById(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyVO vo = dCfxxsjyDomain.selectById(dCfxxsjyVO);
        if (vo != null) {
            return ResponseWrapper.success(vo);
        } else {
            return ResponseWrapper.fail();
        }
    }

    /**
     * 根据仓房编号获取仓房信息
     *
     * @return
     */
    @Override
    public DCfxxsjyVO getDCfxxxsjyInfo(String cfbh) {
        return dCfxxsjyDomain.getDCfxxxsjyInfo(cfbh);
    }

    /**
     * 分页获取仓房信息列表
     *
     * @param dCfxxsjyVO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<DCfxxsjyVO> getDCfxxxsjyPage(DCfxxsjyVO dCfxxsjyVO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return dCfxxsjyDomain.getDCfxxxsjyList(dCfxxsjyVO);
    }

    /**
     * 根据仓房编号获取仓房信息
     *
     * @return
     */
    @Override
    public DCfxxsjyVO getDCfxxxsjyInfoBycfbh(String cfbh) {
        DCfxxsjyVO dCfxxxsjyInfo = dCfxxsjyDomain.getDCfxxxsjyInfo(cfbh);
        if (null != dCfxxxsjyInfo) {
            DCfxxsjyDO paramInfo = new DCfxxsjyDO(dCfxxxsjyInfo.getKddm(), dCfxxxsjyInfo.getCfxh());
            List<DCfxxsjyDO> dCfxxsjyDOList = dCfxxsjyDao.getDCfxxxsjy(paramInfo);
            List<DHwxxsjyVO> dHwxxsjyVOS = new ArrayList<>();
            for (DCfxxsjyDO dCfxxsjyDO : dCfxxsjyDOList) {
                DHwxxsjyVO hwxxsjyVO = new DHwxxsjyVO();
                hwxxsjyVO.setCfbh(dCfxxsjyDO.getCfbh());
                hwxxsjyVO.setHwxh(dCfxxsjyDO.getHwxh());
                hwxxsjyVO.setHwmc(dCfxxsjyDO.getCfmc());
                hwxxsjyVO.setHwrl(dCfxxsjyDO.getHwrl());
                hwxxsjyVO.setHwzt(dCfxxsjyDO.getCfzt());
                Date qyrq = dCfxxsjyDO.getQyrq();
                hwxxsjyVO.setQyrq(dCfxxsjyDO.getQyrq());
                hwxxsjyVO.setCfmc(dCfxxsjyDO.getCfmc());
                dHwxxsjyVOS.add(hwxxsjyVO);
            }
            dCfxxxsjyInfo.setHwxxsjyVOList(dHwxxsjyVOS);
        }
        return dCfxxxsjyInfo;
    }

    /**
     * 更新仓房信息
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public int update(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyVO dCfxxsjyVOs = dCfxxsjyDomain.getDCfxxxsjyInfo(dCfxxsjyVO.getCfbh());
        if (dCfxxsjyVOs == null) {
            return 0;
        }
        return dCfxxsjyDomain.update(dCfxxsjyVO);
    }

    /**
     * 更新仓房信息 参数为null可入库
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public int updateAll(DCfxxsjyVO dCfxxsjyVO) {
        return dCfxxsjyDomain.updateAll(dCfxxsjyVO);
    }

    @Override
    public PageResult<DCfxxsjyVO> selectDCfxxxsjyPage(DCfxxsjyVO dCfxxsjyVO, Integer pageNum, Integer pageSize) {
        Page<DCfxxsjyVO> voPage = dCfxxsjyDomain.selectDCfxxxsjyPage(pageNum, pageSize, dCfxxsjyVO);
        PageResult<DCfxxsjyVO> pageResult=new PageResult<>(voPage);
        return pageResult;
    }

    @Override
    public List<DCfxxsjyVO> warehouseListNew(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> dCfxxsjyVOS = dCfxxsjyDomain.warehouseListNew(dCfxxsjyVO);
        //return this.cfxxGroupByCfxhCommon(dCfxxsjyVOS);
        return dCfxxsjyVOS;
    }

    @Override
    public List<DCfxxsjyVO> getDCfxxxsjyListCbl(DCfxxsjyVO dCfxxsjyVO) {
        return dCfxxsjyDomain.getDCfxxxsjyListCbl(dCfxxsjyVO);
    }

    /**
     * 带状态更新
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public int warehouseUpdate(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyVO dCfxxsjyVOs = dCfxxsjyDomain.getWarehouseInfo(dCfxxsjyVO.getCfbh());
        if (dCfxxsjyVOs == null) {
            return 0;
        }
        return dCfxxsjyDomain.update(dCfxxsjyVO);
    }

    /**
     * 根据仓房编号删除仓房信息
     *
     * @return
     */
    @Override
    public int delete(String kddm,String cfxh) {
        DCfxxsjyVO param = new DCfxxsjyVO();
        param.setCfxh(cfxh);
        param.setKddm(kddm);
        List<DCfxxsjyVO> dCfxxxsjyList = dCfxxsjyDao.selectByEntity(param);
        if (dCfxxxsjyList.isEmpty()) {
            return 0;
        }
        return dCfxxsjyDao.deleteByKddmAndCfxh(kddm,cfxh);
    }

    /**
     * 添加仓房信息
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Override
    public int insert(DCfxxsjyVO dCfxxsjyVO) {
        DKdxxsjyDO dKdxxsjyDO = dKdxxsjyDomain.getDKdxxsjyInfoBykddm(dCfxxsjyVO.getKddm());
        if (dKdxxsjyDO == null) {
            return -1;
        }
        dCfxxsjyVO.setKqdm(dKdxxsjyDO.getKqdm());
        return dCfxxsjyDomain.insert(dCfxxsjyVO);
    }

	/**
	 * 根据库点代码删除仓房信息列表
	 * @param kddm
	 * @return
	 */
	@Override
	public int deleteList(String kddm) {
		return dCfxxsjyDomain.deleteList(kddm);
	}

    /**
     * 根据库点代码获取仓房列表
     * @param kddm
     * @return
     */
    @Override
    public List<DCfxxsjyVO> getWarehouseListByKddm(String kddm,String grainType) {
        List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDomain.getWarehouseListByKddm(kddm,grainType);
        //return this.cfxxGroupByCfxhCommon(dCfxxsjyVOList);
        return dCfxxsjyVOList;
    }

    @Override
    public List<DCfxxsjyVO> selectByEntity(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDomain.selectByEntity(dCfxxsjyVO);
        //return this.cfxxGroupByCfxhCommon(dCfxxsjyVOList);
        return dCfxxsjyVOList;
    }

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

    /**
     * 仓房信息根据 仓房序号分组
     *
     * @param dCfxxsjyVOList
     * @return
     * @Description 将仓房信息表中的货位信息按照仓房序号分组划分到各个仓房下
     */
    private List<DCfxxsjyVO> cfxxGroupByCfxhCommon(List<DCfxxsjyVO> dCfxxsjyVOList) {
        List<DCfxxsjyVO> resultList = new ArrayList<>();
        Map<String, List<DCfxxsjyVO>> cfxxGroupBycfxh = dCfxxsjyVOList.stream()
                .collect(Collectors.groupingBy(imte -> imte.getCfxh() != null ? imte.getCfxh() : ""));
        for (Map.Entry<String, List<DCfxxsjyVO>> entry : cfxxGroupBycfxh.entrySet()) {
            String key = entry.getKey();
            List<DCfxxsjyVO> value = cfxxGroupBycfxh.get(key).stream().sorted(Comparator.comparing(DCfxxsjyVO::getCfbh)).collect(Collectors.toList());
            DCfxxsjyVO _cfxxVO = value.get(0);
            List<WarehouseDoorVO> warehouseDoorVOS = new ArrayList<>();
            List<WarehouseDoorVO> warehouseDoorVOList = _cfxxVO.getWarehouseDoorVOList();
            if (!CollectionUtils.isEmpty(warehouseDoorVOList)) {
                warehouseDoorVOS.addAll(warehouseDoorVOList);
            }
            List<DHwxxsjyVO> hwxxsjyVOList = new ArrayList<>();
            for (DCfxxsjyVO item : value) {
                DHwxxsjyVO hwxxsjyVO = new DHwxxsjyVO();
                hwxxsjyVO.setCfbh(item.getCfbh());
                hwxxsjyVO.setHwxh(item.getHwxh());
                hwxxsjyVO.setHwmc(item.getCfmc());
                hwxxsjyVO.setHwrl(item.getHwrl());
                hwxxsjyVO.setHwzt(item.getCfzt());
                hwxxsjyVO.setQyrq(item.getQyrq());
                hwxxsjyVO.setCfmc(item.getCfmc01());
                hwxxsjyVO.setHwdm(item.getHwdm());
                hwxxsjyVO.setCfbm(item.getCfbm());
                hwxxsjyVO.setStatus(item.getStatus());
                hwxxsjyVO.setSendResult(item.getSendResult());
                hwxxsjyVOList.add(hwxxsjyVO);
            }
            _cfxxVO.setWarehouseDoorVOList(warehouseDoorVOS);
            _cfxxVO.setHwxxsjyVOList(hwxxsjyVOList);
            resultList.add(_cfxxVO);
        }
        return resultList;
    }

    public DCfxxsjyVO getWarehouseDetail(DCfxxsjyVO dCfxxsjyVO) {
        dCfxxsjyVO.setZts(new String[]{"0","1"});
        List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDomain.getWarehouseDetail(dCfxxsjyVO);
        dCfxxsjyVOList =  this.cfxxGroupByCfxhCommon(dCfxxsjyVOList);
        return dCfxxsjyVOList.get(0);
    }

    public Integer updateDCfxxsjy(DCfxxsjyVO dCfxxsjyVO) {
        return dCfxxsjyDomain.updateDCfxxsjy(dCfxxsjyVO);
    }

    public List<DCfxxsjyVO> getDCfxxxsjyPageNew(DCfxxsjyVO dCfxxsjyVO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DCfxxsjyVO> list = dCfxxsjyDomain.getDCfxxxList(dCfxxsjyVO);
        return list;
    }

    public List<DCfxxsjyVO> getDCfxxxsjyListNew(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> dCfxxxsjyList = dCfxxsjyDomain.getDCfxxxsjyList(dCfxxsjyVO);
        return this.cfxxGroupByCfxhCommon(dCfxxxsjyList);
    }

    public List<DCfxxsjyVO> selectCfxxsjy(DCfxxsjyVO dCfxxsjyVO){
        return dCfxxsjyDao.selectCfxxsjy(dCfxxsjyVO);
    }

    @Override
    public List<DCfxxsjyUpdateLogVO> pageUpdateLog(DCfxxsjyUpdateLogVO params, Integer pageNum, Integer pageSize) {
        return dCfxxsjyDomain.pageUpdateLog(params, pageNum, pageSize);
    }
}
