From 3691553aeb5a4cfce3432f509cf1e9664a5eb553 Mon Sep 17 00:00:00 2001
From: purple <purple_lihe@163.com>
Date: Mon, 29 Jun 2020 19:03:19 +0800
Subject: [PATCH] =?UTF-8?q?bug(=E5=8A=9E=E5=85=AC=E6=AF=8F=E6=9C=88?=
 =?UTF-8?q?=E4=BD=9C=E4=BB=B7)=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=AF=BC?=
 =?UTF-8?q?=E5=85=A5=E4=BA=BA=E5=B7=A5=E4=BF=AE=E6=AD=A3=E5=8A=9E=E5=85=AC?=
 =?UTF-8?q?=E4=BD=9C=E4=BB=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../UltimateOfficeBasePriceController.java    |   2 +-
 .../price/domain/UltimateOfficeBasePrice.java |   2 +-
 .../UltimateOfficeBasePriceServiceImpl.java   |  37 ++-
 .../data/UltimateOfficeBasePriceMapper.xml    |  49 ++--
 .../sql-template/update_office_price.sql      | 264 ++++++++++++++++++
 5 files changed, 326 insertions(+), 28 deletions(-)
 create mode 100644 ruoyi/src/main/resources/sql-template/update_office_price.sql

diff --git a/ruoyi/src/main/java/com/ruoyi/project/data/price/controller/UltimateOfficeBasePriceController.java b/ruoyi/src/main/java/com/ruoyi/project/data/price/controller/UltimateOfficeBasePriceController.java
index 3ec2ca53d..638d01026 100644
--- a/ruoyi/src/main/java/com/ruoyi/project/data/price/controller/UltimateOfficeBasePriceController.java
+++ b/ruoyi/src/main/java/com/ruoyi/project/data/price/controller/UltimateOfficeBasePriceController.java
@@ -115,7 +115,7 @@ public class UltimateOfficeBasePriceController extends BaseController {
     public AjaxResult importData(@PathVariable("yearMonth") Integer yearMonth, MultipartFile file) throws Exception {
         // 修改计价
         ExcelUtil<UltimateOfficeBasePrice> util = new ExcelUtil<>(UltimateOfficeBasePrice.class);
-        List<UltimateOfficeBasePrice> officeBasePriceUltimates = util.importExcel("主表",file.getInputStream());
+        List<UltimateOfficeBasePrice> officeBasePriceUltimates = util.importExcel(file.getInputStream());
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         String operName = loginUser.getUsername();
         String message = officeBasePriceUltimateService.batchImport(yearMonth, officeBasePriceUltimates, operName);
diff --git a/ruoyi/src/main/java/com/ruoyi/project/data/price/domain/UltimateOfficeBasePrice.java b/ruoyi/src/main/java/com/ruoyi/project/data/price/domain/UltimateOfficeBasePrice.java
index 2cabf8b5c..f7176dcb2 100644
--- a/ruoyi/src/main/java/com/ruoyi/project/data/price/domain/UltimateOfficeBasePrice.java
+++ b/ruoyi/src/main/java/com/ruoyi/project/data/price/domain/UltimateOfficeBasePrice.java
@@ -82,7 +82,7 @@ public class UltimateOfficeBasePrice extends BaseEntity {
     private Boolean status;
     @Excel(name = "是否标准楼栋")
     private Boolean isStandardBuilding;
-    @Excel(name = "更改价格说明")
+//    @Excel(name = "更改价格说明")
     private String adjustPriceComment;
 
     public Integer getLastYearMonth() {
diff --git a/ruoyi/src/main/java/com/ruoyi/project/data/price/service/impl/UltimateOfficeBasePriceServiceImpl.java b/ruoyi/src/main/java/com/ruoyi/project/data/price/service/impl/UltimateOfficeBasePriceServiceImpl.java
index aef68e6ce..0770f2ff8 100644
--- a/ruoyi/src/main/java/com/ruoyi/project/data/price/service/impl/UltimateOfficeBasePriceServiceImpl.java
+++ b/ruoyi/src/main/java/com/ruoyi/project/data/price/service/impl/UltimateOfficeBasePriceServiceImpl.java
@@ -1,9 +1,12 @@
 package com.ruoyi.project.data.price.service.impl;
 
 import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.util.*;
 
+import com.baomidou.dynamic.datasource.annotation.DS;
 import com.ruoyi.common.exception.CustomException;
+import com.ruoyi.common.utils.LoadUtil;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.config.UVConfig;
 import com.ruoyi.project.common.UVResponse;
@@ -16,6 +19,7 @@ import com.ruoyi.project.system.service.impl.SysUserServiceImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
@@ -26,6 +30,7 @@ import org.springframework.web.client.RestTemplate;
  * @date 2020-05-20
  */
 @Service
+@DS("compute")
 public class UltimateOfficeBasePriceServiceImpl implements IUltimateOfficeBasePriceService {
 
     private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
@@ -34,7 +39,8 @@ public class UltimateOfficeBasePriceServiceImpl implements IUltimateOfficeBasePr
     private UltimateOfficeBasePriceMapper officeBasePriceUltimateMapper;
     @Autowired
     private UVConfig uvConfig;
-
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
 
     private static Integer getLastYearMonth(Integer yearMonth) {
         Calendar calendar = Calendar.getInstance();
@@ -84,10 +90,31 @@ public class UltimateOfficeBasePriceServiceImpl implements IUltimateOfficeBasePr
             officeBasePriceUltimateMapper.insertArtificialOfficeBasePrice(inputModel);
         });
 
-        RestTemplate restTemplate = new RestTemplate();
-        String url = String.format(uvConfig.getAitificialOfficeBasePriceUrl(), yearMonth, lastYearMonth);
-        UVResponse<Integer> affectCount = restTemplate.getForObject(url, UVResponse.class);
-        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,修改数据:" + affectCount.getData() + "条");
+//        RestTemplate restTemplate = new RestTemplate();
+//        String url = String.format(uvConfig.getAitificialOfficeBasePriceUrl(), yearMonth, lastYearMonth);
+//        UVResponse<Integer> affectCount = restTemplate.getForObject(url, UVResponse.class);
+
+        String rawSql =  LoadUtil.loadContent("sql-template/update_office_price.sql");
+        Calendar calendar = Calendar.getInstance();
+        int year = yearMonth / 100;
+        int month = yearMonth % 100;
+        calendar.set(year, month, 1);
+        Date firstOfMonth = calendar.getTime();
+        calendar.add(Calendar.MONTH, -1);
+        Date lastMonth = calendar.getTime();
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+
+        String sql = rawSql.replace("#yearMonth#", yearMonth.toString())
+                .replace("#lastYearMonth#", lastYearMonth.toString())
+                .replace("#today#", simpleDateFormat.format(firstOfMonth))
+                .replace("#lastMonth#", simpleDateFormat.format(lastMonth));
+        jdbcTemplate.update(sql);
+
+//        String rawSql =
+//        String sql = rawSql.replace("#yearMonth#", yearMonth.toString());
+//        jdbcTemplate.execute(sql);
+
+        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条");
         return successMsg.toString();
     }
 
diff --git a/ruoyi/src/main/resources/mybatis/data/UltimateOfficeBasePriceMapper.xml b/ruoyi/src/main/resources/mybatis/data/UltimateOfficeBasePriceMapper.xml
index 6b00e4238..da5a98e2d 100644
--- a/ruoyi/src/main/resources/mybatis/data/UltimateOfficeBasePriceMapper.xml
+++ b/ruoyi/src/main/resources/mybatis/data/UltimateOfficeBasePriceMapper.xml
@@ -78,37 +78,44 @@
 
     <select id="getCount" parameterType="com.ruoyi.project.data.price.domain.UltimateOfficeBasePrice" resultType="int">
         select count(1) FROM ODS_OFFICE_BUILDING_PRICE_INFO_${yearMonth} a
-        <if test="communityId != null">
-            AND a.PROJECTID_P = #{communityId}
-        </if>
-        <if test="buildingId != null">
-            AND a.BUILDINGID_P = #{buildingId}
-        </if>
-        <if test="status != null">
-            AND a.STATUS = #{status}
-        </if>
+        <where>
+            <if test="communityId != null">
+                AND a.PROJECTID_P = #{communityId}
+            </if>
+            <if test="buildingId != null">
+                AND a.BUILDINGID_P = #{buildingId}
+            </if>
+            <if test="status != null">
+                AND a.STATUS = #{status}
+            </if>
+        </where>
     </select>
 
     <select id="getList" parameterType="com.ruoyi.project.data.price.domain.UltimateOfficeBasePrice"
             resultMap="OfficeBasePriceUltimateResult">
         <include refid="getById"/>
-        <if test="communityId != null">
-            AND a.PROJECTID_P = #{communityId}
-        </if>
-        <if test="buildingId != null">
-            AND a.BUILDINGID_P = #{buildingId}
-        </if>
-        <if test="status != null">
-            AND a.STATUS = #{status}
-        </if>
+        <where>
+            <if test="communityId != null">
+                AND a.PROJECTID_P = #{communityId}
+            </if>
+            <if test="buildingId != null">
+                AND a.BUILDINGID_P = #{buildingId}
+            </if>
+            <if test="status != null">
+                AND a.STATUS = #{status}
+            </if>
+        </where>
+
         order by a.id desc OFFSET #{pageIndex} rows fetch next #{pageSize} rows only;
     </select>
 
     <select id="getById" resultMap="OfficeBasePriceUltimateResult">
         <include refid="getById"/>
-        <if test="id != null">
-            AND a.id=#{id}
-        </if>
+        <where>
+            <if test="id != null">
+                AND a.id=#{id}
+            </if>
+        </where>
     </select>
 
     <!-- 更新基价 -->
diff --git a/ruoyi/src/main/resources/sql-template/update_office_price.sql b/ruoyi/src/main/resources/sql-template/update_office_price.sql
new file mode 100644
index 000000000..fd008d2e5
--- /dev/null
+++ b/ruoyi/src/main/resources/sql-template/update_office_price.sql
@@ -0,0 +1,264 @@
+select a.BuildingID as BuildingID_P
+     , a.ProjectID  as ProjectID_P
+     , a.MainPrice
+     , a.MainPriceRent
+     , a.MainPrice_1
+     , a.MainPriceRent_1
+     , getdate()    as ModifyDate
+     , case
+           when b.BuildingID is not null
+                and cast(a.MainPrice as decimal(18, 1)) <> cast(isnull(b.MainPrice, 0) as decimal(18, 1)) then
+               1
+           else
+               0
+       end          as MainPriceChg
+     , case
+           when b.BuildingID is not null
+                and cast(a.MainPriceRent as decimal(18, 1)) <> cast(isnull(b.MainPriceRent, 0) as decimal(18, 1)) then
+               1
+           else
+               0
+       end          as MainPriceRentChg
+     , case
+           when c.BuildingID is not null
+                and cast(a.MainPrice_1 as decimal(18, 1)) <> cast(isnull(c.MainPrice, 0) as decimal(18, 1)) then
+               1
+           else
+               0
+       end          as MainPrice_1Chg
+     , case
+           when c.BuildingID is not null
+                and cast(a.MainPriceRent_1 as decimal(18, 1)) <> cast(isnull(c.MainPriceRent, 0) as decimal(18, 1)) then
+               1
+           else
+               0
+       end          as MainPriceRent_1Chg
+into #InfoChg
+from dbo.DWA_PROJECTBASEPRICE_OFFICE_MANU_#yearMonth#    a
+    left join ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth# b
+        on a.BuildingID = b.BuildingID_P
+           and b.Status = 1
+           and
+           (
+               cast(a.MainPrice as decimal(18, 1)) <> cast(isnull(b.MainPrice, 0) as decimal(18, 1))
+               or cast(a.MainPriceRent as decimal(18, 1)) <> cast(isnull(b.MainPriceRent, 0) as decimal(18, 1))
+           )
+    left join ODS_OFFICE_BUILDING_PRICE_INFO_#lastYearMonth# c
+        on a.BuildingID = c.BuildingID_P
+           and c.Status = 1
+           and
+           (
+               cast(a.MainPrice_1 as decimal(18, 1)) <> cast(isnull(c.MainPrice, 0) as decimal(18, 1))
+               or cast(a.MainPriceRent_1 as decimal(18, 1)) <> cast(isnull(c.MainPriceRent, 0) as decimal(18, 1))
+           )
+where b.BuildingID_P is not null
+      or c.BuildingID_P is not null;
+
+
+select a.UnifiedID
+     , a.BuildingID_P
+     , a.ProjectID_P
+     , b.MainPrice_1
+     , b.MainPriceRent_1
+     , a.MainPricePst
+     , a.MainPriceRentPst
+     , a.MainPriceType
+     , a.MainPriceRentType
+     , b.ModifyDate
+     , 1         as Status
+     , a.BuildingStd
+     , case
+           when MainPrice_1Chg = 0 then
+               ''
+           else
+               'MainPrice_1Chg|'
+       end + case
+                 when MainPriceRent_1Chg = 0 then
+                     ''
+                 else
+                     'MainPriceRent_1Chg|'
+             end as AdjEvd
+into #InfoChgLst
+from ODS_OFFICE_BUILDING_PRICE_INFO_#lastYearMonth# a
+    inner join #InfoChg                    b
+        on a.BuildingID_P = b.BuildingID_P
+where (
+          b.MainPrice_1Chg = 1
+          or b.MainPriceRent_1Chg = 1
+      )
+      and a.Status = 1;
+
+update a
+set a.Status = 0
+from ODS_OFFICE_BUILDING_PRICE_INFO_#lastYearMonth# a
+    left join #InfoChgLst                  b
+        on a.BuildingID_P = b.BuildingID_P
+where b.BuildingID_P is not null
+      and a.Status = 1;
+
+insert into dbo.ODS_OFFICE_BUILDING_PRICE_INFO_#lastYearMonth#
+(
+    ID
+  , UnifiedID
+  , BuildingID_P
+  , ProjectID_P
+  , MainPrice
+  , MainPriceRent
+  , MainPricePst
+  , MainPriceRentPst
+  , MainPriceType
+  , MainPriceRentType
+  , ModifyDate
+  , Status
+  , BuildingStd
+  , AdjEvd
+)
+select newid()
+     , UnifiedID
+     , BuildingID_P
+     , ProjectID_P
+     , MainPrice_1
+     , MainPriceRent_1
+     , MainPricePst
+     , MainPriceRentPst
+     , MainPriceType
+     , MainPriceRentType
+     , getdate()
+     , Status
+     , BuildingStd
+     , AdjEvd
+from #InfoChgLst;
+
+select a.UnifiedID
+     , a.BuildingID_P
+     , a.ProjectID_P
+     , b.MainPrice
+     , b.MainPriceRent
+     , b.MainPrice * 1.0 / c.MainPrice         as MainPricePst
+     , b.MainPriceRent * 1.0 / c.MainPriceRent as MainPriceRentPst
+     , a.MainPriceType
+     , a.MainPriceRentType
+     , b.ModifyDate
+     , 1                                       as Status
+     , a.BuildingStd
+     , case
+           when MainPriceChg = 0 then
+               ''
+           else
+               'MainPriceChg|'
+       end + case
+                 when MainPriceRentChg = 0 then
+                     ''
+                 else
+                     'MainPriceRentChg|'
+             end                               as AdjEvd
+into #InfoChgCurr
+from ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth#          a
+    inner join #InfoChg                             b
+        on a.BuildingID_P = b.BuildingID_P
+    left join ODS_OFFICE_BUILDING_PRICE_INFO_#lastYearMonth# c
+        on a.BuildingID = c.BuildingID_P
+where (
+          b.MainPriceChg = 1
+          or b.MainPriceRentChg = 1
+      )
+      and a.Status = 1
+      and c.Status = 1;
+
+update a
+set a.Status = 0
+from ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth# a
+    left join #InfoChgCurr                 b
+        on a.BuildingID_P = b.BuildingID_P
+where b.BuildingID_P is not null
+      and a.Status = 1;
+
+insert into dbo.ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth#
+(
+    ID
+  , UnifiedID
+  , BuildingID_P
+  , ProjectID_P
+  , MainPrice
+  , MainPriceRent
+  , MainPricePst
+  , MainPriceRentPst
+  , MainPriceType
+  , MainPriceRentType
+  , ModifyDate
+  , Status
+  , BuildingStd
+  , AdjEvd
+)
+select newid()
+     , UnifiedID
+     , BuildingID_P
+     , ProjectID_P
+     , MainPrice
+     , MainPriceRent
+     , MainPricePst
+     , MainPriceRentPst
+     , MainPriceType
+     , MainPriceRentType
+     , getdate()
+     , Status
+     , BuildingStd
+     , AdjEvd
+from #InfoChgCurr;
+
+
+drop table #InfoChg;
+drop table #InfoChgLst;
+drop table #InfoChgCurr;
+
+
+-- 更新上月基价;
+update a
+set a.ITEM_MAINAREAPRICE = b.MainPrice_1
+from obpm_LianCheng_Data.dbo.TLK_基价信息              a
+    join dbo.ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth# b
+        on a.ITEM_AIRAID = b.ProjectID_P
+where a.ITEM_PROJECTTYPE = 2
+      and a.ITEM_PRICETYPE = 1
+      and a.ITEM_VALUEPOINT = '#lastMonth#'
+      and b.Status = 1
+      and b.BuildingStd = 1;
+
+update a
+set a.ITEM_MAINAREAPRICE = b.MainPriceRent_1
+from obpm_LianCheng_Data.dbo.TLK_基价信息              a
+    join dbo.ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth# b
+        on a.ITEM_AIRAID = b.ProjectID_P
+where a.ITEM_PROJECTTYPE = 2
+      and a.ITEM_PRICETYPE = 2
+      and a.ITEM_VALUEPOINT = '#lastMonth#'
+      and b.Status = 1
+      and b.BuildingStd = 1
+
+-- 插入本月新增信息;
+insert into obpm_LianCheng_Data.dbo.TLK_基价信息
+(LASTMODIFIED,FORMNAME,AUTHOR,AUTHOR_DEPT_INDEX,CREATED,FORMID,ISTMP,VERSIONS,APPLICATIONID,STATEINT,LASTMODIFIER,DOMAINID,AUDITORLIST,COAUDITORLIST,ID,ITEM_PriceID,ITEM_AIRAID,ITEM_PROJECTTYPE,ITEM_PRICETYPE,ITEM_STANDARDPRICE,ITEM_MAINAREAPRICE,ITEM_VALUEPOINT,ITEM_PRICEEXPLAIN,ITEM_UPDATEPERSON,ITEM_UPDATEDATE,ITEM_PRICECHG)
+select getdate(),'物业信息管理/基价信息','amQRUkvYQAsAec1JGLp','Os6qe4Pmq5viTO8lTnW',getdate(),'ybte0OakLV17UzAyoVU',0,1,'Ts7TykYmuEzzZgWhXHj',0,'amQRUkvYQAsAec1JGLp','BclzHtmfLQoAA5ICTb5','{}','{}',newid(),newid(),ProjectID_P,2,1,null,MainPrice,'#today#',AdjEvd,'李贺',getdate(),MainPricePst
+from dbo.ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth# a
+join obpm_LianCheng_Data.dbo.TLK_基价信息 b
+    on a.ProjectID_P=b.ITEM_AIRAID and b.ITEM_VALUEPOINT='#today#' and b.ITEM_PRICETYPE=1
+where Status=1 and BuildingStd=1 and b.ID is null;
+
+insert into obpm_LianCheng_Data.dbo.TLK_基价信息
+(LASTMODIFIED,FORMNAME,AUTHOR,AUTHOR_DEPT_INDEX,CREATED,FORMID,ISTMP,VERSIONS,APPLICATIONID,STATEINT,LASTMODIFIER,DOMAINID,AUDITORLIST,COAUDITORLIST,ID,ITEM_PriceID,ITEM_AIRAID,ITEM_PROJECTTYPE,ITEM_PRICETYPE,ITEM_STANDARDPRICE,ITEM_MAINAREAPRICE,ITEM_VALUEPOINT,ITEM_PRICEEXPLAIN,ITEM_UPDATEPERSON,ITEM_UPDATEDATE,ITEM_PRICECHG)
+select getdate(),'物业信息管理/基价信息','amQRUkvYQAsAec1JGLp','Os6qe4Pmq5viTO8lTnW',getdate(),'ybte0OakLV17UzAyoVU',0,1,'Ts7TykYmuEzzZgWhXHj',0,'amQRUkvYQAsAec1JGLp','BclzHtmfLQoAA5ICTb5','{}','{}',newid(),newid(),ProjectID_P,2,2,null,MainPriceRent,'#today#',AdjEvd,'李贺',getdate(),MainPriceRentPst
+from dbo.ODS_OFFICE_BUILDING_PRICE_INFO_#yearMonth# a
+join obpm_LianCheng_Data.dbo.TLK_基价信息 b
+    on a.ProjectID_P=b.ITEM_AIRAID and b.ITEM_VALUEPOINT='#today#' and b.ITEM_PRICETYPE=2
+where Status=1 and BuildingStd=1 and b.ID is null;
+
+-- 插入t_document
+insert into obpm_LianCheng_Data.dbo.T_DOCUMENT
+(ID,LASTMODIFIED,FORMNAME,AUTHOR,AUTHOR_DEPT_INDEX,CREATED,FORMID,ISTMP,VERSIONS,APPLICATIONID,STATEINT,LASTMODIFIER,DOMAINID,AUDITORLIST,COAUDITORLIST,MAPPINGID)
+ SELECT a.ID,a.LASTMODIFIED,a.FORMNAME,a.AUTHOR,a.AUTHOR_DEPT_INDEX,a.CREATED,a.FORMID,a.ISTMP,a.VERSIONS,a.APPLICATIONID,a.STATEINT,a.LASTMODIFIER,a.DOMAINID,a.AUDITORLIST,a.COAUDITORLIST,a.ID
+from obpm_LianCheng_Data.dbo.TLK_基价信息            a
+    left join obpm_LianCheng_Data.dbo.T_DOCUMENT b
+        on a.ID = b.ID
+where a.ITEM_VALUEPOINT='#today#' and a.ITEM_PROJECTTYPE=2 and b.id is null;
+
+-- 计算之前,更新上个月的价格;