liujie
2023-08-16 db7fa6a91b9534ac90e219b6f554c54c43c83a5a
driver/guns-admin/src/main/java/com/supersavedriving/driver/modular/system/service/impl/DriverServiceImpl.java
@@ -1,5 +1,6 @@
package com.supersavedriving.driver.modular.system.service.impl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
@@ -9,12 +10,13 @@
import com.supersavedriving.driver.core.shiro.ShiroUser;
import com.supersavedriving.driver.core.util.JwtTokenUtil;
import com.supersavedriving.driver.core.util.ToolUtil;
import com.supersavedriving.driver.modular.system.dao.DriverMapper;
import com.supersavedriving.driver.modular.system.model.*;
import com.supersavedriving.driver.modular.system.service.*;
import com.supersavedriving.driver.modular.system.dao.DriverMapper;
import com.supersavedriving.driver.modular.system.util.*;
import com.supersavedriving.driver.modular.system.util.MallBook.model.*;
import com.supersavedriving.driver.modular.system.util.MallBook.util.TrhRequest;
import com.supersavedriving.driver.modular.system.util.*;
import com.supersavedriving.driver.modular.system.util.huawei.OBSUtil;
import com.supersavedriving.driver.modular.system.util.mongodb.model.Location;
import com.supersavedriving.driver.modular.system.util.rongyun.RongYunUtil;
import com.supersavedriving.driver.modular.system.util.weChat.WeChatUtil;
@@ -41,12 +43,16 @@
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
/**
* 司机
@@ -109,6 +115,9 @@
    @Autowired
    private WeChatUtil weChatUtil;
    @Autowired
    private IDriverOnlineTimeService driverOnlineTimeService;
    @Value("${callbackPath}")
    private String callbackPath;
@@ -159,6 +168,10 @@
            }
            driver.setCreateTime(new Date());
            this.insert(driver);
            String s = wechatMiniProgramORCode(driver.getId());
            driver.setReferralCode(s);
            this.updateById(driver);
            //司机邀请注册奖励
            if(null != driver.getInviterId()){
@@ -187,6 +200,16 @@
    }
    //生成小程序二维码
    public String wechatMiniProgramORCode(Integer driverId) throws Exception{
        InputStream release = weChatUtil.getwxacodeunlimit("pages/index/index", "driverId=" + driverId, "release");
        String s = OBSUtil.putObjectToBucket(release, "driver_" + driverId + "_" + UUIDUtil.getNumberRandom(5) + ".jpg");
        return s;
    }
    /**
     * 组装个人信息
     * @param driver
@@ -195,6 +218,7 @@
     */
    public Driver setDriverParamete(Driver driver, DriverRegisterWarpper driverRegisterWarpper) throws Exception{
        driver.setAvatar(driverRegisterWarpper.getAvatar());
        driver.setName(driverRegisterWarpper.getName());
        driver.setPhone(driverRegisterWarpper.getPhone());
        driver.setEmergencyContact(driverRegisterWarpper.getEmergencyContact());
        driver.setEmergencyPhone(driverRegisterWarpper.getEmergencyPhone());
@@ -232,6 +256,9 @@
        driver.setApprovalUserId(null);
        driver.setStatus(1);
        driver.setSource(driverRegisterWarpper.getSource());
        driver.setName(driverRegisterWarpper.getName());
        driver.setIdcard(driverRegisterWarpper.getIdcard());
        driver.setCarAge(driverRegisterWarpper.getCarAge());
        return driver;
    }
@@ -287,6 +314,11 @@
            }
        }
        if(ToolUtil.isEmpty(driver.getReferralCode())){
            String s = wechatMiniProgramORCode(driver.getId());
            driver.setReferralCode(s);
            this.updateById(driver);
        }
        return ResultUtil.success(tokenWarpper);
    }
