Browse Source

(水务版DMP_Spring17): 客户管理增加子客户需求开发

chenlong 4 years ago
parent
commit
51898589c0

+ 27 - 4
smart-city-platform/src/main/java/com/bz/smart_city/commom/util/TreeUtil.java

@@ -2,10 +2,7 @@ package com.bz.smart_city.commom.util;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.bz.smart_city.dto.AreaDto;
-import com.bz.smart_city.dto.BuildingSelectDto;
-import com.bz.smart_city.dto.ChannelDto;
-import com.bz.smart_city.dto.OrganizationDto;
+import com.bz.smart_city.dto.*;
 import com.bz.smart_city.dto.assistant.InstallPlanDataDTO;
 import com.bz.smart_city.entity.MeterRecordStat;
 import com.bz.smart_city.entity.Permission;
@@ -235,4 +232,30 @@ public class TreeUtil {
             }
         }
     }
+
+    public static List<CustomerDto> getCustomerTree(List<CustomerDto> list, Integer id, Integer leve) {
+        List<CustomerDto> temList = newArrayList();
+        if (list != null) {
+            int total = 0;
+            for (CustomerDto customerDto : list) {
+                if (id.equals(customerDto.getParentId())) {
+                    List<CustomerDto> chidren = getCustomerTree(list, customerDto.getId(), ++leve);
+                    CustomerDto temCustomerDto = new CustomerDto();
+                    BeanUtils.copyProperties(customerDto, temCustomerDto);
+                    temCustomerDto.setChildren(chidren);
+                    if(leve != null && leve > 1) {
+                        ++total;
+                    }
+                    temList.add(temCustomerDto);
+                    leve--;
+                }
+                customerDto.setTotal(total);
+            }
+        }
+        if(temList.size() > 0){
+            return temList;
+        }else {
+            return null;
+        }
+    }
 }

+ 13 - 0
smart-city-platform/src/main/java/com/bz/smart_city/controller/water/CustomerManageController.java

@@ -17,6 +17,8 @@ import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 @Controller
 @ResponseBody
 @RequestMapping("system/customer")
@@ -45,6 +47,17 @@ public class CustomerManageController {
         return new AjaxMessage<>(ResultStatus.OK, pageInfo);
     }
 
+    @GetMapping("getCustomerTree")
+    @ApiOperation(value = "获取客户树形数据", notes = "权限:sys:customer:query")
+    @PreAuthorize("hasAuthority('sys:customer:query')")
+    public AjaxMessage<Pagination<CustomerDto>> getCustomerTree(
+            @ApiParam(value = "客户名称", required = false) @RequestParam(required = false) String customerName
+    ) {
+        Pagination<CustomerDto> result = customerService.getCustomerTree(customerName);
+        return new AjaxMessage<>(ResultStatus.OK, result);
+    }
+
+
     @PostMapping("add")
     @ApiOperation(value = "添加客户", notes = "权限:sys:customer:add")
     @PreAuthorize("hasAuthority('sys:customer:add')")

+ 4 - 1
smart-city-platform/src/main/java/com/bz/smart_city/dao/CustomerMapper.java

@@ -1,7 +1,6 @@
 package com.bz.smart_city.dao;
 
 import com.bz.smart_city.dto.CustomerDto;
-import com.bz.smart_city.entity.ProgramItem;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import com.bz.smart_city.entity.Customer;
@@ -44,4 +43,8 @@ public interface CustomerMapper {
     List<Customer> findUserCustomerList(@Param("userId") Integer userId);
 
     List<Customer> getListById(@Param("customerId") Integer customerId);
+
+    List<CustomerDto> getCustomerTree(@Param("siteId") Integer siteId, @Param("customerName") String customerName);
+
+    Integer countChildrenNum(@Param("customerId") Integer customerId);
 }

+ 10 - 0
smart-city-platform/src/main/java/com/bz/smart_city/dto/CustomerDto.java

@@ -2,12 +2,22 @@ package com.bz.smart_city.dto;
 
 import com.bz.smart_city.entity.Customer;
 import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 
