whycxzp
2021-10-12 d5cf76188ad37f062f37ebce3464097203b4db05
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.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.dto.ApplicationConfigDTO;
import com.whyc.dto.ApplicationDTO;
import com.whyc.dto.Response;
import com.whyc.mapper.*;
import com.whyc.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class ApplicationService {
 
    @Resource
    private ApplicationMapper mapper;
 
    @Resource
    private ApplicationConfigMapper configMapper;
 
    /*======应用管理======*/
    public Response insert(Application app) {
        //设置当前用户下的其他应用激活状态为0,本应用为1
        UpdateWrapper<Application> wrapper = Wrappers.update();
        wrapper.set("active_status",0).eq("user_id",app.getUserId());
        mapper.update(null,wrapper);
 
        app.setActiveStatus(1);
        mapper.insert(app);
        return new Response<>().set(1,app,"创建成功");
    }
 
    public Response getAll(Integer userId) {
        QueryWrapper<Application> wrapper = Wrappers.query();
        /**wrapper.eq("user_id",userId);*/
        //新增默认应用,在查询的时候,添加上默认应用
        wrapper.in("user_id",userId,0);
        List<Application> applications = mapper.selectList(wrapper);
        //如果没有设置为激活的,则将默认应用设置为手动设置为激活,供展示
        boolean noMatchElement = applications.stream().noneMatch(application -> application.getActiveStatus() == 1);
        if(noMatchElement){
            for(Application temp:applications){
                if(temp.getId()==0){
                    temp.setActiveStatus(1);
                    break;
                }
            }
        }
        return new Response<>().set(1,applications);
    }
 
    @Transactional
    public Response update(Application app) {
        int res = 0;
        if(app.getId()!=0) {
            res = mapper.updateById(app);
        }else{
            //更新的为默认应用-布局
            //复制默认布局为当前用户布局
            app.setId(null);
            app.setActiveStatus(0);
            int res1 = mapper.insert(app);
            //查询默认布局
            ApplicationConfigDTO allConfig = configMapper.getAllConfig(0);
            List<ApplicationConfig> applicationConfigList = allConfig.getChildren();
            for (ApplicationConfig config:applicationConfigList){
                config.setAppId(app.getId());
            }
            int res2 = configMapper.insertBatchSomeColumn(applicationConfigList);
            if(res1==1 && res2==applicationConfigList.size()){
                res = 1;
            }
        }
        if (res == 1) {
            return new Response<>().setMsg(1, "更新成功");
        } else {
            return new Response<>().setMsg(0, "更新失败");
        }
    }
 
    public Response delete(Integer appId) {
        if(appId!=0) {
            //删除应用
            mapper.deleteById(appId);
            //删除应用的模块配置
            UpdateWrapper<ApplicationConfig> updateWrapper = Wrappers.update();
            updateWrapper.eq("app_id", appId);
            configMapper.delete(updateWrapper);
            return new Response<>().setMsg(1,"删除成功");
        }else{
            return new Response<>().setMsg(0,"无法删除默认大屏展示");
        }
 
    }
 
    public Response updateActive(Application app) {
        UpdateWrapper<Application> wrapper = Wrappers.update();
        wrapper.set("active_status",0).eq("user_id",app.getUserId()).eq("active_status",1).last("limit 1");
        mapper.update(null,wrapper);
 
        if(app.getId()!=0) {
            app.setActiveStatus(1);
            mapper.updateById(app);
        }else{
            //如果是激活 默认布局,无需操作
 
        }
 
        return new Response().setMsg(1,"激活成功");
    }
 
    public Response getActive(Integer userId) {
        QueryWrapper<Application> wrapper = Wrappers.query();
        wrapper.select("id","name").eq("user_id",userId).eq("active_status",1);
        Application application = mapper.selectOne(wrapper);
        //如果没有激活的应用,则展示 默认应用布局
        if(application==null){
            QueryWrapper<Application> wrapper2 = Wrappers.query();
            wrapper2.select("id","name").eq("id",0);
            application = mapper.selectOne(wrapper2);
        }
        return new Response().set(1,application);
    }
 
    /*======应用配置======*/
    @Transactional
    public Response saveConfig(ApplicationConfigDTO configDTO) {
        try {
            //保存应用的图片信息
            Application app = new Application();
            app.setId(configDTO.getAppId());
            app.setBgPic(configDTO.getBgPic());
            app.setHeadPic(configDTO.getHeadPic());
 
            mapper.updateById(app);
 
            if (configDTO.getAppId() != 0){
                //首先删除原有配置,再保存配置
                UpdateWrapper<ApplicationConfig> wrapper = Wrappers.update();
                wrapper.eq("app_id", configDTO.getAppId());
                configMapper.delete(wrapper);
                configMapper.saveConfig(configDTO);
                return new Response<>().setMsg(1,"保存成功");
            }else{
                return new Response<>().setMsg(0,"修改默认大屏展示布局前,需先重命名应用名称");
            }
        }catch (Exception e){
            return new Response<>().setMsg(0,"保存失败");
        }
    }
 
    /**查询应用和对应的配置模块*/
    public Response getAllConfig(Integer appId) {
        ApplicationConfigDTO applicationConfigDTO = configMapper.getAllConfig(appId);
        return new Response<>().set(1,applicationConfigDTO);
    }
 
}