李旭东
2023-12-08 7efc6ed86025b610cab109a2e9f83362740d8ed4
hd/pipe/secure/src/main/java/com/thhy/secure/modules/biz/integralAccount/service/impl/IntegralDetailServiceImpl.java
对比新文件
@@ -0,0 +1,222 @@
package com.thhy.secure.modules.biz.integralAccount.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.ExcelUtils;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.secure.modules.biz.integralAccount.entity.IntegralAccountEntity;
import com.thhy.secure.modules.biz.integralAccount.entity.IntegralDetailDto;
import com.thhy.secure.modules.biz.integralAccount.entity.IntegralDetailEntity;
import com.thhy.secure.modules.biz.integralAccount.mapper.IntegralDetailMapper;
import com.thhy.secure.modules.biz.integralAccount.service.IntegralDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author QiuYuHao
 * @CreateDate 2023-11-27 18:33:40
 * 积分超市实现
 */
@Service
public class IntegralDetailServiceImpl implements IntegralDetailService
{
   @Autowired
   private IntegralDetailMapper integralDetailMapper;
   private static final Object Lock= new Object();
   /**
    * 积分超市新增和修改
    * @param integralDetailEntity
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public BasicResult insertAndUpdate(IntegralDetailEntity integralDetailEntity){
      String id = integralDetailEntity.getId();
      SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
      String sysUserId = sysUserInfo.getUserId();
      String companyId = sysUserInfo.getCompanyId();
      //扣积分逻辑
      synchronized (Lock){
         IntegralAccountEntity integralAccountEntity = IntegralAccountEntity
               .builder()
               .userId(integralDetailEntity.getUserId())
               .updateUser(sysUserId)
               .updateTime(new Date())
               .id(UUIDUtils.create())
               .build();
         Map map = new HashMap();
         map.put("userId",integralDetailEntity.getUserId());
         //查询账户
         IntegralAccountEntity queryAccount = integralDetailMapper.selectByOne(map);
         if(queryAccount == null || queryAccount.getTotalIntegral()<integralDetailEntity.getIntegralWater()){//无账户
            return BasicResult.faild("500","积分不足,无法扣除积分",null);
         }
         //有账户
         Double totalConsumption = queryAccount.getTotalConsumption();//总消耗
         Double accumulateIntegral = queryAccount.getAccumulateIntegral();//累计积分
         Double totalIntegral = queryAccount.getTotalIntegral();//当前余额
         integralAccountEntity.setTotalIntegral(totalIntegral-integralDetailEntity.getIntegralWater());
         integralAccountEntity.setAccumulateIntegral(accumulateIntegral);
         integralAccountEntity.setTotalConsumption(totalConsumption+integralDetailEntity.getIntegralWater());
         //修改账户
         integralDetailMapper.insertAndUpdateAccount(integralAccountEntity);
         if(id  == null){
            integralDetailEntity.setCompanyId(companyId);
            integralDetailEntity.setId(UUIDUtils.create());
            integralDetailEntity.setCreateTime(new Date());
            integralDetailEntity.setCreateUser(sysUserId);
         }
         integralDetailEntity.setBalanc(totalIntegral-integralDetailEntity.getIntegralWater());//拿到当前余额
         integralDetailEntity.setType(2);
         integralDetailEntity.setIntegralSource("积分超市");
         //添加或修改明细
         integralDetailMapper.insertDetailAndUpdate(integralDetailEntity);
      }
      return BasicResult.success();
   }
   /**
    * 积分超市
    * @param map
    * @return
    */
   @Override
   public BasicResult selectPageList(Map map){
      SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
      String companyId = sysUserInfo.getCompanyId();
      int pageNum = (int) map.get("pageNum");
      int pageSize = (int) map.get("pageSize");
      PageHelper.startPage(pageNum,pageSize);
      map.put("companyId",companyId);
      List<IntegralDetailEntity> integralDetailEntities = integralDetailMapper.selectPageList(map);
      return BasicResult.success(new PageInfo<>(integralDetailEntities));
   }
   /**
    * 积分超市删除
    * @param id
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public BasicResult delete(String id){
      Map map =new HashMap();
      map.put("id",id);
      IntegralDetailDto integralDetailEntity = integralDetailMapper.selectSafeIntegralInfo(map).get(0);
      Double integralWater = integralDetailEntity.getIntegralWater();//获取消耗积分
      Map account = new HashMap();
      account.put("userId",integralDetailEntity.getUserId());
      //查询账户
      IntegralAccountEntity queryAccount = integralDetailMapper.selectByOne(account);
      Double totalConsumption = queryAccount.getTotalConsumption();//总消耗
      Double accumulateIntegral = queryAccount.getAccumulateIntegral();//累计积分
      Double totalIntegral = queryAccount.getTotalIntegral();//当前余额
      synchronized (Lock){
         IntegralAccountEntity integralAccountEntity = IntegralAccountEntity
               .builder()
               .userId(integralDetailEntity.getUserId())
               .totalConsumption(totalConsumption - integralWater)
               .accumulateIntegral(accumulateIntegral + integralWater)
               .totalIntegral(totalIntegral + integralWater)
               .build();
         integralDetailMapper.insertAndUpdateAccount(integralAccountEntity);
         integralDetailMapper.delete(id);
      }
      return BasicResult.success();
   }
   /**
    * 安全积分列表
    */
   @Override
   public BasicResult selectSafeIntegralPageList(Map map){
      SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
      String companyId = sysUserInfo.getCompanyId();
      int pageNum = (int) map.get("pageNum");
      int pageSize = (int) map.get("pageSize");
      PageHelper.startPage(pageNum,pageSize);
      map.put("companyId",companyId);
      List<IntegralAccountEntity> integralAccountEntities = integralDetailMapper.selectSafeIntegralPageList(map);
      return BasicResult.success(new PageInfo<>(integralAccountEntities));
   }
   /**
    * 安全积分明细
    * @param map
    * @return
    */
   @Override
   public BasicResult selectSafeIntegralInfo(Map map){
      SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
      String companyId = sysUserInfo.getCompanyId();
      IntegralAccountEntity integralAccountEntity = integralDetailMapper.selectByOne(map);
      map.put("integral",integralAccountEntity.getTotalIntegral());
      int pageNum = (int) map.get("pageNum");
      int pageSize = (int) map.get("pageSize");
      PageHelper.startPage(pageNum,pageSize);
      map.put("companyId",companyId);
      List<IntegralDetailDto> integralDetailEntities = integralDetailMapper.selectSafeIntegralInfo(map);
      map.put("data",new PageInfo<>(integralDetailEntities));
      return BasicResult.success(map);
   }
   @Override
   public void exportList(Map map, HttpServletResponse rsp) {
      SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
      String companyId = sysUserInfo.getCompanyId();
      map.put("companyId",companyId);
      List<IntegralDetailEntity> integralDetailEntities = integralDetailMapper.selectPageList(map);
      integralDetailEntities.forEach(obj->{
         if(obj.getUserType() ==1){
            obj.setUserTypeName("管服人员");
         }
         if(obj.getUserType() ==2){
            obj.setUserTypeName("劳务人员");
         }
      });
      ExcelUtils.downExcel(integralDetailEntities,IntegralDetailEntity.class,rsp,"班组列表");
   }
   @Override
   public void exportSafeIntegralInfo(Map map, HttpServletResponse rsp) {
      SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
      String companyId = sysUserInfo.getCompanyId();
      IntegralAccountEntity integralAccountEntity = integralDetailMapper.selectByOne(map);
      map.put("integral",integralAccountEntity.getTotalIntegral());
      map.put("companyId",companyId);
      List<IntegralDetailDto> integralDetailEntities = integralDetailMapper.selectSafeIntegralInfo(map);
      integralDetailEntities.forEach(obj->{
         if(obj.getUserType() ==1){
            obj.setUserTypeName("管服人员");
         }
         if(obj.getUserType() ==2){
            obj.setUserTypeName("劳务人员");
         }
         if(obj.getType() == 1){
            obj.setTypeName("获取");
         }
         if(obj.getType() == 2){
            obj.setTypeName("消耗");
         }
      });
      ExcelUtils.downExcel(integralDetailEntities,IntegralDetailDto.class,rsp,"班组列表");
   }
}