MrShi
2 天以前 4eac422e52a4d28fb651b75d0f054697c7a2c0fa
server/dmmall_service/src/main/java/com/doumee/service/business/impl/WithdrawRecordServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,378 @@
package com.doumee.service.business.impl;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.RedisUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.dao.web.dto.MultiFileDTO;
import com.doumee.dao.web.request.DealIntegralRequest;
import com.doumee.dao.web.request.WithdrawApplyRequest;
import com.doumee.service.business.IntegralService;
import com.doumee.service.business.WithdrawRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.With;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.bouncycastle.cert.dane.DANEEntry;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
 * ç”¨æˆ·æçŽ°è®°å½•Service实现
 * @author æ±Ÿè¹„蹄
 * @date 2026/01/16 11:50
 */
@Service
public class WithdrawRecordServiceImpl implements WithdrawRecordService {
    @Autowired
    private WithdrawRecordMapper withdrawRecordMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IntegralMapper integralMapper;
    @Autowired
    private MemberBankMapper memberBankMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private MultifileMapper multifileMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public Integer create(WithdrawRecord withdrawRecord) {
        withdrawRecordMapper.insert(withdrawRecord);
        return withdrawRecord.getId();
    }
    @Override
    public void deleteById(Integer id) {
        withdrawRecordMapper.deleteById(id);
    }
    @Override
    public void delete(WithdrawRecord withdrawRecord) {
        UpdateWrapper<WithdrawRecord> deleteWrapper = new UpdateWrapper<>(withdrawRecord);
        withdrawRecordMapper.delete(deleteWrapper);
    }
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        withdrawRecordMapper.deleteBatchIds(ids);
    }
    /**
     * æ‰“款审核
     * @param withdrawRecord å®žä½“对象
     */
    @Override
    @Transactional
    public void updateById(WithdrawRecord withdrawRecord, IntegralService integralService) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsObject(withdrawRecord.getStatus(),Constants.ONE)
                && !Constants.equalsObject(withdrawRecord.getStatus(),Constants.TWO)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        WithdrawRecord model = withdrawRecordMapper.selectById(withdrawRecord.getId());
        if(model == null || Constants.equalsObject(model.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsObject(model.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请已审批,请尝试刷新页面查看");
        }
        Date date = new Date();
        model.setPayBank(withdrawRecord.getPayBank());
        model.setEditor(user.getId());
        model.setEditDate(date);
        model.setAuditRemark(withdrawRecord.getAuditRemark());
        model.setAuditTime(date);
        model.setAuditUser(user.getId());
        model.setStatus(withdrawRecord.getStatus());
        model.setPayBank(withdrawRecord.getPayBank());
        withdrawRecordMapper.updateById(model);
        dealBatchMultiFiles(model,withdrawRecord.getPayFileList());
        //审批未通过 é€€å›žæçŽ°é‡‘é¢
        if (Constants.equalsObject(model.getStatus(), Constants.TWO)) {
            DealIntegralRequest dealIntegralRequest = new DealIntegralRequest();
            dealIntegralRequest.setIntegralObjType(Constants.IntegralObjType.WITHDRAW_APPLY_BACK);
            dealIntegralRequest.setIntegralNum(model.getAmount());
            dealIntegralRequest.setObjId(withdrawRecord.getId());
            dealIntegralRequest.setOrderCode(withdrawRecord.getCode().toString());
            dealIntegralRequest.setMemberId(model.getMemberId());
            dealIntegralRequest.setDealType(Constants.ZERO);
            integralService.dealShopAmount(dealIntegralRequest);
        }
    }
    public void dealBatchMultiFiles(WithdrawRecord model, List<Multifile> fileList  ) {
        //清空原有的
        if(fileList!=null && fileList.size()>0){
            List<Multifile> multifileList = new ArrayList<>();
            fileList.stream().forEach(s -> {
                if(StringUtils.isNotBlank(s.getFileurl())){
                    s.setIsdeleted(Constants.ZERO);
                    s.setCreator(model.getAuditUser());
                    s.setCreateDate(model.getAuditTime());
                    s.setObjId(model.getId());
                    s.setType(Constants.ZERO);
                    s.setObjType(Constants.MultiFile.TRANSFER_FILE.getKey());
                    multifileList.add(s);
                }
            });
            if(multifileList.size()>0){
                multifileMapper.insert(multifileList);
            }
        }
    }
    @Override
    public void updateByIdInBatch(List<WithdrawRecord> withdrawRecords) {
        if (CollectionUtils.isEmpty(withdrawRecords)) {
            return;
        }
        for (WithdrawRecord withdrawRecord: withdrawRecords) {
//            this.updateById(withdrawRecord);
        }
    }
    @Override
    public WithdrawRecord findById(Integer id) {
        MPJLambdaWrapper<WithdrawRecord> queryWrapper = new MPJLambdaWrapper<WithdrawRecord>()
                .selectAll(WithdrawRecord.class)
                .selectAs(SystemUser::getRealname,WithdrawRecord::getAuditUserName)
                .selectAs(Shop::getName,WithdrawRecord::getShopName)
                .selectAs(Shop::getCode,WithdrawRecord::getShopCode)
                .leftJoin(Shop.class,Shop::getId,WithdrawRecord::getMemberId)
                .leftJoin(SystemUser.class,SystemUser::getId,WithdrawRecord::getAuditUser)
                .eq(WithdrawRecord::getId,id)
                .eq(WithdrawRecord::getIsdeleted,Constants.ZERO);
        WithdrawRecord withdrawRecord = withdrawRecordMapper.selectJoinOne(WithdrawRecord.class,queryWrapper);
        if (Objects.isNull(withdrawRecord) ) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(withdrawRecord.getStatus(), Constants.ZERO)){
            String path = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode() +
                    systemDictDataBiz.queryByCode(Constants.OSS, Constants.TRANSFER_FILE).getCode();
            Multifile queryfile = new Multifile();
            queryfile.setObjId(withdrawRecord.getId());
            queryfile.setObjType(  Constants.MultiFile.TRANSFER_FILE.getKey());
            queryfile.setIsdeleted(Constants.ZERO);
            List<Multifile> filelist = multifileMapper.selectList(new QueryWrapper<>(queryfile));
            if(filelist !=null && filelist.size()>0){
                for (Multifile multifile:filelist) {
                    if(StringUtils.isNotBlank(multifile.getFileurl())){
                        multifile.setFileFullUrl(path + multifile.getFileurl());
                    }
                }
                withdrawRecord.setPayFileList(filelist);
            }
        }
        return withdrawRecord;
    }
    @Override
    public WithdrawRecord findOne(WithdrawRecord withdrawRecord) {
        QueryWrapper<WithdrawRecord> wrapper = new QueryWrapper<>(withdrawRecord);
        return withdrawRecordMapper.selectOne(wrapper);
    }
    @Override
    public List<WithdrawRecord> findList(WithdrawRecord withdrawRecord) {
        QueryWrapper<WithdrawRecord> wrapper = new QueryWrapper<>(withdrawRecord);
        return withdrawRecordMapper.selectList(wrapper);
    }
    @Override
    public PageData<WithdrawRecord> findPage(PageWrap<WithdrawRecord> pageWrap) {
        IPage<WithdrawRecord> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<WithdrawRecord> queryWrapper = new MPJLambdaWrapper<WithdrawRecord>()
                .selectAll(WithdrawRecord.class)
                .selectAs(Shop::getName,WithdrawRecord::getShopName)
                .selectAs(Shop::getCode,WithdrawRecord::getShopCode)
                .leftJoin(Shop.class,Shop::getId,WithdrawRecord::getMemberId);
        Utils.MP.blankToNull(pageWrap.getModel());
        pageWrap.getModel().setIsdeleted(Constants.ZERO);
        if (pageWrap.getModel().getId() != null) {
           queryWrapper.eq(WithdrawRecord::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getCreator() != null) {
           queryWrapper.eq(WithdrawRecord::getCreator, pageWrap.getModel().getCreator());
        }
        if (pageWrap.getModel().getCreateDate() != null) {
           queryWrapper.ge(WithdrawRecord::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
           queryWrapper.le(WithdrawRecord::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
        }
        if (pageWrap.getModel().getEditor() != null) {
           queryWrapper.eq(WithdrawRecord::getEditor, pageWrap.getModel().getEditor());
        }
        if (pageWrap.getModel().getEditDate() != null) {
           queryWrapper.ge(WithdrawRecord::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
           queryWrapper.le(WithdrawRecord::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
        }
        if (pageWrap.getModel().getIsdeleted() != null) {
           queryWrapper.eq(WithdrawRecord::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getRemark() != null) {
           queryWrapper.eq(WithdrawRecord::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getMemberId() != null) {
           queryWrapper.eq(WithdrawRecord::getMemberId, pageWrap.getModel().getMemberId());
        }
        if (pageWrap.getModel().getCode() != null) {
           queryWrapper.eq(WithdrawRecord::getCode, pageWrap.getModel().getCode());
        }
        if (pageWrap.getModel().getBankId() != null) {
           queryWrapper.eq(WithdrawRecord::getBankId, pageWrap.getModel().getBankId());
        }
        if (pageWrap.getModel().getStatus() != null) {
           queryWrapper.eq(WithdrawRecord::getStatus, pageWrap.getModel().getStatus());
        }
        if (pageWrap.getModel().getAuditUser() != null) {
           queryWrapper.eq(WithdrawRecord::getAuditUser, pageWrap.getModel().getAuditUser());
        }
        if (pageWrap.getModel().getAuditTime() != null) {
           queryWrapper.ge(WithdrawRecord::getAuditTime, Utils.Date.getStart(pageWrap.getModel().getAuditTime()));
           queryWrapper.le(WithdrawRecord::getAuditTime, Utils.Date.getEnd(pageWrap.getModel().getAuditTime()));
        }
        if (pageWrap.getModel().getAuditRemark() != null) {
           queryWrapper.eq(WithdrawRecord::getAuditRemark, pageWrap.getModel().getAuditRemark());
        }
        if (pageWrap.getModel().getPayBank() != null) {
           queryWrapper.eq(WithdrawRecord::getPayBank, pageWrap.getModel().getPayBank());
        }
        queryWrapper.ge(pageWrap.getModel().getStarttime() != null,WithdrawRecord::getCreateDate, pageWrap.getModel().getStarttime());
        queryWrapper.le(pageWrap.getModel().getEndtime() != null,WithdrawRecord::getCreateDate, pageWrap.getModel().getEndtime());
        queryWrapper.like(pageWrap.getModel().getShopName() != null,Shop::getName, pageWrap.getModel().getShopName());
        queryWrapper.orderByDesc(WithdrawRecord::getCreateDate);
        PageData<WithdrawRecord> rr = PageData.from(withdrawRecordMapper.selectJoinPage(page,WithdrawRecord.class, queryWrapper));
        if(pageWrap.getPage() == 1){
            //如果查询第一页,做经销商销售信息统计业务查询
            WithdrawRecord count   =  withdrawRecordMapper.selectJoinOne(WithdrawRecord.class,new MPJLambdaWrapper<WithdrawRecord>()
                     .selectSum(WithdrawRecord::getAmount)
                     .leftJoin(Shop.class,Shop::getId,WithdrawRecord::getMemberId)
                     .ge(pageWrap.getModel().getStarttime() != null,WithdrawRecord::getCreateDate, pageWrap.getModel().getStarttime())
                     .le(pageWrap.getModel().getEndtime() != null,WithdrawRecord::getCreateDate, pageWrap.getModel().getEndtime())
                     .like(pageWrap.getModel().getShopName() != null,Shop::getName, pageWrap.getModel().getShopName())
                     .eq(WithdrawRecord::getIsdeleted,Constants.ZERO)
                     .eq(pageWrap.getModel().getStatus()!=null,WithdrawRecord::getStatus,pageWrap.getModel().getStatus())
            );
            if(count == null){
                count = new WithdrawRecord();
            }
            count.setAmount(Constants.formatBigdecimal4Float(count.getAmount()));
            rr.setCountData(count);
        }
        return rr;
    }
    @Override
    public long count(WithdrawRecord withdrawRecord) {
        QueryWrapper<WithdrawRecord> wrapper = new QueryWrapper<>(withdrawRecord);
        return withdrawRecordMapper.selectCount(wrapper);
    }
    @Override
    public void withdrawApply(WithdrawApplyRequest request, IntegralService integralService){
        if (Objects.isNull( request)
            || request.getMemberId() == null
            || request.getBankId() == null
            || request.getAmount() == null) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        //查询商户剩余余额
        Shop shop = shopMapper.selectById(request.getMemberId());
        if(Objects.isNull(shop)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到商户信息");
        }
        if(shop.getAmount().compareTo(request.getAmount()) < 0){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"商户余额不足");
        }
        MemberBank memberBank = memberBankMapper.selectById(request.getBankId());
        if(Objects.isNull(memberBank)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到收款银行信息");
        }
        WithdrawRecord withdrawRecord = new WithdrawRecord();
        withdrawRecord.setCreateDate(new Date());
        withdrawRecord.setIsdeleted(Constants.ZERO);
        withdrawRecord.setEditDate(new Date());
        withdrawRecord.setBankId(request.getBankId());
        withdrawRecord.setAmount(request.getAmount());
        withdrawRecord.setMemberId(request.getMemberId());
        withdrawRecord.setCode(this.getNextInCode());
        withdrawRecord.setStatus(Constants.ZERO);
        withdrawRecord.setBankName(memberBank.getBankName());
        withdrawRecord.setName(memberBank.getName());
        withdrawRecord.setBankAccount(memberBank.getBankAccount());
        withdrawRecordMapper.insert(withdrawRecord);
        DealIntegralRequest dealIntegralRequest = new DealIntegralRequest();
        dealIntegralRequest.setIntegralObjType(Constants.IntegralObjType.WITHDRAW_APPLY);
        dealIntegralRequest.setIntegralNum(request.getAmount());
        dealIntegralRequest.setObjId(withdrawRecord.getId());
        dealIntegralRequest.setOrderCode(withdrawRecord.getCode().toString());
        dealIntegralRequest.setMemberId(request.getMemberId());
        dealIntegralRequest.setDealType(Constants.ONE);
        integralService.dealShopAmount(dealIntegralRequest);
    }
    public synchronized  Long  getNextInCode(){
        String prefix =  DateUtil.getDate(new Date(),"yyyyMMdd") ;
        Integer countNum  = RedisUtil.getObject(redisTemplate, Constants.RedisKeys.WITHDRAW_KEY,Integer.class);
        countNum = Constants.formatIntegerNum(countNum)+1;
        //更新缓存
        RedisUtil.addObject(redisTemplate,Constants.RedisKeys.WITHDRAW_KEY,countNum);
        String nextIndex =Long.toString( countNum);
        return Long.parseLong(prefix + StringUtils.leftPad(nextIndex,4,"0"));
    }
}