Administrator
2018-10-30 c0c91fbda1ba601c4c8cb6d12fd43dfbe02eea5d
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
package com.yeshi.fanli.service.impl.goods;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoGoodsBriefRecordMapper;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBriefRecord;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefRecordService;
 
@Service
public class TaoBaoGoodsBriefRecordServiceImpl implements TaoBaoGoodsBriefRecordService {
 
    @Resource
    private TaoBaoGoodsBriefRecordMapper taoBaoGoodsBriefRecordMapper;
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return taoBaoGoodsBriefRecordMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(TaoBaoGoodsBriefRecord record) {
        return taoBaoGoodsBriefRecordMapper.insert(record);
    }
 
    @Override
    public int insertSelective(TaoBaoGoodsBriefRecord record) {
        return taoBaoGoodsBriefRecordMapper.insertSelective(record);
    }
 
    @Override
    public TaoBaoGoodsBriefRecord selectByPrimaryKey(Long id) {
        return taoBaoGoodsBriefRecordMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(TaoBaoGoodsBriefRecord record) {
        return taoBaoGoodsBriefRecordMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(TaoBaoGoodsBriefRecord record) {
        return taoBaoGoodsBriefRecordMapper.updateByPrimaryKey(record);
    }
 
    
    @Override
    public void insertBatch(List<TaoBaoGoodsBrief> record) {
        
        /* 数据为空 */
        if (record == null || record.size() == 0) {
            return;
        }
        
        
        /* 根据auctionId 查询数据库是否存在数据 */
        List<String> auctionIdList = new ArrayList<String>();
        for (int i = 0; i < record.size(); i++) { 
            TaoBaoGoodsBrief taoBaoGoodsBrief = record.get(i);
            Long auctionId = taoBaoGoodsBrief.getAuctionId();
            if (auctionId != null) {
                auctionIdList.add(auctionId+"");
            }
        }
        
        /* 查询结果比对*/
        List<String> existAuctionIdList = new ArrayList<String>();
        if (auctionIdList.size() > 0) {
            List<TaoBaoGoodsBrief> existList = taoBaoGoodsBriefRecordMapper.queryGoodsByAuctionId(auctionIdList);
            
            if (existList != null && existList.size() > 0) {
                for (TaoBaoGoodsBrief taoBaoGoodsBrief : existList) {
                     Long auctionId = taoBaoGoodsBrief.getAuctionId();
                     if (auctionIdList.contains(auctionId+"")) {
                         existAuctionIdList.add(auctionId +"");
                     }
                }
            }
        }
        
        
        
        /* 每100 数据 插入数据库 */
        int j = 0;
        List<TaoBaoGoodsBrief> newList = new ArrayList<TaoBaoGoodsBrief>();
        
        for (int i = 0; i < record.size(); i++) {
            
            TaoBaoGoodsBrief taoBaoGoodsBrief = record.get(i);
            
            Long auctionId = taoBaoGoodsBrief.getAuctionId();
            
            
            if (existAuctionIdList.size() > 0){
                // 已经存在数据库
                if (existAuctionIdList.contains(auctionId+ "")){
                    continue;
                }
            }
            
            taoBaoGoodsBrief.setCreatetime(new Date());
            j++;
            newList.add(taoBaoGoodsBrief);
            
            if (j == 100) {
                taoBaoGoodsBriefRecordMapper.insertBatch(newList);
                j = 0;
                newList.clear();
                
            } else if (i == record.size() - 1) {
                taoBaoGoodsBriefRecordMapper.insertBatch(newList);
            }
            
        }
        
    }
 
    @Override
    public List<TaoBaoGoodsBrief> queryByAuctionId(Long auctionId) {
        return taoBaoGoodsBriefRecordMapper.queryByAuctionId(auctionId);
    }
    
}