package com.yeshi.fanli.service.impl.hongbao;
|
|
import java.io.Serializable;
|
import java.math.BigDecimal;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.fanli.dao.mybatis.ThreeSaleGiftMapper;
|
import com.yeshi.fanli.dao.user.ThreeSaleGiftDao;
|
import com.yeshi.fanli.dto.HongBao;
|
import com.yeshi.fanli.entity.bus.user.OrderItem;
|
import com.yeshi.fanli.entity.bus.user.ThreeSaleGift;
|
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleGiftService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
|
@Service
|
public class ThreeSaleGiftServiceImpl implements ThreeSaleGiftService {
|
|
@Resource
|
private ThreeSaleGiftDao dao;
|
|
@Resource
|
private ThreeSaleGiftMapper threeSaleGiftMapper;
|
|
@Override
|
public void save(ThreeSaleGift threeSaleGift) {
|
dao.save(threeSaleGift);
|
}
|
|
@Override
|
public void update(OrderItem orderItem) {
|
|
List<ThreeSaleGift> list = dao.list("from ThreeSaleGift tsg where tsg.orderItem.id = ?",
|
new Serializable[] { orderItem.getId() });
|
Integer state = orderItem.getState();
|
BigDecimal fanMoney = orderItem.getFanMoney();
|
list.parallelStream().forEach(tsg -> {
|
tsg.setState(state);
|
tsg.setMoney(MoneyBigDecimalUtil.mul(tsg.getRate(), fanMoney));
|
dao.update(tsg);
|
});
|
|
}
|
|
@Override
|
public List<ThreeSaleGift> findThreeSaleGiftList(long orderItemId) {
|
return threeSaleGiftMapper.selectByOrderItem(orderItemId);
|
}
|
|
@Override
|
public void update(ThreeSaleGift threeSaleGift) {
|
dao.update(threeSaleGift);
|
}
|
|
@Override
|
public ThreeSaleGift conver(HongBao oldHongBao, OrderItem parent) {
|
|
int threeSaleGiftState = getThreeSaleGiftState(oldHongBao.getState());
|
|
String hql = " from ThreeSaleGift tsg where tsg.orderItem.id = ? and tsg.money=? ";
|
if (threeSaleGiftState == 0) {
|
hql += " and (tsg.state = 1 or tsg.state=2) ";
|
} else {
|
hql += " and tsg.state=" + threeSaleGiftState;
|
}
|
List<ThreeSaleGift> list = dao.list(hql, 0, 1, new Serializable[] { parent.getId(), oldHongBao.getMoney() });
|
if (list.size() > 0) {
|
return list.get(0);
|
}
|
|
return null;
|
}
|
|
private int getThreeSaleGiftState(int hbState) {
|
int state = 0;
|
if (hbState == Constant.HB_GET) {
|
state = 2;
|
} else if (hbState == Constant.HB_GOT) {
|
state = 3;
|
} else if (hbState == Constant.HB_DISABLE) {
|
state = -1;
|
}
|
return state;
|
}
|
|
@Override
|
@Transactional
|
public void updateByOrderItem(OrderItem orderItem) {
|
List<ThreeSaleGift> list = threeSaleGiftMapper.selectByOrderItem(orderItem.getId());
|
Integer state = orderItem.getState();
|
BigDecimal fanMoney = orderItem.getFanMoney();
|
list.parallelStream().forEach(tsg -> {
|
ThreeSaleGift updateTsg = new ThreeSaleGift();
|
updateTsg.setId(tsg.getId());
|
updateTsg.setState(state);
|
updateTsg.setMoney(MoneyBigDecimalUtil.mul(tsg.getRate(), fanMoney));
|
threeSaleGiftMapper.updateByPrimaryKeySelective(updateTsg);
|
});
|
}
|
|
}
|