101captain
2022-01-06 42659976e1907337b54b128620a9a886b79aa1df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.panzhihua.service_community.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.property.CommonPage;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsOrderVO;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.SmsUtil;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.common.utlis.WxPayUtils;
import com.panzhihua.service_community.entity.McsOrder;
import com.panzhihua.service_community.dao.McsOrderDao;
import com.panzhihua.service_community.service.McsMerchantService;
import com.panzhihua.service_community.service.McsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
 
/**
 * 商业街订单表(McsOrder)表服务实现类
 *
 * @author makejava
 * @since 2022-01-04 16:10:07
 */
@Slf4j
@Service
public class McsOrderServiceImpl extends ServiceImpl<McsOrderDao, McsOrder> implements McsOrderService {
    @Value("${min.app.isTest}")
    private Boolean isTest;
    @Value("${min.app.appid}")
    private String appid;
    @Value("${min.app.payKey}")
    private String payKey;
    @Value("${min.app.mchId}")
    private String mchId;
    //@Value("${min.app.notifyUrl}")
    private String notifyUrl="www.baidu.com";
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private McsMerchantService mcsMerchantService;
    @Override
    public R pageList(CommonPage commonPage) {
        return R.ok(this.baseMapper.pageList(new Page(commonPage.getPage(),commonPage.getSize()),commonPage));
    }
 
    @Override
    public R insert(McsOrderVO mcsOrderVO) {
        McsOrder mcsOrder=new McsOrder();
        BeanUtils.copyProperties(mcsOrderVO,mcsOrder);
        mcsOrder.setStatus(0);
        mcsOrder.setCreateTime(new Date());
        mcsOrder.setOrderNo(DateUtils.getCurrentDateStr());
        this.baseMapper.insert(mcsOrder);
        rabbitTemplate.convertAndSend("mcsOrder.exchange", "mcsOrder.key", mcsOrder, message -> {
            message.getMessageProperties().setHeader("x-delay",  1000 * 1800);
            return message;
        });
        return R.ok(mcsOrder);
    }
 
    @Override
    public R detail(Long id) {
        return R.ok(this.baseMapper.detail(id));
    }
 
    @Override
    public R wxPay(McsOrderVO mcsOrderVO) {
        McsOrder mcsOrder=this.baseMapper.selectById(mcsOrderVO.getId());
        if(mcsOrder!=null&&mcsOrder.getStatus()==0){
            try {
                BigDecimal money = mcsOrder.getMoney();
                if (isTest) {
                    money = BigDecimal.valueOf(0.01);
                }
                // 调用wx支付
                String result= WxPayUtils.h5pay(appid, mchId, payKey, notifyUrl, "点亮支付", mcsOrderVO.getOpenId(),
                        mcsOrderVO.getOrderNo().toString(), money,mcsOrderVO.getTradeType());
                return R.ok(result);
            } catch (Exception e) {
                log.error("调用微信支付异常,异常原因:" + e.getMessage());
            }
        }
        return R.fail("订单状态异常");
    }
 
    @Override
    public R sendContent(Long id) {
        String phone=this.baseMapper.selectPhone(id);
        if(StringUtils.isNotEmpty(phone)){
            SmsUtil smsUtil=new SmsUtil();
            int result=smsUtil.sendContent(phone);
            if(result>0){
                return R.ok();
            }
        }
        return R.fail("短信发送异常");
    }
 
    @Override
    @Transactional
    public R notify(McsOrderVO mcsOrderVO) {
        McsOrder mcsOrderBase=this.baseMapper.selectOne(new QueryWrapper<McsOrder>().lambda().eq(McsOrder::getOrderNo,mcsOrderVO.getOrderNo()));
        mcsOrderBase.setStatus(2);
        mcsOrderBase.setPayNo(mcsOrderVO.getPayNo());
        mcsOrderBase.setPayTime(mcsOrderVO.getPayTime());
        int i=this.baseMapper.updateById(mcsOrderBase);
        if(i>0){
            mcsMerchantService.updateOrAddMcsMerchantAfterOrder(mcsOrderBase.getMerchantName(),mcsOrderBase.getConfigId(),mcsOrderBase.getUserId());
        }
        return R.ok();
    }
}