puzhibing
2023-07-18 2386300f1ed591e6c46b7f32539cac7f2fd7d434
cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
@@ -1,10 +1,28 @@
package com.dsh.other.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.other.entity.Site;
import com.dsh.other.entity.SiteBooking;
import com.dsh.other.entity.SiteType;
import com.dsh.other.entity.Store;
import com.dsh.other.mapper.SiteMapper;
import com.dsh.other.model.QuerySiteInfoVo;
import com.dsh.other.model.QuerySiteList;
import com.dsh.other.model.QuerySiteListVo;
import com.dsh.other.model.QuerySiteTimes;
import com.dsh.other.service.ISiteBookingService;
import com.dsh.other.service.ISiteService;
import com.dsh.other.service.ISiteTypeService;
import com.dsh.other.service.StoreService;
import com.dsh.other.util.GeodesyUtil;
import com.dsh.other.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author zhibing.pu
@@ -12,4 +30,112 @@
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements ISiteService {
    @Autowired
    private ISiteTypeService siteTypeService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private ISiteBookingService siteBookingService;
    /**
     * 获取场地列表
     * @param querySiteList
     * @return
     * @throws Exception
     */
    @Override
    public List<QuerySiteListVo> querySiteList(QuerySiteList querySiteList) throws Exception {
        querySiteList.setPageNum((querySiteList.getPageNum() - 1) *querySiteList.getPageSize());
        List<QuerySiteListVo> querySiteListVos = this.baseMapper.querySiteList(querySiteList);
        for (QuerySiteListVo querySiteListVo : querySiteListVos) {
            if(ToolUtil.isEmpty(querySiteList.getLon())){
                querySiteListVo.setDistance(0D);
                continue;
            }
            Map<String, Double> distance = GeodesyUtil.getDistance(querySiteList.getLon() + "," + querySiteList.getLat(), querySiteListVo.getStoreLon() + "," + querySiteListVo.getStoreLat());
            querySiteListVo.setDistance(distance.get("WGS84"));
        }
        return querySiteListVos;
    }
    /**
     * 获取场地详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public QuerySiteInfoVo querySiteInfo(Integer id) throws Exception {
        Site site = this.getById(id);
        SiteType siteType = siteTypeService.getById(site.getSiteTypeId());
        Store store = storeService.getById(site.getStoreId());
        QuerySiteInfoVo querySiteInfoVo = new QuerySiteInfoVo();
        querySiteInfoVo.setId(site.getId());
        querySiteInfoVo.setName(site.getName());
        querySiteInfoVo.setSiteTypeName(siteType.getName());
        querySiteInfoVo.setStoreName(store.getName());
        querySiteInfoVo.setStoreAddress(store.getAddress());
        querySiteInfoVo.setStoreLon(store.getLon());
        querySiteInfoVo.setStoreLat(store.getLat());
        querySiteInfoVo.setStorePhone(store.getPhone());
        querySiteInfoVo.setCashPrice(site.getCashPrice());
        querySiteInfoVo.setPlayPaiCoin(site.getPlayPaiCoin());
        return querySiteInfoVo;
    }
    /**
     * 获取场地预约日期数据
     * @param id
     * @param day
     * @return
     * @throws Exception
     */
    @Override
    public List<QuerySiteTimes> querySiteTimes(Integer id, String day) throws Exception {
        Site site = this.getById(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar s = Calendar.getInstance();
        s.setTime(sdf.parse(day + " " + site.getAppointmentStartTime()));
        Calendar e = Calendar.getInstance();
        e.setTime(sdf.parse(day + " " + site.getAppointmentEndTime()));
        int hour = e.get(Calendar.HOUR_OF_DAY);
        int minute = e.get(Calendar.MINUTE);
        List<QuerySiteTimes> list = new ArrayList<>();
        while (true){
            int s_hour = s.get(Calendar.HOUR_OF_DAY);
            int s_minute = s.get(Calendar.MINUTE);
            String start = s_hour + ":" + s_minute;
            s.set(Calendar.MINUTE, s.get(Calendar.MINUTE) + 30);
            int e_hour = s.get(Calendar.HOUR_OF_DAY);
            int e_minute = s.get(Calendar.MINUTE);
            String end = e_hour + ":" + e_minute;
            QuerySiteTimes querySiteTimes = new QuerySiteTimes();
            querySiteTimes.setTime(start + "-" + end);
            querySiteTimes.setSelectable(1);
            SiteBooking siteBooking = siteBookingService.getOne(new QueryWrapper<SiteBooking>().eq("siteId", id).eq("state", 1)
                    .in("status", Arrays.asList(3, 4, 5)).last(" and DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') <= '" + day + " " + start + "' and DATE_FORMAT(endTime, '%Y-%m-%d %H:%i') >= '" + day + " " + end + "'"));
            if(null != siteBooking){
                querySiteTimes.setSelectable(0);
            }
            list.add(querySiteTimes);
            if(e_hour == hour && minute == e_minute){
                break;
            }
        }
        return list;
    }
}