package com.bohuikeji.delivery.backstage.controller.admin;

import com.alipay.api.response.AlipayFundAccountQueryResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bohuikeji.delivery.backstage.constants.KeyConst;
import com.bohuikeji.delivery.backstage.constants.WxAppEm;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.entity.vo.CourierAddressVo;
import com.bohuikeji.delivery.backstage.entity.wx.TWxUser;
import com.bohuikeji.delivery.backstage.pay.AliPay;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.annotation.InitLog;
import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.PageEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysChart;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.service.ISysChartService;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import com.bohuikeji.frame.module.admin.utils.YiXunGmsUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 派送员信息
 * @author 10552
 */
@Slf4j
@RestController
@RequestMapping("/courier")
public class CourierController extends ControllerBase {

    @Autowired
    ISysChartService chartService;
    @Autowired
    ITCourierService courierService;
    @Autowired
    ITCourierWithdrawService withdrawService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ITNewsService newsService;
    @Autowired
    ITCourierCardService cardService;
    @Autowired
    ITStoreWalletService storeWalletService;
    @Autowired
    ITCourierWithdrawService courierWithdrawService;
    @Autowired
    IWxUserService wxUserService;
    @Autowired
    RedisUtil redisUtil;



    /**
     * 派送员认证审核通过
     * @param id
     */
    @InitLog(value = "派送员认证审核通过")
    @RequestMapping("/getEditStatus")
    public APIEntity getEditStatus(Integer id){
        TCourier courier=courierService.getById(id);
        courier.setCourierStatus("正常");
        courier.setCourierAuth("认证成功");
        courier.updateById();
        newsService.pushCourierMsg1(courier.getCourierId(),"认证成功通知","派送员：{0}您好，您提交的材料已审核通过。如有问题，可搜索关注微信公众号：同城急送  使用对话框进行咨询。",courier.getCourierName());
        return APIEntity.success();
    }

    /**
     * 根据id获取派送员
     * @return
     */
    @GetMapping("/get/id")
    public APIEntity getById(Integer id){
        return APIEntity.success(courierService.getById(id));
    }

    /**
     * 根据id获取派送员绑定的提现账户
     * @param id 派送员ID
     * @param type 账户类型
     * @return
     */
    @GetMapping("/get/card/id")
    public APIEntity getCardById(Integer id,String type){
        TCourierCard card;
        if("微信".equals(type)){
            card=cardService.getOne(new QueryWrapper<TCourierCard>().eq("courier_id",id).eq("card_bank",type).eq("bank_type","APP").orderByDesc("card_id"),false);
            if(card == null){
                WxAppEm app= WxAppEm.MP_COURIER;
                TWxUser wxUser=wxUserService.getUserByType(id, WxAppEm.WX_USER_TYPE_COURIER,app);
                if (wxUser==null){
                    return card==null?APIEntity.fail("当前配送员未绑定"+type+"账户，无法进行在线结算。"):APIEntity.success(card);
                }
                card = cardService.getOne(new QueryWrapper<TCourierCard>().eq("courier_id",id).eq("card_bank",type).eq("bank_type","WXMP").orderByDesc("card_id"),false);
                if(card == null){
                    TCourier courier = courierService.getOne(new QueryWrapper<TCourier>().eq("courier_id",id));
                    card = new TCourierCard();
                    card.setCreateDate(LocalDateTime.now());
                    card.setCardName(courier.getCourierName());
                    card.setIdNumber(courier.getCourierCard());
                    card.setCourierId(id);
                    card.setCardBank("微信");
                    card.setCardNum(wxUser.getWxUserOpenid());
                    card.setBankType("WXMP");
                    cardService.save(card);
                }
            }
        }else {
            card=cardService.getOne(new QueryWrapper<TCourierCard>().eq("courier_id",id).eq("card_bank",type).orderByDesc("card_id"),false);
        }
        return card==null?APIEntity.fail("当前配送员未绑定"+type+"账户，无法进行在线结算。"):APIEntity.success(card);
    }

