| package com.matrix.system.hive.service.imp; | 
|   | 
| import com.matrix.core.constance.MatrixConstance; | 
| import com.matrix.core.exception.GlobleException; | 
| import com.matrix.core.pojo.PaginationVO; | 
| import com.matrix.core.tools.WebUtil; | 
| import com.matrix.system.common.bean.SysUsers; | 
| import com.matrix.system.constance.Dictionary; | 
| import com.matrix.system.hive.bean.*; | 
| import com.matrix.system.hive.dao.*; | 
| import com.matrix.core.tools.DateUtil; | 
| import com.matrix.system.hive.plugin.util.MoneyUtil; | 
| import com.matrix.system.hive.service.MoneyCardUseService; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.text.DecimalFormat; | 
| import java.util.Date; | 
| import java.util.List; | 
|   | 
|   | 
| /** | 
|  * @date 2016-09-17 10:17 | 
|  */ | 
| @Service("moneyCardUseService") | 
| public class MoneyCardUseServiceImpl implements MoneyCardUseService { | 
|   | 
|   | 
|     @Autowired | 
|     private MoneyCardUseDao moneyCardUseDao; | 
|   | 
|     @Autowired | 
|     private MoneyCardUseFreezeDao moneyCardUseFreezeDao; | 
|   | 
|   | 
|     @Autowired | 
|     private SysVipInfoDao infoDao; | 
|     @Autowired | 
|     private SysOrderItemDao orderItemDao; | 
|   | 
|     @Autowired | 
|     private SysVipLevelDao viplevelDao; | 
|   | 
|     @Override | 
|     public int add(MoneyCardUse moneyCardUse) { | 
|   | 
|         return moneyCardUseDao.insert(moneyCardUse); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public int modify(MoneyCardUse moneyCardUse) { | 
|   | 
|         return moneyCardUseDao.update(moneyCardUse); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public int remove(List<Long> list) { | 
|   | 
|         return moneyCardUseDao.deleteByIds(list); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public int removeById(Long id) { | 
|   | 
|         return moneyCardUseDao.deleteById(id); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public List<MoneyCardUse> findInPage(MoneyCardUse moneyCardUse, PaginationVO pageVo) { | 
|   | 
|         return moneyCardUseDao.selectInPage(moneyCardUse, pageVo); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public List<MoneyCardUse> findByModel(MoneyCardUse moneyCardUse) { | 
|   | 
|         return moneyCardUseDao.selectByModel(moneyCardUse); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public int findTotal(MoneyCardUse moneyCardUse) { | 
|   | 
|         return moneyCardUseDao.selectTotalRecord(moneyCardUse); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public MoneyCardUse findById(Long id) { | 
|         return moneyCardUseDao.selectById(id); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public MoneyCardUse findByVipId(Long id) { | 
|         return moneyCardUseDao.selectVipCard(id); | 
|     } | 
|   | 
|     @Override | 
|     public List<MoneyCardUse> findVipCardUseInPage(MoneyCardUse moneyCardUse, PaginationVO pageVo) { | 
|         return moneyCardUseDao.selectVipCardUseInPage(moneyCardUse, pageVo); | 
|     } | 
|   | 
|     @Override | 
|     public List<MoneyCardUse> findVipCardUse(MoneyCardUse moneyCardUse) { | 
|         return moneyCardUseDao.selectVipCardUse(moneyCardUse); | 
|     } | 
|   | 
|     @Override | 
|     public Integer findTotalVipCardUse(MoneyCardUse moneyCardUse) { | 
|         return moneyCardUseDao.selectVipCardUseTotal(moneyCardUse); | 
|     } | 
|   | 
|     @Override | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public int freeze(MoneyCardUse moneyCardUse) { | 
|         // 态为有效的才能冻结 | 
|         MoneyCardUse moneyCard = moneyCardUseDao.selectById(moneyCardUse.getId()); | 
|         if (!moneyCard.getStatus().equals(Dictionary.TAOCAN_STATUS_YX)) { | 
|             throw new GlobleException("不是有效充值卡"); | 
|         } | 
|   | 
|         // 将充值卡的状态改为冻结 | 
|         moneyCard.setStatus(Dictionary.TAOCAN_STATUS_DJ); | 
|         moneyCardUseDao.update(moneyCard); | 
|         // 冻结记录 | 
|         MoneyCardUseFreeze freeze = new MoneyCardUseFreeze(); | 
|         freeze.setFreezeTime(new Date()); | 
|         freeze.setCardUseId(moneyCard.getId()); | 
|         ; | 
|         MoneyCardUseFreeze getfreeze = moneyCardUseFreezeDao.selectByMoneyCardUseId(moneyCard.getId()); | 
|   | 
|         //更新会员等级 | 
|         moneyCard.getVipInfo().setLevelId(0L); | 
|         infoDao.update(moneyCard.getVipInfo()); | 
|   | 
|         int i = 0; | 
|         if (getfreeze != null) { | 
|             freeze.setId(getfreeze.getId()); | 
|             i = moneyCardUseFreezeDao.update(freeze); | 
|         } else { | 
|             i = moneyCardUseFreezeDao.insert(freeze); | 
|         } | 
|         //记录流水 | 
|         String content = moneyCard.getCardName() + "冻结"; | 
|         return i; | 
|     } | 
|   | 
|     //转让充值卡 | 
|     @Override | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public void transfer(MoneyCardUse moneyCardUse, Long vipId2, Double money) { | 
|         Long oldId = moneyCardUse.getId(); | 
|         moneyCardUse = moneyCardUseDao.selectById(moneyCardUse.getId()); | 
|         if (moneyCardUse.getVipId().equals(vipId2)) { | 
|             throw new GlobleException("转让人不能是该充值卡拥有者!"); | 
|         } | 
|         System.out.println("id ====== " + moneyCardUse.getId()); | 
|         // 查询要转让的充值卡 | 
|         MoneyCardUse getObj = moneyCardUseDao.selectById(moneyCardUse.getId()); | 
|   | 
|         //检测商品是否已付款完成 | 
|         if (getObj.getOrderItemId() != 1) {//不是数据迁移过来的判断付款状态 | 
|             checkOrderStatu(getObj.getOrderItemId()); | 
|         } | 
|   | 
|         if (!(getObj.getStatus().equals(Dictionary.TAOCAN_STATUS_YX))) { | 
|             throw new GlobleException("不是有效的充值卡!"); | 
|         } | 
|         SysVipInfo info = infoDao.selectById(vipId2); | 
|   | 
|         if (money >= getObj.getRealMoney()) {//如果全部转让出去 | 
|             //更新状态 | 
|             moneyCardUse.setStatus(Dictionary.TAOCAN_SOURCE_ZR); | 
|             moneyCardUseDao.update(moneyCardUse); | 
|         } | 
|   | 
|         //更新会员等级 | 
|         getObj.getVipInfo().setLevelId(0L); | 
|         infoDao.update(getObj.getVipInfo()); | 
|         //新增转让流水 | 
|         StringBuilder content = new StringBuilder(); | 
|   | 
|         content.append(getObj.getCardName() + "转让给会员" + getObj.getCardName()); | 
|   | 
|   | 
|         MoneyCardUse infoCard = moneyCardUseDao.selectVipCard(info.getId()); | 
|         //清空content | 
|   | 
|   | 
|         //判断会员没有会籍卡,或者该充值卡不为会籍卡 | 
|         if (infoCard == null || Dictionary.FLAG_YES_Y.equals(getObj.getIsVipCar())) { | 
|             //添加一条接受转让的记录到充值卡使用情况表中 | 
|   | 
|             if (!getObj.getSource().contains(Dictionary.TAOCAN_STATUS_ZR)) { | 
|                 getObj.setSource(getObj.getSource() + Dictionary.TAOCAN_STATUS_ZR); | 
|             } | 
|             getObj.setVipId(vipId2); | 
|             getObj.setId(null); | 
|             content = new StringBuilder(); | 
|             moneyCardUseDao.insert(getObj); | 
|             content.append("获取会员" + getObj.getVipInfo().getVipName() + "的" + getObj.getCardName()) | 
|                     .append(",余次:" + getObj.getLastCountName()) | 
|                     .append(",本金余额:" + getObj.getRealMoney()) | 
|                     .append(",有效期:" + DateUtil.dateToString(getObj.getFailTime(), DateUtil.DATE_FORMAT_MM)) | 
|                     .append(",是否赠送:" + getObj.getSourceName()); | 
|         } | 
|     } | 
|   | 
|     //退款 | 
|     @Override | 
|     public int returnMoney(MoneyCardUse moneyCardUse, Long hjkId) { | 
|   | 
|   | 
|         MoneyCardUse realObj = moneyCardUseDao.selectById(moneyCardUse.getId()); | 
|   | 
|         //检测商品是否已付款完成 | 
|         if (realObj.getOrderItemId() != 1) {//不是数据迁移过来的判断付款状态 | 
|             checkOrderStatu(realObj.getOrderItemId()); | 
|         } | 
|   | 
|         if (!realObj.getStatus().equals(Dictionary.TAOCAN_STATUS_YX)) { | 
|             throw new GlobleException("不是有效充值卡"); | 
|         } | 
|   | 
|         if (realObj.getSource().contains(Dictionary.TAOCAN_SOURCE_ZR)) { | 
|             throw new GlobleException("转让的充值卡不能退款"); | 
|         } | 
|         if (realObj.getSource().contains(Dictionary.TAOCAN_SOURCE_ZS)) { | 
|             throw new GlobleException("赠送的充值卡不能退款"); | 
|         } | 
|   | 
|         if (Dictionary.FLAG_YES_Y.equals(realObj.getIsVipCar())) { | 
|             //更新会员等级 | 
|             realObj.getVipInfo().setLevelId(0L); | 
|             infoDao.update(realObj.getVipInfo()); | 
|         } | 
|         StringBuilder content = new StringBuilder(); | 
|   | 
|         //有会籍卡退到会籍卡中,无则退了现金 | 
|         if (hjkId != null && !hjkId.equals("")) { | 
|             MoneyCardUse vipCard = moneyCardUseDao.selectById(hjkId); | 
|             vipCard.setRealMoney(vipCard.getRealMoney() + realObj.getRealMoney()); | 
|             moneyCardUseDao.update(vipCard); | 
|             content.append(realObj.getCardName() + "退款¥" + realObj.getRealMoney() + "到会籍卡") | 
|                     .append(vipCard.getCardName()) | 
|                     .append(",余次:" + vipCard.getLastCountName()) | 
|                     .append(",本金余额:" + vipCard.getRealMoney()) | 
|                     .append(",有效期:" + DateUtil.dateToString(vipCard.getFailTime(), DateUtil.DATE_FORMAT_MM)) | 
|                     .append(",是否赠送:" + vipCard.getSourceName()); | 
|         } else { | 
|             content.append(realObj.getCardName() + "退款现金¥" + moneyCardUse.getReturnMonery() + ""); | 
|         } | 
|         DecimalFormat df = new DecimalFormat("#.00"); | 
|         double d = Double.valueOf(df.format(realObj.getRealMoney() - Double.valueOf(moneyCardUse.getReturnMonery()))); | 
|         //退款的本金为零,状态为已退款 | 
|         realObj.setRealMoney(d); | 
|         //赠送金额也清零 | 
|         realObj.setGiftMoney(0.0); | 
|         if (d <= 0) { | 
|             realObj.setStatus(Dictionary.TAOCAN_STATUS_TK); | 
|         } | 
|         int i = moneyCardUseDao.update(realObj); | 
|         SysUsers user = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY); | 
|         //新增退款流水 | 
|         return i; | 
|     } | 
|   | 
|     //设置为有效 | 
|     @Override | 
|     public void active(MoneyCardUse proj) { | 
|   | 
|         proj.setStatus(Dictionary.TAOCAN_STATUS_YX); | 
|         if (Dictionary.FLAG_YES_Y.equals(proj.getIsVipCar())) { | 
|             MoneyCardUse cardUse = moneyCardUseDao.selectVipCard(proj.getVipId()); | 
|             if (cardUse != null && !cardUse.getId().equals(proj.getId())) { | 
|                 throw new GlobleException("该会员已经存在有效的会籍卡!"); | 
|             } | 
|         } | 
|         moneyCardUseDao.update(proj); | 
|         StringBuilder content = new StringBuilder(); | 
|         content.append(proj.getCardName() + "设置有效"); | 
|     } | 
|   | 
|     //设置为失效 | 
|     @Override | 
|     public void invalid(MoneyCardUse proj) { | 
|   | 
|         proj.setStatus(Dictionary.TAOCAN_STATUS_WX); | 
|         moneyCardUseDao.update(proj); | 
|         StringBuilder content = new StringBuilder(); | 
|         content.append(proj.getCardName() + "设置失效"); | 
|     } | 
|   | 
|     /** | 
|      * 检测订单条目是否已付款完毕 | 
|      */ | 
|     public void checkOrderStatu(Long id) { | 
|         SysOrderItem item = orderItemDao.selectById(id); | 
|         if (!Dictionary.ORDER_STATU_YFK.equals(item.getStatus())) { | 
|             throw new GlobleException("该商品还存在欠款!"); | 
|         } | 
|     } | 
|   | 
| } |