yujian
2019-09-03 2ddf5b8a1290123e2d1c05bcc851ec35cd217afc
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
package com.yeshi.fanli.service.impl.customerservice;
 
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.yeshi.utils.StringUtil;
 
import com.yeshi.fanli.dao.mybatis.customerservice.CustomerServiceCommonQuestionMapper;
import com.yeshi.fanli.entity.customerservice.CustomerServiceCommonQuestion;
import com.yeshi.fanli.exception.user.CustomerServiceCommonQuestionException;
import com.yeshi.fanli.service.inter.customerservice.CustomerServiceCommonQuestionService;
 
@Service
public class CustomerServiceCommonQuestionServiceImpl implements CustomerServiceCommonQuestionService {
 
    @Resource
    private CustomerServiceCommonQuestionMapper customerServiceCommonQuestionMapper;
 
    @Override
    public CustomerServiceCommonQuestion searchByKey(String key) {
        List<CustomerServiceCommonQuestion> list = customerServiceCommonQuestionMapper.selectByKey(key);
        if (list == null || list.size() == 0)
            return null;
        Comparator<CustomerServiceCommonQuestion> cm = new Comparator<CustomerServiceCommonQuestion>() {
 
            @Override
            public int compare(CustomerServiceCommonQuestion o1, CustomerServiceCommonQuestion o2) {
                return o1.getKey().replace(key, "").length() - o2.getKey().replace(key, "").length();
            }
        };
        Collections.sort(list, cm);
        return list.get(0);
    }
 
    @Override
    public CustomerServiceCommonQuestion searchByKeyCache(String key) {
        return searchByKey(key);
    }
 
    @Override
    public void addCustomerServiceCommonQuestion(CustomerServiceCommonQuestion question)
            throws CustomerServiceCommonQuestionException {
        if (question == null || StringUtil.isNullOrEmpty(question.getKey())
                || StringUtil.isNullOrEmpty(question.getContent())
                || StringUtil.isNullOrEmpty(question.getContentType()))
            throw new CustomerServiceCommonQuestionException(1, "问题信息不完整");
 
        CustomerServiceCommonQuestion oldQuestion = searchByKey(question.getKey());
        if (oldQuestion != null && oldQuestion.getKey().equalsIgnoreCase(question.getKey()))
            throw new CustomerServiceCommonQuestionException(2, "已有该问题");
        customerServiceCommonQuestionMapper.insertSelective(question);
    }
 
    @Override
    public void updateCustomerServiceCommonQuestion(CustomerServiceCommonQuestion question)
            throws CustomerServiceCommonQuestionException {
        if (question == null || question.getId() == null)
            return;
        customerServiceCommonQuestionMapper.updateByPrimaryKeySelective(question);
    }
 
    @Override
    public void deleteCustomerServiceCommonQuestion(Long id) {
        customerServiceCommonQuestionMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public List<String> listKeysCache() {
        return customerServiceCommonQuestionMapper.selectKeys();
    }
 
    @Override
    public List<CustomerServiceCommonQuestion> listCustomerServiceCommonQuestion(int page, int pageSize) {
        return customerServiceCommonQuestionMapper.selectOrderByUpdateTime((page - 1) * pageSize, pageSize);
    }
 
    @Override
    public Long countCustomerServiceCommonQuestion() {
        return customerServiceCommonQuestionMapper.selectCount();
    }
    
    
    @Override
    public List<CustomerServiceCommonQuestion> listQuery(long start, int count, String key, 
            String type, Integer state, Integer sort){
        return customerServiceCommonQuestionMapper.listQuery(start, count, key, type, state, sort);
    }
    
    @Override
    public long countQuery(String key, String type, Integer state) {
        return customerServiceCommonQuestionMapper.countQuery(key, type, state);
    }
 
    @Override
    public int deleteBatchByPrimaryKey(List<Long> list) {
        return customerServiceCommonQuestionMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public int insert(CustomerServiceCommonQuestion record) {
        return customerServiceCommonQuestionMapper.insert(record);
    }
 
    @Override
    public int insertSelective(CustomerServiceCommonQuestion record) {
        return customerServiceCommonQuestionMapper.insertSelective(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(CustomerServiceCommonQuestion record) {
        return customerServiceCommonQuestionMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(CustomerServiceCommonQuestion record) {
        return customerServiceCommonQuestionMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public CustomerServiceCommonQuestion selectByPrimaryKey(Long id) {
        return customerServiceCommonQuestionMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public void save(CustomerServiceCommonQuestion record) throws Exception, CustomerServiceCommonQuestionException {
        if (record == null) {
            throw new  CustomerServiceCommonQuestionException(1, "参数不能为空");
        }
        
        String key = record.getKey();
        String content = record.getContent();
        String contentType = record.getContentType();
        if (key == null || key.trim().length() == 0 || content == null || content.trim().length() == 0
                || contentType == null || contentType.trim().length() == 0) {
            throw new  CustomerServiceCommonQuestionException(1, "编辑内容不能为空");
        }
        
        Long id = record.getId();
        if (id == null) {
            // 新增
            record.setWeight(0);
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            customerServiceCommonQuestionMapper.insert(record);
        } else {
            CustomerServiceCommonQuestion commonQuestion = customerServiceCommonQuestionMapper.selectByPrimaryKey(id);
            if (commonQuestion == null) {
                throw new  CustomerServiceCommonQuestionException(1, "数据已不存在");
            }
            // 修改
            record.setUpdateTime(new Date());
            // 保存之前的状态
            record.setState(commonQuestion.isState());
            
            customerServiceCommonQuestionMapper.updateByPrimaryKeySelective(record);
        }
        
    }
    
}