Authored by LUOXC

添加结算明细API

... ... @@ -2,6 +2,7 @@ package com.yohoufo.dal.order;
import com.yohoufo.dal.order.model.InviteSettlementItem;
import com.yohoufo.dal.order.model.InviteSettlementItemStats;
import org.apache.ibatis.annotations.Param;
import java.util.List;
... ... @@ -32,4 +33,17 @@ public interface InviteSettlementItemMapper {
@Param("type") Integer type,
@Param("inviteSettlementId") Integer inviteSettlementId);
InviteSettlementItemStats selectStats(@Param("uid") Integer uid,
@Param("type") Integer type,
@Param("startTime") Integer startTime,
@Param("endTime") Integer endTime);
List<InviteSettlementItem> select(@Param("uid") Integer uid,
@Param("type") Integer type,
@Param("startTime") Integer startTime,
@Param("endTime") Integer endTime,
@Param("start") Integer start,
@Param("limit") Integer limit);
}
\ No newline at end of file
... ...
... ... @@ -17,4 +17,6 @@ public interface InviteSettlementMapper {
@Param("uid") Integer uid, @Param("status") Integer status,
@Param("start") Integer start, @Param("limit") Integer limit);
List<InviteSettlement> selectByUidAndIds(@Param("uid") Integer uid, @Param("ids") List<Integer> ids);
}
\ No newline at end of file
... ...
... ... @@ -38,6 +38,8 @@ public class InviteSettlementItem {
private Integer type;
private BigDecimal settleAmount;
private Integer inviteSettlementId;
private Integer status;
... ...
package com.yohoufo.dal.order.model;
import lombok.Data;
import java.math.BigDecimal;
/**
* @author LUOXC
* @date 2019/4/11 9:41
*/
@Data
public class InviteSettlementItemStats {
Integer totalElements;
BigDecimal totalOrderAmount;
BigDecimal totalSettleAmount;
}
... ...
... ... @@ -10,6 +10,7 @@
<result column="order_amount" property="orderAmount" jdbcType="DECIMAL" />
<result column="order_create_time" property="orderCreateTime" jdbcType="INTEGER" />
<result column="type" property="type" jdbcType="INTEGER" />
<result column="settle_amount" property="settleAmount" jdbcType="DECIMAL" />
<result column="invite_settlement_id" property="inviteSettlementId" jdbcType="INTEGER" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="status_desc" property="statusDesc" jdbcType="VARCHAR" />
... ... @@ -17,17 +18,35 @@
<result column="order_num" property="orderNum" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, uid, buyer_uid, buyer_order_code, seller_uid, order_amount,order_create_time,type, invite_settlement_id,
id, uid, buyer_uid, buyer_order_code, seller_uid, order_amount,order_create_time,type,settle_amount, invite_settlement_id,
status, status_desc, create_time
</sql>
<insert id="insert" parameterType="com.yohoufo.dal.order.model.InviteSettlementItem" >
insert into invite_settlement_item (id, uid, buyer_uid,
buyer_order_code, seller_uid, order_amount,order_create_time,type,
invite_settlement_id, status, status_desc,
insert into invite_settlement_item (id,
uid,
buyer_uid,
buyer_order_code,
seller_uid,
order_amount,
order_create_time,
type,
settle_amount,
invite_settlement_id,
status,
status_desc,
create_time)
values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{buyerUid,jdbcType=INTEGER},
#{buyerOrderCode,jdbcType=BIGINT}, #{sellerUid,jdbcType=INTEGER}, #{orderAmount,jdbcType=DECIMAL}, #{orderCreateTime,jdbcType=INTEGER}, #{type,jdbcType=INTEGER},
#{inviteSettlementId,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, #{statusDesc,jdbcType=VARCHAR},
values (#{id,jdbcType=INTEGER},
#{uid,jdbcType=INTEGER},
#{buyerUid,jdbcType=INTEGER},
#{buyerOrderCode,jdbcType=BIGINT},
#{sellerUid,jdbcType=INTEGER},
#{orderAmount,jdbcType=DECIMAL},
#{orderCreateTime,jdbcType=INTEGER},
#{type,jdbcType=INTEGER},
#{settleAmount,jdbcType=DECIMAL},
#{inviteSettlementId,jdbcType=INTEGER},
#{status,jdbcType=INTEGER},
#{statusDesc,jdbcType=VARCHAR},
#{createTime,jdbcType=INTEGER})
</insert>
... ... @@ -74,4 +93,28 @@
from invite_settlement_item
where uid = #{uid,jdbcType=INTEGER} and type = #{type,jdbcType=INTEGER} and invite_settlement_id = #{inviteSettlementId,jdbcType=INTEGER}
</select>
<select id="selectStats" resultType="com.yohoufo.dal.order.model.InviteSettlementItemStats" >
select
count(1) as 'totalElements',
sum(order_amount) as 'totalOrderAmount'
from invite_settlement_item
where uid = #{uid,jdbcType=INTEGER}
and type = #{type,jdbcType=INTEGER}
and status in (1,3)
and order_create_time between #{startTime,jdbcType=INTEGER} and #{endTime,jdbcType=INTEGER}
</select>
<select id="select" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from invite_settlement_item
where uid = #{uid,jdbcType=INTEGER}
and type = #{type,jdbcType=INTEGER}
and status in (1,3)
and order_create_time between #{startTime,jdbcType=INTEGER} and #{endTime,jdbcType=INTEGER}
order by order_create_time desc
limit #{start,jdbcType=INTEGER},#{limit,jdbcType=INTEGER}
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -49,4 +49,15 @@
limit #{start,jdbcType=INTEGER}, #{limit,jdbcType=INTEGER}
</select>
<select id="selectByUidAndIds" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from invite_settlement
where uid = #{uid,jdbcType=INTEGER}
and id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
</mapper>
\ No newline at end of file
... ...
package com.yohoufo.order.controller;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.order.model.InviteSettlementItemListVO;
import com.yohoufo.order.model.InviteSettlementListVO;
import com.yohoufo.order.service.IInviteSettlementService;
import lombok.extern.slf4j.Slf4j;
... ... @@ -18,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
public class InviteSettlementController {
@Autowired
private IInviteSettlementService inviterService;
private IInviteSettlementService inviteSettlementService;
/**
* 我的收款记录
... ... @@ -29,13 +30,29 @@ public class InviteSettlementController {
@RequestMapping(params = "method=ufo.invite.getInviteSettlementList")
public ApiResponse getInviteSettlementList(@RequestParam(value = "uid") int uid) {
log.info("get inviter settlement list, uid is {}", uid);
InviteSettlementListVO inviteSettlementListVO = inviterService.getInviteSettlementList(uid);
InviteSettlementListVO inviteSettlementListVO = inviteSettlementService.getInviteSettlementList(uid);
return new ApiResponse.ApiResponseBuilder().code(200).data(inviteSettlementListVO).message("ok").build();
}
/**
* 我的收款记录明细
*
* @param uid
* @return
*/
@RequestMapping(params = "method=ufo.invite.getInviteSettlementItemList")
public ApiResponse getInviteSettlementItemList(@RequestParam(value = "uid") Integer uid,
@RequestParam(value = "month") String month,
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
log.info("get inviter settlement item list, uid is {}", uid);
InviteSettlementItemListVO resp = inviteSettlementService.getInviteSettlementItemList(uid, month, page, limit);
return new ApiResponse.ApiResponseBuilder().code(200).data(resp).message("ok").build();
}
@RequestMapping(value = "/erp/invite/settlement/help/settle")
public ApiResponse settle() {
inviterService.settle();
inviteSettlementService.settle();
return new ApiResponse.ApiResponseBuilder().code(200).message("ok").build();
}
... ...
package com.yohoufo.order.model;
import lombok.Data;
import lombok.experimental.Builder;
import java.util.List;
/**
* @author LUOXC
* @date 2019/4/2 15:00
*/
@Builder
@Data
public class InviteSettlementItemListVO {
int page;
int pageTotal;
int pageSize;
int totalElements;
String totalOrderAmount;
String totalSettleAmount;
List<InviteSettlementItemVO> list;
@Data
public static class InviteSettlementItemVO {
String buyerOrderCode;
String sellerName;
String orderAmount;
String orderCreateTime;
String settleAmount;
String statusDesc;
}
}
... ...
package com.yohoufo.order.service;
import com.yohoufo.order.model.InviteSettlementItemListVO;
import com.yohoufo.order.model.InviteSettlementListVO;
public interface IInviteSettlementService {
... ... @@ -12,6 +13,8 @@ public interface IInviteSettlementService {
*/
InviteSettlementListVO getInviteSettlementList(Integer uid);
InviteSettlementItemListVO getInviteSettlementItemList(Integer uid, String month, Integer page, Integer limit);
/**
* 结算
*/
... ...
package com.yohoufo.order.service.impl;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.InviteSettlementItemMapper;
import com.yohoufo.dal.order.InviteSettlementMapper;
import com.yohoufo.dal.order.InviterMapper;
import com.yohoufo.dal.order.model.InviteSettlement;
import com.yohoufo.dal.order.model.InviteSettlementItem;
import com.yohoufo.dal.order.model.InviteSettlementItemStats;
import com.yohoufo.dal.order.model.Inviter;
import com.yohoufo.order.model.InviteSettlementItemListVO;
import com.yohoufo.order.model.InviteSettlementListVO;
import com.yohoufo.order.service.IInviteSettlementService;
import com.yohoufo.order.utils.IdUtils;
import com.yohoufo.order.utils.InviteSettlementUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.RoundingMode;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
/**
* @author LUOXC
* @date 2019/4/2 15:27
... ... @@ -37,6 +51,9 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService {
private InviteSettlementMapper inviteSettlementMapper;
@Autowired
private InviteSettlementItemMapper inviteSettlementItemMapper;
@Autowired
private InviteSettlementItemCreator inviteSettlementItemCreator;
@Override
... ... @@ -46,12 +63,12 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService {
List<InviteSettlement> inviteSettlementList = inviteSettlementMapper.selectByUidAndStatusWithLimit(uid, InviteSettlement.STATUS_PAID, 0, 100);
// build vo
ZoneOffset zoneOffset = ZoneOffset.of("+8");
List<InviteSettlementListVO.InviteSettlementVO> items = inviteSettlementList.stream()
.map(inviteSettlement -> {
InviteSettlementListVO.InviteSettlementVO item = new InviteSettlementListVO.InviteSettlementVO();
item.setSettleCode(String.format("%010d", inviteSettlement.getSettleCode()));
item.setSettleAmount("+" + inviteSettlement.getSettleAmount().setScale(1, RoundingMode.FLOOR));
item.setSettleAmount(InviteSettlementUtils.format("+%s", inviteSettlement.getSettleAmount()));
item.setSettleTime(DateUtil.formatYYMMddHHmmssPoint(inviteSettlement.getSettleTime()));
item.setPaidTime(DateUtil.formatYYMMddHHmmssPoint(inviteSettlement.getPaidTime()));
return item;
... ... @@ -63,6 +80,73 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService {
}
@Override
public InviteSettlementItemListVO getInviteSettlementItemList(Integer uid, String month, Integer page, Integer limit) {
Pair<Integer, Integer> time = getTimeRangeOfMonth(month);
// 只查询结算规则1的记录
int settlementType = 1;
InviteSettlementItemStats stats = inviteSettlementItemMapper.selectStats(uid, settlementType, time.getLeft(), time.getRight());
int totalElements = Objects.isNull(stats.getTotalElements()) ? 0 : stats.getTotalElements();
BigDecimal totalOrderAmount = Objects.isNull(stats.getTotalOrderAmount()) ? BigDecimal.ZERO : stats.getTotalOrderAmount();
BigDecimal totalSettleAmount = Objects.isNull(stats.getTotalSettleAmount()) ? BigDecimal.ZERO : stats.getTotalSettleAmount();
InviteSettlementItemListVO.InviteSettlementItemListVOBuilder builder = InviteSettlementItemListVO.builder();
builder.page(page)
.pageSize(limit)
.pageTotal((totalElements % limit == 0) ? (totalElements / limit) : (totalElements / limit + 1))
.totalElements(totalElements)
.totalOrderAmount(InviteSettlementUtils.format(totalOrderAmount))
.totalSettleAmount(InviteSettlementUtils.format(totalSettleAmount));
if (totalElements == 0) {
return builder.list(Lists.newArrayList()).build();
}
int start = (page - 1) * limit;
List<InviteSettlementItem> list = inviteSettlementItemMapper.select(uid, settlementType, time.getLeft(), time.getRight(), start, limit);
Map<Integer, Integer> settlementStatusMap = buildInviteSettlementIdStatusMap(uid, list);
return builder
.list(list.stream().map(item -> {
Integer settlementStatus = settlementStatusMap.getOrDefault(item.getInviteSettlementId(), InviteSettlement.STATUS_WAIT_SETTLE);
InviteSettlementItemListVO.InviteSettlementItemVO vo = new InviteSettlementItemListVO.InviteSettlementItemVO();
vo.setBuyerOrderCode(item.getBuyerOrderCode().toString());
vo.setSellerName(item.getSellerUid().toString());
vo.setOrderAmount(InviteSettlementUtils.format(item.getOrderAmount()));
vo.setOrderCreateTime(DateUtil.formatYYMMddHHmmssPoint(item.getOrderCreateTime()));
vo.setSettleAmount(InviteSettlementUtils.format(item.getSettleAmount()));
vo.setStatusDesc(settlementStatus.equals(InviteSettlement.STATUS_PAID) ? "已付款" : "待付款");
return vo;
}).collect(Collectors.toList()))
.build();
}
private Map<Integer, Integer> buildInviteSettlementIdStatusMap(Integer uid, List<InviteSettlementItem> list) {
val inviteSettlementIds = list.stream()
.map(InviteSettlementItem::getInviteSettlementId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(inviteSettlementIds)) {
return Collections.emptyMap();
} else {
return inviteSettlementMapper.selectByUidAndIds(uid, inviteSettlementIds).stream()
.collect(Collectors.toMap(InviteSettlement::getId, InviteSettlement::getStatus));
}
}
@VisibleForTesting
Pair<Integer, Integer> getTimeRangeOfMonth(String month) {
LocalDateTime requestMonth = null;
try {
requestMonth = LocalDateTime.parse(month, DateTimeFormatter.ofPattern("yyyyMM"));
} catch (Exception e) {
throwServiceException("日期格式不正确");
}
ZoneOffset zoneOffset = ZoneOffset.of("+8");
int startTime = (int) LocalDateTime.of(requestMonth.getYear(), requestMonth.getMonthValue(), 1, 0, 0, 0)
.toEpochSecond(zoneOffset);
int endTime = (int) LocalDateTime.of(requestMonth.getYear(), requestMonth.getMonthValue() + 1, 1, 0, 0, 0)
.toEpochSecond(zoneOffset) - 1;
return Pair.of(startTime, endTime);
}
@Override
public void settle() {
inviterMapper.selectAll().stream()
.map(Inviter::getUid)
... ...
package com.yohoufo.order.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
/**
* @author LUOXC
* @date 2019/4/11 11:04
*/
public class InviteSettlementUtils {
public static String format(BigDecimal src) {
return format("%s", src);
}
public static String format(String format, BigDecimal src) {
if (Objects.isNull(src)) {
return null;
} else {
return String.format(format, src.setScale(1, RoundingMode.FLOOR));
}
}
}
... ...
package com.yohoufo.order.service.impl;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
public class InviteSettlementServiceImplTest {
@Test
public void getTimeRangeOfMonth() {
Pair<Integer, Integer> integerIntegerPair = new InviteSettlementServiceImpl().getTimeRangeOfMonth("201903");
Assert.assertEquals(integerIntegerPair.getLeft().intValue(),1551369600);
Assert.assertEquals(integerIntegerPair.getRight().intValue(),1554047999);
}
}
\ No newline at end of file
... ...