xiaoyong931011
2022-08-09 489b38cff851af79c1908c7493e9025eec87be7c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.xcong.farmer.cms;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.common.utils.FileUtils;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.modules.system.dto.AdminArticleDto;
import com.xcong.farmer.cms.modules.system.dto.WebArticleInPageDto;
import com.xcong.farmer.cms.modules.system.entity.*;
import com.xcong.farmer.cms.modules.system.mapper.ArticleMapper;
import com.xcong.farmer.cms.modules.system.mapper.CmsTemplateMapper;
import com.xcong.farmer.cms.modules.system.mapper.ColumnMapper;
import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.modules.system.vo.AdminArticleVo;
import com.xcong.farmer.cms.modules.system.vo.WebArticleVo;
import com.xcong.farmer.cms.modules.test.dao.TestUserDao;
import com.xcong.farmer.cms.modules.test.entity.TestUserEntity;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@SpringBootTest
class KssframeworkApplicationTests {
 
    @Resource
    private TestUserDao testUserDao;
 
    @Resource
    private CmsTemplateMapper cmsTemplateMapper;
 
    @Test
    public void testFileWrite(){
        Long companyId = 24L;
        Long id = 38L;
        CmsTemplateEntity cmsTemplateEntity = cmsTemplateMapper.selectByIdAndCompanyId(id,companyId);
        String name = cmsTemplateEntity.getName();
        Integer type = cmsTemplateEntity.getType();
        String templatePath = "D:\\cmshtml";
        String path = cmsTemplateEntity.getPath();
 
        cmsTemplateMapper.delete(id,companyId);
        String pathNew = FileUtils.path(templatePath, path);
        File file = new File(pathNew);
        FileUtil.touch(file);
 
        //文件写入,直接覆盖
        FileWriter writer = new FileWriter(file);
        writer.write("测试121634964949", false);
 
        CmsTemplateEntity cmsTemplate = new CmsTemplateEntity();
        cmsTemplate.setCompanyId(companyId);
        cmsTemplate.setType(type);
        cmsTemplate.setName(name);
        cmsTemplate.setPath(path);
        cmsTemplateMapper.insert(cmsTemplate);
    }
    @Test
    public void testUserInsert() {
        for(int i = 0; i < 20; i++) {
            TestUserEntity testUser = new TestUserEntity();
            testUser.setCreateBy("123");
            testUser.setCreateTime(new Date());
            testUser.setUpdateBy("123");
            testUser.setUpdateTime(new Date());
            testUser.setAccount("12345" + i);
            testUser.setName("hehe" + i);
            testUser.setPassword("33333");
            testUserDao.insert(testUser);
        }
 
    }
 
    @Test
    public void start() {
        System.out.println(1);
    }
 
 
    @Autowired
    private CmsProperties cmsProperties;
 
    @Test
    public void viewTemplateInfo() {
        Long id = 4L;
        CmsTemplateEntity cmsTemplateEntity = cmsTemplateMapper.selectById(id);
        String htmlUrl = "" + cmsProperties.getTemplatePath();
        File uploadDir = new File(htmlUrl);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
        htmlUrl = "D:\\cmshtml";
        String pathName = htmlUrl+"\\"+cmsTemplateEntity.getPath();
        System.out.println(pathName);
        byte[] bytes = new byte[0];
        try {
            bytes = Files.readAllBytes(Paths.get(pathName));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("未找到模板");
        }
 
        String content = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(content);
    }
 
    @Test
    public void path(){
        Long id = 6L;
        CmsTemplateEntity cmsTemplateEntity = cmsTemplateMapper.selectById(id);
        String templatePath = cmsProperties.getTemplatePath();
        String pathName = FileUtils.path(templatePath, cmsTemplateEntity.getPath());
        System.out.println(templatePath);
        System.out.println(pathName);
        byte[] bytes = new byte[0];
        try {
            bytes = Files.readAllBytes(Paths.get(pathName));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("未找到模板");
        }
 
        String content = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(content);
    }
 
    @Autowired
    private ColumnMapper columnMapper;
 
    @Autowired
    private ArticleMapper articleMapper;
 
    @Autowired
    private CompanyMapper companyMapper;
 
