admin
2020-12-05 145a6efab30d7a0acb76eee6cd512eba93665efe
大淘客编码问题解决,mybatis优化
7个文件已修改
1个文件已添加
1532 ■■■■■ 已修改文件
facade-goods/src/main/java/com/yeshi/goods/facade/utils/taobao/DaTaoKeApiUtil.java 58 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/controller/h5/H5S11Controller.java 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java 1016 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeApiUtil.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/src/main/java/org/yeshi/utils/CMQUtil.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/src/main/java/org/yeshi/utils/mongo/MongoDaoUtil.java 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/src/main/java/org/yeshi/utils/mybatis/ColumnParseUtil.java 112 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/src/test/java/com/yeshi/utils/MyBatisTest.java 305 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
facade-goods/src/main/java/com/yeshi/goods/facade/utils/taobao/DaTaoKeApiUtil.java
@@ -109,6 +109,25 @@
        }
    }
    private static InputStream baseGetForStream(String url, Map<String, String> params) {
        HttpClient client = new HttpClient();
        try {
            Iterator<String> keys = params.keySet().iterator();
            String key;
            for (url = url + "?"; keys.hasNext(); url = url + String.format("%s=%s&", key, URLEncoder.encode((String) params.get(key), "UTF-8"))) {
                key = keys.next();
            }
            GetMethod method = new GetMethod(url);
            client.executeMethod(method);
            InputStream inputStream = method.getResponseBodyAsStream();
            return inputStream;
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }
    private static String request(String url) {
        String result = null;
@@ -625,7 +644,7 @@
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("version", "v1.3.0");
        params.put("appKey", app.getAppKey());
        params.put("rankType", type + "");
        if (cid != null)
@@ -637,17 +656,6 @@
            result = baseGet("https://openapi.dataoke.com/api/goods/get-ranking-list", params);
        } catch (Exception e) {
            result = baseGet("https://openapi.dataoke.com/api/goods/get-ranking-list", params);
        }
        try {
//            System.out.println("GBK:"+new String(result.getBytes("GBK"),"UTF-8"));
//            System.out.println("ISO-8859-1:"+new String(result.getBytes("ISO-8859-1"),"UTF-8"));
//            System.out.println("GB2312:"+new String(result.getBytes("GB2312"),"UTF-8"));
            result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject json = JSONObject.fromObject(result);
@@ -836,4 +844,30 @@
        }
        return daTaoKeGoodsResult;
    }
    /**
     * 解析淘口令
     *
     * @param content
     * @return
     */
    public static Long parseTaoBaoToken(String content) {
        TaoKeAppInfo app = getRandomApp();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.0.0");
        params.put("appKey", app.getAppKey());
        params.put("content", content);
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = baseGet("https://openapi.dataoke.com/api/tb-service/parse-taokouling", params);
        System.out.println(result);
        JSONObject resultObj = JSONObject.fromObject(result);
        if (resultObj.optInt("code") == 0) {
            String goodsId = resultObj.optJSONObject("data").optString("goodsId");
            if (!StringUtil.isNullOrEmpty(goodsId)) {
                return Long.parseLong(goodsId);
            }
        }
        return null;
    }
}
fanli/src/main/java/com/yeshi/fanli/controller/h5/H5S11Controller.java
@@ -40,10 +40,10 @@
    private UserInfoService userInfoService;
    /**
     * 获取超级红包分享口令
     *
     * @param acceptData
     *  获取超级红包分享口令
     * @param sign
     * @param uid
     * @param callback
     * @param out
     */
    @RequestMapping(value = "getSuperHongBaoToken")
