package com.prolog.rdc.tms.service.basicData.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.DateUtils;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.TmsBaseStartingStationMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseStationMapper;
import com.prolog.rdc.tms.model.dto.basicData.StartingStationViewDTO;
import com.prolog.rdc.tms.model.dto.basicData.StationDistanceDTO;
import com.prolog.rdc.tms.model.dto.basicData.StationPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.StationViewDTO;
import com.prolog.rdc.tms.model.entity.TmsBaseStartingStationModel;
import com.prolog.rdc.tms.model.entity.TmsBaseStationModel;
import com.prolog.rdc.tms.model.vo.basicData.*;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.basicData.StationService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 基础数据-站点资料实现层
 *
 * @author :lhb
 * @version :1.0.0
 * @className :StationServiceImpl
 * @creationTime :2023/03/25
 * @description :
 * @modifier :lhb
 * @modifyTime :2023/03/25
 * @reviseRemarks:
 */
@Slf4j
@Service
public class StationServiceImpl implements StationService {

    @Autowired
    private TmsBaseStationMapper tmsBaseStationMapper;

    @Autowired
    private TmsBaseStartingStationMapper tmsBaseStartingStationMapper;

    @Autowired
    private SysRefdatadService sysRefdatadService;

    @Autowired
    private SysCodeengineService sysCodeengineService;