    /**
     * 根据名字和工号搜索
     * @return
     */
    @GetMapping("/search/key")
    public APIEntity getSearchByName(String key){
        /*QueryWrapper<TCourier> qu=getFranQueryWrapper();
        qu.eq("courier_auth","认证成功")
                .in("courier_status","正常","")
                .apply(" (courier_number like \""+key+"%\" or courier_name like \""+key+"%\"  or courier_phone like \""+key+"%\")");*/
        log.info("搜索条件:[{}]",key);
        List<TCourier> courierList = null;
        if(StringUtils.isBlank(key)){
            return APIEntity.success(courierList);
        }
        courierList = courierService.list(new QueryWrapper<TCourier>().eq("courier_auth","认证成功").in("courier_status","正常","").apply(" (courier_number like \""+key+"%\" or courier_name like \""+key+"%\"  or courier_phone like \""+key+"%\")"));
        return APIEntity.success(courierList);
    }

    /**
     * 根据名字和工号搜索
     * @return
     */
    @PostMapping("/reject/submit")
    public APIEntity rejectSubmit(Integer courierId,String desc){
        TCourier courier=courierService.getById(courierId);
        courier.setCourierAuth("认证失败");
        courier.setCourierStatus("未审核");
        if(courier.updateById()){
            newsService.pushCourierMsg1(courier.getCourierId(),"认证失败通知","派送员：{0}您好，您提交的材料审核未通过。原因：{1}。"+
                    "如有问题，可搜索关注微信公众号：同城急送  使用对话框进行咨询。",courier.getCourierName(),desc);
            return  APIEntity.success();
        }

        log.info("[{}]-[{}]",courierId,desc);
        return  APIEntity.fail("系统繁忙，请稍后再试！");
        //return APIEntity.fail("抱歉，审核模块正在调试中，请您稍后再使用。");
    }

    /**
     * 切换职位类型
     * @return
     */
    @PostMapping("/type/switch/submit")
    public APIEntity switchSubmit(Integer courierId,String courierType,String desc){
        TCourier courier=courierService.getById(courierId);
        if(orderService.list(new QueryWrapper<TOrder>().eq("courier_id",courierId).notIn("order_status","待接单","已取消","已退款","已完成")).size()>0){
            return  APIEntity.fail("当前骑手有正在进行的订单，无法切换职位类型，请稍后再试！");
        }
        if(StringUtils.isBlank(courierType)){
            return  APIEntity.fail("职位类型不能为空，请选择！");
        }
        if(courierType.equals(courier.getCourierType())){
            return  APIEntity.fail("切换类型选择重复，无法切换职 ！");
        }
        courier.setCourierType(courierType);
        if(courier.updateById()){
            newsService.pushCourierMsg1(courier.getCourierId(),"职位类型切换通知","派送员：{0}您好，您的职位类型已切换。原因：{1}。"+
                    "如有问题，可搜索关注微信公众号：同城急送  使用对话框进行咨询。",courier.getCourierName(),desc);
            return  APIEntity.success("切换成功");
        }

        log.info("骑手id-骑手类型[{}]-[{}]",courierId,courierType);
        return  APIEntity.fail("系统繁忙，请稍后再试！");
    }