@@ -70,7 +70,7 @@
        if (StringUtil.isNullOrEmpty(token)) {
            try {
                TaoKeOfficialActivityConvertResultDTO dto = TaoKeApiUtil.officialActivityConvert("19507100253",
                        "20150318020001156", info.getRelationId());
                        "20150318020002508", info.getRelationId());
                String link = dto.getClick_url();
                if (!StringUtil.isNullOrEmpty(link)) {
@@ -125,7 +125,7 @@
        if (StringUtil.isNullOrEmpty(token)) {
            try {
                TaoKeOfficialActivityConvertResultDTO dto = TaoKeApiUtil.officialActivityConvert("19507100253",
                        "20150318020001158", info.getRelationId());
                        "20150318020002508", info.getRelationId());
                String link = dto.getClick_url();
                if (!StringUtil.isNullOrEmpty(link)) {
                    token = TaoKeApiUtil.getTKToken("http://img.flqapp.com/resource/s11/banner.jpg", "预售商品付定金,得奖金!",
fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java
@@ -11,6 +11,7 @@
import javax.annotation.Resource;
import com.yeshi.goods.facade.utils.taobao.DaTaoKeApiUtil;
import org.springframework.stereotype.Component;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.NumberUtil;
@@ -52,575 +53,586 @@
/**
 * 粘贴板解析
 *
 * @author Administrator
 *
 * @author Administrator
 */
@Component
public class ClipboardAnalysisManager {
    @Resource
    private JDGoodsLinkParseManager jdGoodsLinkParseManager;
    @Resource
    private JDGoodsLinkParseManager jdGoodsLinkParseManager;
    @Resource
    private TokenRecordService tokenRecordService;
    @Resource
    private TokenRecordService tokenRecordService;
    @Resource
    private TaoBaoTokenAuctionIdMapManager taoBaoTokenAuctionIdMapManager;
    @Resource
    private TaoBaoTokenAuctionIdMapManager taoBaoTokenAuctionIdMapManager;
    @Resource
    private RedisManager redisManager;
    @Resource
    private RedisManager redisManager;
    /**
     * @Title: parseContentType @Description: 解析文本的类型 @param text @return Set
     *         <ClipboardContentType> 返回类型 @throws
     */
    public static Set<ClipboardContentType> parseContentType(String originText, String platform, String version) {
        Set<ClipboardContentType> typeSet = new HashSet<>();
        StringBuffer textBuffer = new StringBuffer(originText);
        String text = textBuffer.toString();
        // 自有口令
        String parseToken = TokenUtil.parseToken(text);
        if (!StringUtil.isNullOrEmpty(parseToken)) {
            typeSet.add(ClipboardContentType.systemToken);
            text = text.replace(parseToken, "");
        }
    /**
     * @Title: parseContentType @Description: 解析文本的类型 @param text @return Set
     * <ClipboardContentType> 返回类型 @throws
     */
    public static Set<ClipboardContentType> parseContentType(String originText, String platform, String version) {
        Set<ClipboardContentType> typeSet = new HashSet<>();
        StringBuffer textBuffer = new StringBuffer(originText);
        String text = textBuffer.toString();
        // 自有口令
        String parseToken = TokenUtil.parseToken(text);
        if (!StringUtil.isNullOrEmpty(parseToken)) {
            typeSet.add(ClipboardContentType.systemToken);
            text = text.replace(parseToken, "");
        }
        // 链接
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(text);
        while (matcher.find()) {
            String url = matcher.group();
            if (!StringUtil.isNullOrEmpty(url)) {
                typeSet.add(ClipboardContentType.link);
                text = text.replace(url, "");
            }
        }
        // 链接
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(text);
        while (matcher.find()) {
            String url = matcher.group();
            if (!StringUtil.isNullOrEmpty(url)) {
                typeSet.add(ClipboardContentType.link);
                text = text.replace(url, "");
            }
        }
        // 淘口令
        List<String> taoBaoTokenList = null;
        if (VersionUtil.greaterThan_2_0_7(platform, version))
            taoBaoTokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text);
        else
            taoBaoTokenList = TaoBaoUtil.getTokenListFromText(text);
        if (taoBaoTokenList != null && taoBaoTokenList.size() > 0) {
            typeSet.add(ClipboardContentType.taoBaoToken);
            for (String token : taoBaoTokenList) {
                text = text.replace(token, "");
            }
        }
        // 文本
        if (text != null && text.trim().length() > 0) {
            typeSet.add(ClipboardContentType.text);
        }
        // 淘口令
        List<String> taoBaoTokenList = null;
        if (VersionUtil.greaterThan_2_0_7(platform, version))
            taoBaoTokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text);
        else
            taoBaoTokenList = TaoBaoUtil.getTokenListFromText(text);
        if (taoBaoTokenList != null && taoBaoTokenList.size() > 0) {
            typeSet.add(ClipboardContentType.taoBaoToken);
            for (String token : taoBaoTokenList) {
                text = text.replace(token, "");
            }
        }
        // 文本
        if (text != null && text.trim().length() > 0) {
            typeSet.add(ClipboardContentType.text);
        }
        return typeSet;
    }
        return typeSet;
    }
    /**
     *
     * @Title: parse @Description: 解析 @param text @param typeSet void
     *         返回类型 @throws
     */
    public void parse(String platform, String version, String text, Long uid, IClipboardAnalysisResult resultListener) {
        if (text == null)
            return;
        text = text.trim();
    /**
     * @Title: parse @Description: 解析 @param text @param typeSet void
     * 返回类型 @throws
     */
    public void parse(String platform, String version, String text, Long uid, IClipboardAnalysisResult resultListener) {
        if (text == null)
            return;
        text = text.trim();
        Set<ClipboardContentType> typeSet = parseContentType(text, platform, version);
        Set<ClipboardContentType> typeSet = parseContentType(text, platform, version);
        // 系统口令
        if (typeSet.contains(ClipboardContentType.systemToken)) {
            try {
                TokenVO tokenVO = tokenRecordService.discernToken(text, uid, platform, version);
                resultListener.onResult(tokenVO);
            } catch (TokenRecordException e) {
                if (e.getCode() == 1001) { // 需要用户登录
                    resultListener.needLogin(e.getMsg());
                } else {
                    resultListener.none();
                }
            }
            return;
        }
        // 系统口令
        if (typeSet.contains(ClipboardContentType.systemToken)) {
            try {
                TokenVO tokenVO = tokenRecordService.discernToken(text, uid, platform, version);
                resultListener.onResult(tokenVO);
            } catch (TokenRecordException e) {
                if (e.getCode() == 1001) { // 需要用户登录
                    resultListener.needLogin(e.getMsg());
                } else {
                    resultListener.none();
                }
            }
            return;
        }
        //优先解析淘口令
        Long goodsId = DaTaoKeApiUtil.parseTaoBaoToken(text);
        if (goodsId != null) {
            CommonGoods commonGoods = getTaoBaoGoods(goodsId);
            if (commonGoods != null)
                resultListener.onResult(commonGoods);
            else
                resultListener.onResult(text);
        // 只有链接 淘宝链接/京东链接/拼多多链接
        if (typeSet.size() == 1) {
            if (typeSet.contains(ClipboardContentType.link)) {// 纯链接
                // 解析商品
                CommonGoods goods = parseLink(text);
                if (goods == null) {
                    // 判断链接之中是否包含口令
                    try {
                        String newText = URLDecoder.decode(text, "UTF-8");
                        List<String> tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(newText);
                        if (tokenList != null && tokenList.size() > 0) {
                            try {
                                goods = parseTaoBaoToken(tokenList.get(0));
                            } catch (TaoBaoTokenParseException e) {
                            }
                            if (goods != null && goods.getGoodsId() != null) {
                                resultListener.onResult(goods);
                                return;
                            }
                        }
            return;
        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    resultListener.none();
                } else {
                    resultListener.onResult(goods);
                }
        // 只有链接 淘宝链接/京东链接/拼多多链接
        if (typeSet.size() == 1) {
            if (typeSet.contains(ClipboardContentType.link)) {// 纯链接
                // 解析商品
                CommonGoods goods = parseLink(text);
                if (goods == null) {
                    // 判断链接之中是否包含口令
                    try {
                        String newText = URLDecoder.decode(text, "UTF-8");
                        List<String> tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(newText);
                        if (tokenList != null && tokenList.size() > 0) {
                            try {
                                goods = parseTaoBaoToken(tokenList.get(0));
                            } catch (TaoBaoTokenParseException e) {
                            }
                            if (goods != null && goods.getGoodsId() != null) {
                                resultListener.onResult(goods);
                                return;
                            }
                        }
            } else if (typeSet.contains(ClipboardContentType.taoBaoToken)) {// 纯淘口令
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                try {
                    CommonGoods commonGoods = parseTaoBaoToken(text);
                    if (commonGoods != null)
                        resultListener.onResult(commonGoods);
                    else
                        resultListener.onResult(text);
                    resultListener.none();
                } else {
                    resultListener.onResult(goods);
                }
                } catch (TaoBaoTokenParseException e1) {
                    resultListener.onResult(text);
                }
            } else if (typeSet.contains(ClipboardContentType.taoBaoToken)) {// 纯淘口令
            } else {// 纯文本
                // 文本推荐
                if (text.length() > 256)
                    resultListener.none();
                else
                    resultListener.onResult(text);
            }
        } else if (typeSet.size() == 2) {
            List<String> urlList = HttpUtil.getUrlListFromText(text);
            List<String> tokenList = null;
            if (VersionUtil.greaterThan_2_0_7(platform, version))
                tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text);
            else
                tokenList = TaoBaoUtil.getTokenListFromText(text);
                try {
                    CommonGoods commonGoods = parseTaoBaoToken(text);
                    if (commonGoods != null)
                        resultListener.onResult(commonGoods);
                    else
                        resultListener.onResult(text);
            String link = null;
            String token = null;
                } catch (TaoBaoTokenParseException e1) {
                    resultListener.onResult(text);
                }
            if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.taoBaoToken)) {// 链接+口令
                if (text.indexOf(urlList.get(0)) < text.indexOf(tokenList.get(0)))// 第一个链接在第一个口令前面
                    link = urlList.get(0);
                else// 第一个口令在第一个链接前面
                    token = tokenList.get(0);
            } else if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.text)) {// 链接+文本
                link = urlList.get(0);
            } else {// 纯文本
                // 文本推荐
                if (text.length() > 256)
                    resultListener.none();
                else
                    resultListener.onResult(text);
            }
        } else if (typeSet.size() == 2) {
            List<String> urlList = HttpUtil.getUrlListFromText(text);
            List<String> tokenList = null;
            if (VersionUtil.greaterThan_2_0_7(platform, version))
                tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text);
            else
                tokenList = TaoBaoUtil.getTokenListFromText(text);
            } else {// 口令+文本
                token = tokenList.get(0);
            }
            String link = null;
            String token = null;
            if (!StringUtil.isNullOrEmpty(link)) {
                CommonGoods goods = parseLink(urlList.get(0));
                if (goods == null || goods.getGoodsId() == null) {
                    if (tokenList.size() == 0 && urlList.size() == 2
                            && urlList.get(0).indexOf("://uland.taobao.com") > -1
                            && urlList.get(1).indexOf("?id=") > -1) {// 有2个链接的文案,第一个是店铺券链接,其中二个是淘宝商品详情
                        CommonGoods goods1 = parseLink(urlList.get(1));
                        if (goods1 != null && goods1.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) {
                            resultListener.onResult(goods1);
                        } else {
                            resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0)));
                        }
            if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.taoBaoToken)) {// 链接+口令
                if (text.indexOf(urlList.get(0)) < text.indexOf(tokenList.get(0)))// 第一个链接在第一个口令前面
                    link = urlList.get(0);
                else// 第一个口令在第一个链接前面
                    token = tokenList.get(0);
            } else if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.text)) {// 链接+文本
                link = urlList.get(0);
                    } else if (urlList.size() > 1)
                        resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0)));
                    else {// 单链接
                            // 判断是否包含可转链的链接
                        if (!isContainsLinkConvert(link))
                            resultListener.none();
                        else
                            resultListener.onResult(new GoodsDocParseResultVO(text, link));
                    }
                } else {
                    if (VersionUtil.greaterThan_2_0_7(platform, version)) {
                        if (urlList.size() == 1)// 单链接+文本
                            resultListener.onResult(goods);
                        else
                            resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                    } else {
                        resultListener.onResult(goods);
                    }
                }
            } else {
                CommonGoods goods = null;
                try {
                    goods = parseTaoBaoToken(token);
                } catch (TaoBaoTokenParseException e) {
                    resultListener.onResult(new GoodsDocParseResultVO(text));
                    return;
                }
                if (goods != null && goods.getGoodsId() != null) {
                    if (tokenList.size() == 1) {// 单个口令
                        resultListener.onResult(goods);
                    } else
                        resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                } else {
                    resultListener.onResult(new GoodsDocParseResultVO(text));
                }
            }
            } else {// 口令+文本
                token = tokenList.get(0);
            }
        } else if (typeSet.size() == 3) {// 口令+链接+文本
            // 淘口令
            List<String> tokenList = null;
            if (VersionUtil.greaterThan_2_0_7(platform, version))
                tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text);
            else
                tokenList = TaoBaoUtil.getTokenListFromText(text);
            if (!StringUtil.isNullOrEmpty(link)) {
                CommonGoods goods = parseLink(urlList.get(0));
                if (goods == null || goods.getGoodsId() == null) {
                    if (tokenList.size() == 0 && urlList.size() == 2
                            && urlList.get(0).indexOf("://uland.taobao.com") > -1
                            && urlList.get(1).indexOf("?id=") > -1) {// 有2个链接的文案,第一个是店铺券链接,其中二个是淘宝商品详情
                        CommonGoods goods1 = parseLink(urlList.get(1));
                        if (goods1 != null && goods1.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) {
                            resultListener.onResult(goods1);
                        } else {
                            resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0)));
                        }
            List<String> urlList = HttpUtil.getUrlListFromText(text);
            if (text.contains("【") && tokenList != null && tokenList.size() == 1 && urlList != null
                    && urlList.size() == 1) {// Android淘宝链接复制
                CommonGoods goods = null;
                try {
                    goods = parseTaoBaoToken(tokenList.get(0));
                } catch (TaoBaoTokenParseException e1) {
                    e1.printStackTrace();
                }
                if (goods == null) {
                    if (text.trim().indexOf("【") > -1 && text.trim().indexOf("】") > 0) {
                        // 截取标题
                        String title = text.trim().substring(text.trim().indexOf("【") + 1,
                                text.trim().lastIndexOf("】"));
                        goods = new CommonGoods();
                        goods.setTitle(title);
                        goods.setPicture("http://");
                    }
                }
                    } else if (urlList.size() > 1)
                        resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0)));
                    else {// 单链接
                        // 判断是否包含可转链的链接
                        if (!isContainsLinkConvert(link))
                            resultListener.none();
                        else
                            resultListener.onResult(new GoodsDocParseResultVO(text, link));
                    }
                } else {
                    if (VersionUtil.greaterThan_2_0_7(platform, version)) {
                        if (urlList.size() == 1)// 单链接+文本
                            resultListener.onResult(goods);
                        else
                            resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                    } else {
                        resultListener.onResult(goods);
                    }
                }
            } else {
                CommonGoods goods = null;
                try {
                    goods = parseTaoBaoToken(token);
                } catch (TaoBaoTokenParseException e) {
                    resultListener.onResult(new GoodsDocParseResultVO(text));
                    return;
                }
                if (goods != null && goods.getGoodsId() != null) {
                    if (tokenList.size() == 1) {// 单个口令
                        resultListener.onResult(goods);
                    } else
                        resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                } else {
                    resultListener.onResult(new GoodsDocParseResultVO(text));
                }
            }
                if (goods != null) {
                    resultListener.onResult(goods);
                }
        } else if (typeSet.size() == 3) {// 口令+链接+文本
            // 淘口令
            List<String> tokenList = null;
            if (VersionUtil.greaterThan_2_0_7(platform, version))
                tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text);
            else
                tokenList = TaoBaoUtil.getTokenListFromText(text);
            } else {// 文案
            List<String> urlList = HttpUtil.getUrlListFromText(text);
            if (text.contains("【") && tokenList != null && tokenList.size() == 1 && urlList != null
                    && urlList.size() == 1) {// Android淘宝链接复制
                CommonGoods goods = null;
                try {
                    goods = parseTaoBaoToken(tokenList.get(0));
                } catch (TaoBaoTokenParseException e1) {
                    e1.printStackTrace();
                }
                if (goods == null) {
                    if (text.trim().indexOf("【") > -1 && text.trim().indexOf("】") > 0) {
                        // 截取标题
                        String title = text.trim().substring(text.trim().indexOf("【") + 1,
                                text.trim().lastIndexOf("】"));
                        goods = new CommonGoods();
                        goods.setTitle(title);
                        goods.setPicture("http://");
                    }
                }
                String token = null;
                token = tokenList.get(0);
                if (goods != null) {
                    resultListener.onResult(goods);
                }
                if (!StringUtil.isNullOrEmpty(token)) {
                    CommonGoods goods = null;
                    try {
                        goods = parseTaoBaoToken(token);
                        if (goods != null && goods.getGoodsId() != null) {
                            // 如:http://rd.wechat.com/qrcode/confirm?block_type=101&content=%E3%80%90%E5%8E%9F%E4%BB%B7%E3%80%9120%E5%85%83%0A%E3%80%90%E5%88%B8%E5%90%8E%E4%BB%B7%E3%80%9120%E5%85%83%0A-----------------%0A%E5%BE%A9%E8%A3%BD%E8%BF%99%E6%9D%A1%EF%BC%8C(psNX1UPRJNe)%EF%BC%8C%E8%BF%9B%E5%85%A5%E3%80%90Tao%E5%AE%9D%E3%80%91%E5%8D%B3%E5%8F%AF%E6%8A%A2%E8%B4%AD&lang=zh_CN&scene=37
                            if (text.contains("//rd.wechat.com/qrcode/")) {// 微信复制过来的
                                resultListener.onResult(goods);
                            } else
                                resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                        } else {
                            resultListener.onResult(new GoodsDocParseResultVO(text));
                        }
                    } catch (TaoBaoTokenParseException e) {
                        resultListener.onResult(new GoodsDocParseResultVO(text));
                    }
                } else {
                    CommonGoods goods = parseLink(urlList.get(0));
                    if (goods == null || goods.getGoodsId() == null) {
                        resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0)));
                    } else {
                        resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                    }
                }
            }
            return;
        }
    }
            } else {// 文案
    /**
     *
     * @Title: parseLink @Description: 解析单链接 @param link @return CommonGoods
     *         返回类型 @throws
     */
    public CommonGoods parseLink(String link) {
                String token = null;
                token = tokenList.get(0);
        SimpleCommonGoods simpleGoods = parseGoodsIdFromLink(link);
        if (simpleGoods == null) {
            return null;
        }
                if (!StringUtil.isNullOrEmpty(token)) {
                    CommonGoods goods = null;
                    try {
                        goods = parseTaoBaoToken(token);
                        if (goods != null && goods.getGoodsId() != null) {
                            // 如:http://rd.wechat.com/qrcode/confirm?block_type=101&content=%E3%80%90%E5%8E%9F%E4%BB%B7%E3%80%9120%E5%85%83%0A%E3%80%90%E5%88%B8%E5%90%8E%E4%BB%B7%E3%80%9120%E5%85%83%0A-----------------%0A%E5%BE%A9%E8%A3%BD%E8%BF%99%E6%9D%A1%EF%BC%8C(psNX1UPRJNe)%EF%BC%8C%E8%BF%9B%E5%85%A5%E3%80%90Tao%E5%AE%9D%E3%80%91%E5%8D%B3%E5%8F%AF%E6%8A%A2%E8%B4%AD&lang=zh_CN&scene=37
                            if (text.contains("//rd.wechat.com/qrcode/")) {// 微信复制过来的
                                resultListener.onResult(goods);
                            } else
                                resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                        } else {
                            resultListener.onResult(new GoodsDocParseResultVO(text));
                        }
                    } catch (TaoBaoTokenParseException e) {
                        resultListener.onResult(new GoodsDocParseResultVO(text));
                    }
                } else {
                    CommonGoods goods = parseLink(urlList.get(0));
                    if (goods == null || goods.getGoodsId() == null) {
                        resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0)));
                    } else {
                        resultListener.onResult(new GoodsDocParseResultVO(text, goods));
                    }
                }
            }
            return;
        }
    }
        CommonGoods commonGoods = null;
        if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) {
            TaoBaoGoodsBrief goods = null;
            try {
                goods = redisManager.getTaoBaoGoodsBrief(Long.parseLong(simpleGoods.getGoodsId()));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (TaobaoGoodsDownException e) {
                e.printStackTrace();
            }
    /**
     * @Title: parseLink @Description: 解析单链接 @param link @return CommonGoods
     * 返回类型 @throws
     */
    public CommonGoods parseLink(String link) {
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            } else {
                goods = TaoBaoUtil.getTmallGoodsInfo(simpleGoods.getGoodsId());
                if (goods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(goods.getTitle());
                    commonGoods.setPicture(goods.getPictUrl());
                } else {
                    // TODO 根据淘宝商品ID解析标题与图片
                }
            }
        SimpleCommonGoods simpleGoods = parseGoodsIdFromLink(link);
        if (simpleGoods == null) {
            return null;
        }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_JD) {
            // 优先采用高级接口请求数据
            JDGoods jdGoods = null;
            try {
                // 防止高级接口被收回
                jdGoods = JDApiUtil.queryGoodsDetail(Long.parseLong(simpleGoods.getGoodsId()));
            } catch (Exception e) {
            }
            if (jdGoods == null) {
                jdGoods = JDApiUtil.getGoodsDetail(Long.parseLong(simpleGoods.getGoodsId()));
            }
            if (jdGoods != null) {
                commonGoods = CommonGoodsFactory.create(jdGoods);
            } else {// 商品并未参与推广
                jdGoods = JDUtil.getSimpleGoodsInfo(simpleGoods.getGoodsId());
                if (jdGoods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(jdGoods.getSkuName());
                    commonGoods.setPicture(jdGoods.getPicUrl());
                }
            }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_PDD) {
            PDDGoodsDetail goods = PinDuoDuoApiUtil.getGoodsDetail(Long.parseLong(simpleGoods.getGoodsId()));
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            } else {
                goods = PinDuoDuoUtil.getPDDGoodsInfo(simpleGoods.getGoodsId());
                if (goods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(goods.getGoodsName());
                    commonGoods.setPicture(goods.getGoodsThumbnailUrl());
                }
            }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_VIP) {
            VIPGoodsInfo goods = VipShopApiUtil.getGoodsDetail(simpleGoods.getGoodsId());
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_SUNING) {
            SuningGoodsInfo goods = SuningApiUtil.getGoodsDetail(simpleGoods.getGoodsId().split("-")[1],
                    simpleGoods.getGoodsId().split("-")[0]);
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            }
        }
        CommonGoods commonGoods = null;
        if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) {
            TaoBaoGoodsBrief goods = null;
            try {
                goods = redisManager.getTaoBaoGoodsBrief(Long.parseLong(simpleGoods.getGoodsId()));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (TaobaoGoodsDownException e) {
                e.printStackTrace();
            }
        return commonGoods;
    }
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            } else {
                goods = TaoBaoUtil.getTmallGoodsInfo(simpleGoods.getGoodsId());
                if (goods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(goods.getTitle());
                    commonGoods.setPicture(goods.getPictUrl());
                } else {
                    // TODO 根据淘宝商品ID解析标题与图片
                }
            }
    /**
     * 从链接中解析商品ID
     *
     * @return
     */
    public SimpleCommonGoods parseGoodsIdFromLink(String link) {
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_JD) {
            // 优先采用高级接口请求数据
            JDGoods jdGoods = null;
            try {
                // 防止高级接口被收回
                jdGoods = JDApiUtil.queryGoodsDetail(Long.parseLong(simpleGoods.getGoodsId()));
            } catch (Exception e) {
            }
            if (jdGoods == null) {
                jdGoods = JDApiUtil.getGoodsDetail(Long.parseLong(simpleGoods.getGoodsId()));
            }
            if (jdGoods != null) {
                commonGoods = CommonGoodsFactory.create(jdGoods);
            } else {// 商品并未参与推广
                jdGoods = JDUtil.getSimpleGoodsInfo(simpleGoods.getGoodsId());
                if (jdGoods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(jdGoods.getSkuName());
                    commonGoods.setPicture(jdGoods.getPicUrl());
                }
            }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_PDD) {
            PDDGoodsDetail goods = PinDuoDuoApiUtil.getGoodsDetail(Long.parseLong(simpleGoods.getGoodsId()));
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            } else {
                goods = PinDuoDuoUtil.getPDDGoodsInfo(simpleGoods.getGoodsId());
                if (goods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(goods.getGoodsName());
                    commonGoods.setPicture(goods.getGoodsThumbnailUrl());
                }
            }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_VIP) {
            VIPGoodsInfo goods = VipShopApiUtil.getGoodsDetail(simpleGoods.getGoodsId());
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            }
        } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_SUNING) {
            SuningGoodsInfo goods = SuningApiUtil.getGoodsDetail(simpleGoods.getGoodsId().split("-")[1],
                    simpleGoods.getGoodsId().split("-")[0]);
            if (goods != null) {
                commonGoods = CommonGoodsFactory.create(goods);
            }
        }
        String jdId = parseJDSkuIdFromLink(link);
        return commonGoods;
    }
        if (!StringUtil.isNullOrEmpty(jdId)) {
            return new SimpleCommonGoods(jdId, Constant.SOURCE_TYPE_JD);
        }
    /**
     * 从链接中解析商品ID
     *
     * @return
     */
    public SimpleCommonGoods parseGoodsIdFromLink(String link) {
        // 解析拼多多
        String pddId = PinDuoDuoUtil.getPDDGoodsId(link);
        if (!StringUtil.isNullOrEmpty(pddId)) {
            return new SimpleCommonGoods(pddId, Constant.SOURCE_TYPE_PDD);
        }
        String jdId = parseJDSkuIdFromLink(link);
        // 解析苏宁
        String suningId = SuningUtil.parseGoodsIdByUrl(link);
        if (!StringUtil.isNullOrEmpty(suningId)) {
            return new SimpleCommonGoods(suningId, Constant.SOURCE_TYPE_SUNING);
        }
        if (!StringUtil.isNullOrEmpty(jdId)) {
            return new SimpleCommonGoods(jdId, Constant.SOURCE_TYPE_JD);
        }
        // 解析唯品会
        String vipId = VipShopUtil.parseGoodsIdByUrl(link);
        if (!StringUtil.isNullOrEmpty(vipId)) {
            return new SimpleCommonGoods(vipId, Constant.SOURCE_TYPE_VIP);
        }
        // 解析拼多多
        String pddId = PinDuoDuoUtil.getPDDGoodsId(link);
        if (!StringUtil.isNullOrEmpty(pddId)) {
            return new SimpleCommonGoods(pddId, Constant.SOURCE_TYPE_PDD);
        }
        // 解析淘宝
        String auctionId = parseTBAuctionIdFromLink(link);
        if (!StringUtil.isNullOrEmpty(auctionId)) {
            return new SimpleCommonGoods(auctionId, Constant.SOURCE_TYPE_TAOBAO);
        }
        // 解析苏宁
        String suningId = SuningUtil.parseGoodsIdByUrl(link);
        if (!StringUtil.isNullOrEmpty(suningId)) {
            return new SimpleCommonGoods(suningId, Constant.SOURCE_TYPE_SUNING);
        }
        return null;
    }
        // 解析唯品会
        String vipId = VipShopUtil.parseGoodsIdByUrl(link);
        if (!StringUtil.isNullOrEmpty(vipId)) {
            return new SimpleCommonGoods(vipId, Constant.SOURCE_TYPE_VIP);
        }
    /**
     * 从链接中解析京东商品的SKUID
     *
     * @param link
     * @return
     */
    public String parseJDSkuIdFromLink(String link) {
        // 解析其他商品
        String jdId = JDUtil.getJDGoodsId(link);
        // 解析淘宝
        String auctionId = parseTBAuctionIdFromLink(link);
        if (!StringUtil.isNullOrEmpty(auctionId)) {
            return new SimpleCommonGoods(auctionId, Constant.SOURCE_TYPE_TAOBAO);
        }
        // 微信链接
        if (StringUtil.isNullOrEmpty(jdId)) {
            jdId = JDUtil.getJDGoodsIdByWeiXin(link);
        }
        return null;
    }
        // 领券短连接
        if (StringUtil.isNullOrEmpty(jdId) && link.contains("u.jd.com")) {
            jdId = jdGoodsLinkParseManager.parseGoodsIdByJDShortUrl(link);
        }
    /**
     * 从链接中解析京东商品的SKUID
     *
     * @param link
     * @return
     */
    public String parseJDSkuIdFromLink(String link) {
        // 解析其他商品
        String jdId = JDUtil.getJDGoodsId(link);
        return jdId;
    }
        // 微信链接
        if (StringUtil.isNullOrEmpty(jdId)) {
            jdId = JDUtil.getJDGoodsIdByWeiXin(link);
        }
    public String parseTBAuctionIdFromLink(String link) {
        TaoBaoGoodsBrief tb = null;
        String auctionId = null;
        if (link.startsWith("https://a.m.taobao.com/i"))// 淘宝账号未登录状态
        {
            link = link.substring("https://a.m.taobao.com/i".length(), link.indexOf(".htm"));
            if (NumberUtil.isNumeric(link)) {
                auctionId = link + "";
            }
        } else if (link.contains("ju.taobao.com") || link.contains(".juhuasuan.com")) {// 聚划算
            int index = link.indexOf("item_id");
            if (index >= 0) {
                link = link.substring(index);
                int last = link.indexOf("&");
                String id = "";
                if (last > 0)
                    id = link.substring(link.indexOf("=") + 1, link.indexOf("&"));
                else {
                    id = link.substring(link.indexOf("=" + 1));
                }
                auctionId = id;
            }
        } else if (link.contains("http://zmnxbc.com")) { // 手机端天猫APP分享
            tb = TaoBaoUtil.parsePhoneShareUrlByTM(link);
            if (tb != null)
                auctionId = tb.getAuctionId() + "";
        } else if (link.contains("h5.m.taobao") || link.contains("detail.m.tmall") || link.contains("item.taobao")
                || link.contains("detail.tmall")) { // 手机页面和电脑页面
            Map<String, String> map = Utils.parseURL(link);
            String id = "";
            id = map.get("id").replace("}", "");
            auctionId = id;
        } else if (link.contains("v.douyin.com")) { // 抖音
            tb = analysisDouYin(link);
            if (tb != null) {
                if (!StringUtil.isNullOrEmpty(tb.getAuctionUrl())) {// 抖音解析到淘宝商品链接
                    CommonGoods goods = parseLink(tb.getAuctionUrl());
                    if (goods != null) {
                        auctionId = goods.getGoodsId() + "";
                    }
                }
            }
        } else {
            tb = TaoBaoUtil.parsePhoneShareUrlByTB(link);
            if (tb != null)
                auctionId = tb.getAuctionId() + "";
        }
        // 领券短连接
        if (StringUtil.isNullOrEmpty(jdId) && link.contains("u.jd.com")) {
            jdId = jdGoodsLinkParseManager.parseGoodsIdByJDShortUrl(link);
        }
        return auctionId;
        return jdId;
    }
    }
    public String parseTBAuctionIdFromLink(String link) {
        TaoBaoGoodsBrief tb = null;
        String auctionId = null;
        if (link.startsWith("https://a.m.taobao.com/i"))// 淘宝账号未登录状态
        {
            link = link.substring("https://a.m.taobao.com/i".length(), link.indexOf(".htm"));
            if (NumberUtil.isNumeric(link)) {
                auctionId = link + "";
            }
        } else if (link.contains("ju.taobao.com") || link.contains(".juhuasuan.com")) {// 聚划算
            int index = link.indexOf("item_id");
            if (index >= 0) {
                link = link.substring(index);
                int last = link.indexOf("&");
                String id = "";
                if (last > 0)
                    id = link.substring(link.indexOf("=") + 1, link.indexOf("&"));
                else {
                    id = link.substring(link.indexOf("=" + 1));
                }
                auctionId = id;
            }
        } else if (link.contains("http://zmnxbc.com")) { // 手机端天猫APP分享
            tb = TaoBaoUtil.parsePhoneShareUrlByTM(link);
            if (tb != null)
                auctionId = tb.getAuctionId() + "";
        } else if (link.contains("h5.m.taobao") || link.contains("detail.m.tmall") || link.contains("item.taobao")
                || link.contains("detail.tmall")) { // 手机页面和电脑页面
            Map<String, String> map = Utils.parseURL(link);
            String id = "";
            id = map.get("id").replace("}", "");
            auctionId = id;
        } else if (link.contains("v.douyin.com")) { // 抖音
            tb = analysisDouYin(link);
            if (tb != null) {
                if (!StringUtil.isNullOrEmpty(tb.getAuctionUrl())) {// 抖音解析到淘宝商品链接
                    CommonGoods goods = parseLink(tb.getAuctionUrl());
                    if (goods != null) {
                        auctionId = goods.getGoodsId() + "";
                    }
                }
            }
        } else {
            tb = TaoBaoUtil.parsePhoneShareUrlByTB(link);
            if (tb != null)
                auctionId = tb.getAuctionId() + "";
        }
    private CommonGoods parseTaoBaoToken(String token) throws TaoBaoTokenParseException {
        Long auctionId = taoBaoTokenAuctionIdMapManager.getAuctionIdByToken(token);
        if (auctionId != null) {//
            TaoBaoGoodsBrief goods = TaoBaoUtil.isAlimama(auctionId + "");
            if (goods == null) {
                goods = TaoBaoUtil.getTaoBaoGoodsBriefNotInPub(auctionId);
            }
            if (goods != null) {
                CommonGoods commonGoods = CommonGoodsFactory.create(goods);
                return commonGoods;
            } else
                return null;
        } else {
            return null;
        }
    }
        return auctionId;
    /**
     *
     * @Title: analysisDouYin @Description: 抖音解析 @param link @return CommonGoods
     *         返回类型 @throws
     */
    private TaoBaoGoodsBrief analysisDouYin(String link) {
        String location = HttpUtil.getLocation(link);
        if (!StringUtil.isNullOrEmpty(location)) {
            if (location.contains("taobao.com")) { // 淘宝商品
                String realUrl = TaoBaoUtil.getRealUrl(location);
                if (!StringUtil.isNullOrEmpty(realUrl)) {
                    TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief();
                    goods.setAuctionUrl(realUrl);
                    return goods;
                }
            } else if (location.contains("haohuo.jinritemai.com")) { // 抖音商品
                DouYinGoods goods = DouYinUtil.getGoodsInfo(location);
                JSONObject goodsJSON = new JSONObject();
                goodsJSON.put("title", goods.getName());
                goodsJSON.put("pictUrl", goods.getImg());
    }
                TaoBaoGoodsBrief commonGoods = new TaoBaoGoodsBrief();
                commonGoods.setPictUrl(goods.getImg());
                commonGoods.setTitle(goods.getName());
                return commonGoods;
            }
        }
        return null;
    }
    private CommonGoods parseTaoBaoToken(String token) throws TaoBaoTokenParseException {
        Long auctionId = taoBaoTokenAuctionIdMapManager.getAuctionIdByToken(token);
        if (auctionId != null) {//
            return getTaoBaoGoods(auctionId);
        } else {
            return null;
        }
    }
    private boolean isContainsLinkConvert(String link) {
        if (link.contains(".jd.com"))
            return true;
        else if (link.contains(".taobao.com") || link.contains(".tmall"))
            return true;
        else if (link.contains(".pinduoduo.com") || link.contains(".yangkeduo.com"))
            return true;
        return false;
    }
    private CommonGoods getTaoBaoGoods(Long auctionId) {
        TaoBaoGoodsBrief goods = TaoBaoUtil.isAlimama(auctionId + "");
        if (goods == null) {
            goods = TaoBaoUtil.getTaoBaoGoodsBriefNotInPub(auctionId);
        }
        if (goods != null) {
            CommonGoods commonGoods = CommonGoodsFactory.create(goods);
            return commonGoods;
        } else
            return null;
    }
    class SimpleCommonGoods {
        private String goodsId;
        private int goodsType;
    /**
     * @Title: analysisDouYin @Description: 抖音解析 @param link @return CommonGoods
     * 返回类型 @throws
     */
    private TaoBaoGoodsBrief analysisDouYin(String link) {
        String location = HttpUtil.getLocation(link);
        if (!StringUtil.isNullOrEmpty(location)) {
            if (location.contains("taobao.com")) { // 淘宝商品
                String realUrl = TaoBaoUtil.getRealUrl(location);
                if (!StringUtil.isNullOrEmpty(realUrl)) {
                    TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief();
                    goods.setAuctionUrl(realUrl);
                    return goods;
                }
            } else if (location.contains("haohuo.jinritemai.com")) { // 抖音商品
                DouYinGoods goods = DouYinUtil.getGoodsInfo(location);
                JSONObject goodsJSON = new JSONObject();
                goodsJSON.put("title", goods.getName());
                goodsJSON.put("pictUrl", goods.getImg());
        public SimpleCommonGoods(String goodsId, int goodsType) {
            this.goodsId = goodsId;
            this.goodsType = goodsType;
        }
                TaoBaoGoodsBrief commonGoods = new TaoBaoGoodsBrief();
                commonGoods.setPictUrl(goods.getImg());
                commonGoods.setTitle(goods.getName());
                return commonGoods;
            }
        }
        return null;
    }
        public String getGoodsId() {
            return goodsId;
        }
    private boolean isContainsLinkConvert(String link) {
        if (link.contains(".jd.com"))
            return true;
        else if (link.contains(".taobao.com") || link.contains(".tmall"))
            return true;
        else if (link.contains(".pinduoduo.com") || link.contains(".yangkeduo.com"))
            return true;
        return false;
    }
        public void setGoodsId(String goodsId) {
            this.goodsId = goodsId;
        }
    class SimpleCommonGoods {
        private String goodsId;
        private int goodsType;
        public int getGoodsType() {
            return goodsType;
        }
        public SimpleCommonGoods(String goodsId, int goodsType) {
            this.goodsId = goodsId;
            this.goodsType = goodsType;
        }
        public void setGoodsType(int goodsType) {
            this.goodsType = goodsType;
        }
    }
        public String getGoodsId() {
            return goodsId;
        }
        public void setGoodsId(String goodsId) {
            this.goodsId = goodsId;
        }
        public int getGoodsType() {
            return goodsType;
        }
        public void setGoodsType(int goodsType) {
            this.goodsType = goodsType;
        }
    }
}
/**
 * 粘贴板解析结果
 *
 * @author Administrator
 *
 * @author Administrator
 */
fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeApiUtil.java
@@ -2021,6 +2021,8 @@
        try {
            JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
            System.out.println(json.toString());
            try {
                return json.optJSONObject("tbk_tpwd_convert_response").optJSONObject("data").optLong("num_iid");
            } catch (Exception e) {
utils/src/main/java/org/yeshi/utils/CMQUtil.java
@@ -8,6 +8,7 @@
import com.qcloud.cmq.Queue;
import com.qcloud.cmq.QueueMeta;
import com.qcloud.cmq.Topic;
import org.springframework.data.annotation.Transient;
//腾讯CMQ消息
public class CMQUtil {
utils/src/main/java/org/yeshi/utils/mongo/MongoDaoUtil.java
@@ -1,6 +1,8 @@
package org.yeshi.utils.mongo;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.yeshi.utils.entity.wx.WXAPPInfo;
@@ -41,19 +43,31 @@
        List<Field> fields = getValidField(clz);
        StringBuilder builder = new StringBuilder();
        builder.append(String.format("void updateSelective(%s bean){\r\n", clz.getSimpleName()));
        builder.append(String.format("public void updateSelective(%s bean) {\r\n", clz.getSimpleName()));
        builder.append("    Update update=new Update();\r\n");
        builder.append("    Query query = new Query();\r\n");
        builder.append("    query.addCriteria(Criteria.where(\"id\").is(bean.getId()));\r\n");
        builder.append("    Update update = new Update();\r\n");
        for (Field f : fields) {
            if ("id".equalsIgnoreCase(f.getName()) || "createTime".equalsIgnoreCase(f.getName()))
                continue;
            if (f.getModifiers() == 25) {//public final static修饰
                continue;
            }
            String getMethod = String.format("bean.get%s()", f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1));
            builder.append(String.format("  if(%s!=null){\r\n", getMethod));
            builder.append(String.format("      update.set(\"%s\",%s)\r\n", f.getName(), getMethod));
            builder.append("    }\r\n");
            if (f.getName().equalsIgnoreCase("updateTime")) {
                builder.append(String.format("    update.set(\"%s\", %s);\r\n", f.getName(), "new Date()"));
            } else {
                builder.append(String.format("    if (%s != null) {\r\n", getMethod));
                builder.append(String.format("        update.set(\"%s\", %s);\r\n", f.getName(), f.getName().equalsIgnoreCase("updateTime") ? "new Date()" : getMethod));
                builder.append("    }\r\n");
            }
        }
        builder.append("    update(query, update);\r\n");
        builder.append("}");
        System.out.println(builder.toString());
        return builder.toString();
    }
utils/src/main/java/org/yeshi/utils/mybatis/ColumnParseUtil.java
@@ -1,42 +1,90 @@
package org.yeshi.utils.mybatis;
import org.springframework.data.annotation.Transient;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class ColumnParseUtil {
    public static void parseColumn(Class<?> clz, String path) {
        Field[] fields = clz.getDeclaredFields();
        for (Field fd : fields) {
            Annotation[] as = fd.getAnnotations();
            for (Annotation a : as) {
                if (a instanceof Column) {
                    Column c = (Column) a;
                    System.out.println(c.name());
                    ColumnUtil.addColumnToMapper(path, c.name(), fd.getName(), getJDBCType(fd.getType().getName()));
                }
            }
        }
    }
    /**
     * 将属性转为数据库字段
     *
     * @param fieldName
     * @return
     */
    private static String convertFieldToColumn(String fieldName) {
        String column = "";
        for (int i = 0; i < fieldName.length(); i++) {
            //大写字母
            if (fieldName.charAt(i) >= 65 && fieldName.charAt(i) <= 90) {
                //前面一个为非大写
                if (i > 0 && (fieldName.charAt(i - 1) < 65 || fieldName.charAt(i - 1) > 90)) {
                    column += "_" + fieldName.charAt(i);
                } else {
                    column += fieldName.charAt(i);
                }
            } else {
                column += fieldName.charAt(i);
            }
        }
        return column.toLowerCase();
    }
    public static String getJDBCType(String type) {
        if (type.endsWith("java.lang.Long")) {
            return "BIGINT";
        } else if (type.endsWith("java.lang.Integer")) {
            return "INTEGER";
        } else if (type.endsWith("java.lang.String")) {
            return "VARCHAR";
        } else if (type.endsWith("java.lang.Boolean")) {
            return "BOOLEAN";
        } else if (type.endsWith("java.math.BigDecimal")) {
            return "DECIMAL";
        } else if (type.endsWith("java.util.Date")) {
            return "TIMESTAMP";
        } else if (type.endsWith("java.lang.Double")) {
            return "DOUBLE";
        } else {
            return "VARCHAR";
        }
    }
    public static void parseColumn(Class<?> clz, String path) {
        Field[] fields = clz.getDeclaredFields();
        for (Field fd : fields) {
            //private/public 非final,static属性才会参与解析
            if (fd.getModifiers() != 1 && fd.getModifiers() != 2) {
                continue;
            }
            String columName = convertFieldToColumn(fd.getName());
            Annotation[] as = fd.getAnnotations();
            for (Annotation a : as) {
                //不参与解析
                if (a instanceof Column) {
                    Column c = (Column) a;
                    columName = c.name();
                }
            }
            for (Annotation a : as) {
                //不参与解析
                if (a instanceof Transient || a instanceof java.beans.Transient) {
                    columName = null;
                    break;
                }
            }
            if (columName != null) {
                ColumnUtil.addColumnToMapper(path, columName, fd.getName(), getJDBCType(fd.getType().getName()));
            }
        }
    }
    public static String getJDBCType(String type) {
        if (type.endsWith("java.lang.Long")) {
            return "BIGINT";
        } else if (type.endsWith("java.lang.Integer")) {
            return "INTEGER";
        } else if (type.endsWith("java.lang.String")) {
            return "VARCHAR";
        } else if (type.endsWith("java.lang.Boolean")) {
            return "BOOLEAN";
        } else if (type.endsWith("java.math.BigDecimal")) {
            return "DECIMAL";
        } else if (type.endsWith("java.util.Date")) {
            return "TIMESTAMP";
        } else if (type.endsWith("java.lang.Double")) {
            return "DOUBLE";
        } else {
            return "VARCHAR";
        }
    }
}
utils/src/test/java/com/yeshi/utils/MyBatisTest.java
New file
@@ -0,0 +1,305 @@
package com.yeshi.utils;
import org.springframework.data.annotation.Transient;
import org.yeshi.utils.mybatis.Column;
import org.yeshi.utils.mybatis.MyBatisMapperUtil;
import org.yeshi.utils.mybatis.Table;
import java.util.Date;
public class MyBatisTest {
    public static void main(String[] args) {
        MyBatisMapperUtil.createMapper(TestBean.class);
    }
    @Table("test")
    class TestBean {
        //未提交审核
        public final static int STATE_NOT_SUBMIT_VERIFY = -2;
        //审核被拒绝
        public final static int STATE_VERIFY_REJECT = -1;
        //等待审核
        public final static int STATE_WAITING_VERIFY = 0;
        //审核通过,未发布
        public final static int STATE_NOT_PUBLISH = 1;
        //已经开始,强制结束
        public final static int STATE_FOUCE_FINISH = 2;
        //已发布,未开始
        public final static int STATE_NOT_STARTED = 10;
        //已经开始
        public final static int STATE_STARTED = 20;
        //已经结束,等待预开奖
        public final static int STATE_FINISH = 30;
        //已开奖,等待显示
        public final static int STATE_PRE_OPENED = 40;
        //已开奖,已经显示
        public final static int STATE_OPENED = 41;
        private Long id;
        private Long appId;
        private Long sponsorId;
        private String name;
        private String materialPoster;
        private String materialTagImage;
        private String material;
        private String desc;
        /**
         * 最大人数
         */
        private Integer maxPersonCount;
        /**
         * 现有人数
         */
        @Column(name = "current_person_count")
        private Integer currentPersonCount;
        @Column(name = "`state`")
        private Integer state;
        @Column(name = "state_remarks")
        private String stateRemarks;
        @Column(name = "pre_start_time")
        private Date preStartTime;
        @Column(name = "actual_start_time")
        private Date actualStartTime;
        @Column(name = "pre_finish_time")
        private Date preFinishTime;
        @Column(name = "actual_finish_time")
        private Date actualFinishTime;
        @Column(name = "pre_open_time")
        private Date preOpenTime;
        @Column(name = "actual_open_time")
        private Date actualOpenTime;
        @Column(name = "create_time")
        private Date createTime;
        @Column(name = "update_time")
        private Date updateTime;
        @Transient
        //奖项列表
        private String awardsList;
        @Transient
        //赞助信息
        private String sponsorInfo;
        @Transient
        //开奖信息
        private String openInfo;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public Long getAppId() {
            return appId;
        }
        public void setAppId(Long appId) {
            this.appId = appId;
        }
        public Long getSponsorId() {
            return sponsorId;
        }
        public void setSponsorId(Long sponsorId) {
            this.sponsorId = sponsorId;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getMaterialPoster() {
            return materialPoster;
        }
        public void setMaterialPoster(String materialPoster) {
            this.materialPoster = materialPoster;
        }
        public String getMaterialTagImage() {
            return materialTagImage;
        }
        public void setMaterialTagImage(String materialTagImage) {
            this.materialTagImage = materialTagImage;
        }
        public String getMaterial() {
            return material;
        }
        public void setMaterial(String material) {
            this.material = material;
        }
        public String getDesc() {
            return desc;
        }
        public void setDesc(String desc) {
            this.desc = desc;
        }
        public Integer getMaxPersonCount() {
            return maxPersonCount;
        }
        public void setMaxPersonCount(Integer maxPersonCount) {
            this.maxPersonCount = maxPersonCount;
        }
        public Integer getCurrentPersonCount() {
            return currentPersonCount;
        }
        public void setCurrentPersonCount(Integer currentPersonCount) {
            this.currentPersonCount = currentPersonCount;
        }
        public Integer getState() {
            return state;
        }
        public void setState(Integer state) {
            this.state = state;
        }
        public String getStateRemarks() {
            return stateRemarks;
        }
        public void setStateRemarks(String stateRemarks) {
            this.stateRemarks = stateRemarks;
        }
        public Date getPreStartTime() {
            return preStartTime;
        }
        public void setPreStartTime(Date preStartTime) {
            this.preStartTime = preStartTime;
        }
        public Date getActualStartTime() {
            return actualStartTime;
        }
        public void setActualStartTime(Date actualStartTime) {
            this.actualStartTime = actualStartTime;
        }
        public Date getPreFinishTime() {
            return preFinishTime;
        }
        public void setPreFinishTime(Date preFinishTime) {
            this.preFinishTime = preFinishTime;
        }
        public Date getActualFinishTime() {
            return actualFinishTime;
        }
        public void setActualFinishTime(Date actualFinishTime) {
            this.actualFinishTime = actualFinishTime;
        }
        public Date getPreOpenTime() {
            return preOpenTime;
        }
        public void setPreOpenTime(Date preOpenTime) {
            this.preOpenTime = preOpenTime;
        }
        public Date getActualOpenTime() {
            return actualOpenTime;
        }
        public void setActualOpenTime(Date actualOpenTime) {
            this.actualOpenTime = actualOpenTime;
        }
        public Date getCreateTime() {
            return createTime;
        }
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
        public Date getUpdateTime() {
            return updateTime;
        }
        public void setUpdateTime(Date updateTime) {
            this.updateTime = updateTime;
        }
        public String getAwardsList() {
            return awardsList;
        }
        public void setAwardsList(String awardsList) {
            this.awardsList = awardsList;
        }
        public String getSponsorInfo() {
            return sponsorInfo;
        }
        public void setSponsorInfo(String sponsorInfo) {
            this.sponsorInfo = sponsorInfo;
        }
        public String getOpenInfo() {
            return openInfo;
        }
        public void setOpenInfo(String openInfo) {
            this.openInfo = openInfo;
        }
    }
}