whycxzp
2021-03-15 cc01405e87a3088500ce147c1c5068a52f6a9755
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
package com.whyc.service;
 
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;
 
@Service
public class ApplicationService {
 
    @Resource
    private ApplicationMapper mapper;
 
    @Resource
    private ApplicationConfigMapper configMapper;
 
    @Resource
    private ApplicationConfigPicMapper configPicMapper;
 
    @Resource
    private UserInfMapper userInfMapper;
 
    /*======应用管理,admin才有权限更改======*/
    public Response insert(ApplicationDTO app) {
        UserInf userInf = userInfMapper.selectById(app.getUserId());
        if(!userInf.getUName().equals("admin")){
            return new Response<>().setMsg(0,"创建应用需admin权限");
        }
        mapper.insertApp(app.getApplication());
        return new Response<>().set(1,app,"创建成功");
    }
 
    public Response getAll() {
        List<Application> applications = mapper.selectList(null);
        return new Response<>().set(1,applications);
    }
 
    public Response update(ApplicationDTO app) {
        UserInf userInf = userInfMapper.selectById(app.getUserId());
        if(!userInf.getUName().equals("admin")){
            return new Response<>().setMsg(0,"更新应用需admin权限");
        }
        int res = mapper.updateById(app.getApplication());
        if(res==1){
            return new Response<>().setMsg(1,"更新成功");
        }else{
            return new Response<>().setMsg(0,"更新失败");
        }
    }
 
    public Response delete(ApplicationDTO app) {
        UserInf userInf = userInfMapper.selectById(app.getUserId());
        if(!userInf.getUName().equals("admin")){
            return new Response<>().setMsg(0,"创建应用需admin权限");
        }
        //删除应用
        mapper.deleteById(app.getApplication().getId());
        //删除应用的模块配置
        UpdateWrapper<ApplicationConfig> updateWrapper = Wrappers.update();
        updateWrapper.eq("app_id",app.getApplication().getId());
        configMapper.delete(updateWrapper);
        //删除应用的模块图片
        UpdateWrapper<ApplicationConfigPic> wrapper = Wrappers.update();
        wrapper.eq("app_id",app.getApplication().getId());
        configPicMapper.delete(wrapper);
 
        return new Response<>().setMsg(1,"删除成功");
    }
 
    /*======应用配置======*/
    @Transactional
    public Response saveConfig(ApplicationConfigDTO configDTO) {
        try {
            //保存应用的图片信息,appId+userId为联合主键
            ApplicationConfigPic configPic = new ApplicationConfigPic();
            configPic.setAppId(configDTO.getAppId());
            configPic.setUserId(configDTO.getUserId());
            configPic.setBgPic(configDTO.getBgPic());
            configPic.setHeadPic(configDTO.getHeadPic());
 
            configPicMapper.insertOrUpdate(configPic);
 
            //首先删除原有配置,再保存配置
            UpdateWrapper<ApplicationConfig> wrapper = Wrappers.update();
            wrapper.eq("app_id",configDTO.getAppId()).eq("user_id",configDTO.getUserId());
            configMapper.delete(wrapper);
            configMapper.saveConfig(configDTO);
        }catch (Exception e){
            return new Response<>().setMsg(0,"保存失败");
        }
        return new Response<>().setMsg(1,"保存成功");
    }
 
    /**查询应用和对应的配置模块*/
    public Response getAllConfig(int appId,int userId) {
        ApplicationConfigDTO applicationConfigDTO = configMapper.getAllConfig(appId,userId);
        return new Response<>().set(1,applicationConfigDTO);
    }
 
}