    @InitLog("派送员提现处理完成")
    @PostMapping("/withdraw/success")
    public APIEntity withdraw(Integer id,String num,String price,String payWay) throws Exception {
        if(StringUtils.isBlank(price)){
            return APIEntity.fail("请填写实际提现金额");
        }
        TCourierWithdraw withdraw=withdrawService.getById(id);
        TCourierWithdraw tCourierWithdraw = withdrawService.getOne(new QueryWrapper<TCourierWithdraw>().eq("withdraw_type","奖励").eq("withdraw_group",withdraw.getWithdrawGroup()));
        if(tCourierWithdraw != null){
            TStoreWallet eaxmWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","考核钱包").eq("fran_id",tCourierWithdraw.getFranId()));
            TStoreWallet storeWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","门店钱包").eq("fran_id",tCourierWithdraw.getFranId()));
            if(eaxmWallet==null && storeWallet == null){
                log.info("门店没有钱包请及时充值");
                return APIEntity.fail("您好，您的门店钱包余额不足无法发放奖励，请及时充值");
            }
            if(eaxmWallet != null && eaxmWallet.getWalletPrice().compareTo(tCourierWithdraw.getApplyAmount())==-1 && storeWallet != null && storeWallet.getWalletPrice().compareTo(tCourierWithdraw.getApplyAmount())==-1){
                log.info("门店钱包金额不足请及时充值");
                return APIEntity.fail("您好，您的门店钱包余额不足无法发放奖励，请及时充值");
            }
        }
        SysUser loginUser=getLoginUser();
        if(loginUser.getRoleId().equals(7)){
            /*if("已通过".equals(withdraw.getFranExam())){
                return APIEntity.fail("该审核记录您已通过，请不要重复审核");
            }*/
            if("已驳回".equals(withdraw.getFranExam())){
                return APIEntity.fail("该审核记录您已驳回，请不要重复审核");
            }
        }
        if(loginUser.getRoleId().equals(1)){
           /* if("已通过".equals(withdraw.getPlatformExam())){
                return APIEntity.fail("该审核记录您已通过，请不要重复审核");
            }*/
            if("已驳回".equals(withdraw.getFranExam())){
                return APIEntity.fail("该审核记录加盟商已驳回，请进行“驳回”处理");
            }
            if("待审核".equals(withdraw.getFranExam()) && !withdraw.getFranId().equals(loginUser.getFranId()) && Duration.between(withdraw.getCreateDate(),LocalDateTime.now()).toHours()<1){
                return APIEntity.fail("提交时间未超过1小时，暂由加盟商审核");
            }
            /*if("微信在线转账".equals(payWay)){
                return APIEntity.fail("您好，微信在线转账暂未开通");
            }*/
            if("支付宝在线转账".equals(payWay)){
                AlipayFundAccountQueryResponse res= AliPay.queryAccount();
                log.info("支付宝账户余额:[{}]",res.getAvailableAmount());
                if(new BigDecimal(price).compareTo(new BigDecimal(res.getAvailableAmount()))==1){
                    return APIEntity.fail("您好，支付宝账户余额不足，请及时充值");
                }
            }
        }
        String[] orderIds=withdraw.getOrderIds().split(",");
        switch (payWay){
            case "微信在线转账":
                log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@微信结算转账");
                withdrawService.wechatWithdrawal(withdraw,loginUser,num,price,payWay);
                orderService.update(new UpdateWrapper<TOrder>().set("courier_sett_status","已结算").in("order_id",orderIds));
                newsService.pushCourierMsg(id,"提现通知","提现成功通知","您发起的提现申请已处理完成，提现金额：￥{0}，已到达您微信零钱中，交易单号：{1}",withdraw.getRealityAmount(),withdraw.getPayNum());break;
            case "支付宝在线转账":
                log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@支付宝结算转账");
                withdrawService.alipayWithdrawal(withdraw,loginUser,num,price,payWay);
                orderService.update(new UpdateWrapper<TOrder>().set("courier_sett_status","已结算").in("order_id",orderIds));
                newsService.pushCourierMsg(id,"提现通知","提现成功通知","您发起的提现申请已处理完成，提现金额：￥{0}，已到达您支付宝账户中，交易单号：{1}",withdraw.getRealityAmount(),withdraw.getPayNum());break;
            case "银行转账":case "微信转账":case "支付转账":case "其他方式":
                log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@线下结算转账");
                withdrawService.offlineWithdrawal(withdraw,loginUser,num,price,payWay);
                newsService.pushCourierMsg(id,"提现通知","提现成功通知","您发起的提现申请已处理完成，提现金额：￥{0}，交易单号：{1}",withdraw.getRealityAmount(),withdraw.getPayNum());break;
            default:
                return APIEntity.fail("无法识别参数[payWay="+payWay+"]");
        }
        return APIEntity.success(payWay+"成功");
    }

