From 57dd7c30431407f60b303a7800de0e87dd16843e Mon Sep 17 00:00:00 2001
From: huangdeliang <huangdeliang@skieer.com>
Date: Wed, 26 May 2021 18:11:12 +0800
Subject: [PATCH 1/5] =?UTF-8?q?=E6=9A=82=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../custom/SysServiceQuestionController.java  |  40 ++++-
 .../custom/WechatAppletController.java        |  70 +++++---
 .../core/controller/BaseController.java       |  60 +++----
 .../custom/domain/SysServicesTopic.java       |  73 ++++++++
 .../service/ISysServicesQuestionService.java  |   2 +-
 .../impl/SysServicesQuestionServiceImp.java   |  19 ++-
 .../custom/SysServicesQuestionMapper.xml      |  16 +-
 .../mapper/custom/SysServicesTopicMapper.xml  | 156 ++++++++++++++++++
 8 files changed, 363 insertions(+), 73 deletions(-)
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java
 create mode 100644 stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml

diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceQuestionController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceQuestionController.java
index c80f1b488..4f7cf6fb7 100644
--- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceQuestionController.java
+++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceQuestionController.java
@@ -2,11 +2,15 @@ package com.stdiet.web.controller.custom;
 
 import com.stdiet.common.core.controller.BaseController;
 import com.stdiet.common.core.domain.AjaxResult;