@@ -451,7 +483,8 @@
    @Override
    public ResultUtil<List<String>> queryDriverPosition(Integer uid) throws Exception {
    public ResultUtil< HashMap<String, Object> > queryDriverPosition(Integer uid) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        DriverWork driverWork = driverWorkService.selectOne(new EntityWrapper<DriverWork>().eq("driverId", uid).eq("status", 1));
        if(null == driverWork){
            return ResultUtil.error("请先上班");
@@ -471,21 +504,44 @@
            // 构造query对象
            Query query = Query.query(Criteria.where("location").withinSphere(circle));
            List<Location> locations = mongoTemplate.find(query, Location.class);
            List<Integer> collect = locations.stream().map(Location::getDriverId).collect(Collectors.toList());
            List<Driver> drivers = this.selectBatchIds(collect);
            for (Driver driver : drivers) {
                String value1 = redisUtil.getValue("DRIVER" + driver.getId());
                if(ToolUtil.isEmpty(value1)){
                    continue;
            locations.forEach(s -> {
                int i=0;
                Driver driver = this.baseMapper.selectById(s.getDriverId());
                i = driver.getServerStatus();
                if(driver.getServerStatus()==2){
                    List<Order> orders = orderService.selectList(new EntityWrapper<Order>().eq("driverId", s.getDriverId()).eq("state", 102));
                    if(orders.size()>0){
                        i = 3;
                    }
                }
                if(driver.getId().compareTo(uid) == 0){
                    continue;
                String value1 = redisUtil.getValue("DRIVER" + s.getDriverId());
                if(s.getDriverId().compareTo(uid) != 0 && ToolUtil.isNotEmpty(value1)){
                    list.add(s.getLocation().getX() + "," + s.getLocation().getY()+","+i+","+driver.getName());
                }
                list.add(value1 + "," + driver.getServerStatus());
            }
            });
        }
        return ResultUtil.success(list);
        List<Order> orders = orderService.selectList(new EntityWrapper<Order>().eq("hallOrder",1).eq("state",101));
        map.put("driver",list);
        ArrayList<String> blue = new ArrayList<>();
        orders.forEach(e-> blue.add(e.getStartLng()+","+e.getStartLat()));
        map.put("orderList",blue);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.HOUR_OF_DAY, -2);
        Date time = calendar.getTime();
        List<Order> ordersOne = orderService.selectList(new EntityWrapper<Order>().eq("state",301).between("createTime",time,new Date()));
        ArrayList<String> red = new ArrayList<>();
        ordersOne.forEach(e-> red.add(e.getStartLng()+","+e.getStartLat()));
        map.put("cancelOrder",red);
        return ResultUtil.success(map);
    }
    /**
@@ -537,13 +593,19 @@
    @Override
    public PromotionWarpper queryPromotionQRCode(Integer uid) throws Exception {
        PromotionWarpper promotionWarpper = new PromotionWarpper();
        promotionWarpper.setUrl("https://chaoshengdaijia.com/share/driverShare/index.html?inviterId=" + uid);
        Driver driver1 = this.selectById(uid);
        if(ToolUtil.isEmpty(driver1.getReferralCode())){
            String s = wechatMiniProgramORCode(driver1.getId());
            driver1.setReferralCode(s);
            this.updateById(driver1);
        }
//        promotionWarpper.setUrl("https://chaoshengdaijia.com/share/driverShare/index.html?inviterId=" + uid);
        promotionWarpper.setUrl(driver1.getReferralCode());
        int user = appUserService.selectCount(new EntityWrapper<AppUser>().eq("inviterType", 2).eq("inviterId", uid).eq("status", 1));
        int driver = this.selectCount(new EntityWrapper<Driver>().eq("inviterType", 2).eq("inviterId", uid).eq("approvalStatus", 2).eq("status", 1));
        promotionWarpper.setTotal(user + driver);
        promotionWarpper.setUser(user);
        promotionWarpper.setDriver(driver);
        Driver driver1 = this.selectById(uid);
        promotionWarpper.setAvatar(driver1.getAvatar());
        promotionWarpper.setName(driver1.getName());
        return promotionWarpper;
@@ -555,20 +617,6 @@
     */
    public void addDurationCredits(Integer driverId) throws Exception {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String value = redisUtil.getValue("ONLINE" + driverId);
        if(ToolUtil.isEmpty(value)){
            redisUtil.setStrValue("ONLINE" + driverId, "0_" + sdf1.format(new Date()) + "_0", 25 * 60 * 60);
            return;
        }
        String today = sdf1.format(new Date());
        String[] s = value.split("_");
        if(!today.equals(s[1])){
            redisUtil.setStrValue("ONLINE" + driverId, "0_" + sdf1.format(new Date()) + "_0", 25 * 60 * 60);
            return;
        }
        Integer second = Integer.valueOf(s[0]) + 5;
        Integer time = Integer.valueOf(s[2]);
        SystemConfig systemConfig = systemConfigService.selectOne(new EntityWrapper<SystemConfig>().eq("type", 4));
        if(null == systemConfig){
            return;
@@ -582,9 +630,37 @@
        long end = sdf.parse(sdf1.format(new Date()) + " " + num2 + ":00").getTime();
        long l = System.currentTimeMillis();
        if(start <= l && end > l){
            Driver driver = this.selectById(driverId);
            String value = redisUtil.getValue("ONLINE" + driverId);
            if(ToolUtil.isEmpty(value)){
                redisUtil.setStrValue("ONLINE" + driverId, "0_" + sdf1.format(new Date()) + "_0", 25 * 60 * 60);
                return;
            }
            String today = sdf1.format(new Date());
            String[] s = value.split("_");
            if(!today.equals(s[1])){
                //修改在线时长记录
                DriverOnlineTime driverOnlineTime = driverOnlineTimeService.selectOne(new EntityWrapper<DriverOnlineTime>().eq("driverId", driverId).eq("DATE_FORMAT(`day`, '%Y-%m-%d')", s[1]));
                if(null != driverOnlineTime){
                    driverOnlineTime.setOnlineTime(new BigInteger(s[0]));
                    driverOnlineTimeService.updateById(driverOnlineTime);
                }else{
                    driverOnlineTime = new DriverOnlineTime();
                    driverOnlineTime.setDriverId(driverId);
                    driverOnlineTime.setDay(sdf1.parse(s[1]));
                    driverOnlineTime.setOnlineTime(new BigInteger(s[0]));
                    driverOnlineTimeService.insert(driverOnlineTime);
                }
                driver.setOnlineTime(new BigInteger(s[0]));
                this.updateById(driver);
                redisUtil.setStrValue("ONLINE" + driverId, "0_" + sdf1.format(new Date()) + "_0", 25 * 60 * 60);
                return;
            }
            Integer second = Integer.valueOf(s[0]) + 5;
            Integer time = Integer.valueOf(s[2]);
            int h = Double.valueOf(second / 3600).intValue();
            if(h > 0 && h > time){
                Driver driver = this.selectById(driverId);
                AccountChangeDetail accountChangeDetail = new AccountChangeDetail();
                accountChangeDetail.setCode(System.currentTimeMillis() + UUIDUtil.getNumberRandom(5));
                accountChangeDetail.setUserType(2);
@@ -595,11 +671,26 @@
                accountChangeDetail.setNewData(driver.getIntegral().doubleValue());
                accountChangeDetail.setExplain("在线时长奖励");
                accountChangeDetailService.saveData(accountChangeDetail);
                driver.setOnlineTime(new BigInteger(s[0]));
                this.updateById(driver);
                time++;
                //修改在线时长记录
                DriverOnlineTime driverOnlineTime = driverOnlineTimeService.selectOne(new EntityWrapper<DriverOnlineTime>().eq("driverId", driverId).eq("DATE_FORMAT(`day`, '%Y-%m-%d')", s[1]));
                if(null != driverOnlineTime){
                    driverOnlineTime.setOnlineTime(new BigInteger(s[0]));
                    driverOnlineTimeService.updateById(driverOnlineTime);
                }else{
                    driverOnlineTime = new DriverOnlineTime();
                    driverOnlineTime.setDriverId(driverId);
                    driverOnlineTime.setDay(sdf1.parse(s[1]));
                    driverOnlineTime.setOnlineTime(new BigInteger(s[0]));
                    driverOnlineTimeService.insert(driverOnlineTime);
                }
            }
            redisUtil.setStrValue("ONLINE" + driverId, second + "_" + s[1] + "_" + time);
        }
        redisUtil.setStrValue("ONLINE" + driverId, second + "_" + s[1] + "_" + time);
    }
@@ -1005,4 +1096,40 @@
        this.updateById(driver);
        return ResultUtil.success();
    }
    @Override
    public ResultUtil<HashMap<String, Object>> queryDriverOrderNum(String time,Integer uid) {
        String sTime = null;
        String eTime =null;
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        EntityWrapper<AppUser> wrapperOne = new EntityWrapper<>();
        if(ToolUtil.isNotEmpty(time)){
            sTime = time.split(" - ")[0]+" 00:00:00";
            eTime = time.split(" - ")[1]+" 23:59:59";
            wrapper.between("orderTakingTime",sTime,eTime);
            wrapperOne.between("createTime",sTime,eTime);
        }
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(101);
        objects.add(201);
        objects.add(301);
        objects.add(401);
        wrapper.eq("driverId",uid);
        wrapper.notIn("state",objects);
        HashMap<String, Object> map = new HashMap<>();
        int orderNum = orderService.selectCount(wrapper);
        map.put("orderNum",orderNum);
        // 有效时长
        Integer okTime =  this.baseMapper.getTime(sTime,eTime,uid);
        map.put("okTime",okTime==null?0:okTime);
        wrapperOne.eq("inviterType",2);
        wrapperOne.eq("inviterId",uid);
        int userNum = appUserService.selectCount(wrapperOne);
        map.put("userNum",userNum);
        return ResultUtil.success(map);
    }
}