    /*@InitLog("派送员提现处理完成")
    @PostMapping("/withdraw/success")
    public APIEntity withdraw(Integer id,String num,String price,String payWay){
        if(StringUtils.isBlank(price)){
            return APIEntity.fail("请填写实际提现金额");
        }
        TCourierWithdraw withdraw=withdrawService.getById(id);
        TCourier courier=courierService.getById(withdraw.getCourierId());
        String[] orderIds=withdraw.getOrderIds().split(",");
        SysUser loginUser=getLoginUser();
        if ("支付宝在线转账".equals(payWay)){
                if(loginUser.getRoleId().equals(7)){
                    if("已通过".equals(withdraw.getFranExam())){
                        return APIEntity.fail("该审核记录您已通过，请不要重复审核");
                    }
                    if("已驳回".equals(withdraw.getFranExam())){
                        return APIEntity.fail("该审核记录您已驳回，请不要重复审核");
                    }
                    withdraw.setPayCard(num);
                    withdraw.setRealityAmount(withdraw.getApplyAmount());
                    withdraw.setPayWay(payWay);
                    withdraw.setFranExam("已通过");
                    withdraw.setFranAuditUser(loginUser.getUserName());
                }
                if(loginUser.getRoleId().equals(1)){
                    if("已通过".equals(withdraw.getPlatformExam())){
                        return APIEntity.fail("该审核记录您已通过，请不要重复审核");
                    }
                    if("已驳回".equals(withdraw.getFranExam())){
                        return APIEntity.fail("该审核记录加盟商已驳回，请进行“驳回”处理");
                    }
                    if("待审核".equals(withdraw.getFranExam()) && !withdraw.getFranId().equals(loginUser.getFranId()) && Duration.between(withdraw.getCreateDate(),LocalDateTime.now()).toHours()<1){
                        return APIEntity.fail("提交时间未超过1小时，暂由加盟商审核");
                    }
                    AlipayFundAccountQueryResponse res= AliPay.queryAccount();
                    log.info("@@@@@@@支付宝资金余额:{}",new BigDecimal(res.getAvailableAmount()));
                    if(new BigDecimal(price).compareTo(new BigDecimal(res.getAvailableAmount()))==1){
                        return APIEntity.fail("您好，支付宝账户余额不足，请及时充值");
                    }
                    AlipayFundTransUniTransferResponse response= AliPay.transferToAlipay(withdraw.getWithdrawId()+"",price,courier.getCourierName(),num);
                    if(response.isSuccess()){
                        withdraw.setPayNum(response.getOrderId());
                        withdraw.setPayDate(LocalDateTime.parse(response.getTransDate(),StringUtils.dataTimeFormat));
                    }
                    withdraw.setRealityAmount(withdraw.getApplyAmount());
                    withdraw.setPayWay(payWay);
                    withdraw.setWithdrawState("已提现");
                    withdraw.setFranExam("已通过");
                    withdraw.setPlatformExam("已通过");
                    withdraw.setPlatformAuditUser(loginUser.getUserName());
                }
                withdrawService.updateById(withdraw);
                orderService.update(new UpdateWrapper<TOrder>().set("courier_sett_status","已结算").in("order_id",orderIds));
                newsService.pushCourierMsg(id,"提现通知","提现成功通知","您发起的提现申请已处理完成，提现金额：￥{0}，已到达您支付宝账户，交易单号：{1}",withdraw.getRealityAmount(),withdraw.getPayNum());
                return APIEntity.success();
        } else{
            if(orderService.update(new UpdateWrapper<TOrder>().in("order_id",orderIds).set("courier_sett_status","已结算"))){
                if(loginUser.getRoleId().equals(7)){
                    if("已通过".equals(withdraw.getFranExam())){
                        return APIEntity.fail("该审核记录您已通过，请不要重复审核");
                    }
                    if("已驳回".equals(withdraw.getFranExam())){
                        return APIEntity.fail("该审核记录您已驳回，请不要重复审核");
                    }
                    withdraw.setWithdrawState("审核中");
                    withdraw.setFranExam("已通过");
                    withdraw.setFranAuditUser(loginUser.getUserName());
                }
                if(loginUser.getRoleId().equals(1)){
                    if("已通过".equals(withdraw.getPlatformExam())){
                        return APIEntity.fail("该审核记录您已通过，请不要重复审核");
                    }
                    if("已驳回".equals(withdraw.getFranExam())){
                        return APIEntity.fail("该审核记录加盟商已驳回，请进行“驳回”处理");
                    }
                    if("待审核".equals(withdraw.getFranExam()) && !withdraw.getFranId().equals(loginUser.getFranId()) && Duration.between(withdraw.getCreateDate(),LocalDateTime.now()).toHours()<1){
                        return APIEntity.fail("提交时间未超过1小时，暂由加盟商审核");
                    }
                    withdraw.setWithdrawState("已提现");
                    withdraw.setFranExam("已通过");
                    withdraw.setPlatformExam("已通过");
                    withdraw.setPlatformAuditUser(loginUser.getUserName());
                }
                withdraw.setPayWay(payWay);
                withdraw.setPayNum(num);
                withdraw.setPayDate(LocalDateTime.now());
                withdraw.setRealityAmount(withdraw.getApplyAmount());
                withdraw.setPayCard("线下收款");
                if(withdrawService.updateById(withdraw)){
                    newsService.pushCourierMsg(id,"提现通知","提现成功通知","您发起的提现申请已处理完成，提现金额：￥{0}，交易单号：{1}",withdraw.getRealityAmount(),withdraw.getPayNum());
                    return APIEntity.success();
                }
            }
            return APIEntity.fail("系统繁忙");
        }
    }*/

