package com.yeshi.fanli.service.impl.user; import java.math.BigDecimal; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.yeshi.utils.DateUtil; import com.yeshi.fanli.dao.mybatis.share.UserShareGoodsGroupMapper; import com.yeshi.fanli.entity.bus.share.UserShareGoodsGroup; import com.yeshi.fanli.entity.bus.share.UserShareGoodsRecord.ShareSourceTypeEnum; import com.yeshi.fanli.entity.bus.user.HongBao; import com.yeshi.fanli.exception.share.UserShareGoodsRecordException; import com.yeshi.fanli.service.inter.goods.CommonGoodsService; import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService; import com.yeshi.fanli.service.inter.user.UserShareGoodsGroupService; import com.yeshi.fanli.util.MoneyBigDecimalUtil; @Service public class UserShareGoodsGroupServiceImpl implements UserShareGoodsGroupService { @Resource private HongBaoManageService hongBaoManageService; @Resource private CommonGoodsService commonGoodsService; @Resource private UserShareGoodsGroupMapper userShareGoodsGroupMapper; @Override public int insert(UserShareGoodsGroup record) { return userShareGoodsGroupMapper.insert(record); } @Override public int insertSelective(UserShareGoodsGroup record) { return userShareGoodsGroupMapper.insertSelective(record); } @Override public UserShareGoodsGroup selectByPrimaryKey(Long id) { return userShareGoodsGroupMapper.selectByPrimaryKey(id); } @Override public int updateByPrimaryKeySelective(UserShareGoodsGroup record) { return userShareGoodsGroupMapper.updateByPrimaryKeySelective(record); } @Override public int updateByPrimaryKey(UserShareGoodsGroup record) { return userShareGoodsGroupMapper.updateByPrimaryKey(record); } @Override public List listByRecordId(Long recordId) { return userShareGoodsGroupMapper.listByRecordId(recordId); } @Override public UserShareGoodsGroup getSingleGoods(Long cid, Long uid) { return userShareGoodsGroupMapper.getSingleGoods(ShareSourceTypeEnum.goodsDetail,cid, uid ); } @Override public int insertBatch(List list) { return userShareGoodsGroupMapper.insertBatch(list); } @Override public UserShareGoodsGroup getNewestRecord(Long uid, Long auctionId) { return userShareGoodsGroupMapper.getNewestRecord(uid, auctionId); } /** * 更新订单记录数据 * @param uid * @param auctionId */ @Override public void updateOrderRecord (HongBao hongBao) throws UserShareGoodsRecordException{ if (hongBao == null) { throw new UserShareGoodsRecordException(1, "hongBao不能为空"); } if (hongBao.getUserInfo() == null) { throw new UserShareGoodsRecordException(1, "用户信息不能为空"); } Long uid = hongBao.getUserInfo().getId(); if (uid == null) { throw new UserShareGoodsRecordException(1, "用户ID不能为空"); } if (hongBao.getAuctionId() == null) { throw new UserShareGoodsRecordException(1, "商品ID不能为空"); } UserShareGoodsGroup newestRecord = getNewestRecord(uid, hongBao.getAuctionId()); if (newestRecord != null) { // 更新订单数量 UserShareGoodsGroup shareGoodsGroup = new UserShareGoodsGroup(newestRecord.getId()); Integer totalOrder = newestRecord.getTotalOrder(); if (totalOrder == null) { totalOrder = 0; } shareGoodsGroup.setTotalOrder(totalOrder + 1); // 更新预计收益 BigDecimal totalMoney = newestRecord.getTotalMoney(); if (totalMoney == null) { totalMoney = new BigDecimal(0); } BigDecimal resultMoney = MoneyBigDecimalUtil.add(totalMoney, hongBao.getMoney()); shareGoodsGroup.setTotalMoney(resultMoney); updateByPrimaryKeySelective(shareGoodsGroup); } } /** * 更新订单记录数据 * @param uid * @param auctionId */ @Override public void updateBrowseRecord (Long uid, Long auctionId, int count) throws UserShareGoodsRecordException{ if (uid == null) { throw new UserShareGoodsRecordException(1, "用户ID不能为空"); } if (auctionId == null) { throw new UserShareGoodsRecordException(1, "商品Id不能为空"); } UserShareGoodsGroup group = getNewestRecord(uid, auctionId); if (group != null) { UserShareGoodsGroup shareGoodsGroup = new UserShareGoodsGroup(group.getId()); // 累计浏览 Integer totalBrowse = group.getTotalBrowse(); if (totalBrowse == null) { totalBrowse = 0; } shareGoodsGroup.setTotalBrowse(totalBrowse + count); // 今日浏览 Date date = new Date(); Date browseTime = group.getBrowseTime(); if (DateUtil.isSameDay(browseTime, date)) { shareGoodsGroup.setTodayBrowse(group.getTodayBrowse() + count); } else { shareGoodsGroup.setTodayBrowse(count); } shareGoodsGroup.setBrowseTime(date); userShareGoodsGroupMapper.updateByPrimaryKeySelective(shareGoodsGroup); } } }