package com.unis.base.server.domain;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.unis.base.common.vo.*;
import com.unis.base.server.dao.*;
import com.unis.base.server.entity.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.unis.base.common.vo.DCfxxsjyVO;
import com.unis.base.server.dao.DCfxxsjyDao;
import com.unis.base.server.entity.DCfxxsjyDO;
import com.unis.common.utils.LsBeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

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

    @Autowired
    private DCfxxsjyDao dCfxxsjyDao;
    @Autowired
    private DYgxxsjyDao dYgxxsjyDao;
    @Autowired
    private DKdxxsjyDao dKdxxsjyDao;
    @Autowired
    private TzSlqyDao tzSlqyDao;

    @Autowired
    private CommonDao commonDao;

    @Autowired
    private WarehouseDoorDomain warehouseDoorDomain;

    @Autowired
    TzSlqyDao tzSlqyMapper;

    @Autowired
    DCfxxsjyUpdateLogDao dCfxxsjyUpdateLogDao;

    public List<DCfxxsjyVO> getDCfxxxsjyList(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        String kddm = dCfxxsjyVO.getKddm();
        Date jssj = dCfxxsjyVO.getJssj();
        if (dCfxxsjyVO.getStartJssj() != null) {
            dCfxxsjyVO.setStartJssj(dCfxxsjyVO.getStartJssj() + "-01-01 00:00:00");
        }
        if (dCfxxsjyVO.getEndJssj() != null) {
            dCfxxsjyVO.setEndJssj(dCfxxsjyVO.getEndJssj() + "-12-31 23:59:59");
        }
        if (jssj != null) {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sf.format(jssj);
            dCfxxsjyVO.setCxsj(format);
        }
        List<String> dataList = dCfxxsjyDao.listGfcfdm(dCfxxsjyDO);

        List<DCfxxsjyDO> dCfxxxsjyDOList = dCfxxsjyDao.getDCfxxxsjy_ver2(dCfxxsjyDO);
        LinkedHashMap<String, DCfxxsjyDO> cfxxsjyDOMap = dCfxxxsjyDOList.stream()
                .collect(Collectors.toMap(DCfxxsjyDO::getGfcfdm, Function.identity(), (k1, k2) -> k1, LinkedHashMap::new));
        List<DCfxxsjyVO> dCfxxxsjyVOList = dataList.stream().map(item -> {
            DCfxxsjyVO cfxxsjyVO = new DCfxxsjyVO();
            LsBeanUtils.copyAllProperties( cfxxsjyDOMap.get(item), cfxxsjyVO);
            return cfxxsjyVO;
        }).collect(Collectors.toList());

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

        //获取总库容和总仓房数
        List<DKdxxsjyDO> dKdxxsjyDOs = dKdxxsjyDao.getKdyxcrAndCfsByKddm(kddm);
        if (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(dCfxxsjyVO.getTyshxydm());
        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());
                dCfxxxsjyVOList.get(0).setRefreshTime(tzSlqyDO.getRefreshTime());
                dCfxxxsjyVOList.get(0).setZcr(tzSlqyDO.getZcr());
            }
        }
        return dCfxxxsjyVOList;
    }

    public List<DCfxxsjyVO> getChengPinList(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        String kddm = dCfxxsjyVO.getKddm();
        List<DCfxxsjyVO> dCfxxxsjyVOList = new ArrayList<>();
        Date jssj = dCfxxsjyVO.getJssj();
        if (dCfxxsjyVO.getStartJssj() != null) {
            dCfxxsjyVO.setStartJssj(dCfxxsjyVO.getStartJssj() + "-01-01 00:00:00");
        }
        if (dCfxxsjyVO.getEndJssj() != null) {
            dCfxxsjyVO.setEndJssj(dCfxxsjyVO.getEndJssj() + "-12-31 23:59:59");
        }
        if (jssj != null) {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sf.format(jssj);
            dCfxxsjyVO.setCxsj(format);
        }
        List<DCfxxsjyDO> dCfxxxsjyDOList = dCfxxsjyDao.getChengPinList(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;
    }

    public List<BaseKcVO> getYuanLiangList(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        List<BaseKcVO> voList = dCfxxsjyDao.getYuanLiangList(dCfxxsjyDO);

        return voList;
    }

    /**
     * 根据仓房编号获取仓房信息
     *
     * @param dCfxxsjyVO
     * @return
     */
    public DCfxxsjyVO selectById(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        return getWarehouse(dCfxxsjyDao.selectById(dCfxxsjyDO));
    }

    /**
     * 根据仓房编号获取仓房信息
     *
     * @param cfbh
     * @return
     */
    public DCfxxsjyVO getDCfxxxsjyInfo(String cfbh) {
        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);
    }

    /**
     * 根据仓房编号更新仓房信息 参数为null可入库
     *
     * @param dCfxxsjyVO
     * @return
     */
    @Transactional
    public int updateAll(DCfxxsjyVO dCfxxsjyVO) {
        Integer sfba = dCfxxsjyVO.getSfba();

        DCfxxsjyVO vo = new DCfxxsjyVO();
        vo.setCfxh(dCfxxsjyVO.getCfxh());
        vo.setKddm(dCfxxsjyVO.getKddm());
        vo.setGfcfdm(dCfxxsjyVO.getGfcfdm());
        List<DCfxxsjyVO> dCfxxList = dCfxxsjyDao.selectCfxxsjy(vo);

        DCfxxsjyVO currentData = dCfxxsjyDao.getFirstData(dCfxxsjyVO.getGfcfdm());
        Integer dataVer = currentData.getDataVer();
        List<DCfxxsjyUpdateLogVO> currentVerList = dCfxxsjyUpdateLogDao.listCurrentVerData(currentData.getGfcfdm(), dataVer);

        if (null != sfba && 5 != sfba && 3 != sfba) {
            //检查是否添加过当前仓房
            if (!CollectionUtils.isEmpty(dCfxxList)) {
                return 0;
            }
            String refresher = dCfxxsjyVO.getRefresher();
            List<Integer> updateOperate = Arrays.asList(DCfxxsjyVO.SFBA.DTJ.getValue(), DCfxxsjyVO.SFBA.DSH.getValue());
            List<DCfxxsjyUpdateLogVO> updateDataList = currentData.checkInfo(dCfxxsjyVO);
            if (updateOperate.contains(sfba)) {
                String changeReason = dCfxxsjyVO.getChangeReason();
                if (currentVerList.size() == 0) {
                    if (DCfxxsjyVO.SFBA.DSH.equalsVal(sfba)) {
                        boolean isNoChange = updateDataList.stream().allMatch(i -> {
                            if (DCfxxsjyUpdateLogVO.COL.CFYT.equName(i.getUpdateCol())) {
                                return i.getBeforeData().equals(dCfxxsjyVO.getCfyt());
                            }else if (DCfxxsjyUpdateLogVO.COL.CFZT.equName(i.getUpdateCol())) {
                                return i.getBeforeData().equals(dCfxxsjyVO.getCfzt());
                            }
                            return true;
                        });
                        if (!isNoChange && StringUtils.isBlank(changeReason)) {
                            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 变更原因未填写");
                            return DCfxxsjyVO.UPDATE_RES.INVALID.getValue();
                        }
                    }
                    for (DCfxxsjyUpdateLogVO updateLog : updateDataList) {
                        updateLog.setDataVer(dataVer);
                        updateLog.setCreator(refresher);
                        updateLog.setRefresher(refresher);
                        updateLog.setChangeReason(changeReason);
                        dCfxxsjyUpdateLogDao.insertSelective(updateLog);
                    }
                }else {
                    if (DCfxxsjyVO.SFBA.DSH.equalsVal(sfba)) {
                        boolean isNoChange = currentVerList.stream().allMatch(i -> {
                            if (DCfxxsjyUpdateLogVO.COL.CFYT.equName(i.getUpdateCol())) {
                                return i.getBeforeData().equals(dCfxxsjyVO.getCfyt());
                            }else if (DCfxxsjyUpdateLogVO.COL.CFZT.equName(i.getUpdateCol())) {
                                return i.getBeforeData().equals(dCfxxsjyVO.getCfzt());
                            }
                            return true;
                        });
                        if (!isNoChange && StringUtils.isBlank(changeReason)) {
                            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 变更原因未填写");
                            return DCfxxsjyVO.UPDATE_RES.INVALID.getValue();
                        }
                        int i = dCfxxsjyUpdateLogDao.updateRefresher(currentData.getGfcfdm(), dataVer, refresher, changeReason);
                        log.info("更新 {} 条记录", i);
                    }
                }
            }
            DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
            LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
            dCfxxsjyDO.setHcjcfs("02");
            dCfxxsjyDO.setCfsfwh(StringUtils.isEmpty(dCfxxsjyVO.getCfzt()) ? "" : dCfxxsjyVO.getCfzt().equals("1") ? dCfxxsjyVO.getCfzt() : dCfxxsjyVO.getCfzt().equals("9") ? dCfxxsjyVO.getCfzt() : "0");
            List<DHwxxsjyVO> hwxxsjyVOList = dCfxxsjyVO.getHwxxsjyVOList();
            int result = 0;
            if (!hwxxsjyVOList.isEmpty()) {
                List<DCfxxsjyDO> dCfxxxsjyList = new ArrayList<>();
                DCfxxsjyDO checkDO = new DCfxxsjyDO();
                for (DHwxxsjyVO dHwxxsjyVO : hwxxsjyVOList) {
                    checkDO = new DCfxxsjyDO();
                    dCfxxxsjyList = new ArrayList<>();
                    checkDO.setHwxh(dHwxxsjyVO.getHwxh());
                    checkDO.setCfxh(dCfxxsjyVO.getCfxh());
                    checkDO.setKddm(dCfxxsjyVO.getKddm());
                    dCfxxxsjyList = dCfxxsjyDao.warehouseListNew(checkDO);
                    if (!CollectionUtils.isEmpty(dCfxxxsjyList) && !dCfxxxsjyList.get(0).getCfbh().equals(dHwxxsjyVO.getCfbh())) {
                        return -2;
                    }
                    if (null == dHwxxsjyVO.getCfbh()) {
                        result = saveCfxx(dCfxxsjyVO, dHwxxsjyVO, result);
                    } else {
                        dCfxxsjyDO.setCfmc(dHwxxsjyVO.getCfmc());
                        dCfxxsjyDO.setHwrl(dHwxxsjyVO.getHwrl());
                        dCfxxsjyDO.setQyrq(dHwxxsjyVO.getQyrq());
                        dCfxxsjyDO.setCfbh(dHwxxsjyVO.getCfbh());
                        dCfxxsjyDO.setHwxh(dHwxxsjyVO.getHwxh());
                        dCfxxsjyDO.setSyzt(dHwxxsjyVO.getSyzt());
                        dCfxxsjyDO.setStatus(dHwxxsjyVO.getStatus());
                        dCfxxsjyDO.setRefresher(refresher);
                        dCfxxsjyDO.setRefresherCode(dCfxxsjyVO.getRefresherCode());
                        dCfxxsjyDao.updateAll(dCfxxsjyDO);
                    }
                }
            }
            warehouseDoorDomain.updateWarehouseDoorList(dCfxxsjyVO.getWarehouseDoorVOList(), dCfxxsjyVO.getGfcfdm());
            //return dCfxxsjyDao.updateAll(dCfxxsjyDO);
        } else {
            if (DCfxxsjyVO.SFBA.TG.equalsVal(sfba)) {
                for (DCfxxsjyUpdateLogVO currentVerOldData : currentVerList) {
                    if (DCfxxsjyUpdateLogVO.COL.CFYT.equName(currentVerOldData.getUpdateCol())) {
                        if (!currentVerOldData.getBeforeData().equals(dCfxxsjyVO.getCfyt())) {
                            int r = dCfxxsjyUpdateLogDao.updateStatusByVer(dCfxxsjyVO.getGfcfdm(), dataVer, DCfxxsjyUpdateLogVO.COL.CFYT.getName(), dCfxxsjyVO.getCfyt());
                        }
                    }else if (DCfxxsjyUpdateLogVO.COL.CFZT.equName(currentVerOldData.getUpdateCol())) {
                        if (!currentVerOldData.getBeforeData().equals(dCfxxsjyVO.getCfzt())) {
                            int r = dCfxxsjyUpdateLogDao.updateStatusByVer(dCfxxsjyVO.getGfcfdm(), dataVer, DCfxxsjyUpdateLogVO.COL.CFZT.getName(), dCfxxsjyVO.getCfzt());
                        }
                    }
                }
                dCfxxsjyVO.setDataVer(dataVer + 1);
            }

            Integer i = dCfxxsjyDao.updateDCfxxsjy(dCfxxsjyVO);
            // 刷新单位更新时间，触发数据上传重新抽取单位信息更新仓房数
            if (DCfxxsjyVO.SFBA.TG.equalsVal(sfba)) {
                TzSlqyDO tzSlqyDO = new TzSlqyDO();
                tzSlqyDO.setTyshxydm(dCfxxsjyVO.getTyshxydm());
                tzSlqyDO.setRefreshTime(new Date());
                tzSlqyDO.setSjcr(new BigDecimal(1));
                tzSlqyMapper.updateTimeAndQyCr(tzSlqyDO);
            }
        }
        return 1;
    }

    /**
     * 新增仓房信息
     *
     * @param dCfxxsjyVO
     * @return
     */
    public int insert(DCfxxsjyVO dCfxxsjyVO) {

        //检查是否添加过当前仓房
        DCfxxsjyDO checkDO = new DCfxxsjyDO();
        checkDO.setCfxh(dCfxxsjyVO.getCfxh());
        checkDO.setKddm(dCfxxsjyVO.getKddm());

        List<DCfxxsjyDO> dCfxxxsjyList = dCfxxsjyDao.warehouseListNew(checkDO);

        if (!CollectionUtils.isEmpty(dCfxxxsjyList)) {
            if(dCfxxsjyVO.getIsImport()) {
                BeanUtils.copyProperties(dCfxxxsjyList.get(0),dCfxxsjyVO);
            } else {
                return 0;
            }
        }

        int result = 0;
        List<DHwxxsjyVO> hwxxsjyVOList = dCfxxsjyVO.getHwxxsjyVOList();
        if (!CollectionUtils.isEmpty(hwxxsjyVOList)) {
            if(!StringUtils.isNotBlank(dCfxxsjyVO.getGfcfdm())) {
                String gfcfdm = getMaxCodeById(commonDao.getMaxCode("kddm",dCfxxsjyVO.getKddm(),null,null,"gfcfdm","d_cfxxsjy"),dCfxxsjyVO.getKqdm().concat("0"),3);
                String ajdh = gfcfdm.concat("001");
                dCfxxsjyVO.setGfcfdm(gfcfdm);
                dCfxxsjyVO.setAjdh(ajdh);
            }
            for (int i=0;i<hwxxsjyVOList.size();i++) {
                checkDO.setHwxh(hwxxsjyVOList.get(i).getHwxh());
                dCfxxxsjyList = dCfxxsjyDao.warehouseListNew(checkDO);
                if (!CollectionUtils.isEmpty(dCfxxxsjyList)) {
                    return -2;
                }
                result = saveCfxx(dCfxxsjyVO,hwxxsjyVOList.get(i),result) ;
            }
        }
        // 添加仓门信息
        warehouseDoorDomain.insertWarehouseDoorList(dCfxxsjyVO.getWarehouseDoorVOList(), dCfxxsjyVO.getGfcfdm());
        return result;
    }

    private int saveCfxx(DCfxxsjyVO dCfxxsjyVO,DHwxxsjyVO hwxxsjyVO, int result) {
        // 生成企业编码
        String kddm = dCfxxsjyVO.getKddm();
        String tyshxydm = kddm.substring(0, 18);
        TzSlqyDO tzSlqyDO = tzSlqyDao.getCompanyBytyshxydm(tyshxydm);
        String qybm = tzSlqyDO.getQybm();
        dCfxxsjyVO.setQybm(qybm);
        // 生成仓房编号
        String cfbhMaxById = dCfxxsjyDao.getMaxById(kddm);
        String cfbh = getMaxCodeById(cfbhMaxById, kddm, 3);
        dCfxxsjyVO.setCfbh(cfbh);
        // 生成仓房编码: 120000+三位顺序号+三位仓房顺序号
        String cfbmB = cfbh.substring(cfbh.length() - 3);
        dCfxxsjyVO.setCfbm(qybm.concat(cfbmB));

        if (dCfxxsjyVO.getSfba() == null) {
            dCfxxsjyVO.setSfba(0);
        }

        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        // 生成符合国粮局规范的 仓房代码
        //String cfdm = dCfxxsjyVO.getKqdm().concat("0").concat(dCfxxsjyVO.getCfxh());
        //dCfxxsjyDO.setCfdm(cfdm);
        // 生成符合国粮局规范的 廒间代码
        //String ajdh = cfdm.concat("001");
        //dCfxxsjyDO.setAjdh(ajdh);
        // 生成符合国粮局规范的 货位代码
        //String hwdm = ajdh.concat(hwxxsjyVO.getHwxh());
        String hwdm = getMaxCodeById(commonDao.getMaxCode("kddm",kddm,"cfxh",dCfxxsjyVO.getCfxh(),"hwdm","d_cfxxsjy"),dCfxxsjyVO.getAjdh(),2);
        dCfxxsjyDO.setHwdm(hwdm);
        // 仓房名称+货位名称
        //dCfxxsjyDO.setCfmc(hwxxsjyVO.getCfmc());
        // 仓房代码
        //dCfxxsjyDO.setGfcfdm(cfdm);
        dCfxxsjyDO.setAjxh("001");
        // 仓房名称
        dCfxxsjyDO.setGfcfmc(dCfxxsjyVO.getGfcfmc());
        dCfxxsjyDO.setSyzt(hwxxsjyVO.getSyzt());
        dCfxxsjyDO.setHwrl(hwxxsjyVO.getHwrl());
        dCfxxsjyDO.setStatus(hwxxsjyVO.getStatus());
        dCfxxsjyDO.setCfmc(hwxxsjyVO.getCfmc());
        dCfxxsjyDO.setQyrq(hwxxsjyVO.getQyrq());
        dCfxxsjyDO.setHwxh(hwxxsjyVO.getHwxh());
        dCfxxsjyDO.setHcjcfs("02");
        dCfxxsjyDO.setCfsfwh(StringUtils.isEmpty(dCfxxsjyVO.getCfzt())?"":dCfxxsjyVO.getCfzt().equals("1")?dCfxxsjyVO.getCfzt():dCfxxsjyVO.getCfzt().equals("9")?dCfxxsjyVO.getCfzt():"0");
        dCfxxsjyDO.setCreator(dCfxxsjyVO.getCreator());
        dCfxxsjyDO.setCreatorCode(dCfxxsjyVO.getCreatorCode());
        result += dCfxxsjyDao.insert(dCfxxsjyDO);
        return result;
    }

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

    /**
     * 根据库点代码获取仓房列表
     *
     * @param kddm
     * @return
     */
    public List<DCfxxsjyVO> getWarehouseListByKddm(String kddm,String grainType) {
        List<DCfxxsjyDO> dCfxxsjyDOList = dCfxxsjyDao.getWarehouseListByKddm(kddm,grainType);
        List<DCfxxsjyVO> dCfxxsjyVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dCfxxsjyDOList)) {
            for (DCfxxsjyDO dCfxxsjyDO : dCfxxsjyDOList) {
                // 如果dCfxxsjyDOList当中的gfcfdm一样的话，取cfbh最小的那个，其他的都不要
                if (dCfxxsjyDOList.stream().filter(i -> i.getGfcfdm().equals(dCfxxsjyDO.getGfcfdm())).count() > 1) {
                    if (!dCfxxsjyDO.getCfbh().equals(dCfxxsjyDOList.stream().filter(i -> i.getGfcfdm().equals(dCfxxsjyDO.getGfcfdm())).min(Comparator.comparing(DCfxxsjyDO::getCfbh)).get().getCfbh())) {
                        continue;
                    }
                }
                DCfxxsjyVO dCfxxsjyVO = new DCfxxsjyVO();
                LsBeanUtils.copyAllProperties(dCfxxsjyDO, dCfxxsjyVO);
                dCfxxsjyVO.setWarehouseDoorVOList(warehouseDoorDomain.getWarehouseDoorListByCfbh(dCfxxsjyDO.getCfbh()));
                dCfxxsjyVOList.add(dCfxxsjyVO);
            }
        }
        return dCfxxsjyVOList;
    }

    public List<DCfxxsjyVO> getDCfxxsjyByEntity(DCfxxsjyDO dCfxxsjyDO) {
        List<DCfxxsjyVO> dCfxxsjyVOS = new ArrayList<>();
        List<DCfxxsjyDO> dCfxxsjyDOList = dCfxxsjyDao.getDCfxxxsjy(dCfxxsjyDO);
        if (!CollectionUtils.isEmpty(dCfxxsjyDOList)) {
            LsBeanUtils.copyCollection(dCfxxsjyDOList, dCfxxsjyVOS, DCfxxsjyVO.class);
            return dCfxxsjyVOS;
        }
        return null;
    }

    public List<DCfxxsjyVO> selectByEntity(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDao.selectByEntity(dCfxxsjyVO);
        for (DCfxxsjyVO vo : dCfxxsjyVOList) {
            vo.setWarehouseDoorVOList(warehouseDoorDomain.getWarehouseDoorListByCfbh(vo.getGfcfdm()));
        }
        return dCfxxsjyVOList;
    }


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

    public Page<DCfxxsjyVO> selectDCfxxxsjyPage(Integer pageNum, Integer pageSize, DCfxxsjyVO dCfxxsjyVO) {
        PageHelper.startPage(pageNum, pageSize);
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        String kddm = dCfxxsjyVO.getKddm();
        List<DCfxxsjyVO> dCfxxxsjyVOList = new ArrayList<>();
        Date jssj = dCfxxsjyVO.getJssj();
        if (dCfxxsjyVO.getStartJssj() != null) {
            dCfxxsjyVO.setStartJssj(dCfxxsjyVO.getStartJssj() + "-01-01 00:00:00");
        }
        if (dCfxxsjyVO.getEndJssj() != null) {
            dCfxxsjyVO.setEndJssj(dCfxxsjyVO.getEndJssj() + "-12-31 23:59:59");
        }
        if (jssj != null) {
            SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
            String format = sf.format(jssj);
            dCfxxsjyVO.setCxsj(format);
        }
        Page<DCfxxsjyVO> dCfxxxsjyDOLists = dCfxxsjyDao.getDCfxxxsjyLists(dCfxxsjyVO);
        //List<DCfxxsjyDO> dCfxxxsjyDOList = dCfxxsjyDao.getDCfxxxsjyList(dCfxxsjyVO);
        for (DCfxxsjyVO cfxxsjyDO : dCfxxxsjyDOLists) {
            //DCfxxsjyVO cfxxsjyVO = new DCfxxsjyVO();
            //LsBeanUtils.copyAllProperties(cfxxsjyDO,cfxxsjyVO);
            List<TzSlqyDO> tzSlqyDO = tzSlqyDao.getQyxxByXydm(cfxxsjyDO.getKddm().substring(0, cfxxsjyDO.getKddm()
                    .length() - 3));
            cfxxsjyDO.setQymc(tzSlqyDO.get(0).getQymc());
            cfxxsjyDO.setQytxdz(tzSlqyDO.get(0).getQytxdz());
            cfxxsjyDO.setFddbr(tzSlqyDO.get(0).getFddbr());
            cfxxsjyDO.setCreateDate(tzSlqyDO.get(0).getCreateDate());
            //dCfxxxsjyVOList.add(cfxxsjyVO);
        }

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

    public List<DCfxxsjyVO> warehouseListNew(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        List<DCfxxsjyVO> dCfxxxsjyVOList = new ArrayList<>();
        List<DCfxxsjyDO> dCfxxxsjyDOList = dCfxxsjyDao.warehouseListNew(dCfxxsjyDO);
        for (DCfxxsjyDO cfxxsjyDO : dCfxxxsjyDOList) {
            DCfxxsjyVO cfxxsjyVO = new DCfxxsjyVO();
            LsBeanUtils.copyAllProperties(cfxxsjyDO, cfxxsjyVO);
            dCfxxxsjyVOList.add(cfxxsjyVO);
        }
        List<DYgxxsjyDO> ygxxsjyDOList = dYgxxsjyDao.selectYgList(dCfxxsjyVO);
        for (DYgxxsjyDO dYgxxsjyDO : ygxxsjyDOList) {
            DCfxxsjyVO cfxxsjyVO = new DCfxxsjyVO();
            cfxxsjyVO.setKddm(dYgxxsjyDO.getKddm());
            cfxxsjyVO.setCfbh(dYgxxsjyDO.getYgbh());
            cfxxsjyVO.setCfmc(dYgxxsjyDO.getYgmc());
            cfxxsjyVO.setCflx(dYgxxsjyDO.getYglx());
            cfxxsjyVO.setSjcr(dYgxxsjyDO.getSjgr());
            dCfxxxsjyVOList.add(cfxxsjyVO);
        }
        return dCfxxxsjyVOList;
    }

    public List<DCfxxsjyVO> getDCfxxxsjyListCbl(DCfxxsjyVO dCfxxsjyVO) {
        DCfxxsjyDO dCfxxsjyDO = new DCfxxsjyDO();
        LsBeanUtils.copyAllProperties(dCfxxsjyVO, dCfxxsjyDO);
        String kddm = dCfxxsjyVO.getKddm();
        List<DCfxxsjyVO> dCfxxxsjyVOList = new ArrayList<>();

        List<DCfxxsjyDO> dCfxxxsjyDOList = dCfxxsjyDao.getDCfxxxsjyCbl(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;
	}

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

    public Integer updateDCfxxsjy(DCfxxsjyVO dCfxxsjyVO) {
        Integer i = dCfxxsjyDao.updateDCfxxsjy(dCfxxsjyVO);
        // 刷新单位更新时间，触发数据上传重新抽取单位信息更新油罐数
        if(null != dCfxxsjyVO.getSfba() && 5==dCfxxsjyVO.getSfba()) {
            TzSlqyDO tzSlqyDO = new TzSlqyDO();
            tzSlqyDO.setTyshxydm(dCfxxsjyVO.getTyshxydm());
            tzSlqyDO.setRefreshTime(new Date());
            tzSlqyDO.setSjcr(new BigDecimal(1));
            tzSlqyMapper.updateTimeAndQyCr(tzSlqyDO);
        }
        return i;
    }

    public List<DCfxxsjyVO> getWarehouseDetail(DCfxxsjyVO dCfxxsjyVO) {
        List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyDao.getWarehouseDetail(dCfxxsjyVO);
        for (DCfxxsjyVO vo : dCfxxsjyVOList) {
            vo.setWarehouseDoorVOList(warehouseDoorDomain.getWarehouseDoorListByCfbh(vo.getGfcfdm()));
        }
        return dCfxxsjyVOList;
    }

    public List<DCfxxsjyUpdateLogVO> pageUpdateLog(DCfxxsjyUpdateLogVO params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return dCfxxsjyUpdateLogDao.listUpdateLog(params);
    }
}
