zjh
2025-02-26 263413a9ade62a8501acbbc60157d48ca51a9f9d
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.ltkj.web.controller.mall;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ltkj.common.utils.JacksonUtil;
import com.ltkj.framework.config.UserHoder;
import com.ltkj.hosp.domain.TjReservation;
import com.ltkj.hosp.domain.Wxuser;
import com.ltkj.hosp.service.ITbTransitionService;
import com.ltkj.hosp.service.ITjReservationService;
import com.ltkj.mall.domain.MallOrder;
import com.ltkj.mall.domain.MallTimeConfig;
import com.ltkj.mall.domain.OrderRefund;
import com.ltkj.mall.mallOrderUtils.OrderConstants;
import com.ltkj.mall.service.IMallOrderService;
import com.ltkj.mall.service.IMallTimeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ltkj.common.annotation.Log;
import com.ltkj.common.core.controller.BaseController;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.enums.BusinessType;
import com.ltkj.mall.domain.MallAftersale;
import com.ltkj.mall.service.IMallAftersaleService;
import com.ltkj.common.utils.poi.ExcelUtil;
import com.ltkj.common.core.page.TableDataInfo;
 
/**
 * 售后Controller
 *
 * @author ltkj_赵佳豪&李格
 * @date 2023-07-12
 */
@RestController
@RequestMapping("/mall/aftersale")
@Api(tags = "PC端售后审批接口集")
public class MallAftersaleController extends BaseController {
    @Autowired
    private IMallAftersaleService mallAftersaleService;
    @Autowired
    private IMallOrderService orderService;
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private IMallTimeConfigService mallTimeConfigService;
    @Resource
    private ITjReservationService reservationService;
    @Resource
    private ITbTransitionService transitionService;
 
    /**
     * 查询售后列表
     */
    @PreAuthorize("@ss.hasPermi('mall:aftersale:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询售后列表")
    public TableDataInfo list(MallAftersale mallAftersale) {
        startPage();
        if(null !=mallAftersale.getEndTime()){
            String endTime = mallAftersale.getEndTime();
            DateTime dateTime = DateUtil.endOfDay(DateUtil.parse(endTime));
            String format = DateUtil.format(dateTime, "yyyy-MM-dd HH:mm:ss");
            mallAftersale.setEndTime(format);
        }
        List<MallAftersale> list = mallAftersaleService.selectMallAftersaleList(mallAftersale);
        if(null !=list && list.size()>0){
            for (MallAftersale aftersale : list) {
                MallOrder order = orderService.selectMallOrderById(Long.valueOf(aftersale.getOrderId()));
                if(null !=order){
                    aftersale.setMobile(order.getMobile());
                }
            }
        }
        return getDataTable(list);
    }
 
    /**
     * 导出售后列表
     */
    @PreAuthorize("@ss.hasPermi('mall:aftersale:export')")
    @Log(title = "售后", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallAftersale mallAftersale) {
        List<MallAftersale> list = mallAftersaleService.selectMallAftersaleList(mallAftersale);
        ExcelUtil<MallAftersale> util = new ExcelUtil<MallAftersale>(MallAftersale.class);
        util.exportExcel(response, list, "售后数据");
    }
 
