From 3ced1dd8f97aa1628d5ff19bb43add39dc4cb318 Mon Sep 17 00:00:00 2001
From: licc <lichuchuan@jtep.com.cn>
Date: Mon, 8 Mar 2021 15:42:13 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E6=88=B7=E8=A1=A8=E9=95=9C=E5=83=8F?=
 =?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../wisenergy/mapper/LastAccountMapper.java   | 24 ++++++++++++++-
 .../resources/mapper/LastAccountMapper.xml    | 14 +++++++++
 .../wisenergy/service/app/AccountService.java |  5 ++++
 .../service/app/impl/AccountServiceImpl.java  | 27 +++++++++++++++++
 .../controller/app/LastAccountController.java | 30 +++++++++++++++++++
 5 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/LastAccountController.java

diff --git a/wisenergy-mapper/src/main/java/cn/wisenergy/mapper/LastAccountMapper.java b/wisenergy-mapper/src/main/java/cn/wisenergy/mapper/LastAccountMapper.java
index ebc0780..32dcda0 100644
--- a/wisenergy-mapper/src/main/java/cn/wisenergy/mapper/LastAccountMapper.java
+++ b/wisenergy-mapper/src/main/java/cn/wisenergy/mapper/LastAccountMapper.java
@@ -1,6 +1,5 @@
 package cn.wisenergy.mapper;
 
-import cn.wisenergy.model.app.AccountInfo;
 import cn.wisenergy.model.app.LastMonthAccount;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
@@ -15,4 +14,27 @@ public interface LastAccountMapper extends BaseMapper<LastMonthAccount> {
      * @return 账户信息
      */
     LastMonthAccount getByUserIdAndTime(@Param("userId") String userId, @Param("yearMonth") String yearMonth);
+
+    /**
+     * 把 A 表的结构数据 复制给 B表
+     *
+     * @param oldTable A
+     * @param newTable B
+     */
+    void copyTable(@Param("oldTable") String oldTable, @Param("newTable") String newTable);
+
+    /**
+     * 删除表
+     *
+     * @param tableName 表名
+     */
+    void deleteTable(@Param("tableName") String tableName);
+
+    /**
+     * 更新表名
+     *
+     * @param oldTableName 旧表名
+     * @param newTableName 新表名
+     */
+    void updateTableName(@Param("oldTableName") String oldTableName, @Param("newTableName") String newTableName);
 }
diff --git a/wisenergy-mapper/src/main/resources/mapper/LastAccountMapper.xml b/wisenergy-mapper/src/main/resources/mapper/LastAccountMapper.xml
index 2041c23..e01b71b 100644
--- a/wisenergy-mapper/src/main/resources/mapper/LastAccountMapper.xml
+++ b/wisenergy-mapper/src/main/resources/mapper/LastAccountMapper.xml
@@ -56,6 +56,10 @@
         <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
     </sql>
 
+    <delete id="deleteTable">
+        drop table ${tableName}
+    </delete>
+
 
     <select id="getByUserIdAndTime" resultType="cn.wisenergy.model.app.LastMonthAccount">
         select
@@ -68,4 +72,14 @@
         </where>
     </select>
 
+
+    <update id="copyTable">
+        CREATE TABLE ${newTable} SELECT * FROM ${oldTable};
+    </update>
+
+    <update id="updateTableName">
+        rename table ${oldTableName} to ${newTableName};
+    </update>
+
+
 </mapper>
\ No newline at end of file
diff --git a/wisenergy-service/src/main/java/cn/wisenergy/service/app/AccountService.java b/wisenergy-service/src/main/java/cn/wisenergy/service/app/AccountService.java
index d7cbf27..f4f5243 100644
--- a/wisenergy-service/src/main/java/cn/wisenergy/service/app/AccountService.java
+++ b/wisenergy-service/src/main/java/cn/wisenergy/service/app/AccountService.java
@@ -52,4 +52,9 @@ public interface AccountService {
      * @return true or false
      */
     R<Boolean> progressPrizeCount();
+
+    /**
+     * 账户表镜像---每月更新一次,保存上一个的数据
+     */
+    void  mirrorImage();
 }
diff --git a/wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/AccountServiceImpl.java b/wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/AccountServiceImpl.java
index 5c40ff9..740487d 100644
--- a/wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/AccountServiceImpl.java
+++ b/wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/AccountServiceImpl.java
@@ -55,6 +55,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
     @Autowired
     private ProgressPrizeMapper progressPrizeMapper;
 
+    @Autowired
+    private LastAccountMapper lastAccountMapper;
+
     private static final String PATTERN = "yyyy-MM";
 
     private static final Integer TWENTY = 20;
@@ -282,6 +285,30 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
         return R.ok(0, true);
     }
 
+    @Override
+    public void mirrorImage() {
+        //1、把账户表account_info复制给表account_image  CREATE TABLE table_2  SELECT * FROM table_1;
+        lastAccountMapper.copyTable("account_info", "account_image");
+
+        //删除上月备份
+        lastAccountMapper.deleteTable("account_backup");
+
+        //备份
+        lastAccountMapper.copyTable("account_info", "account_backup");
+
+        //2、把上月账户表last_month_account 复制给month_account_image
+        lastAccountMapper.copyTable("last_month_account", "month_account_image");
+
+        //3、删除last_month_account
+        lastAccountMapper.deleteTable("last_month_account");
+
+        //4、把account_image 更名为 last_month_account  rename table table_2 to table_1;
+        lastAccountMapper.updateTableName("account_image", "last_month_account");
+
+        //5、删除month_account_image   DROP table table_2;
+        lastAccountMapper.deleteTable("month_account_image");
+    }
+
     public void getUser(List<User> list, String userId) {
         User user = usersMapper.getByUserId(userId);
         list.add(user);
diff --git a/wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/LastAccountController.java b/wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/LastAccountController.java
new file mode 100644
index 0000000..0b9f53e
--- /dev/null
+++ b/wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/LastAccountController.java
@@ -0,0 +1,30 @@
+package cn.wisenergy.web.admin.controller.app;
+
+
+import cn.wisenergy.service.app.AccountService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author 86187
+ */
+@Api(tags = "表镜像管理")
+@RestController
+@RequestMapping("/image")
+@Slf4j
+public class LastAccountController {
+    @Autowired
+    private AccountService accountService;
+
+    @ApiOperation(value = "复制表-结构和数据", notes = "复制表-结构和数据", httpMethod = "PUT")
+    @PutMapping("/add")
+    public void copyTable(){
+        accountService.mirrorImage();
+    }
+}
-- 
2.18.1