기록날짜 |
---|
2020.10.28 |
기록날짜 20-12-06 arraylist sorting
BlackCard 프로젝트에서. 경매 돈을 입력하면 돈에 따라서 rank가 달라지게 return이 돼야 한다.
database
List<Auction>
은 여러 record_id와 money를 갖고 있다. 이 값을 sorting해주어서 rank를 매겨준다.--> 여기서 힘들었었다. sorting을 할 때 먼저 이차원배열을 생각했었는데, List<Auction>
을 stream().forEach() 를 돌릴 때 외부의 int 값을 사용할 수 없다. 그래서 어떡하지 했었다. 그러다가 ArrayList를 sorting하는 방법을 검색해보다가 ArrayList 객체정렬 을 이용해서 정렬을 했다.
ArrayList 객체정렬
5. 정렬이 된 값의 id값은 정렬된 ArrayList의 최상단부터 index를 가져갈 것이다. 그래서 record_id 값에 따라 rank값을 넣어준다.
6. rank값이 모두 업데이트 됐으므로 Record 값을 return 해준다.
package com.project.blackcard.service;
import com.project.blackcard.dto.Header;
import com.project.blackcard.dto.request.RecordApiRequest;
import com.project.blackcard.dto.response.RecordApiResponse;
import com.project.blackcard.entity.Auction;
import com.project.blackcard.entity.Record;
import com.project.blackcard.entity.User;
import com.project.blackcard.entity.repository.AuctionRepository;
import com.project.blackcard.entity.repository.RecordRepository;
import com.project.blackcard.entity.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Service
public class RecordService {
@Autowired
private RecordRepository recordRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private AuctionRepository auctionRepository;
public Header<List<RecordApiResponse>> batting(Header<RecordApiRequest> request){
RecordApiRequest recordApiRequest = request.getData();
Optional<User> findUser = userRepository.findById(recordApiRequest.getUserId());
User user = findUser.get();
Optional<Auction> findAuction = auctionRepository.findById(recordApiRequest.getAuctionId());
Auction auction = findAuction.get();
Optional<Record> addRecord = recordRepository.findByAuctionAndUser(auction,user);
if(addRecord.isPresent()){
Record forUsingMoneyRecord = addRecord.get();
int BeforeMoney = forUsingMoneyRecord.getMoney();
User updateUser = User.builder()
.recordList(user.getRecordList())
.artist(user.getArtist())
.email(user.getEmail())
.name(user.getName())
.nickname(user.getNickname())
.id(user.getId())
.password(user.getPassword())
.paymentList(user.getPaymentList())
.profileImg(user.getProfileImg())
.money(user.getMoney()-recordApiRequest.getMoney()+BeforeMoney)
.build();
userRepository.save(updateUser);
}else if(!addRecord.isPresent()){
User updateUser = User.builder()
.recordList(user.getRecordList())
.artist(user.getArtist())
.email(user.getEmail())
.name(user.getName())
.nickname(user.getNickname())
.id(user.getId())
.password(user.getPassword())
.paymentList(user.getPaymentList())
.profileImg(user.getProfileImg())
.money(user.getMoney()-recordApiRequest.getMoney())
.build();
userRepository.save(updateUser);
}
if(addRecord.isPresent()){
Record record = addRecord.get();
Record newRecord = Record.builder()
.money(recordApiRequest.getMoney())
.auction(auction)
.user(user)
.id(record.getId())
.build();
recordRepository.save(newRecord);
}else if(!addRecord.isPresent()){
Record newRecord = Record.builder()
.auction(auction)
.user(user)
.money(recordApiRequest.getMoney())
.build();
recordRepository.save(newRecord);
}
Optional<List<Record>> findRecordList = recordRepository.findByAuction(auction);
ArrayList<RankCalculator> rankCalculators = new ArrayList<>();
List<Record> RecordList = findRecordList.get();
RecordList.stream().forEach(item->{
RankCalculator rankCalculator = new RankCalculator(item.getId(),item.getMoney());
// record id, record money 저장
rankCalculators.add(rankCalculator);
});
Collections.sort(rankCalculators);
for(int i=0;i<rankCalculators.size();i++){
Optional<Record> findRecord = recordRepository.findById(rankCalculators.get(i).id);
Record rankRecord = findRecord.get();
Record newRankRecord = Record.builder()
.auction(rankRecord.getAuction())
.rank(i+1)
.id(rankRecord.getId())
.money(rankRecord.getMoney())
.user(rankRecord.getUser())
.build();
recordRepository.save(newRankRecord);
}
List<Record> returnRecordList = recordRepository.findAll();
List<RecordApiResponse> recordApiResponseList = new ArrayList<>();
returnRecordList.stream().forEach(item->{
RecordApiResponse recordApiResponse = RecordApiResponse.builder()
.recordId(item.getId())
.auctionId(item.getAuction().getId())
.money(item.getMoney())
.rank(item.getRank())
.userId(item.getUser().getId())
.build();
recordApiResponseList.add(recordApiResponse);
});
return Header.OK(recordApiResponseList);
}
}
class RankCalculator implements Comparable<RankCalculator> {
Long id;
Integer money;
public RankCalculator(Long id,Integer money){
this.id = id;
this.money = money;
}
public Long getId() {
return id;
}
public Integer getMoney() {
return money;
}
@Override
public int compareTo(RankCalculator s) {
if (this.money < s.getMoney()) {
return 1;
} else if (this.money > s.getMoney()) {
return -1;
}
return 0;
}
}