    /**
     * 获取售后详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:aftersale:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取售后详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mallAftersaleService.selectMallAftersaleById(id));
    }
 
    /**
     * 新增售后
     */
    @PreAuthorize("@ss.hasPermi('mall:aftersale:add')")
    @Log(title = "售后", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增售后")
    public AjaxResult add(@RequestBody MallAftersale mallAftersale) {
        return toAjax(mallAftersaleService.insertMallAftersale(mallAftersale));
    }
 
    /**
     * 修改售后
     */
    @PreAuthorize("@ss.hasPermi('mall:aftersale:edit')")
    @Log(title = "售后", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改售后")
    public AjaxResult edit(@RequestBody MallAftersale mallAftersale) {
        return toAjax(mallAftersaleService.updateMallAftersale(mallAftersale));
    }
 
    /**
     * 删除售后
     */
    @PreAuthorize("@ss.hasPermi('mall:aftersale:remove')")
    @Log(title = "售后", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除售后")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mallAftersaleService.deleteMallAftersaleByIds(ids));
    }
 
 
    /**
     * 订单同意退款
     * @param orderRefund          订单信息,{ orderId:xxx }
     * @return 订单退款操作结果
     */
    @PostMapping("/agreeToRefund")
    @ApiOperation(value = "订单同意退款")
    @Transactional
    public AjaxResult agreeToRefund(@RequestBody OrderRefund orderRefund) {
        MallOrder order = orderService.getById(orderRefund.getId());
        if(null==order){
            return AjaxResult.error("订单数据不存在!!!");
        }
        if (!OrderConstants.isRefundStatus(order)&&!OrderConstants.isRefundVerify(order)) {
            return AjaxResult.error( "退款申请状态下订单才可以审核退款");
        }
        BeanUtil.copyProperties(orderRefund,order);
        Date now = new Date();
        order.setOrderStatus(Long.valueOf(OrderConstants.STATUS_REFUND_CONFIRM));
        order.setUpdateTime(now);
        order.setRefundTime(now);
        order.setCloseTime(now);
        // 返还优惠券
//        List<MallCouponUser> couponUsers = couponUserService.findByOid(orderId);
//        for (MallCouponUser couponUser: couponUsers) {
//            // 优惠券状态设置为可使用
//            couponUser.setStatus(CouponUserConstant.STATUS_USABLE);
//            couponUser.setUpdateTime(LocalDateTime.now());
//            couponUserService.update(couponUser);
//        }
        if (1 == orderRefund.getRefundType()) {
            //原路退回的话要调用支付商接口
            // 微信退款
            WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
            wxPayRefundRequest.setOutTradeNo(order.getOrderSn());
            wxPayRefundRequest.setOutRefundNo("refund_" + order.getOrderSn());
            // 元转成分
            Integer totalFee = order.getActualPrice().multiply(new BigDecimal(100)).intValue();
            wxPayRefundRequest.setTotalFee(totalFee);
            wxPayRefundRequest.setRefundFee(totalFee);
            WxPayRefundResult wxPayRefundResult;
            try {
                wxPayRefundResult = wxPayService.refund(wxPayRefundRequest);
            } catch (WxPayException e) {
                return AjaxResult.error("订单退款失败");
            }
            if (!"SUCCESS".equals(wxPayRefundResult.getReturnCode())) {
                return AjaxResult.error("订单退款失败");
            }
            if (!"SUCCESS".equals(wxPayRefundResult.getResultCode())) {
                return AjaxResult.error("订单退款失败");
            }
        }
        /*notifyService.notifySmsTemplate(order.getMobile(), NotifyType.REFUND,
                new String[]{order.getOrderSn().substring(8, 14)});*/
        orderService.updateById(order);
        MallAftersale aftersale = mallAftersaleService.getOne(new LambdaQueryWrapper<MallAftersale>().eq(MallAftersale::getOrderId, order.getId()));
        aftersale.setStatus(3L);
        aftersale.setHandleTime(new Date());
        mallAftersaleService.updateById(aftersale);
        //对应预约时间数量减1
        final TjReservation byId = reservationService.getById(order.getReservationId());
        final Date reservationTime = byId.getReservationTime();
        LambdaQueryWrapper<MallTimeConfig> wq=new LambdaQueryWrapper<>();
        wq.eq(MallTimeConfig::getTime,reservationTime);
        final MallTimeConfig one = mallTimeConfigService.getOne(wq);
        one.setNowNum(one.getNowNum()+1);
        mallTimeConfigService.updateById(one);
//        byId.setIsExpire(1);
        reservationService.removeById(byId.getId());
        transitionService.deletedTbTransitionByCusId(byId.getIdCard());
        return AjaxResult.success();
    }
 
 
    /**
     * 售后拒绝接口
     */
    @PostMapping("/refuse")
    @ApiOperation(value = "售后拒绝接口")
    public AjaxResult refuse(@RequestBody String id) {
        Long idd = JacksonUtil.parseLong(id, "id");
        MallAftersale aftersale = mallAftersaleService.getById(idd);
        aftersale.setStatus(4L);
        aftersale.setHandleTime(new Date());
        return toAjax(mallAftersaleService.updateById(aftersale));
    }
}