    /**
     * 站点资料分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public Page<StationPageDTO> pageStationData(StationQueryVO vo) {
        ToolKits.isParamNull(vo, "站点资料查询请求不为空");
        ToolKits.isParamNull(vo.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vo.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        List<String> params = Lists.newArrayList(DataDictionaryConstant.GENERAL_STATUS, // 状态
                DataDictionaryConstant.STATION_TYPE //站点类型
        );
        Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

        vo.setEnterpriseId(user.getEnterpriseId());
        // 判断账号类型
        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            vo.setIsDept("Y");
            // 判断是否为部门权限
            if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
                vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
            } else {
                vo.setIsData("Y");
                vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
            }
        } else {
            throw new PrologException("当前人员没有操作权限！");
        }
        // 分页
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<StationPageDTO> stationPageDTOS = tmsBaseStationMapper.pageStationData(vo);
        if (ToolKits.isEmpty(stationPageDTOS)) {
            return null;
        }
        for (StationPageDTO dto : stationPageDTOS) {
            // 响应对象
            dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.GENERAL_STATUS, String.valueOf(dto.getStatus())));
            dto.setTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.STATION_TYPE, String.valueOf(dto.getType())));
        }

        Page<StationPageDTO> page = PageUtils.getPage(stationPageDTOS);
        if (stationPageDTOS instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<StationPageDTO> p0 = (com.github.pagehelper.Page<StationPageDTO>) stationPageDTOS;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }
        return page;
    }

    /**
     * 站点资料弹窗
     *
     * @param vo
     * @return
     */
    public Page<StationPageDTO> popupStationData(StationQueryVO vo) {
        ToolKits.isParamNull(vo, "站点资料查询请求不为空");
        ToolKits.isParamNull(vo.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vo.getPageSize(), "页数不为空");

        vo.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        List<String> params = Lists.newArrayList(DataDictionaryConstant.GENERAL_STATUS, // 状态
                DataDictionaryConstant.STATION_TYPE //站点类型
        );
        Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

        vo.setEnterpriseId(user.getEnterpriseId());
        // 判断账号类型
        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            vo.setIsDept("Y");
            // 判断是否为部门权限
            if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
                vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
            } else {
                vo.setIsData("Y");
                vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
            }
        }
        // 分页
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<StationPageDTO> stationPageDTOS = tmsBaseStationMapper.pageStationData(vo);
        if (ToolKits.isEmpty(stationPageDTOS)) {
            return null;
        }
        for (StationPageDTO dto : stationPageDTOS) {
            // 响应对象
            dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.GENERAL_STATUS, String.valueOf(dto.getStatus())));
            dto.setTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.STATION_TYPE, String.valueOf(dto.getType())));
        }

        Page<StationPageDTO> page = PageUtils.getPage(stationPageDTOS);
        if (stationPageDTOS instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<StationPageDTO> p0 = (com.github.pagehelper.Page<StationPageDTO>) stationPageDTOS;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }
        return page;
    }

    /**
     * 根据行政区查询站点资料
     *
     * @param vo
     * @return
     */
    public StationPageDTO queryStationByAdministrativeData(StationQueryByAreasVO vo) {
        ToolKits.isParamNull(vo, "站点资料查询请求不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        vo.setEnterpriseId(user.getEnterpriseId());
        // 判断账号类型
        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            vo.setIsDept("Y");
            // 判断是否为部门权限
            if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
                vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
            } else {
                vo.setIsData("Y");
                vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
            }
        }
        vo.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        List<StationPageDTO> stationPageDTOS = tmsBaseStationMapper.queryStationByAdministrativeData(vo);
        if (ToolKits.isNotEmpty(stationPageDTOS) && 1 == stationPageDTOS.size()) {
            return stationPageDTOS.get(0);
        } else {
            return null;
        }
    }

    /**
     * 站点资料查看
     *
     * @param vo
     * @return
     */
    @Override
    public StationViewDTO viewStationData(StationQueryVO vo) {
        ToolKits.isParamNull(vo, "站点资料查看请求不为空");
        ToolKits.isParamNull(vo.getId(), "站点ID不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 站点信息
        TmsBaseStationModel stationModel = tmsBaseStationMapper.findById(vo.getId(), TmsBaseStationModel.class);
        if (ToolKits.isEmpty(stationModel)) {
            throw new PrologException(StrUtil.format("站点{}不存在", vo.getId()));
        }
        StationViewDTO stationViewDTO = BeanUtil.copyProperties(stationModel, StationViewDTO.class);

        List<String> params = Lists.newArrayList(DataDictionaryConstant.GENERAL_STATUS, // 状态
                DataDictionaryConstant.STATION_TYPE //站点类型
        );
        Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);
        stationViewDTO.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.GENERAL_STATUS, String.valueOf(stationViewDTO.getStatus())));
        stationViewDTO.setTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.STATION_TYPE, String.valueOf(stationViewDTO.getType())));

        // 始发站点信息
        Criteria criteria = Criteria.forClass(TmsBaseStartingStationModel.class);
        Restriction r1 = Restrictions.eq(TmsBaseStartingStationModel::getStationId, stationModel.getId());
        Restriction r2 = Restrictions.eq(TmsBaseStartingStationModel::getEnterpriseId, user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2));
        List<TmsBaseStartingStationModel> startingStationModels = tmsBaseStartingStationMapper.findByCriteria(criteria);

        List<StartingStationViewDTO> startingStationViewDTOS = Lists.newArrayList();
        if (ToolKits.isEmpty(startingStationModels)) {
            stationViewDTO.setStartingStationViewDTOList(startingStationViewDTOS);
            return stationViewDTO;
        }

        for (TmsBaseStartingStationModel model : startingStationModels) {
            StartingStationViewDTO startingStationViewDTO = BeanUtil.copyProperties(model, StartingStationViewDTO.class);
            startingStationViewDTOS.add(startingStationViewDTO);
        }
        stationViewDTO.setStartingStationViewDTOList(startingStationViewDTOS);
        return stationViewDTO;
    }

    /**
     * 站点资料查看
     *
     * @param vo
     * @return
     */
    public StationViewDTO viewStationByNameData(StationQueryVO vo) {
        ToolKits.isParamNull(vo, "站点资料查看请求不为空");
        ToolKits.isParamNull(vo.getName(), "站点名称不为空");

        // 站点信息
        Criteria criteria = Criteria.forClass(TmsBaseStationModel.class);
        Restriction r1 = Restrictions.eq(TmsBaseStationModel::getName, vo.getName());
        criteria.setRestriction(Restrictions.and(r1));

        List<TmsBaseStationModel> stationModels = tmsBaseStationMapper.findByCriteria(criteria);
        if (ToolKits.isEmpty(stationModels)) {
            throw new PrologException(StrUtil.format("站点{}不存在", vo.getName()));
        }
        StationViewDTO stationViewDTO = BeanUtil.copyProperties(stationModels.get(0), StationViewDTO.class);

        return stationViewDTO;
    }
    /**
     * 始发站点资料查询
     *
     * @param vo
     * @return
     */
    @Override
    public List<StartingStationViewDTO> queryStartingStationData(StationQueryVO vo) {
        ToolKits.isParamNull(vo, "站点资料查看请求不为空");
        ToolKits.isParamNull(vo.getId(), "站点ID不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 始发站点信息
        Criteria criteria = Criteria.forClass(TmsBaseStartingStationModel.class);
        Restriction r1 = Restrictions.eq(TmsBaseStartingStationModel::getStationId, vo.getId());
        Restriction r2 = Restrictions.eq(TmsBaseStartingStationModel::getEnterpriseId, user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2));
        List<TmsBaseStartingStationModel> startingStationModels = tmsBaseStartingStationMapper.findByCriteria(criteria);

        if (ToolKits.isEmpty(startingStationModels)) {
            return null;
        }
        List<StartingStationViewDTO> startingStationDTOS = Lists.newArrayList();
        for (TmsBaseStartingStationModel model : startingStationModels) {
            StartingStationViewDTO startingStationViewDTO = BeanUtil.copyProperties(model, StartingStationViewDTO.class);
            startingStationDTOS.add(startingStationViewDTO);
        }
        return startingStationDTOS;
    }

    /**
     * 站点资料新增
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStationData(StationAddVO vo) {
        ToolKits.isParamNull(vo, "站点资料查询请求不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        Criteria criteria = Criteria.forClass(TmsBaseStationModel.class);
        Restriction r1 = ToolKits.isEmpty(vo.getCode()) ? null : Restrictions.eq(TmsBaseStationModel::getCode, vo.getCode());
        Restriction r2 = ToolKits.isEmpty(vo.getName()) ? null : Restrictions.eq(TmsBaseStationModel::getName, vo.getName());
        Restriction r3 = ToolKits.isEmpty(vo.getDeliveryOrder()) ? null : Restrictions.eq(TmsBaseStationModel::getDeliveryOrder, vo.getDeliveryOrder());
        Restriction r4 = Restrictions.eq(TmsBaseStationModel::getEnterpriseId, user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r4));
        List<TmsBaseStationModel> stationModels = tmsBaseStationMapper.findByCriteria(criteria);
        if (ToolKits.isNotEmpty(stationModels)) {
            throw new PrologException("站点编码已经存在");
        }
        criteria.setRestriction(Restrictions.and(r2, r4));
        stationModels = null;
        stationModels = tmsBaseStationMapper.findByCriteria(criteria);
        if (ToolKits.isNotEmpty(stationModels)) {
            throw new PrologException("站点名称已经存在");
        }
        criteria.setRestriction(Restrictions.and(r3, r4));
        stationModels = null;
        stationModels = tmsBaseStationMapper.findByCriteria(criteria);
        if (ToolKits.isNotEmpty(stationModels)) {
            throw new PrologException("站点配送顺序已经存在");
        }
        Date date = DateUtil.date();
        TmsBaseStationModel stationModel = BeanUtil.copyProperties(vo, TmsBaseStationModel.class);

        String stationId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_STATION_ID, "L");
        ToolKits.isParamNull(stationId, "站点ID生成失败");

        stationModel.setId(Long.valueOf(stationId));
        stationModel.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        stationModel.setSource(DataDictionaryConstant.SOURCE_ADD);
        stationModel.setEnterpriseId(user.getEnterpriseId());
        stationModel.setEnterpriseName(user.getEnterpriseName());
        stationModel.setCreateperson(user.getNickname());
        stationModel.setCreatetime(date);
        tmsBaseStationMapper.save(stationModel);
        if (ToolKits.isEmpty(vo.getStartingStationDTOList())) {
            return;
        }
        for (StartingStationVO startingStationVO : vo.getStartingStationDTOList()) {
            TmsBaseStartingStationModel startingStationModel = BeanUtil.copyProperties(startingStationVO, TmsBaseStartingStationModel.class);
            String startingStationId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_STARTING_STATION_ID, "L");
            ToolKits.isParamNull(startingStationId, "站点ID生成失败");
            startingStationModel.setId(Long.valueOf(startingStationId));

            startingStationModel.setStationId(stationModel.getId());
            startingStationModel.setStationCode(stationModel.getCode());
            startingStationModel.setStationName(stationModel.getName());
            startingStationModel.setSource(DataDictionaryConstant.SOURCE_ADD);
            startingStationModel.setEnterpriseId(user.getEnterpriseId());
            startingStationModel.setEnterpriseName(user.getEnterpriseName());
            startingStationModel.setCreateperson(user.getNickname());
            startingStationModel.setCreatetime(date);
            tmsBaseStartingStationMapper.save(startingStationModel);
        }
    }

    /**
     * 站点资料修改
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStationData(StationUpdateVO vo) {
        ToolKits.isParamNull(vo, "站点资料查询请求不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (ToolKits.isNotEmpty(vo.getDeliveryOrder())) {
            Criteria criteria = Criteria.forClass(TmsBaseStationModel.class);
            Restriction r1 = Restrictions.ne(TmsBaseStationModel::getId, vo.getId());
            Restriction r2 = Restrictions.eq(TmsBaseStationModel::getDeliveryOrder, vo.getDeliveryOrder());
            Restriction r3 = Restrictions.eq(TmsBaseStationModel::getEnterpriseId, user.getEnterpriseId());
            criteria.setRestriction(Restrictions.and(r1, r2, r3));
            List<TmsBaseStationModel> stationModels = tmsBaseStationMapper.findByCriteria(criteria);
            if (ToolKits.isNotEmpty(stationModels)) {
                throw new PrologException("站点配送顺序已经存在");
            }
        }
        Date date = DateUtil.date();

        TmsBaseStationModel stationModel = tmsBaseStationMapper.findById(vo.getId(), TmsBaseStationModel.class);
        if (ToolKits.isEmpty(stationModel)) {
            throw new PrologException(StrUtil.format("站点{}不存在", vo.getId()));
        }
        stationModel.setType(vo.getType());
        stationModel.setDeliveryOrder(vo.getDeliveryOrder());
        stationModel.setAreas(vo.getAreas());
        stationModel.setAdministrativeAreas(vo.getAdministrativeAreas());
        stationModel.setAdministrativeAreasFather(vo.getAdministrativeAreasFather());
        stationModel.setLevel(vo.getLevel());
        stationModel.setHigherLevelStationId(vo.getHigherLevelStationId());
        stationModel.setHigherLevelStationCode(vo.getHigherLevelStationCode());
        stationModel.setHigherLevelStationName(vo.getHigherLevelStationName());
        stationModel.setMemo(vo.getMemo());

        stationModel.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        stationModel.setEnterpriseId(user.getEnterpriseId());
        stationModel.setEnterpriseName(user.getEnterpriseName());
        stationModel.setUpdateperson(user.getNickname());
        stationModel.setUpdatetime(date);
        tmsBaseStationMapper.update(stationModel);

        if (ToolKits.isEmpty(vo.getStartingStationDTOList())) {
            return;
        }
        // 删除条件
        Criteria startingCriteria = Criteria.forClass(TmsBaseStartingStationModel.class);
        Restriction r4 = Restrictions.eq(TmsBaseStartingStationModel::getStationId, vo.getId());
        startingCriteria.setRestriction(Restrictions.and(r4));
        tmsBaseStartingStationMapper.deleteByCriteria(startingCriteria);

        for (StartingStationVO startingStationVO : vo.getStartingStationDTOList()) {
            TmsBaseStartingStationModel startingStationModel = BeanUtil.copyProperties(startingStationVO, TmsBaseStartingStationModel.class);
            String startingStationId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_STARTING_STATION_ID, "L");
            ToolKits.isParamNull(startingStationId, "站点ID生成失败");
            startingStationModel.setId(Long.valueOf(startingStationId));

            startingStationModel.setStationId(stationModel.getId());
            startingStationModel.setStationCode(stationModel.getCode());
            startingStationModel.setStationName(stationModel.getName());
            startingStationModel.setSource(DataDictionaryConstant.SOURCE_ADD);
            startingStationModel.setEnterpriseId(user.getEnterpriseId());
            startingStationModel.setEnterpriseName(user.getEnterpriseName());
            startingStationModel.setCreateperson(user.getNickname());
            startingStationModel.setCreatetime(date);
            tmsBaseStartingStationMapper.save(startingStationModel);
        }
    }

    /**
     * 停用
     *
     * @param ids
     */
    @Override
    public void disableData(List<Long> ids) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "没有操作权限！");

        // 当前时间
        Date date = DateUtils.nowDate();

        // 封装修改参数
        Map<String, Object> mapUpdate = Maps.newHashMap();
        mapUpdate.put("status", DataDictionaryConstant.GENERAL_STATUS_STOP);
        mapUpdate.put("updateperson", user.getNickname());
        mapUpdate.put("updatetime", date);

        Criteria criteria = Criteria.forClass(TmsBaseStationModel.class);
        Restriction r1 = Restrictions.in(TmsBaseStationModel::getId, ids.stream().toArray());
        Restriction r2 = Restrictions.eq(TmsBaseStationModel::getEnterpriseId, user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2));

        tmsBaseStationMapper.updateMapByCriteria(mapUpdate, criteria);
    }

    /**
     * 启用
     *
     * @param ids
     */
    @Override
    public void activeData(List<Long> ids) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "没有操作权限！");
        // 当前时间
        Date date = DateUtils.nowDate();
        // 封装修改参数
        Map<String, Object> mapUpdate = Maps.newHashMap();
        mapUpdate.put("status", DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        mapUpdate.put("updateperson", user.getNickname());
        mapUpdate.put("updatetime", date);

        Criteria criteria = Criteria.forClass(TmsBaseStationModel.class);
        Restriction r1 = Restrictions.in(TmsBaseStationModel::getId, ids.stream().toArray());
        Restriction r2 = Restrictions.eq(TmsBaseStationModel::getEnterpriseId, user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2));
        tmsBaseStationMapper.updateMapByCriteria(mapUpdate, criteria);
    }

    /**
     * 站点距离查询
     *
     * @param vos
     */
    @Override
    public List<StationDistanceDTO> queryStationDistanceData(List<StationDistanceVO> vos) {
        log.info("站点{}距离查询开始",vos);
        ToolKits.isParamNull(vos, "站点距离查询请求不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");
        List<StationDistanceDTO> distanceDTOS = Lists.newArrayList();
        for (StationDistanceVO vo : vos) {
            // 始发站点信息
            Criteria criteria = Criteria.forClass(TmsBaseStartingStationModel.class);
            Restriction r1 = Restrictions.eq(TmsBaseStartingStationModel::getStationName, vo.getStationName());
            Restriction r2 = Restrictions.eq(TmsBaseStartingStationModel::getStartingStationName, vo.getStartingStationName());
            Restriction r3 = Restrictions.eq(TmsBaseStartingStationModel::getEnterpriseId, user.getEnterpriseId());
            criteria.setRestriction(Restrictions.and(r1, r2,r3));
            List<TmsBaseStartingStationModel> startingStationModels = tmsBaseStartingStationMapper.findByCriteria(criteria);

            if (ToolKits.isEmpty(startingStationModels)) {
                continue;
            }
            StationDistanceDTO distanceDTO = new StationDistanceDTO();
            distanceDTO.setStationName(vo.getStationName());
            distanceDTO.setStartingStationName(vo.getStartingStationName());
            distanceDTO.setDistance(startingStationModels.get(0).getDistance());
            distanceDTOS.add(distanceDTO);
        }
        log.info("站点距离查询完成,结果:{}",distanceDTOS);
        return distanceDTOS;
    }
}