+import java.util.List;
+
 @Data
 @ApiModel
 @EqualsAndHashCode(callSuper=false)
 public class CustomerDto extends Customer {
+    @ApiModelProperty(value = "子类", position = 100)
+    private List<CustomerDto> children;
+
+    @ApiModelProperty(value = "子客户数", position = 101)
+    private Integer total;
 
+    @ApiModelProperty(value = "是否有子客户", position = 102)
+    private Boolean hasChildren;
 }

+ 4 - 1
smart-city-platform/src/main/java/com/bz/smart_city/entity/Customer.java

@@ -55,4 +55,7 @@ public class Customer {
 
     @ApiModelProperty(value="更新人", hidden = true)
     private String updateBy;
-}
+
+    @ApiModelProperty(value="上级客户ID")
+    private Integer parentId;
+}

+ 2 - 0
smart-city-platform/src/main/java/com/bz/smart_city/service/CustomerService.java

@@ -46,4 +46,6 @@ public interface CustomerService {
     Customer findByOrgId(@Param("customerorgid") Integer customerorgid);
 
     List<CustomerDto> findSiteCustomer();
+
+    Pagination<CustomerDto> getCustomerTree(String customerName);
 }

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

@@ -2,7 +2,9 @@ package com.bz.smart_city.service.impl;
 
 import com.bz.smart_city.commom.exception.ServiceException;
 import com.bz.smart_city.commom.model.Pagination;
+import com.bz.smart_city.commom.util.TreeUtil;
 import com.bz.smart_city.commom.util.UserUtil;
+import com.bz.smart_city.commom.util.Util;
 import com.bz.smart_city.dto.CustomerDto;
 import com.bz.smart_city.dto.LoginUser;
 import com.bz.smart_city.entity.Permission;
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
 import com.bz.smart_city.entity.Customer;
@@ -59,7 +62,7 @@ public class CustomerServiceImpl implements CustomerService {
     }
 
     /**
-     * 根据用户权限来判断用户可见的客户列表 
+     * 根据用户权限来判断用户可见的客户列表
      * 判断是否配置有客户维度 如果配置了则获取配置的维度、没有配置则取站点下所有客户
      * modify by pengdi
      */
