刘俊驰 1 year ago
parent
commit
02a973f679

+ 27 - 6
cooleshow-app/src/main/java/com/yonge/cooleshow/student/controller/TenantGroupAlbumController.java

@@ -21,9 +21,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collector;
 import java.util.stream.Collectors;
 
@@ -72,15 +70,38 @@ public class TenantGroupAlbumController {
         TenantGroupAlbumWrapper.BuyTenantAlbumQuery query = new TenantGroupAlbumWrapper.BuyTenantAlbumQuery();
         query.setUserId(user.getId());
         query.setClientType(ClientEnum.STUDENT);
+        query.setSortFlag(true);
         List<TenantGroupAlbumWrapper.BuyTenantAlbum> buyTenantAlbums = tenantGroupAlbumService.buyAlbumInfo(query);
         // 同ID去重
         if (CollectionUtils.isEmpty(buyTenantAlbums)) {
             return HttpResponseResult.succeed(new ArrayList<>());
         }
-        Collection<TenantGroupAlbumWrapper.BuyTenantAlbum> result = buyTenantAlbums.stream().collect(Collectors
-            .toMap(TenantGroupAlbumWrapper.BuyTenantAlbum::getId, o -> o, (o1, o2) -> o1)).values();
+        //buyTenantAlbums 根据 sort 排序,在根据expireTime 排序
+        // 根据status和sort两个字段对列表buyTenantAlbums进行排序
+        buyTenantAlbums.sort((o1, o2) -> {
+            // 首先根据status字段排序
+            int statusComparison = Boolean.compare(o2.getStatus(), o1.getStatus());
+            if (statusComparison != 0) {
+                return statusComparison;
+            }
 
-        return HttpResponseResult.succeed(result);
+            // 如果status字段相等,则根据sort字段倒序排序
+            return Long.compare(o2.getSort(),o1.getSort());
+        });
+
+        // 使用一个HashSet去重,根据ID字段对列表去重
+        Set<Long> idSet = new HashSet<>();
+        List<TenantGroupAlbumWrapper.BuyTenantAlbum> uniqueList = new ArrayList<>();
+
+        // 遍历列表,去重
+        for (TenantGroupAlbumWrapper.BuyTenantAlbum obj : buyTenantAlbums) {
+            if (idSet.add(obj.getId())) {
+                uniqueList.add(obj);
+            }
+        }
+
+
+        return HttpResponseResult.succeed(uniqueList);
     }
 
     @ApiOperation(value = "学生可购买机构专辑信息")

+ 4 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/mapper/UserTenantAlbumRecordMapper.java

@@ -5,8 +5,10 @@ import java.util.List;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.yonge.cooleshow.biz.dal.entity.TenantAlbum;
+import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
 import com.yonge.cooleshow.biz.dal.vo.VipCardRecordVo;
 import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumWrapper;
+import com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper;
 import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
 import com.yonge.cooleshow.biz.dal.entity.UserTenantAlbumRecord;
@@ -37,4 +39,6 @@ public interface UserTenantAlbumRecordMapper extends BaseMapper<UserTenantAlbumR
     List<Long> ifBuy(@Param("tenantAlbumId") List<Long> tenantAlbumId, @Param("studentId") Long studentId);
 
     List<UserTenantAlbumRecordWrapper.UserTenantAlbumRecord> selectTemporaryRecord();
+
+    List<TenantGroupAlbumWrapper.TenantAlbumSort> getTenantAlbumMaxCreateTime(@Param("userId") Long userId, @Param("clientType") ClientEnum clientType, @Param("tenantAlbumIds") List<Long> tenantAlbumIds);
 }

+ 19 - 4
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/TenantGroupAlbumServiceImpl.java

@@ -12,6 +12,7 @@ import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
 import com.yonge.cooleshow.biz.dal.enums.SubjectTypeEnum;
 import com.yonge.cooleshow.biz.dal.mapper.TenantAlbumMusicMapper;
 import com.yonge.cooleshow.biz.dal.mapper.TenantGroupAlbumMapper;
+import com.yonge.cooleshow.biz.dal.mapper.UserTenantAlbumRecordMapper;
 import com.yonge.cooleshow.biz.dal.service.*;
 import com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper;
 import com.yonge.toolset.base.exception.BizException;
