Bladeren bron

(feat):客户管理新增统计子客户数量

chenlong 4 jaren geleden
bovenliggende
commit
18a2951e19

+ 3 - 0
smart-city-platform/src/main/java/com/bz/smart_city/entity/Customer.java

@@ -58,4 +58,7 @@ public class Customer {
 
     @ApiModelProperty(value="上级客户ID")
     private Integer parentId;
+
+    @ApiModelProperty(value="子客户数量")
+    private Integer childrenNum;
 }

+ 32 - 1
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/CustomerServiceImpl.java

@@ -23,6 +23,7 @@ import java.util.List;
 import com.bz.smart_city.entity.Customer;
 import com.bz.smart_city.dao.CustomerMapper;
 import com.bz.smart_city.service.CustomerService;
+import org.springframework.transaction.annotation.Transactional;
 
 @Slf4j
 @Service
@@ -114,7 +115,10 @@ public class CustomerServiceImpl implements CustomerService {
         return new Pagination<>(list);
     }
 
+
+
     @Override
+    @Transactional
     public void addCustomer(CustomerDto customerDto) {
         String customerNo = (int)((Math.random()*9+1)*100000)+"";
         int sameNoCount = customerMapper.findByNoUnique(customerNo);
@@ -132,10 +136,14 @@ public class CustomerServiceImpl implements CustomerService {
             throw new ServiceException(-900, "客户名称已经存在");
         }
         this.insertSelective(customerDto);
+
+        this.updateParentChildrenNum(customerDto.getParentId());
+
         log.info("End AddCustomer");
     }
 
     @Override
+    @Transactional
     public void editCustomer(Customer customer) {
         log.info("Begin EditCustomer Customer " + customer);
         LoginUser loginUser = UserUtil.getCurrentUser();
@@ -143,12 +151,18 @@ public class CustomerServiceImpl implements CustomerService {
         if(resultName > 0) {
             throw new ServiceException(-900, "客户名称已经存在");
         }
-
+        Integer oldParentId = customerMapper.findById(customer.getId()).getParentId();
         this.updateByPrimaryKeySelective(customer);
+
+        Integer newParentId = customerMapper.findById(customer.getId()).getParentId();
+        this.updateParentChildrenNum(oldParentId);
+        this.updateParentChildrenNum(newParentId);
+
         log.info("End EditCustomer");
     }
 
     @Override
+    @Transactional
     public void deleteCustomer(Integer customerId) {
         log.info("Begin DeleteCustomer CustomerId " + customerId);
         Integer childrenNum = customerMapper.countChildrenNum(customerId);
@@ -158,7 +172,12 @@ public class CustomerServiceImpl implements CustomerService {
         Customer customer = new Customer();
         customer.setId(customerId);
         customer.setStatus(0);
+        customer.setChildrenNum(0);
+        Integer parentId = customerMapper.findById(customerId).getParentId();
         this.updateByPrimaryKeySelective(customer);
+
+        this.updateParentChildrenNum(parentId);
+
         log.info("End DeleteCustomer");
     }
 
@@ -208,4 +227,16 @@ public class CustomerServiceImpl implements CustomerService {
         obj.setList(tree);
         return obj;
     }
+
+    // 更新父级客户的子客户数量
+    protected void updateParentChildrenNum(Integer parentId) {
+        if(parentId > 0) {
+            Integer count = customerMapper.countChildrenNum(parentId);
+            Customer customer = this.findById(parentId);
+            customer.setId(parentId);
+            customer.setChildrenNum(count);
+            this.updateByPrimaryKeySelective(customer);
+        }
+    }
+
 }

+ 16 - 6
smart-city-platform/src/main/resources/mapper/CustomerMapper.xml

@@ -19,6 +19,7 @@
         <result column="create_by" property="createBy" jdbcType="VARCHAR"/>
         <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
         <result column="parent_id" property="parentId" jdbcType="INTEGER"/>
+        <result column="children_num" property="childrenNum" jdbcType="INTEGER"/>
     </resultMap>
 
     <!--auto generated Code-->
@@ -38,7 +39,8 @@
         date_update,
         create_by,
         update_by,
-        parent_id
+        parent_id,
+        children_num
     </sql>
 
     <!--auto generated Code-->
@@ -59,7 +61,8 @@
             date_update,
             create_by,
             update_by,
-            parent_id
+            parent_id,
+            children_num
         ) VALUES (
             #{customer.id,jdbcType=INTEGER},
             #{customer.siteId,jdbcType=INTEGER},
@@ -76,7 +79,8 @@
             #{customer.dateUpdate,jdbcType=TIMESTAMP},
             #{customer.createBy,jdbcType=VARCHAR},
             #{customer.updateBy,jdbcType=VARCHAR},
-            #{customer.parentId,jdbcType=INTEGER}
+            #{customer.parentId,jdbcType=INTEGER},
+            #{customer.childrenNum,jdbcType=INTEGER}
         )
     </insert>
 
@@ -100,6 +104,7 @@
             <if test="customer.createBy!=null"> create_by,</if>
             <if test="customer.updateBy!=null"> update_by,</if>
             <if test="customer.parentId!=null"> parent_id,</if>
+            <if test="customer.childrenNum!=null"> children_num,</if>
         </trim>
         VALUES
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -135,6 +140,8 @@
             </if>
             <if test="customer.parentId!=null">#{customer.parentId,jdbcType=INTEGER},
             </if>
+            <if test="customer.childrenNum!=null">#{customer.childrenNum,jdbcType=INTEGER},
+            </if>
         </trim>
     </insert>
 
@@ -156,7 +163,8 @@
             date_update,
             create_by,
             update_by,
-            parent_id
+            parent_id,
+            children_num
         )VALUES
         <foreach collection="customers" item="customer" index="index" separator=",">
             (
@@ -175,7 +183,8 @@
             #{customer.dateUpdate,jdbcType=TIMESTAMP},
             #{customer.createBy,jdbcType=VARCHAR},
             #{customer.updateBy,jdbcType=VARCHAR},
-            #{customer.parentId,jdbcType=INTEGER}
+            #{customer.parentId,jdbcType=INTEGER},
+            #{customer.childrenNum,jdbcType=INTEGER}
             )
         </foreach>
     </insert>
@@ -198,7 +207,8 @@
             <if test="customer.dateUpdate != null"> date_update= #{customer.dateUpdate,jdbcType=TIMESTAMP},</if>
             <if test="customer.createBy != null"> create_by= #{customer.createBy,jdbcType=VARCHAR},</if>
             <if test="customer.updateBy != null"> update_by= #{customer.updateBy,jdbcType=VARCHAR},</if>
-            <if test="customer.parentId != null"> parent_id= #{customer.parentId,jdbcType=INTEGER}</if>
+            <if test="customer.parentId != null"> parent_id= #{customer.parentId,jdbcType=INTEGER},</if>
+            <if test="customer.childrenNum != null"> children_num= #{customer.childrenNum,jdbcType=INTEGER}</if>
         </set>
         WHERE id = #{customer.id,jdbcType=INTEGER}
     </update>

+ 1 - 0
smart-city-platform/updateLog.md

@@ -4,6 +4,7 @@
 -  2020-07-09 long
 ```
 ALTER TABLE sc_customer ADD parent_id INT(11) NOT NULL DEFAULT 0 COMMENT 'parentId';
+ALTER TABLE sc_customer ADD children_num INT(11) NOT NULL DEFAULT 0 COMMENT 'childrenNum';
 ```
 
 ## sprint2