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);
|
}
|
|
}
|