@@ -22,10 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -58,6 +56,9 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
     private UserTenantAlbumRecordService userTenantAlbumRecordService;
 
     @Autowired
+    private UserTenantAlbumRecordMapper userTenantAlbumRecordMapper;
+
+    @Autowired
     private CoursewareFeignService coursewareFeignService;
 
 	/**
@@ -141,6 +142,9 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
         if (CollectionUtils.isEmpty(buyAlbumInfo)) {
             return buyAlbumInfo;
         }
+        if (query.getTenantGroupId() !=null || query.getTenantGroupAlbumId() != null) {
+            buyAlbumInfo.sort(Comparator.comparing(TenantGroupAlbumWrapper.BuyTenantAlbum::getTenantGroupAlbumId));
+        }
         // 查询专辑可用的分类的曲目数量
         // 机构专辑ID集合
         List<Long> tenantAlbumIds = buyAlbumInfo.stream().map(TenantGroupAlbumWrapper.BuyTenantAlbum::getId).collect(Collectors.toList());
@@ -193,6 +197,16 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
             if (CollectionUtils.isNotEmpty(albumRecordList)) {
                 albumRecordMap = albumRecordList.stream().collect(Collectors.toMap(UserTenantAlbumRecord::getTenantAlbumId, x -> x,(o1,o2)->o1));
             }
+            // 转map
+            Map<Long, Long> sortMap = new HashMap<>();
+            if (query.isSortFlag()) {
+
+                // 获取专辑的最新激活时间排序
+                List<TenantGroupAlbumWrapper.TenantAlbumSort> sorts = userTenantAlbumRecordMapper
+                    .getTenantAlbumMaxCreateTime(query.getUserId(),query.getClientType(),tenantAlbumIds);
+                sortMap = sorts.stream()
+                    .collect(Collectors.toMap(TenantGroupAlbumWrapper.TenantAlbumSort::getId, TenantGroupAlbumWrapper.TenantAlbumSort::getSort));
+            }
 
             for (TenantGroupAlbumWrapper.BuyTenantAlbum item : buyAlbumInfo) {
                 item.setBuyedTimes((int) studentTenantAlbumNum.stream().filter(x -> x.equals(item.getTenantGroupAlbumId())).count());
@@ -204,6 +218,7 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
                 if (userTenantAlbumRecord != null) {
                     item.setExpireTime(userTenantAlbumRecord.getEndTime());
                 }
+                item.setSort(sortMap.getOrDefault(item.getId(),0L));
             }
         }
 

+ 18 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/wrapper/TenantGroupAlbumWrapper.java

@@ -200,6 +200,8 @@ public class TenantGroupAlbumWrapper {
 
         @ApiModelProperty("客户端类型")
         private ClientEnum clientType;
+
+        private boolean sortFlag = false;
     }
 
 
@@ -283,6 +285,10 @@ public class TenantGroupAlbumWrapper {
         @ApiModelProperty("是否生效中")
         private Boolean status = false;
 
+
+        @ApiModelProperty("排序")
+        private long sort =0L;
+
         @ApiModelProperty("到期时间")
         private Date expireTime;
 
@@ -304,4 +310,16 @@ public class TenantGroupAlbumWrapper {
         private Integer count;
     }
 
+
+
+    @Data
+    @ApiModel(" TenantAlbumSort-专辑排序")
+    public static class TenantAlbumSort {
+
+        @ApiModelProperty("专辑ID")
+        private Long id;
+
+        @ApiModelProperty("排序")
+        private long sort =0L;
+    }
 }

+ 14 - 0
cooleshow-user/user-biz/src/main/resources/config/mybatis/UserTenantAlbumRecordMapper.xml

@@ -123,4 +123,18 @@
         </where>
         ORDER BY t.id_ DESC
     </select>
+
+    <select id="getTenantAlbumMaxCreateTime"
+            resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper$TenantAlbumSort">
+        select t.tenant_album_id_ as id,max(UNIX_TIMESTAMP(t.create_time_)) as sort
+        from user_tenant_album_record t
+        where t.user_id_ = #{userId}
+        and t.client_type_ = #{clientType}
+        and t.tenant_album_id_ in
+        <foreach collection="tenantAlbumIds" item="item" separator="," open="(" close=")">
+            #{item}
+        </foreach>
+        group by t.tenant_album_id_
+
+    </select>
 </mapper>