zhaojs
2023-09-27 74098f1401afe40f961d1d167bb18dd0a71c4d59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using BaiDuSdk;
using CommonUtil;
using HtmlAgilityPack;
using Operater.DAL;
using Operater.DbModel;
using Operater.DTO.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YouZanSDKStandard.Api;
using YouZanSDKStandard.Api.Request;
 
namespace ProductDistribution.Handle
{
    public class ProductCheckHandle
    {
        public static string _errFileName = "检测关键词error";
        public static void MainMethod(ProductDistributionMq msg)
        {
            try
            {
                var appSet = (new AppsetDAL().ListGet(t => t.Id != null))[0];
                var proList = new DistributionRelationDAL().ListGet(t => t.ReStatus == 1);
                int totalCount = 0;
              //  var proList = new DistributionRelationDAL().ListPageGet(t => t.ReStatus == 1, 1, 100, ref totalCount);
                //获取过滤关键词
                var filter = new ProductFilterDAL().ListGet(t => t.Id != null);
                int index = 0;
                foreach (var pro in proList)
                {
                    index++;
                    Console.WriteLine($"{index}/{proList.Count}");
                    try
                    {
                        ////获取有赞商品详情
                        YouZanProductDetailGetRequest detailReq = new YouZanProductDetailGetRequest()
                        {
                            item_id = long.Parse(pro.YzProduct)
                        };
                        var detailRs = YouZanClient.Execute(detailReq, appSet.YzToken);
                        if (detailRs.IsNull() || !detailRs.success)
                        {
                            Console.WriteLine("获取有赞商品详情失败");
                            LogUtil.Info("获取有赞商品详情失败:" + detailRs.message, "错误");
                            continue;
                        }
                        string checkResult = "";
                        //检测title
                        string titleErr = FilterMsg(filter, detailRs.data.title);
                        checkResult += titleErr.IsNullOrEmpty() ? "" : ($"标题:[{titleErr}];");
                        //检测详情
                        string descErr = FilterMsg(filter, detailRs.data.desc);
                        checkResult += descErr.IsNullOrEmpty() ? "" : ($"详情文字:[{descErr}];");
                        //检测主图
                        //只检第一张
                        string imgErr = FilterImg(filter, detailRs.data.images[0].image_url);
                        checkResult += imgErr.IsNullOrEmpty() ? "" : ($"主图:[{imgErr}];");
                        //StringBuilder imgErr = new StringBuilder();
                        //foreach (var img in detailRs.data.images)
                        //{
                        //    string err = FilterImg(filter, img.image_url);
                        //    if (!err.IsNullOrEmpty())
                        //    {
                        //        imgErr.Append(err + ",");
                        //    }
                        //}
                        //if (!imgErr.IsNull() && imgErr.Length > 0)
                        //{
                        //    checkResult += ($"主图:[{imgErr.ToString()}];");
                        //}
                        //检测sku图片
                        //StringBuilder skuErr = new StringBuilder();
                        //foreach (var sku in detailRs.data.sku_value_props)
                        //{
                        //    if (sku.img_url.IsNullOrEmpty())
                        //    {
                        //        continue;
                        //    }
                        //    string err = FilterImg(filter, sku.img_url);
                        //    if (!err.IsNullOrEmpty())
                        //    {
                        //        skuErr.Append(err + ",");
                        //    }
                        //}
                        //if (!skuErr.IsNull() && skuErr.Length > 0)
                        //{
                        //    checkResult += ($"sku图片:[{skuErr.ToString()}];");
                        //}
                        //检测商品详情图片
                        //string descImgErr = FilterDesc(filter, detailRs.data.desc);
                        //checkResult += descImgErr.IsNullOrEmpty() ? "" : ($"详情图片:[{descImgErr}];");
                        if (checkResult.IsNullOrEmpty())
                        {
                            continue;
                        }
                        ProductCheckRecord productCheckRecord = new ProductCheckRecord()
                        {
                            Id = Guid.NewGuid().ToString(),
                            YzProductId = pro.YzProduct,
                            AliProductId = pro.SourceProduct,
                            CheckTime = DateTime.Now,
                            TaskId = msg.TaskId,
                            Memo = checkResult,
                            YzAlias = detailRs.data.alias,
                            YzImg = detailRs.data.images[0].image_url,
                            YzTitle = detailRs.data.title
                        };
                        new ProductCheckRecordDAL().Insert(productCheckRecord);
                    }
                    catch (Exception e)
                    {
                        LogUtil.Info("循环错误:" + e.ToString(), "检测关键词error");
                        continue;
                    }
                }
            }
            catch (Exception e)
            {
                LogUtil.Info("错误:" + e.ToString(), "主线程错误");
            }
        }
 
        #region 检测关键词
        public static string FilterMsg(List<ProductFilter> filter, string msg)
        {
            StringBuilder errMsg = new StringBuilder();
            try
            {
                foreach (var fi in filter)
                {
                    if (msg.ToLower().Contains(fi.KeyWord.ToLower()))
                    {
                        errMsg.Append(fi.KeyWord + ",");
                    }
                }
            }
            catch (Exception e)
            {
                LogUtil.Info(e.ToString(), "过滤关键词error");
            }
            if (!errMsg.IsNull() && errMsg.Length > 0)
            {
                return errMsg.ToString();
            }
            return "";
        }
 
        #endregion
 
        #region 图片关键词过滤
        public static string FilterImg(List<ProductFilter> filter, string imgUrl)
        {
            try
            {
                StringBuilder keyError = new StringBuilder();
                //百度ai图片识别
                var res = BaiDuClient.WebImageUrl(imgUrl);
                if (!res.IsSuccess)
                {
                    LogUtil.Info($"百度ai错误:{res.error_msg}--图片:{imgUrl}", "过滤关键词error");
                }
                if (res.words_result_num <= 0)
                {
                    return null;
                }
                var msgList = res.words_result.Select(t => t.words).ToList();
                string msg = string.Join(',', msgList);
                var filterKey = filter.Where(t => msg.ToLower().Contains(t.KeyWord.ToLower())).ToList();
                if (!filterKey.IsNull() && filterKey.Count > 0)
                {
                    var filterList = filterKey.Select(t => t.KeyWord).ToList();
                    return string.Join(',', filterList);
                }
            }
            catch (Exception e)
            {
                LogUtil.Info(e.ToString(), "过滤关键词error");
            }
            return null;
        }
        #endregion
 
        #region 检测商品详情图片
        public static string FilterDesc(List<ProductFilter> filter, string desc)
        {
            StringBuilder errMsg = new StringBuilder();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(desc);
            var imgNode = doc.DocumentNode.SelectNodes("//img");
            foreach (var imgItem in imgNode)
            {
                var imgsrc = imgItem.Attributes["src"].Value;
                string imgError = FilterImg(filter, imgsrc);
                if (!imgError.IsNullOrEmpty())
                {
                    errMsg.Append(imgError + ",");
                }
            }
            if (!errMsg.IsNull() && errMsg.Length > 0)
            {
                return errMsg.ToString();
            }
            return "";
        }
 
        #endregion
    }
}