Skip to content

Commit

Permalink
Merge pull request #27 from hyundai-fruitfruit/HEENDY-93--admin-coupo…
Browse files Browse the repository at this point in the history
…n-statistics

[HEENDY-93--admin-coupon-statistics] Admin 쿠폰통계 API
  • Loading branch information
Choi-Seong-Hyeok authored Mar 5, 2024
2 parents 32b7afd + d21af5d commit 7dac915
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hyundai.app.common.AdventureOfHeendyResponse;
import com.hyundai.app.coupon.domain.Coupon;
import com.hyundai.app.coupon.dto.CouponUsageRateDto;
import com.hyundai.app.coupon.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand Down Expand Up @@ -32,4 +33,10 @@ public AdventureOfHeendyResponse<List<Coupon>> findCouponList() {
int storeId = 1;
return AdventureOfHeendyResponse.success("쿠폰 목록을 가져왔습니다.", couponService.findCouponList(storeId));
}

@ApiOperation("쿠폰 사용률 통계 조회 API")
@GetMapping("/statistics")
public AdventureOfHeendyResponse<List<CouponUsageRateDto>> findCouponUsageRates() {
return AdventureOfHeendyResponse.success("쿠폰 사용률 통계를 가져왔습니다.", couponService.findCouponUsageRates());
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/hyundai/app/coupon/dto/CouponUsageRateDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.hyundai.app.coupon.dto;

import com.hyundai.app.coupon.enumType.CouponType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* @author 최성혁
* @since 2024/03/04
* 쿠폰사용비율 DTO
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CouponUsageRateDto {
private CouponType couponType;
private int totalIssued;
private int totalUsed;
private double usageRate;

public void setUsageRate(double usageRate) {
BigDecimal bd = new BigDecimal(usageRate).setScale(2, RoundingMode.HALF_UP);
this.usageRate = bd.doubleValue();
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/hyundai/app/coupon/mapper/CouponMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hyundai.app.coupon.mapper;

import com.hyundai.app.coupon.domain.Coupon;
import com.hyundai.app.coupon.dto.CouponUsageRateDto;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
Expand All @@ -17,4 +18,6 @@ public interface CouponMapper {
Coupon findById(int couponId);

List<Coupon> findMemberCouponList(String memberId);

List<CouponUsageRateDto> findCouponUsageRates();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.hyundai.app.coupon.mapper;

import com.hyundai.app.coupon.domain.MemberCoupon;
import com.hyundai.app.coupon.dto.CouponUsageRateDto;

import java.util.List;

/**
* @author 엄상은
Expand All @@ -9,6 +12,6 @@
*/
public interface MemberCouponMapper {
void saveMemberCoupon(MemberCoupon memberCoupon);

void useCoupon(MemberCoupon memberCoupon);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hyundai.app.coupon.domain.Coupon;
import com.hyundai.app.coupon.domain.MemberCoupon;
import com.hyundai.app.coupon.dto.CouponUsageRateDto;
import com.hyundai.app.coupon.enumType.CouponType;
import com.hyundai.app.coupon.mapper.CouponMapper;
import com.hyundai.app.coupon.mapper.MemberCouponMapper;
Expand Down Expand Up @@ -42,9 +43,15 @@ public List<Coupon> findMemberCouponList(String memberId) {
return couponList;
}

public List<CouponUsageRateDto> findCouponUsageRates() {
return couponMapper.findCouponUsageRates();
}


public Void useCoupon(String memberId, int couponId) {
MemberCoupon memberCoupon = MemberCoupon.of(memberId, couponId);
memberCouponMapper.useCoupon(memberCoupon);
return null;
}

}
15 changes: 15 additions & 0 deletions src/main/resources/mapper/CouponMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@
WHERE member_id = #{memberId}
AND is_used = 0)
</select>

<select id="findCouponUsageRates" resultType="com.hyundai.app.coupon.dto.CouponUsageRateDto">
SELECT
C.COUPON_TYPE as "couponType",
COUNT(C.ID) as "totalIssued",
SUM(CASE WHEN MC.IS_USED = 1 THEN 1 ELSE 0 END) as "totalUsed",
(SUM(CASE WHEN MC.IS_USED = 1 THEN 1 ELSE 0 END) * 1.0 / COUNT(C.ID)) * 100 as "usageRate"
FROM
COUPON C
LEFT JOIN
MEMBER_COUPON MC ON C.ID = MC.COUPON_ID
GROUP BY
C.COUPON_TYPE
</select>

</mapper>

0 comments on commit 7dac915

Please sign in to comment.