fix
Helius
2021-08-12 4705f9b4084d9ccddb601569b223f0fb41778a94
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.matrix.system.dataMove;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.matrix.component.redis.RedisClient;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.shopXcx.bean.ShopProduct;
import com.matrix.system.shopXcx.bean.ShopProductAttribute;
import com.matrix.system.shopXcx.bean.ShopProductImg;
import com.matrix.system.shopXcx.bean.ShopSku;
import com.matrix.system.shopXcx.dao.ShopProductAttributeDao;
import com.matrix.system.shopXcx.dao.ShopProductDao;
import com.matrix.system.shopXcx.dao.ShopProductImgDao;
import com.matrix.system.shopXcx.dao.ShopSkuDao;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
 
/**
 * @author wzy
 * @date 2021-08-11
 **/
@Service
public class SjfDataMoveServiceImpl {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @Autowired
    private ShopProductAttributeDao shopProductAttributeDao;
    @Autowired
    private ShopProductDao shopProductDao;
    @Autowired
    private ShopSkuDao shopSkuDao;
    @Autowired
    private ShopProductImgDao shopProductImgDao;
 
    @Autowired
    private RedisClient redisClient;
 
    public <T> List<T> mapsToListBean(List<Map<String, Object>> maps, Class<T> t) {
        List<T> list = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            String s = JSONObject.toJSONString(map);
            T object = JSONObject.parseObject(s, t);
            list.add(object);
        }
        return list;
    }
 
    public <T> T mapToBane(Map<String, Object> map, Class<T> t) {
        String s = JSONObject.toJSONString(map);
        return JSONObject.parseObject(s, t);
    }
 
    private final Long companyId = 37L;
    private final Long shopId = 53L;
 
    @DS("slave")
    public List<ShopProductAttribute> oldProductAttr() {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from eb_store_category where is_show=1 and pid=0");
 
        List<ShopProductAttribute> list = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Object classifyId = map.get("id");
 
            List<Map<String, Object>> subMaps = jdbcTemplate.queryForList("select * from eb_store_category where pid=" + classifyId.toString());
 
            List<ShopProductAttribute> child = new ArrayList<>();
            for (Map<String, Object> subMap : subMaps) {
                ShopProductAttribute subAttr = new ShopProductAttribute();
                subAttr.setAttrId((int) Long.parseLong(subMap.get("id").toString()));
                subAttr.setParentId(Integer.parseInt(subMap.get("pid").toString()));
                String subName = subMap.get("cate_name").toString();
                subAttr.setAttrName(subName);
                subAttr.setAttrCode(StringUtils.toHanyuPinyin(subName));
                subAttr.setAttrUrl(map.get("pic").toString());
 
                subAttr.setCreateBy("善吉凡");
                subAttr.setUpdateBy("善吉凡");
                subAttr.setCreateTime(new Date());
                subAttr.setUpdateTime(new Date());
                subAttr.setShopId(shopId);
                subAttr.setCompanyId(companyId);
                child.add(subAttr);
            }
 
            ShopProductAttribute attr = new ShopProductAttribute();
            attr.setAttrId((int) Long.parseLong(map.get("id").toString()));
            attr.setParentId(198);
            attr.setAttrUrl(map.get("pic").toString());
            String name = map.get("cate_name").toString();
            attr.setAttrName(name);
            attr.setAttrCode(StringUtils.toHanyuPinyin(name));
            attr.setChild(child);
 
            attr.setCreateBy("善吉凡");
            attr.setUpdateBy("善吉凡");
            attr.setCreateTime(new Date());
            attr.setUpdateTime(new Date());
            attr.setShopId(shopId);
            attr.setCompanyId(companyId);
            list.add(attr);
        }
        return list;
    }
 
    public void productAttr(List<ShopProductAttribute> list) {
        Map<Integer, Integer> map = new HashMap<>();
        for (ShopProductAttribute shopProductAttribute : list) {
            Integer oldId = shopProductAttribute.getAttrId();
            shopProductAttribute.setAttrId(null);
            shopProductAttributeDao.insert(shopProductAttribute);
 
            for (ShopProductAttribute productAttribute : shopProductAttribute.getChild()) {
                Integer oldChildId = productAttribute.getAttrId();
                productAttribute.setAttrId(null);
                productAttribute.setParentId(shopProductAttribute.getAttrId());
                shopProductAttributeDao.insert(productAttribute);
                map.put(oldChildId, productAttribute.getAttrId());
            }
            map.put(oldId, shopProductAttribute.getAttrId());
        }
 
        redisClient.saveMapValue("attrIds", map);
    }
 
    @DS("slave")
    public List<ShopProduct> oldProduct() {
        List<Map<String, Object>> products = jdbcTemplate.queryForList("select * from eb_store_product where is_show=1 and is_del=0");
 
        List<ShopProduct> result = new ArrayList<>();
        for (Map<String, Object> product : products) {
            ShopProduct shopProduct = new ShopProduct();
            shopProduct.setCategoryId(Integer.parseInt(product.get("cate_id").toString()));
            shopProduct.setTitle(product.get("store_name").toString());
            shopProduct.setBriefIntroduction(product.get("store_info").toString());
            shopProduct.setImgMobile(product.get("image").toString());
            shopProduct.setPrice(Double.parseDouble(product.get("price").toString()));
            shopProduct.setMarkedPrice(Double.parseDouble(product.get("ot_price").toString()));
            shopProduct.setRsVolume(Integer.parseInt(product.get("sales").toString()));
            shopProduct.setDsVolume(Integer.parseInt(product.get("ficti").toString()));
 
            String imagesStr = product.get("slider_image").toString();
            List<String> images = JSONObject.parseArray(imagesStr, String.class);
            int i = 1;
            List<ShopProductImg> imgs = new ArrayList<>();
            for (String image : images) {
                ShopProductImg shopProductImg = new ShopProductImg();
                shopProductImg.setImgPath(image);
                shopProductImg.setSort(i);
                shopProductImg.setCreateBy("善吉凡");
                shopProductImg.setUpdateBy("善吉凡");
                shopProductImg.setCreateTime(new Date());
                shopProductImg.setUpdateTime(new Date());
                i++;
 
                imgs.add(shopProductImg);
            }
            shopProduct.setProductImgs(imgs);
 
            // 商品属性 即sku
            Map<String, Object> attrValue = jdbcTemplate.queryForMap("select * from eb_store_product_attr_value where product_id=" + product.get("id"));
            ShopSku shopSku = new ShopSku();
            shopSku.setName(product.get("store_name").toString());
            shopSku.setStock(Integer.parseInt(attrValue.get("stock").toString()));
            shopSku.setImgPath(attrValue.get("image").toString());
            shopSku.setPrice(new BigDecimal(attrValue.get("price").toString()));
            shopSku.setCreateBy("善吉凡");
            shopSku.setUpdateBy("善吉凡");
            shopSku.setCreateTime(new Date());
            shopSku.setUpdateTime(new Date());
            shopSku.setScore(0);
            shopSku.setSort(0);
            shopSku.setSealCount(Integer.parseInt(product.get("ficti").toString()));
            List<ShopSku> skus = new ArrayList<>();
            skus.add(shopSku);
            shopProduct.setSkus(skus);
 
            Map<String, Object> desc = jdbcTemplate.queryForMap("select * from eb_store_product_description where product_id=" + product.get("id"));
            shopProduct.setMobileDetails(desc.get("description").toString());
 
 
            shopProduct.setDelFlag(2);
            shopProduct.setShopIds(shopId.toString());
            shopProduct.setCompanyId(companyId);
            shopProduct.setAbleSales(1);
            shopProduct.setAbleScorePay(2);
            shopProduct.setStatus(1);
            shopProduct.setCreateBy("善吉凡");
            shopProduct.setUpdateBy("善吉凡");
            shopProduct.setCreateTime(new Date());
            shopProduct.setUpdateTime(new Date());
            result.add(shopProduct);
        }
        return result;
    }
 
    @Transactional
    public void newShopProduct(List<ShopProduct> list) {
        String sysUsersIds = redisClient.getCachedValue("attrIds");
        Map attrIds = JSONObject.parseObject(sysUsersIds, Map.class);
 
 
        for (ShopProduct shopProduct : list) {
            Object o = attrIds.get(shopProduct.getCategoryId().toString());
            shopProduct.setCategoryId(Integer.parseInt(o.toString()));
 
            shopProduct.setAttrValues("");
            shopProduct.setScoreCategoryId(202L);
            shopProduct.setId(null);
            String s = StringEscapeUtils.unescapeHtml4(shopProduct.getMobileDetails());
            shopProduct.setMobileDetails(s);
 
            shopProductDao.insert(shopProduct);
 
            for (ShopProductImg productImg : shopProduct.getProductImgs()) {
                productImg.setPId(shopProduct.getId());
 
                shopProductImgDao.insert(productImg);
            }
 
            for (ShopSku skus : shopProduct.getSkus()) {
                skus.setPId(shopProduct.getId());
 
                shopSkuDao.insert(skus);
            }
        }
    }
 
}