From d5cf76188ad37f062f37ebce3464097203b4db05 Mon Sep 17 00:00:00 2001
From: whycxzp <perryhsu@163.com>
Date: 星期二, 12 十月 2021 15:56:48 +0800
Subject: [PATCH] 去除电池状态-均充

---
 src/main/java/com/whyc/service/ApplicationService.java |  133 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 108 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/whyc/service/ApplicationService.java b/src/main/java/com/whyc/service/ApplicationService.java
index cfa1681..a9b92e0 100644
--- a/src/main/java/com/whyc/service/ApplicationService.java
+++ b/src/main/java/com/whyc/service/ApplicationService.java
@@ -5,16 +5,16 @@
 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.ApplicationConfigMapper;
-import com.whyc.mapper.ApplicationMapper;
-import com.whyc.pojo.Application;
-import com.whyc.pojo.ApplicationConfig;
+import com.whyc.mapper.*;
+import com.whyc.pojo.*;
 import org.springframework.stereotype.Service;
-import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @Service
 public class ApplicationService {
@@ -25,37 +25,112 @@
     @Resource
     private ApplicationConfigMapper configMapper;
 
+    /*======搴旂敤绠$悊======*/
     public Response insert(Application app) {
-        mapper.insertApp(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() {
-        List<Application> applications = mapper.selectList(null);
+    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 = mapper.updateById(app);
-        if(res==1){
-            return new Response<>().setMsg(1,"鏇存柊鎴愬姛");
+        int res = 0;
+        if(app.getId()!=0) {
+            res = mapper.updateById(app);
         }else{
-            return new Response<>().setMsg(0,"鏇存柊澶辫触");
+            //鏇存柊鐨勪负榛樿搴旂敤-甯冨眬
+            //澶嶅埗榛樿甯冨眬涓哄綋鍓嶇敤鎴峰竷灞�
+            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(Application app) {
-        //鍒犻櫎搴旂敤
-        mapper.deleteById(app.getId());
-        //鍒犻櫎搴旂敤鐨勬ā鍧楅厤缃�
-        UpdateWrapper<ApplicationConfig> updateWrapper = Wrappers.update();
-        updateWrapper.eq("app_id",app.getId());
-        configMapper.delete(updateWrapper);
-        return new Response<>().setMsg(1,"鍒犻櫎鎴愬姛");
+    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 {
             //淇濆瓨搴旂敤鐨勫浘鐗囦俊鎭�
@@ -63,18 +138,26 @@
             app.setId(configDTO.getAppId());
             app.setBgPic(configDTO.getBgPic());
             app.setHeadPic(configDTO.getHeadPic());
+
             mapper.updateById(app);
 
-            //淇濆瓨搴旂敤鐨勯厤缃�
-            configMapper.saveConfig(configDTO);
+            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,"淇濆瓨澶辫触");
         }
-        return new Response<>().setCode(1);
     }
 
     /**鏌ヨ搴旂敤鍜屽搴旂殑閰嶇疆妯″潡*/
-    public Response getAllConfig(int appId) {
+    public Response getAllConfig(Integer appId) {
         ApplicationConfigDTO applicationConfigDTO = configMapper.getAllConfig(appId);
         return new Response<>().set(1,applicationConfigDTO);
     }

--
Gitblit v1.9.1