BerthManageServiceImpl.java 15 KB
package com.zteits.park.biz;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.apache.commons.collections4.CollectionUtils;
import com.alibaba.fastjson.JSON;

import com.clouds.common.cache.park.ParkingLotGeoCacheUtil;
import com.clouds.common.utils.BeanValidatorsUtils;
import com.clouds.common.utils.ListCopyUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Lists;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.zteits.clouds.api.apibase.bean.BizResult;
import com.zteits.clouds.api.apibase.bean.PageBean;
import com.zteits.clouds.api.apibase.constants.ErrorType;
import com.zteits.clouds.api.apibase.constants.LonLatTypeEnum;
import com.zteits.clouds.api.apibase.constants.PDAErrorEnum;
import com.zteits.clouds.api.apibase.exception.BizException;
import com.zteits.clouds.api.dto.order.parkorder.param.ParkOrderUpdateCarNumberReq;
import com.zteits.clouds.api.dto.park.dto.BerthsDTO;
import com.zteits.clouds.api.dto.park.dto.ParkOccupancyRateDTO;
import com.zteits.clouds.api.dto.park.dto.ParkingLotDTO;
import com.zteits.clouds.api.dto.park.param.BatchSaveBerthInfoRequest;
import com.zteits.clouds.api.dto.park.param.ChangeBerthAreaRequest;
import com.zteits.clouds.api.dto.park.param.CheckUniqueForBerthNoRequest;
import com.zteits.clouds.api.dto.park.param.DeleteBerthsRequest;
import com.zteits.clouds.api.dto.park.param.QueryBerthForPageRequest;
import com.zteits.clouds.api.dto.park.param.QueryParkLotInfoByPkNoRequest;
import com.zteits.clouds.api.dto.park.param.SaveUpdateBerthInfoRequest;
import com.zteits.clouds.api.service.park.BerthManageService;
import com.zteits.clouds.api.service.park.ParkingLotQueryService;
import com.zteits.park.biz.business.ParkLotQueryBizService;
import com.zteits.park.constant.ParkConstant;
import com.zteits.park.dao.BerthsDao;
import com.zteits.park.dao.ParkInOutRecordDao;
import com.zteits.park.dao.ParkingLotGeoDao;
import com.zteits.park.dao.TpPRegionAreaDao;
import com.zteits.park.domain.ParkingLotGeo;
import com.zteits.park.domain.TpPBerths;
import com.zteits.park.domain.TpPBerthsDO;
import com.zteits.park.domain.TpPRegionArea;
import com.zteits.park.pagepager.PageBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;

@Service("com.zteits.park.biz.BerthManageServiceImpl")
public class BerthManageServiceImpl implements BerthManageService {
    private static final Logger logger = LoggerFactory.getLogger(BerthManageServiceImpl.class);

    @Autowired
    private BerthsDao berthsDao;
    @Autowired
    private ParkingLotQueryService parkingLotQueryService;
    @Autowired
    private ParkingLotGeoDao parkingLotGeoDao;
    @Autowired
    private TpPRegionAreaDao tpPRegionAreaDao;
    @Autowired
    private ParkLotQueryBizService parkLotQueryBizService;

    @Autowired
    private ParkInOutRecordDao parkInOutRecordDao;

    @Override
    public BizResult<PageBean<BerthsDTO>> queryBerthByBerth(QueryBerthForPageRequest request) {
        logger.info("通过车位对象分页查询车位信息入参:" + JSON.toJSONString(request));
        if (null == request) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象不能为空");
        }

        PageInfo<TpPBerthsDO> pageInfo = berthsDao.queryBerthByBerth(request, request.getBaseRequest().getPageNum(),
            request.getBaseRequest().getPageSize());
        PageBean<BerthsDTO> pageBean = new PageBean<BerthsDTO>();
        PageBeanUtil.copyProperties(pageInfo, pageBean, BerthsDTO.class);

        if (pageBean == null || CollectionUtil.isEmpty(pageBean.getDataList())) {
            return new BizResult<PageBean<BerthsDTO>>(pageBean);
        }