+import com.stdiet.common.core.page.TableDataInfo;
+import com.stdiet.common.utils.StringUtils;
 import com.stdiet.custom.domain.SysServicesQuestion;
 import com.stdiet.custom.service.ISysServicesQuestionService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 @RestController
 @RequestMapping("/services/question")
 public class SysServiceQuestionController extends BaseController {
@@ -14,18 +18,38 @@ public class SysServiceQuestionController extends BaseController {
     @Autowired
     private ISysServicesQuestionService sysServicesQuestionService;
 
-    @PostMapping("/list")
-    public AjaxResult list(@RequestBody SysServicesQuestion sysServicesQuestion) {
-        return AjaxResult.success(sysServicesQuestionService.selectSysServicesQuestionByUserIdAndRole(sysServicesQuestion));
+    @GetMapping("/list")
+    public TableDataInfo list(SysServicesQuestion sysServicesQuestion) {
+        startPage();
+        return getDataTable(sysServicesQuestionService.selectSysServicesQuestionByUserIdAndRole(sysServicesQuestion));
     }
 
-    @PutMapping("/updateStatus")
-    public AjaxResult status(@RequestBody SysServicesQuestion sysServicesQuestion) {
-        return toAjax(sysServicesQuestionService.updateSysServicesQuestionStatus(sysServicesQuestion));
-    }
+//    @PutMapping("/update/status")
+//    public AjaxResult status(@RequestBody SysServicesQuestion sysServicesQuestion) {
+//        return toAjax(sysServicesQuestionService.updateSysServicesQuestionStatus(sysServicesQuestion));
+//    }
 
     @PostMapping("/reply")
     public AjaxResult reply(@RequestBody SysServicesQuestion servicesQuestion) {
-        return toAjax(sysServicesQuestionService.inserSysServicesQuestionReply(servicesQuestion));
+        int row = sysServicesQuestionService.inserSysServicesQuestionReply(servicesQuestion);
+        if (row > 0) {
+            // 更新customer未读,id不能有值
+            servicesQuestion.setRead(0);
+            sysServicesQuestionService.updateSysServicesQuestionStatus(servicesQuestion);
+        }
+        return toAjax(row);
+    }
+
+    @GetMapping("/detail")
+    public AjaxResult detail(@RequestParam String queId, @RequestParam Long id) {
+        List<SysServicesQuestion> questions = sysServicesQuestionService.selectSysServicesQuestionSessionByQueId(queId);
+        if (StringUtils.isNotNull(questions)) {
+            // 更新问题对应角色的状态
+            SysServicesQuestion servicesQuestion = new SysServicesQuestion();
+            servicesQuestion.setRead(1);
+            servicesQuestion.setId(id);
+            sysServicesQuestionService.updateSysServicesQuestionStatus(servicesQuestion);
+        }
+        return AjaxResult.success(questions);
     }
 }
diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
index 4509f2940..5c7cb85e2 100644
--- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
+++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
@@ -593,7 +593,7 @@ public class WechatAppletController extends BaseController {
     }
 
     @GetMapping("/services/list")
-    public AjaxResult fetchServiceQuestion(@RequestParam String customerId, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
+    public TableDataInfo fetchServiceQuestion(@RequestParam String customerId, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
         startPage();
 
         Long cusId = StringUtils.isNotEmpty(customerId) ? Long.parseLong(AesUtils.decrypt(customerId)) : 0L;
@@ -602,11 +602,12 @@ public class WechatAppletController extends BaseController {
         servicesQuestion.setRole("customer");
         servicesQuestion.setUserId(cusId);
 
-        return AjaxResult.success(iSysServicesQuestionService.selectSysServicesQuestionByUserIdAndRole(servicesQuestion));
+        return getDataTable(iSysServicesQuestionService.selectSysServicesQuestionByUserIdAndRole(servicesQuestion));
     }
 
     /**
      * 客户添加问题
+     *
      * @param servicesQuestion
      * @param customerId
      * @return
@@ -614,42 +615,69 @@ public class WechatAppletController extends BaseController {
     @PostMapping("/services/post")
     public AjaxResult postServiceQuestion(@RequestBody SysServicesQuestion servicesQuestion, @RequestParam String customerId) {
         Long cusId = StringUtils.isNotEmpty(customerId) ? Long.parseLong(AesUtils.decrypt(customerId)) : 0L;
-        if(cusId == 0L) {
+        if (cusId == 0L) {
             return toAjax(0);
         }
         servicesQuestion.setCusId(cusId);
-        return toAjax(iSysServicesQuestionService.insertSysServicesQuestion(servicesQuestion));
-    }
 
-    @GetMapping("/services/reply")
-    public AjaxResult serviceQuestionReply(@RequestParam String queId) {
-        return AjaxResult.success(iSysServicesQuestionService.selectSysServicesQuestionSessionByQueId(queId));
+        return AjaxResult.success(iSysServicesQuestionService.insertSysServicesQuestion(servicesQuestion));
     }
 
     /**
-     * 设置已读
-     * @param id
+     * 回答问题
+     *
+     * @param servicesQuestion
+     * @param customerId
      * @return
      */
-    @GetMapping("/services/post/update")
-    public AjaxResult updateServiceQuestion(@RequestParam Long id) {
-        SysServicesQuestion servicesQuestion = new SysServicesQuestion();
-        servicesQuestion.setRead(1);
-        servicesQuestion.setId(id);
-
-        return toAjax(iSysServicesQuestionService.updateSysServicesQuestionStatus(servicesQuestion));
-    }
-
     @PostMapping("/services/post/reply")
     public AjaxResult replyServiceQuestion(@RequestBody SysServicesQuestion servicesQuestion, @RequestParam String customerId) {
         Long cusId = StringUtils.isNotEmpty(customerId) ? Long.parseLong(AesUtils.decrypt(customerId)) : 0L;
-
+        if (cusId == 0L) {
+            return toAjax(0);
+        }
         servicesQuestion.setRole("customer");
         servicesQuestion.setUserId(cusId);
 
-        return toAjax(iSysServicesQuestionService.inserSysServicesQuestionReply(servicesQuestion));
+        int row = iSysServicesQuestionService.inserSysServicesQuestionReply(servicesQuestion);
+        if (row > 0) {
+            // 更新三个觉得未读,id不能有值
+            servicesQuestion.setRead(0);
+            iSysServicesQuestionService.updateSysServicesQuestionStatus(servicesQuestion);
+        }
+
+        return toAjax(row);
     }
 
+    /**
+     * 获取问题详情
+     *
+     * @param id
+     * @return
+     */
+    @GetMapping("/services/detail")
+    public AjaxResult serviceQuestionDetail(@RequestParam String queId, @RequestParam Long id) {
+        List<SysServicesQuestion> questions = iSysServicesQuestionService.selectSysServicesQuestionSessionByQueId(queId);
+        if (StringUtils.isNotNull(questions)) {
+            SysServicesQuestion status = new SysServicesQuestion();
+            status.setId(id);
+            status.setRead(1);
+            iSysServicesQuestionService.updateSysServicesQuestionStatus(status);
+        }
+        return AjaxResult.success(questions);
+    }
+
+
+//    @GetMapping("/services/post/update")
+//    public AjaxResult updateServiceQuestion(@RequestParam Long id) {
+//        SysServicesQuestion servicesQuestion = new SysServicesQuestion();
+//        servicesQuestion.setRead(1);
+//        servicesQuestion.setId(id);
+//
+//        return toAjax(iSysServicesQuestionService.updateSysServicesQuestionStatus(servicesQuestion));
+//    }
+
+
 }
 
 
diff --git a/stdiet-common/src/main/java/com/stdiet/common/core/controller/BaseController.java b/stdiet-common/src/main/java/com/stdiet/common/core/controller/BaseController.java
index 3b1cc8d45..250614f38 100644
--- a/stdiet-common/src/main/java/com/stdiet/common/core/controller/BaseController.java
+++ b/stdiet-common/src/main/java/com/stdiet/common/core/controller/BaseController.java
@@ -1,12 +1,5 @@
 package com.stdiet.common.core.controller;
 
-import java.beans.PropertyEditorSupport;
-import java.util.Date;
-import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.InitBinder;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.stdiet.common.constant.HttpStatus;
@@ -17,28 +10,33 @@ import com.stdiet.common.core.page.TableSupport;
 import com.stdiet.common.utils.DateUtils;
 import com.stdiet.common.utils.StringUtils;
 import com.stdiet.common.utils.sql.SqlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+
+import java.beans.PropertyEditorSupport;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
 
 /**
  * web层通用数据处理
- * 
+ *
  * @author stdiet
  */
-public class BaseController
-{
+public class BaseController {
     protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
 
     /**
      * 将前台传递过来的日期格式的字符串,自动转化为Date类型
      */
     @InitBinder
-    public void initBinder(WebDataBinder binder)
-    {
+    public void initBinder(WebDataBinder binder) {
         // Date 类型转换
-        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
-        {
+        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
             @Override
-            public void setAsText(String text)
-            {
+            public void setAsText(String text) {
                 setValue(DateUtils.parseDate(text));
             }
         });
@@ -47,13 +45,11 @@ public class BaseController
     /**
      * 设置请求分页数据
      */
-    protected void startPage()
-    {
+    protected void startPage() {
         PageDomain pageDomain = TableSupport.buildPageRequest();
         Integer pageNum = pageDomain.getPageNum();
         Integer pageSize = pageDomain.getPageSize();
-        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
-        {
+        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
             String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
             PageHelper.startPage(pageNum, pageSize, orderBy);
         }
@@ -62,33 +58,37 @@ public class BaseController
     /**
      * 响应请求分页数据
      */
-    @SuppressWarnings({ "rawtypes", "unchecked" })
-    protected TableDataInfo getDataTable(List<?> list)
-    {
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    protected TableDataInfo getDataTable(List<?> list) {
+
+        PageDomain pageDomain = TableSupport.buildPageRequest();
+        Integer pageNum = pageDomain.getPageNum();
+        Integer pageSize = pageDomain.getPageSize();
+
+        long total = new PageInfo(list).getTotal();
+
         TableDataInfo rspData = new TableDataInfo();
         rspData.setCode(HttpStatus.SUCCESS);
         rspData.setMsg("查询成功");
-        rspData.setRows(list);
-        rspData.setTotal(new PageInfo(list).getTotal());
+        rspData.setRows(total > pageSize * (pageNum - 1) ? list : new ArrayList<>());
+        rspData.setTotal(total);
         return rspData;
     }
 
     /**
      * 响应返回结果
-     * 
+     *
      * @param rows 影响行数
      * @return 操作结果
      */
-    protected AjaxResult toAjax(int rows)
-    {
+    protected AjaxResult toAjax(int rows) {
         return rows > 0 ? AjaxResult.success() : AjaxResult.error();
     }
 
     /**
      * 页面跳转
      */
-    public String redirect(String url)
-    {
+    public String redirect(String url) {
         return StringUtils.format("redirect:{}", url);
     }
 }
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java
new file mode 100644
index 000000000..c21645535
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java
@@ -0,0 +1,73 @@
+package com.stdiet.custom.domain;
+
+import com.alibaba.fastjson.JSONArray;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class SysServicesTopic {
+    /**
+     *
+     */
+    Long id;
+
+    /**
+     * 问题id
+     */
+    String topicId;
+
+    /**
+     * 问题类型
+     */
+    Integer topicType;
+
+    /**
+     * 角色
+     */
+    Long uid;
+
+    Long fromUid;
+
+    Long toUid;
+
+    /**
+     * 问题内容
+     */
+    String content;
+
+    /**
+     * 图片地址
+     */
+    JSONArray img;
+
+    /**
+     * 创建时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    Date createTime;
+
+    /**
+     * 0-未读 1-已读
+     */
+    Integer read;
+
+    /**
+     * 更新时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    Date updateTime;
+
+    /**
+     * 角色
+     */
+    String role;
+
+    // 非持久化字段
+    /**
+     * 角色名字
+     */
+    String name;
+
+}
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesQuestionService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesQuestionService.java
index 329f94e21..f671b9268 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesQuestionService.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesQuestionService.java
@@ -8,7 +8,7 @@ public interface ISysServicesQuestionService {
 
     List<SysServicesQuestion> selectSysServicesQuestionByUserIdAndRole(SysServicesQuestion servicesQuestion);
 
-    int insertSysServicesQuestion(SysServicesQuestion servicesQuestion);
+    SysServicesQuestion insertSysServicesQuestion(SysServicesQuestion servicesQuestion);
 
     int updateSysServicesQuestionStatus(SysServicesQuestion sysServicesQuestion);
 
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesQuestionServiceImp.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesQuestionServiceImp.java
index 1c9691014..fbaca72b1 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesQuestionServiceImp.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesQuestionServiceImp.java
@@ -1,5 +1,6 @@
 package com.stdiet.custom.service.impl;
 
+import com.stdiet.common.utils.DateUtils;
 import com.stdiet.common.utils.uuid.UUID;
 import com.stdiet.custom.domain.SysCustomer;
 import com.stdiet.custom.domain.SysServicesQuestion;
@@ -26,9 +27,11 @@ public class SysServicesQuestionServiceImp implements ISysServicesQuestionServic
     }
 
     @Override
-    public int insertSysServicesQuestion(SysServicesQuestion servicesQuestion) {
+    public SysServicesQuestion insertSysServicesQuestion(SysServicesQuestion servicesQuestion) {
         // 生成uuid
         servicesQuestion.setQueId(UUID.randomUUID().toString().replaceAll("-", ""));
+        servicesQuestion.setRead(1);
+        servicesQuestion.setCreateTime(DateUtils.getNowDate());
 
         servicesQuestionMapper.insertSysServicesQuestion(servicesQuestion);
 
@@ -45,25 +48,31 @@ public class SysServicesQuestionServiceImp implements ISysServicesQuestionServic
         SysServicesQuestion dieticianStatus = new SysServicesQuestion();
         dieticianStatus.setUserId(customer.getMainDietitian());
         dieticianStatus.setRole("dietician");
-        customerStatus.setRead(0);
+        dieticianStatus.setRead(0);
         dieticianStatus.setQueId(servicesQuestion.getQueId());
         statusList.add(dieticianStatus);
 
         SysServicesQuestion afterSaleStatus = new SysServicesQuestion();
         afterSaleStatus.setUserId(customer.getAfterDietitian());
         afterSaleStatus.setRole("after_sale");
-        customerStatus.setRead(0);
+        afterSaleStatus.setRead(0);
         afterSaleStatus.setQueId(servicesQuestion.getQueId());
         statusList.add(afterSaleStatus);
 
         SysServicesQuestion dieticianAssistantStatus = new SysServicesQuestion();
         dieticianAssistantStatus.setUserId(customer.getAssistantDietitian());
         dieticianAssistantStatus.setRole("dietician_assistant");
-        customerStatus.setRead(0);
+        dieticianAssistantStatus.setRead(0);
         dieticianAssistantStatus.setQueId(servicesQuestion.getQueId());
+
         statusList.add(dieticianAssistantStatus);
 
-        return servicesQuestionMapper.insertSysServicesQuestionStatus(statusList);
+        servicesQuestionMapper.insertSysServicesQuestionStatus(statusList);
+
+        servicesQuestion.setId(customerStatus.getId());
+        servicesQuestion.setCusId(null);
+
+        return servicesQuestion;
     }
 
 
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysServicesQuestionMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysServicesQuestionMapper.xml
index e45c1da94..964e6d196 100644
--- a/stdiet-custom/src/main/resources/mapper/custom/SysServicesQuestionMapper.xml
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysServicesQuestionMapper.xml
@@ -48,7 +48,7 @@
                 ) AS user USING(user_id)
             </otherwise>
         </choose>
-        ORDER BY type ASC, update_time DESC
+        ORDER BY `read` ASC, update_time DESC
     </select>
 
     <select id="selectSysServicesQuestionSessionByQueId" resultMap="SysServicesQuestionSessionResult"
@@ -78,7 +78,7 @@
             <if test="cusId != null">cus_id,</if>
             <if test="content != null">content,</if>
             <if test="type != null">type,</if>
-            <if test="cusId != null">create_time,</if>
+            <if test="createTime != null">create_time,</if>
             <if test="img != null">img,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
@@ -86,7 +86,7 @@
             <if test="cusId != null">#{cusId},</if>
             <if test="content != null">#{content},</if>
             <if test="type != null">#{type},</if>
-            <if test="cusId != null">now(),</if>
+            <if test="createTime != null">#{createTime},</if>
             <if test="img != null">
                 #{img,  jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
             </if>
@@ -94,10 +94,11 @@
     </insert>
 
     <!--    插入问题的四个角色:1,用户; 2,营养师; 3,售后; 4,助理-->
-    <insert id="insertSysServicesQuestionStatus" parameterType="java.util.List">
-        insert into sys_services_question_status (que_id, user_id, role, create_time, update_time) values
+    <insert id="insertSysServicesQuestionStatus" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"
+            keyColumn="id">
+        insert into sys_services_question_status (que_id, user_id, role, `read`, create_time, update_time) values
         <foreach collection="list" item="status" index="index" separator=",">
-            (#{status.queId}, #{status.userId}, #{status.role}, now(), now())
+            (#{status.queId}, #{status.userId}, #{status.role}, #{status.read}, now(), now())
         </foreach>
     </insert>
 
@@ -127,8 +128,7 @@
     </update>
 
     <!--    插入问题回复-->
-    <insert id="inserSysServicesQuestionReply" parameterType="SysServicesQuestion" useGeneratedKeys="true"
-            keyProperty="id" keyColumn="id">
+    <insert id="inserSysServicesQuestionReply" parameterType="SysServicesQuestion">
         insert into sys_services_question_session
         <trim prefix="(" suffix=")" suffixOverrides=",">
             <if test="queId != null">que_id,</if>
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml
new file mode 100644
index 000000000..160b5b90b
--- /dev/null
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml
@@ -0,0 +1,156 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.stdiet.custom.mapper.SysServicesTopicMapper">
+
+    <resultMap type="SysServicesTopic" id="SysServicesTopicResult">
+        <result column="id" property="id"/>
+        <result column="topic_id" property="topicId"/>
+        <result column="topic_type" property="topicType"/>
+        <result column="content" property="content"/>
+        <result column="role" property="role"/>
+        <result column="name" property="name"/>
+        <result column="read" property="read"/>
+        <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
+        <result column="create_time" property="createTime"/>
+        <result column="update_time" property="updateTime"/>
+    </resultMap>
+
+    <resultMap id="SysServicesQuestionSessionResult" type="SysServicesQuestion">
+        <result column="id" property="id"/>
+        <result column="type" property="type"/>
+        <result column="content" property="content"/>
+        <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
+        <result column="create_time" property="createTime"/>
+        <association property="name" column="{userId=user_id,role=role}" select="selectUserInfo"/>
+    </resultMap>
+
+    <!--    根据userId和角色查询问题列表-->
+    <select id="selectSysServicesTopicByUserIdAndRole" parameterType="SysServicesTopic"
+            resultMap="SysServicesQuestionResult">
+        SELECT * FROM (
+        SELECT * FROM sys_services_topic_status WHERE role = #{role} AND uid = #{uid}
+        ) AS status
+        LEFT JOIN sys_services_question USING(topic_id)
+        <choose>
+            <when test="role == 'customer'">
+                LEFT JOIN (
+                    SELECT name, id AS uid FROM sys_customer WHERE id = #{uid}
+                ) AS customer USING(uid)
+            </when>
+            <otherwise>
+                LEFT JOIN (
+                    SELECT name, user_id AS uid, nick_name AS name FROM sys_user WHERE user_id = #{uid}
+                ) AS user USING(uid)
+            </otherwise>
+        </choose>
+        ORDER BY `read` ASC, update_time DESC
+    </select>
+
+    <select id="selectSysServicesQuestionSessionByQueId" resultMap="SysServicesQuestionSessionResult"
+            parameterType="String">
+        select que_id, cus_id as user_id, 'customer' as role, content, img, create_time from sys_services_question where que_id = #{queId}
+        union select que_id, user_id, role, content, img, create_time from sys_services_question_session where que_id = #{queId}
+        order by create_time asc
+    </select>
+
+    <select id="selectUserInfo" parameterType="java.util.Map" resultType="String">
+        <choose>
+            <when test="_parameter.get('role') == 'customer'">
+                select name from sys_customer where id = #{userId}
+            </when>
+            <otherwise>
+                select nick_name from sys_user where user_id = #{userId}
+            </otherwise>
+        </choose>
+    </select>
+
+    <!--    插入问题-->
+    <insert id="insertSysServicesQuestion" parameterType="SysServicesQuestion" useGeneratedKeys="true"
+            keyProperty="queId" keyColumn="que_id">
+        insert into sys_services_question
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="queId != null">que_id,</if>
+            <if test="cusId != null">cus_id,</if>
+            <if test="content != null">content,</if>
+            <if test="type != null">type,</if>
+            <if test="createTime != null">create_time,</if>
+            <if test="img != null">img,</if>
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides=",">
+            <if test="queId != null">#{queId},</if>
+            <if test="cusId != null">#{cusId},</if>
+            <if test="content != null">#{content},</if>
+            <if test="type != null">#{type},</if>
+            <if test="createTime != null">#{createTime},</if>
+            <if test="img != null">
+                #{img,  jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
+            </if>
+        </trim>
+    </insert>
+
+    <!--    插入问题的四个角色:1,用户; 2,营养师; 3,售后; 4,助理-->
+    <insert id="insertSysServicesQuestionStatus" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"
+            keyColumn="id">
+        insert into sys_services_question_status (que_id, user_id, role, `read`, create_time, update_time) values
+        <foreach collection="list" item="status" index="index" separator=",">
+            (#{status.queId}, #{status.userId}, #{status.role}, #{status.read}, now(), now())
+        </foreach>
+    </insert>
+
+    <!--    根据状态id更新, role=customer 客户回复,这时更新另外三个角色未读;role != customer,更新客户未读-->
+    <update id="updateSysServicesQuestionStatus" parameterType="SysServicesQuestion">
+        update sys_services_question_status
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="read != null">`read` = #{read},</if>
+            <if test="read != null">update_time = now(),</if>
+        </trim>
+        <where>
+            <choose>
+                <when test="id != null">
+                    id = ${id}
+                </when>
+                <otherwise>
+                    <if test="role == 'customer'">
+                        role != #{role} and
+                    </if>
+                    <if test="role != 'customer'">
+                        role = 'customer' and
+                    </if>
+                    que_id = #{queId}
+                </otherwise>
+            </choose>
+        </where>
+    </update>
+
+    <!--    插入问题回复-->
+    <insert id="inserSysServicesQuestionReply" parameterType="SysServicesQuestion">
+        insert into sys_services_question_session
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="queId != null">que_id,</if>
+            <if test="userId != null">user_id,</if>
+            <if test="content != null">content,</if>
+            <if test="read != null">`read`,</if>
+            <if test="role != null">role,</if>
+            <if test="img != null">img,</if>
+            <if test="queId != null">create_time,</if>
+            <if test="queId != null">update_time,</if>
+
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides=",">
+            <if test="queId != null">#{queId},</if>
+            <if test="userId != null">#{userId},</if>
+            <if test="content != null">#{content},</if>
+            <if test="read != null">#{read},</if>
+            <if test="role != null">#{role},</if>
+            <if test="img != null">
+                #{img,  jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
+            </if>
+            <if test="queId != null">now(),</if>
+            <if test="queId != null">now(),</if>
+        </trim>
+    </insert>
+
+
+</mapper>
\ No newline at end of file

From e5def38565ead311ccdd250f3ecb38912a4af03a Mon Sep 17 00:00:00 2001
From: huangdeliang <huangdeliang@skieer.com>
Date: Thu, 27 May 2021 16:35:09 +0800
Subject: [PATCH 2/5] =?UTF-8?q?=E6=9A=82=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../custom/SysServiceTopicController.java     |  54 +++++
 .../custom/WechatAppletController.java        |  20 ++
 .../custom/domain/SysServicesTopic.java       |  21 +-
 .../custom/mapper/SysServicesTopicMapper.java |  22 ++
 .../service/ISysServicesTopicService.java     |  21 ++
 .../impl/SysServicesTopicServiceImp.java      | 121 +++++++++++
 .../mapper/custom/SysServicesTopicMapper.xml  | 188 +++++++++++++-----
 .../config/properties/DruidProperties.java    |   3 +
 8 files changed, 397 insertions(+), 53 deletions(-)
 create mode 100644 stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceTopicController.java
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysServicesTopicMapper.java
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesTopicService.java
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesTopicServiceImp.java

diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceTopicController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceTopicController.java
new file mode 100644
index 000000000..45b74e292
--- /dev/null
+++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysServiceTopicController.java
@@ -0,0 +1,54 @@
+package com.stdiet.web.controller.custom;
+
+import com.stdiet.common.core.controller.BaseController;
+import com.stdiet.common.core.domain.AjaxResult;
+import com.stdiet.common.core.page.TableDataInfo;
+import com.stdiet.common.utils.StringUtils;
+import com.stdiet.custom.domain.SysServicesTopic;
+import com.stdiet.custom.service.ISysServicesTopicService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/services/topic")
+public class SysServiceTopicController extends BaseController {
+
+    @Autowired
+    private ISysServicesTopicService servicesTopicService;
+
+    @GetMapping("/list")
+    public TableDataInfo list(SysServicesTopic topic) {
+        startPage();
+        return getDataTable(servicesTopicService.selectSysServicesTopicByUserIdAndRole(topic));
+    }
+
+//    @PutMapping("/update/status")
+//    public AjaxResult status(@RequestBody SysServicesQuestion sysServicesQuestion) {
+//        return toAjax(sysServicesQuestionService.updateSysServicesQuestionStatus(sysServicesQuestion));
+//    }
+
+    @PostMapping("/reply")
+    public AjaxResult reply(@RequestBody SysServicesTopic topic) {
+        return AjaxResult.success(servicesTopicService.inserSysServicesTopicReply(topic));
+    }
+
+    @PostMapping("/comment")
+    public AjaxResult comment(@RequestBody SysServicesTopic topic) {
+        return AjaxResult.success(servicesTopicService.inserSysServicesTopicComment(topic));
+    }
+
+    @GetMapping("/detail")
+    public AjaxResult detail(@RequestParam String topicId, @RequestParam String id) {
+        List<SysServicesTopic> questions = servicesTopicService.selectSysServicesTopicSessionByTopicId(topicId);
+        if (StringUtils.isNotNull(questions)) {
+            // 更新问题对应角色的状态
+            SysServicesTopic status = new SysServicesTopic();
+            status.setRead(1);
+            status.setId(id);
+            servicesTopicService.updateSysServicesTopicStatus(status);
+        }
+        return AjaxResult.success(questions);
+    }
+}
diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
index 5c7cb85e2..f012f7582 100644
--- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
+++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
@@ -70,6 +70,8 @@ public class WechatAppletController extends BaseController {
     private IWechatAppletService iWechatAppletService;
     @Autowired
     private ISysServicesQuestionService iSysServicesQuestionService;
+    @Autowired
+    private ISysServicesTopicService iSysServicesTopicService;
 
     /**
      * 查询微信小程序中展示的客户案例
@@ -605,6 +607,24 @@ public class WechatAppletController extends BaseController {
         return getDataTable(iSysServicesQuestionService.selectSysServicesQuestionByUserIdAndRole(servicesQuestion));
     }
 
+    /**
+     * 客户添加问题
+     *
+     * @param topic
+     * @param customerId
+     * @return
+     */
+    @PostMapping("/services/topic/post")
+    public AjaxResult postServiceTopic(@RequestBody SysServicesTopic topic, @RequestParam String customerId) {
+        Long cusId = StringUtils.isNotEmpty(customerId) ? Long.parseLong(AesUtils.decrypt(customerId)) : 0L;
+        if (cusId == 0L) {
+            return toAjax(0);
+        }
+        topic.setUid(cusId);
+
+        return AjaxResult.success(iSysServicesTopicService.insertSysServicesTopic(topic));
+    }
+
     /**
      * 客户添加问题
      *
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java
index c21645535..d76cf691a 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysServicesTopic.java
@@ -2,16 +2,19 @@ package com.stdiet.custom.domain;
 
 import com.alibaba.fastjson.JSONArray;
 import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonInclude;
 import lombok.Data;
 
 import java.util.Date;
+import java.util.List;
 
 @Data
+@JsonInclude(JsonInclude.Include.NON_NULL)
 public class SysServicesTopic {
     /**
      *
      */
-    Long id;
+    String id;
 
     /**
      * 问题id
@@ -23,6 +26,12 @@ public class SysServicesTopic {
      */
     Integer topicType;
 
+    String commentId;
+
+    String replyId;
+
+    Integer replyType;
+
     /**
      * 角色
      */
@@ -64,10 +73,20 @@ public class SysServicesTopic {
      */
     String role;
 
+    String fromRole;
+
+    String toRole;
+
     // 非持久化字段
     /**
      * 角色名字
      */
+    String fromName;
+    String toName;
     String name;
 
+    List<SysServicesTopic> comments;
+
+    List<SysServicesTopic> replys;
+
 }
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysServicesTopicMapper.java b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysServicesTopicMapper.java
new file mode 100644
index 000000000..29ecaadaa
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysServicesTopicMapper.java
@@ -0,0 +1,22 @@
+package com.stdiet.custom.mapper;
+
+import com.stdiet.custom.domain.SysServicesQuestion;
+import com.stdiet.custom.domain.SysServicesTopic;
+
+import java.util.List;
+
+public interface SysServicesTopicMapper {
+    List<SysServicesTopic> selectSysServicesTopicByUserIdAndRole(SysServicesTopic topic);
+
+    int insertSysServicesTopic(SysServicesTopic topic);
+
+    int insertSysServicesTopicStatus(List<SysServicesTopic> topics);
+
+    int updateSysServicesTopicStatus(SysServicesTopic topic);
+
+    int inserSysServicesTopicComment(SysServicesTopic topic);
+
+    int inserSysServicesTopicReply(SysServicesTopic topic);
+
+    List<SysServicesTopic> selectSysServicesTopicSessionByTopicId(String topicId);
+}
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesTopicService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesTopicService.java
new file mode 100644
index 000000000..dedfe3873
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysServicesTopicService.java
@@ -0,0 +1,21 @@
+package com.stdiet.custom.service;
+
+import com.stdiet.custom.domain.SysServicesQuestion;
+import com.stdiet.custom.domain.SysServicesTopic;
+
+import java.util.List;
+
+public interface ISysServicesTopicService {
+
+    List<SysServicesTopic> selectSysServicesTopicByUserIdAndRole(SysServicesTopic topic);
+
+    SysServicesTopic insertSysServicesTopic(SysServicesTopic topic);
+
+    int updateSysServicesTopicStatus(SysServicesTopic topic);
+
+    SysServicesTopic inserSysServicesTopicReply(SysServicesTopic topic);
+
+    SysServicesTopic inserSysServicesTopicComment(SysServicesTopic topic);
+
+    List<SysServicesTopic> selectSysServicesTopicSessionByTopicId(String topicId);
+}
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesTopicServiceImp.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesTopicServiceImp.java
new file mode 100644
index 000000000..a66a0b3dd
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysServicesTopicServiceImp.java
@@ -0,0 +1,121 @@
+package com.stdiet.custom.service.impl;
+
+import com.stdiet.common.utils.DateUtils;
+import com.stdiet.common.utils.uuid.UUID;
+import com.stdiet.custom.domain.SysCustomer;
+import com.stdiet.custom.domain.SysServicesTopic;
+import com.stdiet.custom.mapper.SysCustomerMapper;
+import com.stdiet.custom.mapper.SysServicesTopicMapper;
+import com.stdiet.custom.service.ISysServicesTopicService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class SysServicesTopicServiceImp implements ISysServicesTopicService {
+    @Autowired
+    SysServicesTopicMapper servicesTopicMapper;
+
+    @Autowired
+    SysCustomerMapper sysCustomerMapper;
+
+    @Override
+    public List<SysServicesTopic> selectSysServicesTopicByUserIdAndRole(SysServicesTopic topic) {
+        return servicesTopicMapper.selectSysServicesTopicByUserIdAndRole(topic);
+    }
+
+
+    @Override
+    public SysServicesTopic insertSysServicesTopic(SysServicesTopic topic) {
+        // 生成uuid
+        topic.setTopicId(UUID.randomUUID().toString().replaceAll("-", ""));
+        topic.setRead(1);
+        topic.setCreateTime(DateUtils.getNowDate());
+
+        servicesTopicMapper.insertSysServicesTopic(topic);
+
+        SysCustomer customer = sysCustomerMapper.selectSysCustomerById(topic.getUid());
+
+        List<SysServicesTopic> statusList = new ArrayList<>();
+
+        SysServicesTopic customerStatus = new SysServicesTopic();
+        customerStatus.setUid(customer.getId());
+        customerStatus.setRole("customer");
+        customerStatus.setRead(1);
+        customerStatus.setTopicId(topic.getTopicId());
+        statusList.add(customerStatus);
+
+        SysServicesTopic dieticianStatus = new SysServicesTopic();
+        dieticianStatus.setUid(customer.getMainDietitian());
+        dieticianStatus.setRole("dietician");
+        dieticianStatus.setRead(0);
+        dieticianStatus.setTopicId(topic.getTopicId());
+        statusList.add(dieticianStatus);
+
+        SysServicesTopic afterSaleStatus = new SysServicesTopic();
+        afterSaleStatus.setUid(customer.getAfterDietitian());
+        afterSaleStatus.setRole("after_sale");
+        afterSaleStatus.setRead(0);
+        afterSaleStatus.setTopicId(topic.getTopicId());
+        statusList.add(afterSaleStatus);
+
+        SysServicesTopic dieticianAssistantStatus = new SysServicesTopic();
+        dieticianAssistantStatus.setUid(customer.getAssistantDietitian());
+        dieticianAssistantStatus.setRole("dietician_assistant");
+        dieticianAssistantStatus.setRead(0);
+        dieticianAssistantStatus.setTopicId(topic.getTopicId());
+        statusList.add(dieticianAssistantStatus);
+
+        servicesTopicMapper.insertSysServicesTopicStatus(statusList);
+
+        topic.setId(customerStatus.getId());
+        topic.setUid(null);
+
+        return topic;
+    }
+
+    @Override
+    public int updateSysServicesTopicStatus(SysServicesTopic topic) {
+        return servicesTopicMapper.updateSysServicesTopicStatus(topic);
+    }
+
+
+    @Override
+    public SysServicesTopic inserSysServicesTopicReply(SysServicesTopic topic) {
+        String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
+        topic.setId(uuid);
+        int row = servicesTopicMapper.inserSysServicesTopicReply(topic);
+        if (row > 0) {
+            // 设置未读
+            SysServicesTopic status = new SysServicesTopic();
+            status.setRead(0);
+            status.setTopicId(topic.getTopicId());
+            status.setRole(topic.getRole());
+            servicesTopicMapper.updateSysServicesTopicStatus(status);
+        }
+        return topic;
+    }
+
+    @Override
+    public SysServicesTopic inserSysServicesTopicComment(SysServicesTopic topic) {
+        String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
+        topic.setId(uuid);
+        int row = servicesTopicMapper.inserSysServicesTopicComment(topic);
+        if (row > 0) {
+            // 设置未读
+            SysServicesTopic status = new SysServicesTopic();
+            status.setRead(0);
+            status.setTopicId(topic.getTopicId());
+            status.setRole(topic.getRole());
+            servicesTopicMapper.updateSysServicesTopicStatus(status);
+        }
+        return topic;
+    }
+
+    @Override
+    public List<SysServicesTopic> selectSysServicesTopicSessionByTopicId(String topicId) {
+        return servicesTopicMapper.selectSysServicesTopicSessionByTopicId(topicId);
+    }
+}
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml
index 160b5b90b..d309926c4 100644
--- a/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml
@@ -9,7 +9,6 @@
         <result column="topic_id" property="topicId"/>
         <result column="topic_type" property="topicType"/>
         <result column="content" property="content"/>
-        <result column="role" property="role"/>
         <result column="name" property="name"/>
         <result column="read" property="read"/>
         <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
@@ -17,91 +16,140 @@
         <result column="update_time" property="updateTime"/>
     </resultMap>
 
-    <resultMap id="SysServicesQuestionSessionResult" type="SysServicesQuestion">
-        <result column="id" property="id"/>
-        <result column="type" property="type"/>
-        <result column="content" property="content"/>
-        <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
-        <result column="create_time" property="createTime"/>
-        <association property="name" column="{userId=user_id,role=role}" select="selectUserInfo"/>
-    </resultMap>
 
     <!--    根据userId和角色查询问题列表-->
     <select id="selectSysServicesTopicByUserIdAndRole" parameterType="SysServicesTopic"
-            resultMap="SysServicesQuestionResult">
+            resultMap="SysServicesTopicResult">
         SELECT * FROM (
         SELECT * FROM sys_services_topic_status WHERE role = #{role} AND uid = #{uid}
         ) AS status
-        LEFT JOIN sys_services_question USING(topic_id)
+        LEFT JOIN sys_services_topic USING(topic_id)
         <choose>
             <when test="role == 'customer'">
                 LEFT JOIN (
-                    SELECT name, id AS uid FROM sys_customer WHERE id = #{uid}
-                ) AS customer USING(uid)
+                SELECT name, id AS uid FROM sys_customer WHERE id = #{uid}
+                ) AS customer ON status.uid = customer.uid
             </when>
             <otherwise>
                 LEFT JOIN (
-                    SELECT name, user_id AS uid, nick_name AS name FROM sys_user WHERE user_id = #{uid}
-                ) AS user USING(uid)
+                SELECT user_id AS uid, nick_name AS name FROM sys_user WHERE user_id = #{uid}
+                ) AS user ON status.uid = user.uid
             </otherwise>
         </choose>
         ORDER BY `read` ASC, update_time DESC
     </select>
 
-    <select id="selectSysServicesQuestionSessionByQueId" resultMap="SysServicesQuestionSessionResult"
+    <!--    查询主题-->
+
+    <resultMap id="SysServicesTopicSessionResult" type="SysServicesTopic">
+        <result column="id" property="id"/>
+        <result column="uid" property="uid"/>
+        <result column="topic_id" property="topicId"/>
+        <result column="topic_type" property="topicType"/>
+        <result column="content" property="content"/>
+        <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
+        <result column="create_time" property="createTime"/>
+        <association property="name" column="{uid=uid,role=role}" select="selectUserInfo"/>
+        <collection property="comments" column="topic_id" select="selectServicesTopicCommentByTopicId"/>
+    </resultMap>
+
+    <resultMap id="ServicesTopicCommentResult" type="SysServicesTopic">
+        <result column="id" property="id"/>
+        <result column="topic_id" property="topicId"/>
+        <result column="content" property="content"/>
+        <result column="from_uid" property="fromUid"/>
+        <result column="from_role" property="fromRole"/>
+        <result column="to_uid" property="toUid"/>
+        <result column="to_role" property="toRole"/>
+        <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
+        <result column="create_time" property="createTime"/>
+        <association property="fromName" column="{uid=from_uid,role=from_role}" select="selectUserInfo"/>
+        <association property="toName" column="{uid=to_uid,role=to_role}" select="selectUserInfo"/>
+        <collection property="replys" column="id" ofType="ServicesTopicCommentReplyResult"
+                    select="selectServicesTopicCommentReplyByCommentId"/>
+    </resultMap>
+
+    <resultMap id="ServicesTopicCommentReplyResult" type="SysServicesTopic">
+        <result column="id" property="id"/>
+        <result column="topic_id" property="topicId"/>
+        <result column="content" property="content"/>
+        <result column="from_uid" property="fromUid"/>
+        <result column="from_role" property="fromRole"/>
+        <result column="to_uid" property="toUid"/>
+        <result column="to_role" property="toRole"/>
+        <result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
+        <result column="create_time" property="createTime"/>
+        <association property="fromName" column="{uid=from_uid,role=from_role}" select="selectUserInfo"/>
+        <association property="toName" column="{uid=to_uid,role=to_role}" select="selectUserInfo"/>
+
+    </resultMap>
+
+    <select id="selectSysServicesTopicSessionByTopicId" resultMap="SysServicesTopicSessionResult"
             parameterType="String">
-        select que_id, cus_id as user_id, 'customer' as role, content, img, create_time from sys_services_question where que_id = #{queId}
-        union select que_id, user_id, role, content, img, create_time from sys_services_question_session where que_id = #{queId}
-        order by create_time asc
+        select topic_id, uid, 'customer' as role, content, img, create_time from sys_services_topic where topic_id = #{topicId}
     </select>
 
+    <select id="selectServicesTopicCommentByTopicId" resultMap="ServicesTopicCommentResult">
+        select * from sys_services_topic_comment where topic_id = #{topic_id}
+    </select>
+
+    <select id="selectServicesTopicCommentReplyByCommentId" resultMap="ServicesTopicCommentReplyResult">
+        select * from sys_services_topic_reply where reply_type = 0 and reply_id = #{id}
+    </select>
+
+    <select id="selectServicesTopicCommentReplyReplyByCommentId" resultMap="ServicesTopicCommentReplyResult">
+        select * from sys_services_topic_reply where reply_type = 1 and reply_id = #{id}
+    </select>
+
+
+    <!--    查询人名-->
     <select id="selectUserInfo" parameterType="java.util.Map" resultType="String">
         <choose>
             <when test="_parameter.get('role') == 'customer'">
-                select name from sys_customer where id = #{userId}
+                select name from sys_customer where id = #{uid}
             </when>
             <otherwise>
-                select nick_name from sys_user where user_id = #{userId}
+                select nick_name from sys_user where user_id = #{uid}
             </otherwise>
         </choose>
     </select>
 
     <!--    插入问题-->
-    <insert id="insertSysServicesQuestion" parameterType="SysServicesQuestion" useGeneratedKeys="true"
-            keyProperty="queId" keyColumn="que_id">
-        insert into sys_services_question
+    <insert id="insertSysServicesTopic" parameterType="SysServicesTopic" useGeneratedKeys="true"
+            keyProperty="id" keyColumn="id">
+        insert into sys_services_topic
         <trim prefix="(" suffix=")" suffixOverrides=",">
-            <if test="queId != null">que_id,</if>
-            <if test="cusId != null">cus_id,</if>
+            <if test="topicId != null">topic_id,</if>
+            <if test="uid != null">uid,</if>
             <if test="content != null">content,</if>
-            <if test="type != null">type,</if>
+            <if test="topicType != null">topic_type,</if>
             <if test="createTime != null">create_time,</if>
             <if test="img != null">img,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
-            <if test="queId != null">#{queId},</if>
-            <if test="cusId != null">#{cusId},</if>
+            <if test="topicId != null">#{topicId},</if>
+            <if test="uid != null">#{uid},</if>
             <if test="content != null">#{content},</if>
-            <if test="type != null">#{type},</if>
+            <if test="topicType != null">#{topicType},</if>
             <if test="createTime != null">#{createTime},</if>
             <if test="img != null">
-                #{img,  jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
+                #{img, jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
             </if>
         </trim>
     </insert>
 
     <!--    插入问题的四个角色:1,用户; 2,营养师; 3,售后; 4,助理-->
-    <insert id="insertSysServicesQuestionStatus" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"
+    <insert id="insertSysServicesTopicStatus" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"
             keyColumn="id">
-        insert into sys_services_question_status (que_id, user_id, role, `read`, create_time, update_time) values
+        insert into sys_services_topic_status (topic_id, uid, role, `read`, create_time, update_time) values
         <foreach collection="list" item="status" index="index" separator=",">
-            (#{status.queId}, #{status.userId}, #{status.role}, #{status.read}, now(), now())
+            (#{status.topicId}, #{status.uid}, #{status.role}, #{status.read}, now(), now())
         </foreach>
     </insert>
 
     <!--    根据状态id更新, role=customer 客户回复,这时更新另外三个角色未读;role != customer,更新客户未读-->
-    <update id="updateSysServicesQuestionStatus" parameterType="SysServicesQuestion">
-        update sys_services_question_status
+    <update id="updateSysServicesTopicStatus" parameterType="SysServicesTopic">
+        update sys_services_topic_status
         <trim prefix="SET" suffixOverrides=",">
             <if test="read != null">`read` = #{read},</if>
             <if test="read != null">update_time = now(),</if>
@@ -118,37 +166,73 @@
                     <if test="role != 'customer'">
                         role = 'customer' and
                     </if>
-                    que_id = #{queId}
+                    topic_id = #{topicId}
                 </otherwise>
             </choose>
         </where>
     </update>
 
     <!--    插入问题回复-->
-    <insert id="inserSysServicesQuestionReply" parameterType="SysServicesQuestion">
-        insert into sys_services_question_session
+    <insert id="inserSysServicesTopicComment" parameterType="SysServicesTopic">
+        insert into sys_services_topic_comment
         <trim prefix="(" suffix=")" suffixOverrides=",">
-            <if test="queId != null">que_id,</if>
-            <if test="userId != null">user_id,</if>
+            <if test="id != null">id,</if>
+            <if test="topicId != null">topic_id,</if>
+            <if test="fromUid != null">from_uid,</if>
+            <if test="fromRole != null">from_role,</if>
+            <if test="toUid != null">to_uid,</if>
+            <if test="toRole != null">to_role,</if>
             <if test="content != null">content,</if>
-            <if test="read != null">`read`,</if>
-            <if test="role != null">role,</if>
             <if test="img != null">img,</if>
-            <if test="queId != null">create_time,</if>
-            <if test="queId != null">update_time,</if>
-
+            <if test="topicId != null">create_time,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
-            <if test="queId != null">#{queId},</if>
-            <if test="userId != null">#{userId},</if>
+            <if test="id != null">#{id},</if>
+            <if test="topicId != null">#{topicId},</if>
+            <if test="fromUid != null">#{fromUid},</if>
+            <if test="fromRole != null">#{fromRole},</if>
+            <if test="toUid != null">#{toUid},</if>
+            <if test="toRole != null">#{toRole},</if>
             <if test="content != null">#{content},</if>
-            <if test="read != null">#{read},</if>
-            <if test="role != null">#{role},</if>
             <if test="img != null">
                 #{img,  jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
             </if>
-            <if test="queId != null">now(),</if>
-            <if test="queId != null">now(),</if>
+            <if test="topicId != null">now(),</if>
+        </trim>
+    </insert>
+
+    <!--    插入问题回复-->
+    <insert id="inserSysServicesTopicReply" parameterType="SysServicesQuestion" useGeneratedKeys="true" keyColumn="id"
+            keyProperty="id">
+        insert into sys_services_topic_reply
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="id != null">id,</if>
+            <if test="commentId != null">comment_id,</if>
+            <if test="replyId != null">reply_id,</if>
+            <if test="replyType != null">reply_type,</if>
+            <if test="content != null">content,</if>
+            <if test="fromUid != null">from_uid,</if>
+            <if test="toUid != null">to_uid,</if>
+            <if test="img != null">img,</if>
+            <if test="fromRole != null">from_role,</if>
+            <if test="toRole != null">to_role,</if>
+            <if test="content != null">create_time,</if>
+
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides=",">
+            <if test="id != null">#{id},</if>
+            <if test="commentId != null">#{commentId},</if>
+            <if test="replyId != null">#{replyId},</if>
+            <if test="replyType != null">#{replyType},</if>
+            <if test="content != null">#{content},</if>
+            <if test="fromUid != null">#{fromUid},</if>
+            <if test="toUid != null">#{toUid},</if>
+            <if test="img != null">
+                #{img,  jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
+            </if>
+            <if test="fromRole != null">#{fromRole},</if>
+            <if test="toRole != null">#{toRole},</if>
+            <if test="content != null">now(),</if>
         </trim>
     </insert>
 
diff --git a/stdiet-framework/src/main/java/com/stdiet/framework/config/properties/DruidProperties.java b/stdiet-framework/src/main/java/com/stdiet/framework/config/properties/DruidProperties.java
index 73e8004d3..cbe94f040 100644
--- a/stdiet-framework/src/main/java/com/stdiet/framework/config/properties/DruidProperties.java
+++ b/stdiet-framework/src/main/java/com/stdiet/framework/config/properties/DruidProperties.java
@@ -72,6 +72,9 @@ public class DruidProperties
         datasource.setTestOnBorrow(testOnBorrow);
         /** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
         datasource.setTestOnReturn(testOnReturn);
+
+        datasource.setRemoveAbandoned(true);
+        datasource.setRemoveAbandonedTimeout(180);
         return datasource;
     }
 }

From e333cb007e0b61b9afa76b668b375de99bc4f223 Mon Sep 17 00:00:00 2001
From: xiezhijun <15270898033@163.com>
Date: Thu, 27 May 2021 20:03:43 +0800
Subject: [PATCH 3/5] =?UTF-8?q?=E6=8F=90=E6=88=90=E6=98=8E=E7=BB=86?=
 =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=8A=A0=E4=B8=8A=E6=9B=BF=E6=8D=A2=E7=9A=84?=
 =?UTF-8?q?=E8=AE=A2=E5=8D=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../custom/SysCommisionController.java        |  1 +
 .../stdiet/custom/domain/SysCommision.java    |  3 ++
 ...sOrderNutritionistReplaceRecordMapper.java |  7 ++++
 ...OrderNutritionistReplaceRecordService.java |  7 ++++
 .../impl/SysCommissionDayServiceImpl.java     | 34 +++++++++++++++----
 ...rNutritionistReplaceRecordServiceImpl.java |  9 +++++
 .../mapper/custom/SysOrderMapper.xml          | 10 ++++--
 ...ysOrderNutritionistReplaceRecordMapper.xml |  7 ++++
 8 files changed, 69 insertions(+), 9 deletions(-)

diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysCommisionController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysCommisionController.java
index 126059004..a8851e21a 100644
--- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysCommisionController.java
+++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysCommisionController.java
@@ -255,6 +255,7 @@ public class SysCommisionController extends BaseController {
     @PreAuthorize("@ss.hasPermi('commisionDay:detail:list')")
     @GetMapping("/orderDetailDay")
     public AjaxResult getOrderCommissionDetailDay(SysCommision sysCommision) {
+        sysCommision.setReplaceOrderFlag(true);
         startPage();
         return sysCommissionDayService.calculateOrderCommissionDetail(sysCommision);
     }
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysCommision.java b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysCommision.java
index 85e5c363d..d7177a226 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysCommision.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysCommision.java
@@ -78,4 +78,7 @@ public class SysCommision extends BaseEntity {
 
     //服务结束时间,用于计算该时间段的提成
     private String serverScopeEndTime;
+
+    //是否查询替换订单
+    private Boolean replaceOrderFlag;
 }
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderNutritionistReplaceRecordMapper.java b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderNutritionistReplaceRecordMapper.java
index f0e2db464..241ecc223 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderNutritionistReplaceRecordMapper.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderNutritionistReplaceRecordMapper.java
@@ -66,4 +66,11 @@ public interface SysOrderNutritionistReplaceRecordMapper
      * @return
      */
     List<SysOrderNutritionistReplaceRecord> getSysOrderReplaceRecordByOrderId(@Param("orderId")Long orderId);
+
+    /**
+     * 根据用户ID查询营养师、售后更换记录
+     * @param userId
+     * @return
+     */
+    List<SysOrderNutritionistReplaceRecord> getSysOrderReplaceRecordByUserId(@Param("userId")Long userId);
 }
\ No newline at end of file
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderNutritionistReplaceRecordService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderNutritionistReplaceRecordService.java
index e6e29af7f..848ff6f0d 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderNutritionistReplaceRecordService.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderNutritionistReplaceRecordService.java
@@ -65,4 +65,11 @@ public interface ISysOrderNutritionistReplaceRecordService
      * @return
      */
     List<SysOrderNutritionistReplaceRecord> getSysOrderReplaceRecordByOrderId(Long orderId);
+
+    /**
+     * 根据用户ID查询营养师、售后更换记录
+     * @param userId
+     * @return
+     */
+    List<SysOrderNutritionistReplaceRecord> getSysOrderReplaceRecordByUserId(Long userId);
 }
\ No newline at end of file
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
index be05d3a8c..e78792498 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
@@ -435,14 +435,20 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
      * @param cutOrderFlag 是否根据售后、营养师更换记录进行订单切割计算提成
      * @return
      */
-    public Map<Long, List<SysOrderCommisionDayDetail>> getOrderByList(SysCommision sysCommision, boolean cutOrderFlag){
+    public Map<Long, List<SysOrderCommisionDayDetail>> getOrderByList(SysCommision sysCommision, Boolean cutOrderFlag){
         //查询2021年1月份之后所有订单
+        System.out.println(sysCommision.getReplaceOrderFlag() == null);
         List<SysOrder> orderList = sysOrderMapper.selectSimpleOrderMessage(sysCommision);
-        Map<Long, List<SysOrderNutritionistReplaceRecord>> replaceRecordMap = null;
-        if(cutOrderFlag){
+        Map<Long, List<SysOrderNutritionistReplaceRecord>> replaceRecordMap = dealNutritionistReplaceRecord(sysOrderNutritionistReplaceRecordService.getSysOrderReplaceRecordByOrderId(null));
+        /*if(cutOrderFlag){
             //查询所有订单营养师、售后转移记录
             replaceRecordMap = dealNutritionistReplaceRecord(sysOrderNutritionistReplaceRecordService.getSysOrderReplaceRecordByOrderId(null));
-        }
+        }*/
+        //根据用户ID查询所有替换记录
+        /*List<SysOrderNutritionistReplaceRecord> usertReplaceRecordList = null;
+        if(replaceOrderFlag != null && replaceOrderFlag && sysCommision.getUserId() != null){
+             usertReplaceRecordList = sysOrderNutritionistReplaceRecordService.getSysOrderReplaceRecordByUserId(sysCommision.getUserId());
+        }*/
         //整理出每个用户对应的订单List
         Map<Long, List<SysOrderCommisionDayDetail>> userOrderResultMap = new HashMap<>();
         for (SysOrder sysOrder : orderList) {
@@ -462,9 +468,22 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
                 SysOrderCommisionDayDetail sysOrderCommisionDayDetail = statisticsOrderMessage(sysOrder, sysCommision.getServerScopeStartTime(), sysCommision.getServerScopeEndTime());
                 orderCommisionDayDetailList = cutOrderByReplaceRecord(sysOrder, sysCommision, sysOrderCommisionDayDetail, replaceRecordMap.get(sysOrder.getOrderId()));
             }else{
-                 SysOrderCommisionDayDetail commisionDetail = statisticsOrderMessage(sysOrder, sysCommision.getServerScopeStartTime(), sysCommision.getServerScopeEndTime());
-                 orderCommisionDayDetailList = new ArrayList<>();
-                 orderCommisionDayDetailList.add(commisionDetail);
+                SysOrderCommisionDayDetail commisionDetail = statisticsOrderMessage(sysOrder, sysCommision.getServerScopeStartTime(), sysCommision.getServerScopeEndTime());
+                if(sysCommision.getReplaceOrderFlag() != null && sysCommision.getReplaceOrderFlag() && sysCommision.getUserId() != null &&
+                        replaceRecordMap.containsKey(sysOrder.getOrderId())){
+                    //判断该订单是否属于营养师、售后替换订单
+                    List<SysOrderCommisionDayDetail> userorderCommisionList = cutOrderByReplaceRecord(sysOrder, sysCommision, commisionDetail, replaceRecordMap.get(sysOrder.getOrderId()));
+                    for (SysOrderCommisionDayDetail detail : userorderCommisionList) {
+                        if((detail.getAfterSaleId() != null && detail.getAfterSaleId().longValue() == sysCommision.getUserId()) || (detail.getNutritionistId() != null && detail.getNutritionistId().longValue() == sysCommision.getUserId())){
+                            commisionDetail = detail;
+                            break;
+                        }
+                    }
+                }
+                orderCommisionDayDetailList = orderCommisionDayDetailList == null ? new ArrayList<>() : orderCommisionDayDetailList;
+                if(commisionDetail != null){
+                    orderCommisionDayDetailList.add(commisionDetail);
+                }
             }
             if(orderCommisionDayDetailList != null){
                 for (SysOrderCommisionDayDetail detail : orderCommisionDayDetailList) {
@@ -477,6 +496,7 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
                 }
             }
         }
+        System.out.println(userOrderResultMap.get(131L).size() +"   订单数量");
         return userOrderResultMap;
     }
 
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderNutritionistReplaceRecordServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderNutritionistReplaceRecordServiceImpl.java
index 64bfe8c55..ee22c63fd 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderNutritionistReplaceRecordServiceImpl.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderNutritionistReplaceRecordServiceImpl.java
@@ -105,4 +105,13 @@ public class SysOrderNutritionistReplaceRecordServiceImpl implements ISysOrderNu
     public List<SysOrderNutritionistReplaceRecord> getSysOrderReplaceRecordByOrderId(Long orderId){
         return sysOrderNutritionistReplaceRecordMapper.getSysOrderReplaceRecordByOrderId(orderId);
     }
+
+    /**
+     * 根据用户ID查询营养师、售后更换记录
+     * @param userId
+     * @return
+     */
+    public List<SysOrderNutritionistReplaceRecord> getSysOrderReplaceRecordByUserId(Long userId){
+        return sysOrderNutritionistReplaceRecordMapper.getSysOrderReplaceRecordByUserId(userId);
+    }
 }
\ No newline at end of file
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
index 7ecd014d0..8d6b90985 100644
--- a/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
@@ -437,8 +437,14 @@
         <if test="reviewStatus != null and reviewStatus != ''">
             and review_status = #{reviewStatus}
         </if>
-        <if test="userId != null"><!-- 限于营养师、售后不可能同一个的情况下 -->
-            and (su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId})
+        <if test="userId != null">
+            and (
+              su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId}
+              <!-- 是否查询根据用户ID查询对应替换订单 -->
+              <if test="replaceOrderFlag != null">
+                or o.order_id in (select r.order_id from sys_order_nutritionist_replace_record r where r.del_flag = 0 and (r.after_sale_id = #{userId} or r.nutritionist_id = #{userId}) )
+              </if>
+            )
         </if>
         <if test="endTime != null and endTime != ''">
             AND DATE_FORMAT(o.order_time,'%Y-%m-%d') &lt;= #{endTime}
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysOrderNutritionistReplaceRecordMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysOrderNutritionistReplaceRecordMapper.xml
index 3297c6826..188d690a7 100644
--- a/stdiet-custom/src/main/resources/mapper/custom/SysOrderNutritionistReplaceRecordMapper.xml
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysOrderNutritionistReplaceRecordMapper.xml
@@ -114,4 +114,11 @@
         order by id asc
     </select>
 
+    <select id="getSysOrderReplaceRecordByUserId" parameterType="Long"
+            resultMap="SysOrderNutritionistReplaceRecordResult">
+        select id, order_id, nutritionist_id, after_sale_id, nutri_assis_id, start_time, create_time, create_by from
+        sys_order_nutritionist_replace_record
+        where del_flag = 0 and after_sale_id = #{userId} or nutritionist_id = #{userId} order by id asc
+    </select>
+
 </mapper>
\ No newline at end of file

From 0122c35553300b19a070679f7352bb79ac70bedd Mon Sep 17 00:00:00 2001
From: xiezhijun <15270898033@163.com>
Date: Fri, 28 May 2021 00:46:21 +0800
Subject: [PATCH 4/5] =?UTF-8?q?=E6=8F=90=E6=88=90=E4=BC=98=E5=8C=96?=
 =?UTF-8?q?=EF=BC=8C=E9=9C=80=E8=A6=81=E5=8A=A0=E4=B8=8A=E6=9B=B4=E6=8D=A2?=
 =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=8F=90=E6=88=90=E8=AF=A6=E6=83=85?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../impl/SysCommissionDayServiceImpl.java     | 38 +++++++++----------
 .../mapper/custom/SysOrderMapper.xml          | 12 ++++--
 .../components/OrdercommissDetail/index.vue   | 18 ++++-----
 3 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
index e78792498..3676d57df 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
@@ -437,8 +437,8 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
      */
     public Map<Long, List<SysOrderCommisionDayDetail>> getOrderByList(SysCommision sysCommision, Boolean cutOrderFlag){
         //查询2021年1月份之后所有订单
-        System.out.println(sysCommision.getReplaceOrderFlag() == null);
         List<SysOrder> orderList = sysOrderMapper.selectSimpleOrderMessage(sysCommision);
+        //查询所有订单营养师、售后转移记录
         Map<Long, List<SysOrderNutritionistReplaceRecord>> replaceRecordMap = dealNutritionistReplaceRecord(sysOrderNutritionistReplaceRecordService.getSysOrderReplaceRecordByOrderId(null));
         /*if(cutOrderFlag){
             //查询所有订单营养师、售后转移记录
@@ -459,31 +459,27 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
                 //System.out.println("客户:"+ sysOrder.getCustomer() +",营养师:"+sysOrder.getNutritionist() + ",售后" + sysOrder.getAfterSale());
                 continue;
             }
-            List<SysOrderCommisionDayDetail> orderCommisionDayDetailList = null;
+            List<SysOrderCommisionDayDetail> orderCommisionDayDetailList = new ArrayList<>();
             //将服务结束时间设置为空,因为提成的结束时间需要重新计算
             sysOrder.setServerEndTime(null);
             //判断是否存在营养师、售后更换记录
-            if(cutOrderFlag && replaceRecordMap.containsKey(sysOrder.getOrderId()) && replaceRecordMap.get(sysOrder.getOrderId()).size() > 0){
+            if(replaceRecordMap.containsKey(sysOrder.getOrderId()) && replaceRecordMap.get(sysOrder.getOrderId()).size() > 0){
                 //将订单根据更换记录切割成多个订单
                 SysOrderCommisionDayDetail sysOrderCommisionDayDetail = statisticsOrderMessage(sysOrder, sysCommision.getServerScopeStartTime(), sysCommision.getServerScopeEndTime());
-                orderCommisionDayDetailList = cutOrderByReplaceRecord(sysOrder, sysCommision, sysOrderCommisionDayDetail, replaceRecordMap.get(sysOrder.getOrderId()));
-            }else{
-                SysOrderCommisionDayDetail commisionDetail = statisticsOrderMessage(sysOrder, sysCommision.getServerScopeStartTime(), sysCommision.getServerScopeEndTime());
-                if(sysCommision.getReplaceOrderFlag() != null && sysCommision.getReplaceOrderFlag() && sysCommision.getUserId() != null &&
-                        replaceRecordMap.containsKey(sysOrder.getOrderId())){
-                    //判断该订单是否属于营养师、售后替换订单
-                    List<SysOrderCommisionDayDetail> userorderCommisionList = cutOrderByReplaceRecord(sysOrder, sysCommision, commisionDetail, replaceRecordMap.get(sysOrder.getOrderId()));
-                    for (SysOrderCommisionDayDetail detail : userorderCommisionList) {
+                List<SysOrderCommisionDayDetail> muchCommisionDayDetailList = cutOrderByReplaceRecord(sysOrder, sysCommision, sysOrderCommisionDayDetail, replaceRecordMap.get(sysOrder.getOrderId()));
+                if(sysCommision.getUserId() != null){
+                    for (SysOrderCommisionDayDetail detail : muchCommisionDayDetailList) {
                         if((detail.getAfterSaleId() != null && detail.getAfterSaleId().longValue() == sysCommision.getUserId()) || (detail.getNutritionistId() != null && detail.getNutritionistId().longValue() == sysCommision.getUserId())){
-                            commisionDetail = detail;
+                            orderCommisionDayDetailList.add(detail);
                             break;
                         }
                     }
+                }else{
+                    orderCommisionDayDetailList.addAll(muchCommisionDayDetailList);
                 }
-                orderCommisionDayDetailList = orderCommisionDayDetailList == null ? new ArrayList<>() : orderCommisionDayDetailList;
-                if(commisionDetail != null){
-                    orderCommisionDayDetailList.add(commisionDetail);
-                }
+            }else{
+                SysOrderCommisionDayDetail commisionDetail = statisticsOrderMessage(sysOrder, sysCommision.getServerScopeStartTime(), sysCommision.getServerScopeEndTime());
+                orderCommisionDayDetailList.add(commisionDetail);
             }
             if(orderCommisionDayDetailList != null){
                 for (SysOrderCommisionDayDetail detail : orderCommisionDayDetailList) {
@@ -493,10 +489,12 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
                     if(detail.getNutritionistId() != null && detail.getNutritionistId() > 0L){
                         addUserOrderResultMap(detail.getNutritionistId(), detail, userOrderResultMap);
                     }
+                    if(detail.getAfterSaleId() != null && detail.getAfterSaleId().longValue() == 257L){
+                        System.out.println(detail.getOrderId() + "-" + detail.getOrderAmount());
+                    }
                 }
             }
         }
-        System.out.println(userOrderResultMap.get(131L).size() +"   订单数量");
         return userOrderResultMap;
     }
 
@@ -516,10 +514,12 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
 
         //售后和营养师分类
         for (SysOrderNutritionistReplaceRecord sysOrderRecord : replaceRecordList) {
-            if (sysOrderRecord.getNutritionistId() != null && sysOrderRecord.getNutritionistId().longValue() > 0) {
+            if (sysOrderRecord.getNutritionistId() != null && sysOrderRecord.getNutritionistId().longValue() > 0 &&
+                    sysOrder.getNutritionistId() != null && sysOrder.getNutritionistId().longValue() != sysOrderRecord.getNutritionistId()) {
                 nutritionistRecord.add(sysOrderRecord);
             }
-            if (sysOrderRecord.getAfterSaleId() != null && sysOrderRecord.getAfterSaleId().longValue() > 0) {
+            if (sysOrderRecord.getAfterSaleId() != null && sysOrderRecord.getAfterSaleId().longValue() > 0
+                && sysOrder.getAfterSaleId().longValue() != sysOrderRecord.getAfterSaleId()) {
                 afterSaleRecord.add(sysOrderRecord);
             }
         }
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
index 8d6b90985..d35cca2fc 100644
--- a/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
@@ -441,9 +441,9 @@
             and (
               su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId}
               <!-- 是否查询根据用户ID查询对应替换订单 -->
-              <if test="replaceOrderFlag != null">
+              <!--<if test="replaceOrderFlag != null">-->
                 or o.order_id in (select r.order_id from sys_order_nutritionist_replace_record r where r.del_flag = 0 and (r.after_sale_id = #{userId} or r.nutritionist_id = #{userId}) )
-              </if>
+             <!--</if>-->
             )
         </if>
         <if test="endTime != null and endTime != ''">
@@ -469,7 +469,13 @@
             and review_status = #{reviewStatus}
         </if>
         <if test="userId != null"><!-- 限于营养师、售后不可能同一个的情况下 -->
-            and (su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId})
+            and (
+            su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId}
+            <!-- 是否查询根据用户ID查询对应替换订单 -->
+            <!--<if test="replaceOrderFlag != null">-->
+            or o.order_id in (select r.order_id from sys_order_nutritionist_replace_record r where r.del_flag = 0 and (r.after_sale_id = #{userId} or r.nutritionist_id = #{userId}) )
+            <!--</if>-->
+            )
         </if>
         <if test="endTime != null and endTime != ''">
             AND DATE_FORMAT(o.order_time,'%Y-%m-%d') &lt;= #{endTime}
diff --git a/stdiet-ui/src/components/OrdercommissDetail/index.vue b/stdiet-ui/src/components/OrdercommissDetail/index.vue
index c804f0b3d..d015cf6d6 100644
--- a/stdiet-ui/src/components/OrdercommissDetail/index.vue
+++ b/stdiet-ui/src/components/OrdercommissDetail/index.vue
@@ -24,7 +24,7 @@
                 end-placeholder="结束日期"
                 format="yyyy-MM-dd"
                 value-format="yyyy-MM-dd"
-              
+
               >
               </el-date-picker>
             </el-form-item>
@@ -37,7 +37,7 @@
         <div
           class="order_total_data"
         >
-        
+
        <span class="order_total_data_span_right20">
             当前页总服务金额:{{totalServerAmount}}元
           </span>
@@ -49,7 +49,7 @@
           >
           <span class="order_total_data_span"
             >未发放总提成:{{ totalNotSendCommission }}元</span
-          >  
+          >
         </div>
         <el-row :gutter="10" class="mb8" style="margin-top: 10px;margin-left:10px">
           <el-col :span="1.5">
@@ -187,7 +187,7 @@
                 >订单详情</el-button
               >
             </template>
-          </el-table-column> 
+          </el-table-column>
         </el-table>
         </div>
         <!--<div style="float: right; margin-right: 40px">
@@ -202,7 +202,7 @@
           >
           <span style="font-size:14px;margin-top:-200px"
             >未发放总提成:{{ totalNotSendCommission }}元</span
-          >  
+          >
         </div>-->
         <pagination
           v-show="total > 0"
@@ -210,10 +210,10 @@
           :page.sync="queryParam.pageNum"
           :limit.sync="queryParam.pageSize"
           @pagination="fetchOrderList"
-          :pageSizes="[10, 15, 30, 50, 100]"
+          :pageSizes="[10, 20, 50, 100, 500, 1000]"
         >
         </pagination>
-        
+
       </div>
     </el-drawer>
 
@@ -360,8 +360,8 @@ export default {
 }
 
 .order_total_data {
-  float: right; 
-  margin-right: 40px; 
+  float: right;
+  margin-right: 40px;
   margin-bottom:15px
 }
 

From d48cecc9237d1f39882cd605aa88d797b9b17820 Mon Sep 17 00:00:00 2001
From: xiezhijun <15270898033@163.com>
Date: Fri, 28 May 2021 18:32:20 +0800
Subject: [PATCH 5/5] =?UTF-8?q?=E6=8F=90=E6=88=90=E4=BC=98=E5=8C=96?=
 =?UTF-8?q?=EF=BC=8C=E5=8A=A0=E4=B8=8A=E6=9B=BF=E6=8D=A2=E8=AE=A2=E5=8D=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../service/impl/SysCommissionDayServiceImpl.java | 15 ++++++---------
 .../resources/mapper/custom/SysOrderMapper.xml    |  2 +-
 .../src/components/OrdercommissDetail/index.vue   |  7 ++++++-
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
index 3676d57df..c0a128add 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysCommissionDayServiceImpl.java
@@ -53,7 +53,7 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
         total.setTotalNotSentCommissionAmount(new BigDecimal(0));
         total.setNextMonthCommission(new BigDecimal(0));
         if(list != null && list.size() > 0){
-            sysCommision.setUserId(null);  //由于存在售后、营养师更换问题,不能根据营养师或售后查询订单
+            //sysCommision.setUserId(null);  //由于存在售后、营养师更换问题,不能根据营养师或售后查询订单
             Map<Long, List<SysOrderCommisionDayDetail>> orderDetailMap = getOrderByList(sysCommision, true);
             SysCommissionDayDetail sysCommissionDayDetail = null;
             for(SysCommision commision : list){
@@ -489,9 +489,6 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
                     if(detail.getNutritionistId() != null && detail.getNutritionistId() > 0L){
                         addUserOrderResultMap(detail.getNutritionistId(), detail, userOrderResultMap);
                     }
-                    if(detail.getAfterSaleId() != null && detail.getAfterSaleId().longValue() == 257L){
-                        System.out.println(detail.getOrderId() + "-" + detail.getOrderAmount());
-                    }
                 }
             }
         }
@@ -514,12 +511,12 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
 
         //售后和营养师分类
         for (SysOrderNutritionistReplaceRecord sysOrderRecord : replaceRecordList) {
-            if (sysOrderRecord.getNutritionistId() != null && sysOrderRecord.getNutritionistId().longValue() > 0 &&
-                    sysOrder.getNutritionistId() != null && sysOrder.getNutritionistId().longValue() != sysOrderRecord.getNutritionistId()) {
+            if (sysOrderRecord.getNutritionistId() != null && sysOrderRecord.getNutritionistId().longValue() > 0
+                    && sysOrder.getNutritionistId() != null && sysOrder.getNutritionistId().longValue() != sysOrderRecord.getNutritionistId().longValue()) {
                 nutritionistRecord.add(sysOrderRecord);
             }
             if (sysOrderRecord.getAfterSaleId() != null && sysOrderRecord.getAfterSaleId().longValue() > 0
-                && sysOrder.getAfterSaleId().longValue() != sysOrderRecord.getAfterSaleId()) {
+                    && sysOrder.getAfterSaleId() != null && sysOrder.getAfterSaleId().longValue() != sysOrderRecord.getAfterSaleId().longValue()) {
                 afterSaleRecord.add(sysOrderRecord);
             }
         }
@@ -613,12 +610,12 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
                 }
             }
         }
-        /**for (SysOrderCommisionDayDetail c : sysOrderCommisionDayDetailList) {
+        /*for (SysOrderCommisionDayDetail c : sysOrderCommisionDayDetailList) {
             System.out.println(c.getOrderId() + "-" + c.getNutritionistId() + "-" + c.getAfterSaleId() + "-"+
                     DateUtils.localDateToString(c.getServerStartDate(),"yyyy-MM-dd") + "-" +
                     DateUtils.localDateToString(c.getServerEndDate(),"yyyy-MM-dd") +
                     "-" + c.getDayMoney().doubleValue() + "-" + c.getOrderAmount().doubleValue());
-        }**/
+        }*/
         return sysOrderCommisionDayDetailList;
     }
 
diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
index d35cca2fc..079b847db 100644
--- a/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
+++ b/stdiet-custom/src/main/resources/mapper/custom/SysOrderMapper.xml
@@ -468,7 +468,7 @@
         <if test="reviewStatus != null and reviewStatus != ''">
             and review_status = #{reviewStatus}
         </if>
-        <if test="userId != null"><!-- 限于营养师、售后不可能同一个的情况下 -->
+        <if test="userId != null">
             and (
             su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId}
             <!-- 是否查询根据用户ID查询对应替换订单 -->
diff --git a/stdiet-ui/src/components/OrdercommissDetail/index.vue b/stdiet-ui/src/components/OrdercommissDetail/index.vue
index d015cf6d6..03b826d28 100644
--- a/stdiet-ui/src/components/OrdercommissDetail/index.vue
+++ b/stdiet-ui/src/components/OrdercommissDetail/index.vue
@@ -243,6 +243,7 @@ export default {
       totalSendCommission: 0,
       totalNotSendCommission: 0,
       serverDateScope: null,
+      fileName:""
     };
   },
   computed: {},
@@ -261,14 +262,18 @@ export default {
       this.queryParam.reviewStatus = this.data.reviewStatus;
       this.queryParam.endTime = this.data.endTime;
       this.title = `「${this.data.name}`;
+      this.fileName = this.data.name;
       if (this.data.yearMonth) {
         this.title += " 截止" + `${this.data.yearMonth}`;
+        this.fileName += "截止" + `${this.data.yearMonth}`;
       }
       if (this.queryParam.reviewStatus) {
         this.title +=
           this.queryParam.reviewStatus == "yes" ? " 已审核" : " 未审核";
+          this.fileName += this.queryParam.reviewStatus == "yes" ? "已审核" : "未审核";
       }
       this.title += " 订单提成列表」";
+      this.fileName += "订单提成列表";
       this.visible = true;
       this.fetchOrderList();
     },
@@ -343,7 +348,7 @@ export default {
           return exportOrderDetailDay(queryParams);
         })
         .then((response) => {
-          this.download(response.msg);
+          this.download(response.msg, this.fileName+".xls");
         })
         .catch(function () {});
     }