@@ -148,6 +151,10 @@ public class CustomerServiceImpl implements CustomerService {
     @Override
     public void deleteCustomer(Integer customerId) {
         log.info("Begin DeleteCustomer CustomerId " + customerId);
+        Integer childrenNum = customerMapper.countChildrenNum(customerId);
+        if(childrenNum > 0) {
+            throw new ServiceException(-900, "请先删除该客户下的子客户");
+        }
         Customer customer = new Customer();
         customer.setId(customerId);
         customer.setStatus(0);
@@ -190,4 +197,15 @@ public class CustomerServiceImpl implements CustomerService {
     public List<CustomerDto> findSiteCustomer() {
         return customerMapper.getCustomerList(1,null);
     }
+
+    @Override
+    public Pagination<CustomerDto> getCustomerTree(String customerName) {
+        LoginUser user = UserUtil.getCurrentUser();
+        List<CustomerDto> result = customerMapper.getCustomerTree(user.getSiteId(), customerName);
+        Pagination obj = new Pagination();
+        obj.setTotal(result.size());
+        List tree = TreeUtil.getCustomerTree(result, 0, 1);
+        obj.setList(tree);
+        return obj;
+    }
 }

+ 28 - 7
smart-city-platform/src/main/resources/mapper/CustomerMapper.xml

@@ -18,6 +18,7 @@
         <result column="date_update" property="dateUpdate" jdbcType="TIMESTAMP"/>
         <result column="create_by" property="createBy" jdbcType="VARCHAR"/>
         <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
+        <result column="parent_id" property="parentId" jdbcType="INTEGER"/>
     </resultMap>
 
     <!--auto generated Code-->
@@ -36,7 +37,8 @@
         date_create,
         date_update,
         create_by,
-        update_by
+        update_by,
+        parent_id
     </sql>
 
     <!--auto generated Code-->
@@ -56,7 +58,8 @@
             date_create,
             date_update,
             create_by,
-            update_by
+            update_by,
+            parent_id
         ) VALUES (
             #{customer.id,jdbcType=INTEGER},
             #{customer.siteId,jdbcType=INTEGER},
@@ -72,7 +75,8 @@
             #{customer.dateCreate,jdbcType=TIMESTAMP},
             #{customer.dateUpdate,jdbcType=TIMESTAMP},
             #{customer.createBy,jdbcType=VARCHAR},
-            #{customer.updateBy,jdbcType=VARCHAR}
+            #{customer.updateBy,jdbcType=VARCHAR},
+            #{customer.parentId,jdbcType=INTEGER}
         )
     </insert>
 
@@ -95,6 +99,7 @@
             <if test="customer.dateUpdate!=null"> date_update,</if>
             <if test="customer.createBy!=null"> create_by,</if>
             <if test="customer.updateBy!=null"> update_by,</if>
+            <if test="customer.parentId!=null"> parent_id,</if>
         </trim>
         VALUES
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -128,6 +133,8 @@
             </if>
             <if test="customer.updateBy!=null">#{customer.updateBy,jdbcType=VARCHAR},
             </if>
+            <if test="customer.parentId!=null">#{customer.parentId,jdbcType=INTEGER},
+            </if>
         </trim>
     </insert>
 
@@ -148,7 +155,8 @@
             date_create,
             date_update,
             create_by,
-            update_by
+            update_by,
+            parent_id
         )VALUES
         <foreach collection="customers" item="customer" index="index" separator=",">
             (
@@ -166,7 +174,8 @@
             #{customer.dateCreate,jdbcType=TIMESTAMP},
             #{customer.dateUpdate,jdbcType=TIMESTAMP},
             #{customer.createBy,jdbcType=VARCHAR},
-            #{customer.updateBy,jdbcType=VARCHAR}
+            #{customer.updateBy,jdbcType=VARCHAR},
+            #{customer.parentId,jdbcType=INTEGER}
             )
         </foreach>
     </insert>
@@ -188,7 +197,8 @@
             <if test="customer.dateCreate != null"> date_create= #{customer.dateCreate,jdbcType=TIMESTAMP},</if>
             <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.updateBy != null"> update_by= #{customer.updateBy,jdbcType=VARCHAR},</if>
+            <if test="customer.parentId != null"> parent_id= #{customer.parentId,jdbcType=INTEGER}</if>
         </set>
         WHERE id = #{customer.id,jdbcType=INTEGER}
     </update>
@@ -202,7 +212,7 @@
 		select
 			id,
 			customer_name
-		from sc_customer 
+		from sc_customer
 		where status = 1
 		and token = #{code,jdbcType=VARCHAR}
 	</select>
@@ -263,5 +273,16 @@
         select id,customer_name from sc_customer where status = 1
         and id = #{customerId}
     </select>
+    <select id="getCustomerTree" resultType="com.bz.smart_city.dto.CustomerDto">
+        select <include refid="Base_Column_List"></include> from sc_customer
+        where status = 1
+        <if test="siteId != null"> and site_id = #{siteId} </if>
+        <if test="customerName != null and customerName != ''"> and customer_name like concat('%',#{customerName} ,'%')</if>
+        order by date_create desc
+    </select>
+
+    <select id="countChildrenNum" resultType="java.lang.Integer">
+        select count(1) from sc_customer where status = 1 and parent_id = #{customerId}
+    </select>
 </mapper>
 

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

@@ -1,5 +1,11 @@
 # sql更改记录
 
+## spring17
+-  2020-07-09 long
+```
+ALTER TABLE sc_customer ADD parent_id INT(11) NOT NULL DEFAULT 0 COMMENT 'parentId';
+```
+
 ## sprint2
 
 -  2019-01-09 lin
@@ -358,4 +364,4 @@ CREATE TABLE `sc_assistant_user`  (
   PRIMARY KEY (`id`) USING BTREE
 ) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
 
-```
+```