        List<String> plNos = pageBean.getDataList().stream().map(item -> item.getPlNo()).collect(Collectors.toList());
        List<ParkingLotDTO> parkingLotDTOs = parkLotQueryBizService.QueryParkingLotByPkNos(plNos);
        if (CollectionUtils.isEmpty(parkingLotDTOs)) {
            return new BizResult<PageBean<BerthsDTO>>(pageBean);
        }
        //经纬度查询标志 null或true-查询; false-不查询
        if (request.getLonAndLatFlag() != null && !request.getLonAndLatFlag()) {
            for (ParkingLotDTO parkingLotDTO : parkingLotDTOs) {
                String plName = parkingLotDTO.getPlName();
                for (BerthsDTO berthsDTO : pageBean.getDataList()) {
                    if ((parkingLotDTO.getPlNo()).equals(berthsDTO.getPlNo())) {
                        berthsDTO.setPlName(plName);
                    }
                }

            }
            return new BizResult<PageBean<BerthsDTO>>(pageBean);
        } else {
            //因为车位目前没有经纬度,故先放置停车场经纬度
            for (ParkingLotDTO parkingLotDTO : parkingLotDTOs) {
                String plName = parkingLotDTO.getPlName();
                List<Point> points = ParkingLotGeoCacheUtil.queryPointByPlNo(LonLatTypeEnum.LON_LAT_TYPE_1,
                    parkingLotDTO.getPlNo());
                BigDecimal latId = new BigDecimal(0);
                BigDecimal lonId = new BigDecimal(0);
                if (CollectionUtil.isNotEmpty(points) && points.get(0) != null) {
                    lonId = new BigDecimal(points.get(0).getX());
                    latId = new BigDecimal(points.get(0).getY());
                }
                for (BerthsDTO berthsDTO : pageBean.getDataList()) {
                    if ((parkingLotDTO.getPlNo()).equals(berthsDTO.getPlNo())) {
                        berthsDTO.setPlName(plName);
                        berthsDTO.setLonId(lonId);
                        berthsDTO.setLatId(latId);
                    }
                }

            }
        }

