SendCrmService.java 4.9 KB
package com.huaheng.api.crm.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.crm.domain.CrmResponse;
import com.huaheng.api.crm.domain.CrmSend;
import com.huaheng.api.crm.domain.CrmSendHeader;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.AjaxResultCrm;
import com.huaheng.framework.web.domain.AjaxResultWCS;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.sn.domain.Sn;
import com.huaheng.pc.config.sn.service.SnService;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.shipment.kuaidiHeader.domain.SendQiwei;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 企微发送消息
 *
 * @author huaheng
 * @date 2022-09-07
 */
@Service
public class SendCrmService {

    public static final String crm_appid = "hh1995";

    @Resource
    private AddressService addressService;
    @Resource
    private SnService snService;

    public String getCrmAddress() {
        Address address = addressService.selectAddressByParam("crm", QuantityConstant.WAREHOUSE_KS);
        if(address==null){
            throw new ServiceException("请先配置企微地址");
        }
        return address.getUrl();
    }
    public String getCrmToken() {
        String url= getCrmAddress();
        url=url+"API/Gettoken";
        String json="appid="+crm_appid;
        String result = OkHttpUtils.bodyget(url,json, QuantityConstant.WAREHOUSE_KS);
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("获取crm登录失败");
        }
        //将result中反斜杠去掉
        String cleanedStr = result.replace("\\", "");
        result = cleanedStr.substring(1, cleanedStr.length() - 1);

        JSONObject obj = JSONObject.parseObject(result);
        String code=obj.getString("status");
        if(StringUtils.isNotEmpty(code)&&!code.equals("200")){
            throw new ServiceException(obj.getString("msg"));
        }
        CrmResponse crmResponse=obj.getObject("response",CrmResponse.class);
//        CrmResponse crmResponse = (CrmResponse) ajaxResult.getData();
        return crmResponse.getToken();
    }

    /**
     * 查询适合的sn表数据,发送给crm
     * @return
     */
    public AjaxResult sendCrm() {
        LambdaQueryWrapper<Sn> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Sn::getSyncCrm,1);
        wrapper.ge(Sn::getLastUpdated, DateUtils.getNowPreDays("yyyy-MM-dd",5));
        wrapper.orderByDesc(Sn::getId);
        List<Sn> list=snService.list(wrapper);
        if(list.size()==0){
            return AjaxResult.success();
        }
        String token=getCrmToken();
        CrmSend crmSend=new CrmSend();
        crmSend.setToken( token);
        for(Sn sn:list){
            if(StringUtils.isEmpty(sn.getMocode())){
                sn.setSyncCrm(0);
                sn.setSyncIot(0);
                snService.updateById(sn);
                continue;
            }
            //查找出通单是否已忽略,已忽略才上床crm
            String referCOde=sn.getReferCode();
            if(StringUtils.isEmpty(referCOde)){
                continue;
            }
            CrmSendHeader crmSendHeader=new CrmSendHeader();
            crmSendHeader.setProID(sn.getMocode());
            crmSendHeader.setSNCode(sn.getCode());
            crmSendHeader.setMaterialID(sn.getMaterialCode());
            crmSendHeader.setEndUser(sn.getFianlCustomerName());
            crmSendHeader.setDealer(sn.getCustomerName());
            crmSend.setHead(crmSendHeader);
            String result=OkHttpUtils.bodypost(getCrmAddress()+"API/CreateEquip", JSON.toJSONString(crmSend), QuantityConstant.WAREHOUSE_KS,"SN传递给CRM");
            if (StringUtils.isEmpty(result)) {
                throw new ServiceException("获取crm登录失败");
            }
            //将result中反斜杠去掉
            String cleanedStr = result.replace("\\r\\n", "");
            cleanedStr = cleanedStr.replace("\\", "");
            result = cleanedStr.substring(1, cleanedStr.length() - 1);
            JSONObject obj = JSONObject.parseObject(result);
            String code=obj.getString("status");
            if(StringUtils.isNotEmpty(code)&&code.equals("200")){
                //更改同步状态
                sn.setSyncCrm(2);
                snService.updateById(sn);
            }
        }
        return AjaxResult.success();
    }
}