    @Test
    public void testCompanyId(){
        String website = "127.0.0.1";
        Long companyIdFromWebsite = getCompanyIdFromWebsite(website);
        System.out.print(companyIdFromWebsite);
    }
 
    private Long getCompanyIdFromWebsite(String website){
        List<CompanyEntity> companyEntities = companyMapper.selectList(new QueryWrapper<>());
        Long companyId = 0L;
        if(CollUtil.isNotEmpty(companyEntities)){
            for(CompanyEntity companyEntity : companyEntities){
                boolean contains = StrUtil.contains(companyEntity.getWebAddress(), website);
                if(contains){
                    companyId = companyEntity.getId();
                }
            }
        }
        return companyId;
    }
 
    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            BigDecimal partnerIncomeNum = RandomUtil.randomBigDecimal(new BigDecimal("5000.001"), new BigDecimal("10000.0001")).setScale(6, RoundingMode.HALF_DOWN);
            System.out.println(partnerIncomeNum);
        }
    }
 
    @Test
    public void articleTest(){
        WebArticleInPageDto webArticleInPageDto = new WebArticleInPageDto();
        webArticleInPageDto.setTimeType("一周内");
        webArticleInPageDto.setPageNum(1);
        webArticleInPageDto.setPageSize(8);
        Long companyId = 24L;
        Page<WebArticleVo> page = new Page<>(webArticleInPageDto.getPageNum(), webArticleInPageDto.getPageSize());
        ArticleEntity articleEntity = new ArticleEntity();
        Long columnId = webArticleInPageDto.getColumnId() == null ? 0L : webArticleInPageDto.getColumnId();
        if(columnId != 0L){
            articleEntity.setColumnId(columnId);
        }
        String author = webArticleInPageDto.getAuthor();
        if(StrUtil.isNotEmpty(author)){
            articleEntity.setAuthor(author);
        }
        String queryKey = webArticleInPageDto.getQueryKey();
        if(StrUtil.isNotEmpty(queryKey)){
            articleEntity.setTitle(queryKey);
        }
        if(UserEntity.USER_BELONG_TOP != companyId){
            articleEntity.setCompanyId(companyId);
        }
        String timeType = webArticleInPageDto.getTimeType();
        if(StrUtil.isNotEmpty(timeType)){
            articleEntity.setTimeType(timeType);
        }
        IPage<WebArticleVo> list = articleMapper.selectWebArticleInPage(page,articleEntity);
        System.out.println(list.getRecords());
    }
 
    @Test
    public void dateTest(){
        String columnIdStrs = "68,63";
        AdminArticleDto adminArticleDto = new AdminArticleDto();
 
        adminArticleDto.setColumnIdStr(columnIdStrs);
        adminArticleDto.setPageNum(1);
        adminArticleDto.setPageSize(8);
        Page<AdminArticleVo> page = new Page<>(adminArticleDto.getPageNum(), adminArticleDto.getPageSize());
        ArticleEntity articleEntity = new ArticleEntity();
        String columnIdStr = adminArticleDto.getColumnIdStr();
        if(StrUtil.isNotEmpty(columnIdStr)){
            long[] columnIdLongs = StrUtil.splitToLong(columnIdStr, StrUtil.COMMA);
            List<Long> columnList = new ArrayList<>();
            for(long columnIdLong : columnIdLongs){
                columnList.add(columnIdLong);
            }
            articleEntity.setColumnList(columnList);
        }
        String title = adminArticleDto.getTitle();
        if(StrUtil.isNotEmpty(title)){
            articleEntity.setTitle(title);
        }
        Integer contentType = adminArticleDto.getContentType() == null ? 0 : adminArticleDto.getContentType();
        if(contentType != 0){
            QueryWrapper<ColumnEntity> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("content_type",contentType);
            List<ColumnEntity> columnEntities = columnMapper.selectList(objectQueryWrapper);
            if(CollUtil.isNotEmpty(columnEntities)){
                List<Long> columIds = new ArrayList<>();
                for(ColumnEntity columnEntity : columnEntities){
                    Long id = columnEntity.getId();
                    columIds.add(id);
                }
                articleEntity.setColumnIds(columIds);
            }
        }
        articleEntity.setCompanyId(24L);
        IPage<AdminArticleVo> list = articleMapper.selectAdminArticleInPage(page,articleEntity);
        System.out.println(list.getRecords());
    }
 
}