Your Name
2023-04-23 d526cf637048d71f046667f26e76436e9d489861
service-job/src/main/java/com/java110/job/adapt/Repair/MachineDistributeLeaflets.java
@@ -1,13 +1,18 @@
package com.java110.job.adapt.Repair;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.factory.CommunitySettingFactory;
import com.java110.core.factory.WechatFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.machine.MachinePrinterDto;
import com.java110.dto.owner.OwnerAppUserDto;
import com.java110.dto.printerRule.PrinterRuleDto;
import com.java110.dto.printerRule.PrinterRuleMachineDto;
import com.java110.dto.printerRule.PrinterRuleRepairDto;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairUserDto;
import com.java110.dto.smallWeChat.SmallWeChatDto;
@@ -18,7 +23,7 @@
import com.java110.entity.wechat.Content;
import com.java110.entity.wechat.Data;
import com.java110.entity.wechat.PropertyFeeTemplateMessage;
import com.java110.intf.common.IFileRelInnerServiceSMO;
import com.java110.intf.common.*;
import com.java110.intf.community.ICommunityInnerServiceSMO;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairUserInnerServiceSMO;
@@ -28,7 +33,14 @@
import com.java110.intf.user.IStaffAppAuthInnerServiceSMO;
import com.java110.intf.user.IUserInnerServiceSMO;
import com.java110.job.adapt.DatabusAdaptImpl;
import com.java110.job.printer.IPrinter;
import com.java110.po.owner.RepairUserPo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.cache.UrlCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ImageUtils;
import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
@@ -82,23 +94,58 @@
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    @Autowired
    private IPrinterRuleRepairV1InnerServiceSMO printerRuleRepairV1InnerServiceSMOImpl;
    @Autowired
    private IPrinterRuleV1InnerServiceSMO printerRuleV1InnerServiceSMOImpl;
    @Autowired
    private IPrinterRuleMachineV1InnerServiceSMO printerRuleMachineV1InnerServiceSMOImpl;
    @Autowired
    private IMachinePrinterV1InnerServiceSMO machinePrinterV1InnerServiceSMOImpl;
    //模板信息推送地址
    private static String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
    @Override
    public void execute(Business business, List<Business> businesses) {
        JSONObject data = business.getData();
        JSONArray businessRepairUsers = new JSONArray();
        System.out.println("收到日志:>>>>>>>>>>>>>" + data.toJSONString());
        if (data.containsKey(RepairUserPo.class.getSimpleName())) {
            Object bObj = data.get(RepairUserPo.class.getSimpleName());
            if (bObj instanceof JSONObject) {
                businessRepairUsers.add(bObj);
            } else if (bObj instanceof List) {
                businessRepairUsers = JSONArray.parseArray(JSONObject.toJSONString(bObj));
            } else {
                businessRepairUsers = (JSONArray) bObj;
            }
        } else {
            if (data instanceof JSONObject) {
                businessRepairUsers.add(data);
            }
        }
        for (int bOwnerRepairIndex = 0; bOwnerRepairIndex < businessRepairUsers.size(); bOwnerRepairIndex++) {
            JSONObject businessRepairUser = businessRepairUsers.getJSONObject(bOwnerRepairIndex);
            doDealOwnerRepair(businesses, businessRepairUser);
        }
    }
    private void doDealOwnerRepair(List<Business> businesses, JSONObject businessRepairUser) {
        RepairUserDto repairUserDto = new RepairUserDto();
        repairUserDto.setbId(business.getbId());
        repairUserDto.setStatusCd("0");
        repairUserDto.setRuId(businessRepairUser.getString("ruId"));
        List<RepairUserDto> repairUserDtos = repairUserInnerServiceSMO.queryRepairUsers(repairUserDto);
        //获取员工处理状态(10001 处理中;10002 结单;10003 退单;10004 转单;10005 提交)
        //获取员工处理状态(10001 处理中;10002 结单;10003 退单;10004 转单;10005 提交;10006 已派单;10007 已评价;10008 已回访;10009 待支付;11000 待评价;12000 已支付;12001 暂停)
        String state = repairUserDtos.get(0).getState();
        if (!state.equals("10005")) {
            //获取报修id
            String repairId = repairUserDtos.get(0).getRepairId();
            RepairDto repairDto = new RepairDto();
            repairDto.setRepairId(repairId);
            repairDto.setStatusCd("0");
            List<RepairDto> repairDtos = repairInnerServiceSMO.queryRepairs(repairDto);
            //查询报修状态(1000 未派单;1100 接单;1200 退单;1300 转单;1400 申请支付;1500 支付失败;1700 待评价;1800 电话回访;1900 办理完成;2000 未办理结单)
            String repairState = repairDtos.get(0).getState();
@@ -121,75 +168,133 @@
            CommunityDto communityDto = new CommunityDto();
            communityDto.setCommunityId(communityId);
            List<CommunityDto> communityDtos = communityInnerServiceSMO.queryCommunitys(communityDto);
            //派单方式(100表示抢单;200表示指派;300表示轮训)
            String repairWay = repairDtos.get(0).getRepairWay();
            if (repairState.equals("1100") && repairWay.equals("200")) {
                //获取维修员工的id
                String staffId = repairUserDtos.get(1).getStaffId();
                //获取用户id
                String preStaffId = repairUserDtos.get(0).getPreStaffId();
            if (repairState.equals("1100") && businessRepairUser.getString("state").equals("10006")) { //派单
                JSONObject paramIn = new JSONObject();
                for (Business business : businesses) {
                    String businessTypeCd = business.getBusinessTypeCd();
                    if (!StringUtil.isEmpty(businessTypeCd) && businessTypeCd.equals("130200030001")) {
                        JSONObject data = business.getData();
                        if (!StringUtil.isEmpty(data.getString("state")) && data.getString("state").equals("10001")) {
                            paramIn.put("staffId", data.getString("staffId"));
                            paramIn.put("staffName", data.getString("staffName"));
                        } else if (data.getString("state").equals("10006")) {
                            paramIn.put("preStaffId", data.getString("preStaffId"));
                            paramIn.put("preStaffName", data.getString("preStaffName"));
                        }
                    }
                }
                paramIn.put("repairName", repairName);
                paramIn.put("repairObjName", repairObjName);
                paramIn.put("tel", tel);
                paramIn.put("communityId", communityId);
                paramIn.put("context", context);
                paramIn.put("time", time);
                paramIn.put("staffId", staffId);
                paramIn.put("repairObjId", repairObjId);
                paramIn.put("preStaffId", preStaffId);
                paramIn.put("repairId", repairId);
                //给维修师傅推送信息
                sendMsg(paramIn, communityDtos.get(0));
                //派单成功给业主推送信息
                publishMsg(paramIn, communityDtos.get(0));
                // 自动打印小票
                autoPrintRepair(repairUserDtos.get(0).getRuId(),repairDtos.get(0).getRepairType(),communityDtos.get(0));
                //为企业微信群发消息
                sendMsgToWechatGroup(paramIn, communityDtos.get(0));
            } else if (repairState.equals("1100") && (repairWay.equals("100") || repairWay.equals("300"))) {
                String staffId = "";
                if (repairUserDtos.size() > 1) {
                    staffId = repairUserDtos.get(1).getStaffId();
                } else {
                    //获取维修员工的id
                    staffId = repairUserDtos.get(0).getStaffId();
                }
                //获取用户id
                String preStaffId = repairUserDtos.get(0).getPreStaffId();
            } else if (repairState.equals("1100") && !businessRepairUser.getString("state").equals("10006")) {
                JSONObject paramIn = new JSONObject();
                paramIn.put("staffId", staffId);
                paramIn.put("staffId", businessRepairUser.getString("staffId"));
                paramIn.put("context", context);
                paramIn.put("time", time);
                paramIn.put("repairObjId", repairObjId);
                paramIn.put("preStaffId", preStaffId);
                paramIn.put("preStaffId", businessRepairUser.getString("preStaffId"));
                paramIn.put("repairName", repairName);
                paramIn.put("tel", tel);
                paramIn.put("repairObjName", repairObjName);
                //抢单成功给维修师傅推送信息
                publishMessage(paramIn, communityDtos.get(0));
                //抢单成功给业主推送信息
                publishMsg(paramIn, communityDtos.get(0));
                if (repairUserDtos.size() > 1) {
                    //给维修师傅推送信息
                    sendMsg(paramIn, communityDtos.get(0));
                } else {
                    //抢单成功给维修师傅推送信息
                    publishMessage(paramIn, communityDtos.get(0));
                }
            } else if (repairState.equals("1300")) {   //转单
                //获取维修员工id
                String staffId = repairUserDtos.get(0).getStaffId();
                //获取上级用户姓名
                String preStaffName = repairUserDtos.get(0).getPreStaffName();
                JSONObject paramIn = new JSONObject();
                paramIn.put("repairName", repairName);
                paramIn.put("repairObjName", repairObjName);
                paramIn.put("tel", tel);
                paramIn.put("context", context);
                paramIn.put("time", time);
                paramIn.put("staffId", staffId);
                paramIn.put("preStaffName", preStaffName);
                paramIn.put("staffId", businessRepairUser.getString("staffId"));
                paramIn.put("preStaffName", businessRepairUser.getString("preStaffName"));
                //给维修师傅推送信息
                sendMessage(paramIn, communityDtos.get(0));
            }
        }
    }
    /**
     * // 自动打印小票
     * @param ruId
     * @param repairType
     * @param communityDto
     */
    private void autoPrintRepair(String ruId,String repairType, CommunityDto communityDto) {
        PrinterRuleRepairDto printerRuleRepairDto = new PrinterRuleRepairDto();
        printerRuleRepairDto.setCommunityId(communityDto.getCommunityId());
        printerRuleRepairDto.setRepairType(repairType);
        List<PrinterRuleRepairDto> printerRuleRepairDtos = printerRuleRepairV1InnerServiceSMOImpl.queryPrinterRuleRepairs(printerRuleRepairDto);
        if (printerRuleRepairDtos == null || printerRuleRepairDtos.size() < 1) {
            return;
        }
        PrinterRuleDto printerRuleDto = new PrinterRuleDto();
        printerRuleDto.setRuleId(printerRuleRepairDtos.get(0).getRuleId());
        printerRuleDto.setCommunityId(communityDto.getCommunityId());
        printerRuleDto.setState(PrinterRuleDto.STATE_NORMAL);
        int count = printerRuleV1InnerServiceSMOImpl.queryPrinterRulesCount(printerRuleDto);
        if (count < 1) {
            return;
        }
        PrinterRuleMachineDto printerRuleMachineDto = new PrinterRuleMachineDto();
        printerRuleMachineDto.setCommunityId(communityDto.getCommunityId());
        printerRuleMachineDto.setRuleId(printerRuleRepairDtos.get(0).getRuleId());
        List<PrinterRuleMachineDto> printerRuleMachineDtos = printerRuleMachineV1InnerServiceSMOImpl.queryPrinterRuleMachines(printerRuleMachineDto);
        if (printerRuleMachineDtos == null || printerRuleMachineDtos.size() < 1) {
            return;
        }
        for (PrinterRuleMachineDto tmpPrinterRuleMachineDto : printerRuleMachineDtos) {
            try {
                doPrint(tmpPrinterRuleMachineDto, ruId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void doPrint(PrinterRuleMachineDto tmpPrinterRuleMachineDto, String ruId) {
        MachinePrinterDto machinePrinterDto = new MachinePrinterDto();
        machinePrinterDto.setCommunityId(tmpPrinterRuleMachineDto.getCommunityId());
        machinePrinterDto.setMachineId(tmpPrinterRuleMachineDto.getMachineId());
        List<MachinePrinterDto> machinePrinterDtos = machinePrinterV1InnerServiceSMOImpl.queryMachinePrinters(machinePrinterDto);
        Assert.listOnlyOne(machinePrinterDtos, "云打印机不存在");
        IPrinter printer = ApplicationContextFactory.getBean(machinePrinterDtos.get(0).getImplBean(), IPrinter.class);
        if (printer == null) {
            throw new CmdException("打印机异常,未包含适配器");
        }
        printer.printRepair(ruId, tmpPrinterRuleMachineDto.getCommunityId(), Integer.parseInt(tmpPrinterRuleMachineDto.getQuantity()), machinePrinterDtos.get(0));
    }
    /**
@@ -245,7 +350,7 @@
            data.setRemark(new Content(paramIn.getString("preStaffName") + "转单给您,请及时登录公众号接单确认!"));
            templateMessage.setData(data);
            //获取员工公众号地址
            String wechatUrl = MappingCache.getValue("STAFF_WECHAT_URL");
            String wechatUrl = MappingCache.getValue(MappingConstant.URL_DOMAIN,"STAFF_WECHAT_URL");
            templateMessage.setUrl(wechatUrl);
            logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
            ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, JSON.toJSONString(templateMessage), String.class);
@@ -297,6 +402,7 @@
        //根据 userId 查询到openId
        StaffAppAuthDto staffAppAuthDto = new StaffAppAuthDto();
        staffAppAuthDto.setStaffId(paramIn.getString("staffId"));
        staffAppAuthDto.setStaffName(paramIn.getString("staffName"));
        staffAppAuthDto.setAppType("WECHAT");
        List<StaffAppAuthDto> staffAppAuthDtos = staffAppAuthInnerServiceSMO.queryStaffAppAuths(staffAppAuthDto);
        if (staffAppAuthDtos.size() > 0) {
@@ -313,7 +419,7 @@
            data.setRemark(new Content("请及时登录公众号接单确认!"));
            templateMessage.setData(data);
            //获取员工公众号地址
            String wechatUrl = MappingCache.getValue("STAFF_WECHAT_URL");
            String wechatUrl = MappingCache.getValue(MappingConstant.URL_DOMAIN,"STAFF_WECHAT_URL");
            templateMessage.setUrl(wechatUrl);
            logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
            ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, JSON.toJSONString(templateMessage), String.class);
@@ -371,7 +477,7 @@
            return;
        }
        String imgUrl = MappingCache.getValue("IMG_PATH");
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(paramIn.getString("repairId"));
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
@@ -399,7 +505,7 @@
            image.put("base64", ImageUtils.getBase64ByImgUrl(imageUrl));
            image.put("md5", ImageUtils.getMd5ByImgUrl(imageUrl));
            responseEntity = outRestTemplate.postForEntity(url, rebootParam.toJSONString(), String.class);
            logger.debug("返回信息:"+responseEntity);
            logger.debug("返回信息:" + responseEntity);
        }
    }
@@ -471,10 +577,10 @@
                data.setRemark(new Content("您的报修已受理,请保持电话畅通,以便维修人员及时跟您取得联系!感谢您的使用!"));
                templateMessage.setData(data);
                //获取业主公众号地址
                String wechatUrl = MappingCache.getValue("OWNER_WECHAT_URL");
                if(!StringUtil.isEmpty(wechatUrl) && wechatUrl.contains("?")){
                String wechatUrl = UrlCache.getOwnerUrl();
                if (!StringUtil.isEmpty(wechatUrl) && wechatUrl.contains("?")) {
                    wechatUrl += ("&wAppId=" + weChatDto.getAppId());
                }else{
                } else {
                    wechatUrl += ("?wAppId=" + weChatDto.getAppId());
                }
@@ -531,6 +637,11 @@
        staffAppAuthDto.setStaffId(paramIn.getString("staffId"));
        staffAppAuthDto.setAppType("WECHAT");
        List<StaffAppAuthDto> staffAppAuthDtos = staffAppAuthInnerServiceSMO.queryStaffAppAuths(staffAppAuthDto);
        if(staffAppAuthDtos == null || staffAppAuthDtos.size() < 1){
            logger.error("员工未做员工认证");
            return;
        }
        String openId = staffAppAuthDtos.get(0).getOpenId();
        String url = sendMsgUrl + accessToken;
        Data data = new Data();
@@ -545,7 +656,7 @@
        data.setRemark(new Content("请及时与客户取得联系!"));
        templateMessage.setData(data);
        //获取员工公众号地址
        String wechatUrl = MappingCache.getValue("STAFF_WECHAT_URL");
        String wechatUrl = MappingCache.getValue(MappingConstant.URL_DOMAIN,"STAFF_WECHAT_URL");
        templateMessage.setUrl(wechatUrl);
        logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
        ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, JSON.toJSONString(templateMessage), String.class);