    /**
     * 派送员类型切换申请驳回
     * @return
     */
    @PostMapping("/withdraw/getReject")
    public APIEntity getReject(Integer withdrawId,String desc){
        SysUser loginUser=getLoginUser();
        TCourierWithdraw courierWithdraw=withdrawService.getById(withdrawId);
        TCourier courier = courierService.getById(courierWithdraw.getCourierId());
        String[] orderIds=courierWithdraw.getOrderIds().split(",");
        log.info("当前登陆用户角色:[{}]",loginUser.getRoleId());
        if(loginUser.getRoleId().equals(7)){
            if("已驳回".equals(courierWithdraw.getFranExam())){
                return APIEntity.fail("该审核记录您已驳回，请不要重复审核");
            }
            if("已通过".equals(courierWithdraw.getFranExam())){
                return APIEntity.fail("该审核记录您已通过，请不要重复审核");
            }
            courierWithdraw.setFranExam("已驳回");
            courierWithdraw.setWithdrawExplain(desc);
            courierWithdraw.setFranAuditUser(loginUser.getUserName());
        }
        if(loginUser.getRoleId().equals(1)){
            if("已驳回".equals(courierWithdraw.getPlatformExam())){
                return APIEntity.fail("该审核记录您已驳回，请不要重复审核");
            }
            if("待审核".equals(courierWithdraw.getFranExam()) && !courierWithdraw.getFranId().equals(loginUser.getFranId()) && Duration.between(courierWithdraw.getCreateDate(),LocalDateTime.now()).toHours()<12){
                return APIEntity.fail("提交时间未超过1小时，暂由加盟商审核");
            }
            courierWithdraw.setFranExam("已驳回");
            courierWithdraw.setPlatformExam("已驳回");
            courierWithdraw.setWithdrawState("申请失败");
            courierWithdraw.setWithdrawExplain(desc);
            courierWithdraw.setPlatformAuditUser(loginUser.getUserName());
        }
        if(withdrawService.updateById(courierWithdraw)){
            //订单打赏记录修改
            List<TCourierWithdraw> tCourierWithdraws = courierWithdrawService.list(new QueryWrapper<TCourierWithdraw>().eq("withdraw_group",courierWithdraw.getWithdrawGroup()));
            if(tCourierWithdraws.size()>1){
                courierWithdrawService.updateReject(tCourierWithdraws);
            }
            orderService.update(new UpdateWrapper<TOrder>().set("courier_sett_status","未结算").in("order_id",orderIds));
            newsService.pushCourierMsg1(courier.getCourierId(),"审核驳回通知","派送员：{0}您好，您的提现申请未通过。原因：{1}。"+
                    "如有问题，可搜索关注微信公众号：同城急送  使用对话框进行咨询。",courier.getCourierName(),desc);
            //YiXunGmsUtils.sendTxt(courier.getCourierPhone(),"您的提现申请未通过,原因："+desc);
            return  APIEntity.success();
        }
        return  APIEntity.fail("系统繁忙，请稍后再试！");
    }


