zqy
2024-11-10 e2e816919caa81711a5bdf6768257c4bb3a9f3af
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
package com.ruoyi.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EsModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ZSelfNote;
import com.ruoyi.mapper.ZSelfNoteMapper;
import com.ruoyi.service.EsService;
import com.ruoyi.service.ZSelfNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ojq
 * @since 2023-03-14
 */
@Service
public class ZSelfNoteServiceImpl extends ServiceImpl<ZSelfNoteMapper, ZSelfNote> implements ZSelfNoteService {
 
    @Autowired
    ZSelfNoteService zSelfNoteService;
 
    @Resource
    private EsService esSer;
 
    private LambdaQueryWrapper<ZSelfNote> uniqueCondition(ZSelfNote zSelfNote) {
        LambdaQueryWrapper<ZSelfNote> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(zSelfNote.getAddress()), ZSelfNote::getAddress, zSelfNote.getAddress())
                .eq(StringUtils.isNotEmpty(zSelfNote.getPeople()), ZSelfNote::getPeople, zSelfNote.getPeople())
                .eq(zSelfNote.getHappenTime() != null, ZSelfNote::getHappenTime, zSelfNote.getHappenTime())
                .eq(StringUtils.isNotEmpty(zSelfNote.getTitle()), ZSelfNote::getTitle, zSelfNote.getTitle())
                .eq(StringUtils.isNotEmpty(zSelfNote.getRemark()), ZSelfNote::getRemark, zSelfNote.getRemark())
                .eq(zSelfNote.getUid() != null, ZSelfNote::getUid, zSelfNote.getUid());
        return lqw;
    }
 
    private LambdaQueryWrapper<ZSelfNote> buildCondition(ZSelfNote zSelfNote, Long userId) {
        LambdaQueryWrapper<ZSelfNote> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId != null, ZSelfNote::getUid, userId)
                .like(StringUtils.isNotEmpty(zSelfNote.getPeople()), ZSelfNote::getPeople, zSelfNote.getPeople())
                .like(StringUtils.isNotEmpty(zSelfNote.getTitle()), ZSelfNote::getTitle, zSelfNote.getTitle())
                .like(StringUtils.isNotEmpty(zSelfNote.getAddress()),ZSelfNote::getAddress,zSelfNote.getAddress())
                .like(StringUtils.isNotEmpty(zSelfNote.getRemark()),ZSelfNote::getRemark,zSelfNote.getRemark())
                .between(zSelfNote.getHappenStartTime() != null && zSelfNote.getHappenEndTime() != null, ZSelfNote::getHappenTime, zSelfNote.getHappenStartTime(), zSelfNote.getHappenEndTime())
                .orderByDesc(ZSelfNote::getCreateTime);
        return lqw;
    }
 
    /**
     * 分页查找
     */
    @Override
    public AjaxResult selectDataList(ZSelfNote zSelfNote, Integer pageNum, Integer pageSize) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        LambdaQueryWrapper<ZSelfNote> lqw = buildCondition(zSelfNote, userId);
 
        Page<ZSelfNote> pageBean = new Page<>(pageNum, pageSize);
        Page<ZSelfNote> pageResult = page(pageBean, lqw);
 
        List<ZSelfNote> beanRecords = pageResult.getRecords();//得到查询出来的数据
 
        HashMap<String, Object> data = MapUtils.getResult(pageResult, beanRecords);
        return AjaxResult.success(data);
 
    }
 
 
    @Override
    public List<ZSelfNote> selectByIds(Long[] ids) {
        List<ZSelfNote> list = new ArrayList<>();
        if (ids.length != 0)
            list = listByIds(Arrays.asList(ids));
        else
            list = list();
        return list;
    }
 
    @Override
    public AjaxResult mySave(ZSelfNote zSelfNote) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        zSelfNote.setUid(userId);
 
        //检查是否有重复数据插入
        LambdaQueryWrapper<ZSelfNote> lqw = uniqueCondition(zSelfNote);
        List<ZSelfNote> list = list(lqw);
        if (list.size() > 0) {
            throw new RuntimeException("请勿新增重复数据");
        }
 
        if (save(zSelfNote)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
 
    }
 
    @Override
    public List<ZSelfNote> selectByCondition() {
 
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
 
        ZSelfNote zSelfNote = new ZSelfNote();
        LambdaQueryWrapper<ZSelfNote> lqw = buildCondition(zSelfNote, userId);
        return list(lqw);
    }
 
    @Override
    @Transactional
    public AjaxResult importExcel(MultipartFile file) {
 
        ExcelUtil<ZSelfNote> util = new ExcelUtil<>(ZSelfNote.class);
        List<ZSelfNote> dataList = null;
        try {
            dataList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("没有按照规则导入数据");
        }
 
        assert dataList != null;
 
        for (ZSelfNote zSelfNote : dataList) {
 
            if (zSelfNote.getTitle().length() != 0 && zSelfNote.getPeople().length() !=0){
                mySave(zSelfNote);}
            else {
                throw new RuntimeException("有数据的标题或人物为空");
            }
        }
 
        return AjaxResult.success();
 
    }
 
}