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;
|
|
@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);
|
List<Application> applications = mapper.selectList(wrapper);
|
return new Response<>().set(1,applications);
|
}
|
|
public Response update(Application app) {
|
int res = mapper.updateById(app);
|
if(res==1){
|
return new Response<>().setMsg(1,"更新成功");
|
}else{
|
return new Response<>().setMsg(0,"更新失败");
|
}
|
}
|
|
public Response delete(Integer appId) {
|
//删除应用
|
mapper.deleteById(appId);
|
//删除应用的模块配置
|
UpdateWrapper<ApplicationConfig> updateWrapper = Wrappers.update();
|
updateWrapper.eq("app_id",appId);
|
configMapper.delete(updateWrapper);
|
|
return new Response<>().setMsg(1,"删除成功");
|
}
|
|
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);
|
|
app.setActiveStatus(1);
|
mapper.updateById(app);
|
|
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);
|
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);
|
|
//首先删除原有配置,再保存配置
|
UpdateWrapper<ApplicationConfig> wrapper = Wrappers.update();
|
wrapper.eq("app_id",configDTO.getAppId());
|
configMapper.delete(wrapper);
|
configMapper.saveConfig(configDTO);
|
}catch (Exception e){
|
return new Response<>().setMsg(0,"保存失败");
|
}
|
return new Response<>().setMsg(1,"保存成功");
|
}
|
|
/**查询应用和对应的配置模块*/
|
public Response getAllConfig(Integer appId) {
|
ApplicationConfigDTO applicationConfigDTO = configMapper.getAllConfig(appId);
|
return new Response<>().set(1,applicationConfigDTO);
|
}
|
|
}
|