        //单个的停车场
        //		String plNo = pageBean.getDataList().stream().findFirst().get().getPlNo();
        //		ParkingLotDTO parkingLotDTO = parkLotQueryBizService.QueryParkingLotByPkNo(plNo);
        //		if(parkingLotDTO==null){
        //			return new BizResult<PageBean<BerthsDTO>>(pageBean);
        //		}
        //		String plName=parkingLotDTO.getPlName();
        //		List<Point> points = ParkingLotGeoCacheUtil.queryPointByPlNo(LonLatTypeEnum.LON_LAT_TYPE_1, plNo);
        //		BigDecimal latId=new BigDecimal(0);
        //		BigDecimal lonId=new BigDecimal(0);
        //		if(CollectionUtil.isNotEmpty(points) && points.get(0)!=null){
        //			lonId = new BigDecimal(points.get(0).getX());
        //			latId = new BigDecimal(points.get(0).getY());
        //		}
        //		for (BerthsDTO berthsDTO : pageBean.getDataList()) {
        //			berthsDTO.setPlName(plName);
        //			berthsDTO.setLonId(lonId);
        //			berthsDTO.setLatId(latId);
        //		}
        return new BizResult<PageBean<BerthsDTO>>(pageBean);
    }

    @Override
    public BizResult<List<ParkOccupancyRateDTO>> queryParkOccupancyRate(QueryBerthForPageRequest request) {
        String plName = "";
        BigDecimal latId = new BigDecimal(0);
        BigDecimal lonId = new BigDecimal(0);
        Map<String, Object> map = new HashMap<String, Object>();
        List<ParkOccupancyRateDTO> list = berthsDao.queryParkOccupancyRate(request.getPlNo(),
            request.getParkAreaCode());
        if (CollectionUtils.isEmpty(list)) {
            return new BizResult<List<ParkOccupancyRateDTO>>(list);
        }
        for (ParkOccupancyRateDTO dto : list) {
            String plNo = dto.getPlNo();
            if (!map.containsKey(plNo)) {
                map.put(dto.getPlNo(), dto);
                QueryParkLotInfoByPkNoRequest pkRequest = new QueryParkLotInfoByPkNoRequest();
                pkRequest.setSysCode(request.getSysCode());
                pkRequest.setPklNo(dto.getPlNo());
                BizResult<ParkingLotDTO> result = parkingLotQueryService.QueryParkingLotByPkNo(pkRequest);
                if (null != result.getData()) {
                    plName = result.getData().getPlName();
                }

                List<String> pklNos = Lists.newArrayList();
                pklNos.add(dto.getPlNo());
                Map<String, List<ParkingLotGeo>> geolist = parkingLotGeoDao.queryParkingLotGeoListByParkNos(pklNos,
                    ParkConstant.ParkingLotGeo.GeoType.TYPE_PARKINGLOT, LonLatTypeEnum.LON_LAT_TYPE_1.value());
                if (geolist != null && CollectionUtils.isNotEmpty(geolist.get(dto.getPlNo()))) {
                    if (geolist.get(dto.getPlNo()).size() > 0) {
                        latId = geolist.get(dto.getPlNo()).get(0).getLatId();
                        lonId = geolist.get(dto.getPlNo()).get(0).getLonId();
                    }
                }
            }

            List<String> parkAreaCodes = Lists.newArrayList();
            parkAreaCodes.add(StringUtil.isEmpty(dto.getParkAreaCode()) ? "" : dto.getParkAreaCode());
            Map<String, TpPRegionArea> map2 = tpPRegionAreaDao.queryTpPRegionAreaByParkAreaCodes(parkAreaCodes);
            TpPRegionArea tpPRegionArea = map2.get(dto.getParkAreaCode());
            if (null != tpPRegionArea) {
                dto.setParkAreaName(tpPRegionArea.getParkAreaName());
            }
            dto.setLatId(latId);
            dto.setLonId(lonId);
            dto.setPlName(plName);
        }
        return new BizResult<List<ParkOccupancyRateDTO>>(list);
    }

    @Override
    public BizResult<Boolean> updateBerthById(SaveUpdateBerthInfoRequest request) {
        logger.info("修改停车场泊位信息入参:" + JSON.toJSONString(request));
        if (request.getId() == null) {
            throw new BizException(ErrorType.PARAMM_NULL, "泊位ID");
        }
        TpPBerths tpPBerths = new TpPBerths();
        BeanUtils.copyProperties(request, tpPBerths);
        Boolean result = berthsDao.updateBerthInfoById(tpPBerths);
        return new BizResult<Boolean>(result);
    }

    @Override
    public BizResult<Boolean> updateBerthBusiInfoStatus(DeleteBerthsRequest request) {
        logger.info("修改停车场泊位信息入参:" + JSON.toJSONString(request));
        if (CollectionUtils.isEmpty(request.getIds())) {
            throw new BizException(ErrorType.PARAMM_NULL, "泊位ID");
        }
        Boolean result = berthsDao.updateBerthBusiInfoStatus(request);
        return new BizResult<Boolean>(result);


    }

    @Override
    public BizResult<Boolean> batDeleteBerths(DeleteBerthsRequest request) {
        logger.info("删除停车场泊位信息入参:" + JSON.toJSONString(request));
        BeanValidatorsUtils.validateWithException(request);
        Boolean result = berthsDao.batDeleteBerthsById(request.getIds(), request.getOpId(), request.getPlNo());
        return new BizResult<Boolean>(result);
    }

    @Override
    public BizResult<Boolean> saveBerthInfo(SaveUpdateBerthInfoRequest request) {
        logger.info("begin保存停车场泊位信息入参:" + JSON.toJSONString(request));
        if (StringUtil.isEmpty(request.getBerthNo())) {
            logger.info("泊位编号为空!");
            throw new BizException(ErrorType.PARAMM_NULL, "泊位编号");
        }
        if (StringUtil.isEmpty(request.getPlNo())) {
            logger.info("停车场编号为空!");
            throw new BizException(ErrorType.PARAMM_NULL, "停车场编号");
        }
        TpPBerths berths = new TpPBerths();
        BeanUtils.copyProperties(request, berths);
        berths.setCreateTime(new Date());
        /**新增泊位信息表.*/
        Integer result = berthsDao.insertBerthInfo(berths);
        logger.info("begin保存停车场泊位信息,结果={}", result > 0);
        return new BizResult<Boolean>(result > 0);
    }

    @Override
    public BizResult<List<BerthsDTO>> queryBerthsByPlNo(QueryParkLotInfoByPkNoRequest queryParkLotInfoByPkNoRequest) {
        List<BerthsDTO> rsList = Lists.newArrayList();
        List<TpPBerths> list = berthsDao.queryBerthsByPlNo(queryParkLotInfoByPkNoRequest.getPklNo());
        if (CollectionUtil.isNotEmpty(list)) {
            for (TpPBerths tpPBerths : list) {
                BerthsDTO dto = new BerthsDTO();
                BeanUtils.copyProperties(tpPBerths, dto);
                rsList.add(dto);
            }
        }
        return new BizResult<List<BerthsDTO>>(rsList);
    }

    @Override
    public BizResult<List<String>> getBerthInfoForNoBindEqpByPlNo(QueryParkLotInfoByPkNoRequest request) {
        logger.info("根据停车场查询泊位信息入参:" + JSON.toJSONString(request));
        String plNo = request.getPklNo();
        //根据停车场查询未绑定地磁的泊位编号
        List<TpPBerths> berthList = berthsDao.getBerthNoBindGeoByPlNo(plNo);
        List<String> berthNoList = Optional.ofNullable(berthList).orElse(new ArrayList<>()).stream().map(
            berth -> berth.getBerthNo()).collect(Collectors.toList());
        return new BizResult<List<String>>(berthNoList);
    }

    /**
     * 校验泊位编码的唯一性.<br/>
     *
     * @param request
     * @return 2018年1月5日  wangfs.<br/>
     */
    @Override
    public BizResult<Boolean> checkUniqueForBerthNo(CheckUniqueForBerthNoRequest request) {
        BeanValidatorsUtils.validateWithException(request);
        int flg = berthsDao.checkUniqueForBerthNo(request);
        return new BizResult<Boolean>(flg > 0);
    }

    @Override
    public BizResult<Boolean> changeBerthArea(ChangeBerthAreaRequest request) {
        BeanValidatorsUtils.validateWithException(request);
        berthsDao.changeBerthArea(request.getIds(), request.getParkAreaCode(), request.getOpId());
        return new BizResult<Boolean>(true);
    }

    /**
     * 批量保存泊位信息.<br/>
     *
     * @param request
     * @return 2018年1月6日  wangfs.<br/>
     */
    @Override
    public BizResult<Boolean> batchSaveBerthInfo(BatchSaveBerthInfoRequest request) {
        if (request == null || CollectionUtils.isEmpty(request.getListBerthInfo())) {
            logger.info("泊位list为空!");
            throw new BizException(PDAErrorEnum.PARAMM_NULL, "泊位list");
        }

        List<TpPBerths> list = new ArrayList<>();
        ListCopyUtil.listCopyProperties(request.getListBerthInfo(), list, TpPBerths.class);
        /**1.批量保存泊位信息.*/
        int batchInsertCount = berthsDao.batchSaveBerthInfo(list);
        return new BizResult<Boolean>(batchInsertCount > 0);
    }

    @Override
    public BizResult<Boolean> updateCarNumberByBerthNo(ParkOrderUpdateCarNumberReq req) {

        try {
            Integer num = parkInOutRecordDao.updateParkInOutRecordCarNumberByOrderId(req);
        } catch (Exception e) {
            logger.error("更新进出场记录车牌号异常", e);
        }

        try {
            Boolean flag = berthsDao.updateCarNumberByBerthNo(req);
        } catch (Exception e) {
            logger.error("更新泊位车牌号异常", e);
        }



        return new BizResult<>(true);
    }

}