    /**
     * 按组查询数据图表
     * @return 返回html代码
     */
    @ApiOperation("按组查询数据数据图表")
    @GetMapping("/chart/get/block")
    public PageEntity getHtmlCourier(Integer courierId, String time){
        List<SysChart> charts=chartService.list(
                new QueryWrapper<SysChart>()
                        .eq("chart_group","courier_plane"));
        String[] dateGroup=StringUtils.getDateGroupByStr(time);
        charts.forEach(chart->{
            chart.full(courierId,dateGroup[0],dateGroup[1]);
        });
        //指定时间段的每日平均派送单量，需要二次计算
        SysChart chart=chartService.getOne(new QueryWrapper<SysChart>().eq("chart_group","courier_plane_agv"),false);
        //填充默认sql结果值
        chart.full(courierId,dateGroup[0],dateGroup[1]);
        //计算时间天数差
        long days=LocalDate.parse(dateGroup[1],StringUtils.dataFormat).toEpochDay()-LocalDate.parse(dateGroup[0],StringUtils.dataFormat).toEpochDay();
        //计算平局值
        float avg=(Long.valueOf(chart.getValue()))*1f/((days+1)*1f);
        //格式化并更新
        chart.setValue(avg<1?"<1":new DecimalFormat("0.0").format(avg));
        charts.add(chart);
        return PageEntity.cards(charts);
    }

    @ApiOperation("查询支付账户余额")
    @GetMapping("/pay/query/account")
    public APIEntity queryPayAccount(){
        SysUser user=getLoginUser();
        if (user.getFranId()==1){
            AlipayFundAccountQueryResponse response=AliPay.queryAccount();
            if (response.isSuccess()){
                return APIEntity.success(response.getAvailableAmount());
            }
            return APIEntity.fail(response.getSubMsg());
        }
        return APIEntity.fail("暂无访问权限！");

    }

    /**
     * 查询骑手位置列表
     * @return
     */
    @GetMapping("/map/get/list")
    public APIEntity queryMapList(TCourier courier){
        //log.info("{}",courier);
        SysUser user=getLoginUser();
        Integer courierId=courier.getCourierId();
        if(courierId!=null){
            //页面初始化
            if(courierId.equals(-1)){
                //平台直接查询全部
                if(user.getFranId().equals(1)){
                    return APIEntity.success(redisUtil.getListByPattern(KeyConst.REDIS_COURIER_ADDRESS+"*", CourierAddressVo.class));
                }
            }else{
                //查询单个骑手
                CourierAddressVo cuv=redisUtil.get(KeyConst.REDIS_COURIER_ADDRESS +courier.getCourierId(), CourierAddressVo.class);
                if (cuv==null){
                    return APIEntity.fail("该骑手近期未上线，暂无位置信息！");
                }
                List<CourierAddressVo> one=new ArrayList<>(1);
                one.add(cuv);
                return APIEntity.success(one);
            }

        }
        //组合条件筛选查询
        QueryWrapper<TCourier> qu=new QueryWrapper<TCourier>();
                //针对平台和加盟商区分查询
                if(user.getFranId().equals(1)){
                    qu.eq(courier.getFranId()!=null,"fran_id",courier.getFranId());
                }
                else{
                    qu.eq("fran_id",user.getFranId());
                }
                qu.eq(StringUtils.isNotBlank(courier.getCourierGender()),"courier_gender",courier.getCourierGender())
                .eq(StringUtils.isNotBlank(courier.getCourierType()),"courier_type",courier.getCourierType())
                .eq(StringUtils.isNotBlank(courier.getCourierPayStatus()),"courier_pay_status",courier.getCourierPayStatus())
                .eq(StringUtils.isNotBlank(courier.getCourierStatus()),"courier_status",courier.getCourierStatus())
                .eq(StringUtils.isNotBlank(courier.getCourierAuth()),"courier_auth",courier.getCourierAuth())
                .eq(StringUtils.isNotBlank(courier.getCourierAuthVehicle()),"courier_auth_vehicle",courier.getCourierAuthVehicle())
                .eq(StringUtils.isNotBlank(courier.getJobStatus()),"job_status",courier.getJobStatus()).apply(" 1=1 limit 200");

        List<TCourier> couriers=courierService.list(qu);
        if(couriers==null||couriers.size()<1){
            return APIEntity.fail("暂时没有符合条件的在线骑手！");
        }
        //对找到的骑手到REDIS查询其位置
        List<CourierAddressVo> list=new ArrayList<>(couriers.size());
        couriers.forEach(c ->{
            CourierAddressVo tmp=redisUtil.get(KeyConst.REDIS_COURIER_ADDRESS+c.getCourierId(), CourierAddressVo.class);
            if(tmp!=null){
                list.add(tmp);
            }
        });
        return APIEntity.success(list);
    }

}
