forked from kidgrow-microservices-platform

dougang
2020-12-09 c8cdba0d34f57835a96233a7f0a7eaa35774dd6f
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
package com.kidgrow.oprationcenter.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import com.kidgrow.oprationcenter.mapper.SaasClientPayMapper;
import com.kidgrow.oprationcenter.model.PayManager;
import com.kidgrow.oprationcenter.model.SaasClientPay;
import com.kidgrow.oprationcenter.service.AlipayService;
import com.kidgrow.oprationcenter.service.IPayManagerService;
import com.kidgrow.oprationcenter.service.ISaasClientPayService;
import com.kidgrow.oprationcenter.service.WxService;
import com.kidgrow.oprationcenter.vo.SaasClientPayExcel;
import com.kidgrow.oprationcenter.vo.SaasClientPayVo;
import com.kidgrow.usercenter.feign.SysDoctorService;
import com.kidgrow.usercenter.model.SysDoctor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
//import com.kidgrow.recordcenter.feign.ChildService;
 
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 * @Description: <br>
 * @Project: 用户中心<br>
 * @CreateDate: Created in 2020-09-17 16:21:03 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 * @version 1.0
 */
@Slf4j
@Service
public class SaasClientPayServiceImpl extends SuperServiceImpl<SaasClientPayMapper, SaasClientPay> implements ISaasClientPayService {
    /**
     * 列表
     * @param params
     * @return
     */
    @Autowired
    private SysDoctorService sysDoctorService;
    @Autowired
    WxService wxService;
    @Autowired
    AlipayService alipayService;
    @Autowired
    IPayManagerService payManagerService;
    @Override
    public PageResult<SaasClientPayVo> findList(Map<String, Object> params){
        Long doctorId = MapUtils.getLong(params, "doctorId");
        if (doctorId!=null) {
            ResultBody byId = sysDoctorService.findById(doctorId);
            if (byId.getCode()==0) {
                Object data = byId.getData();
                SysDoctor sysDoctor = JSON.parseObject(JSON.toJSONString(data), SysDoctor.class);
                if (sysDoctor!=null) {
                    params.put("createUserId",sysDoctor.getUserId());
                }
            }
            params.remove("doctorId");
        }
        Page<SaasClientPay> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<SaasClientPay> list  =  baseMapper.findList(page, params);
        List<SaasClientPayVo> listVo=null;
        if (!list.isEmpty()) {
            listVo=new ArrayList<>();
            for (SaasClientPay saasClientPay : list) {
                SaasClientPayVo vo=new SaasClientPayVo();
                BeanCopier beanCopier = BeanCopier.create(SaasClientPay.class, SaasClientPayVo.class, false);
                beanCopier.copy(saasClientPay,vo,null);
                double v = Double.parseDouble(vo.getPayPrice().toString())/100;
                vo.setPrice(v+"");
                listVo.add(vo);
            }
        }
        return PageResult.<SaasClientPayVo>builder().data(listVo).code(0).count(page.getTotal()).build();
    }
 
    /**
   * 根据SaasClientPay对象当做查询条件进行查询
   * @param saasClientPay
   * @return SaasClientPay
   */
    @Override
    public SaasClientPay findByObject(SaasClientPay saasClientPay){
        return baseMapper.findByObject(saasClientPay);
    }
 
    /**
     * 查看一个科室的某个诊断的支付状态
     * @param hospitalId
     * @param departmentId
     * @param diaId
     * @return
     */
    @Override
    public SaasClientPay findModelByObject(Long hospitalId,Long departmentId,String diaId) {
        SaasClientPay saasClientPay = new SaasClientPay();
        saasClientPay.setCreateHospitalDepartid(departmentId.toString());
        saasClientPay.setCreateHospitalId(hospitalId);
        saasClientPay.setDiaId(diaId);
        return baseMapper.findByObject(saasClientPay);
    }
 
    @Override
    public void updateTypeList(List<String> list) {
        for (String s : list) {
            SaasClientPay saasClientPay = this.baseMapper.selectById(s);
            if(saasClientPay!=null){
                saasClientPay.setType(true);
                this.saveOrUpdate(saasClientPay);
            }
        }
    }
 
    @Override
    public  List<SaasClientPayExcel> export(Map<String, Object> params) {
        List<SaasClientPay> list  =  baseMapper.findList(new Page<>(1, -1), params);
        List<SaasClientPayExcel> listVo=null;
        if (!list.isEmpty()) {
            listVo=new ArrayList<>();
            for (SaasClientPay saasClientPay : list) {
                SaasClientPayExcel vo=new SaasClientPayExcel();
                BeanCopier beanCopier = BeanCopier.create(SaasClientPay.class, SaasClientPayExcel.class, false);
                beanCopier.copy(saasClientPay,vo,null);
                Double v = Double.parseDouble(saasClientPay.getPayPrice().toString())/100;
                vo.setPayPrice(v);
                Double vc = Double.parseDouble(saasClientPay.getPayCustom().toString())/100;
                vo.setPayCustom(vc);
                Double vk = Double.parseDouble(saasClientPay.getPayKidgrow().toString())/100;
                vo.setPayKidgrow(vk);
                listVo.add(vo);
            }
        }
        return listVo;
    }
 
    /**
     * 获取微信和支付宝的二维码
     * @param saasClientPay
     * @return
     */
    @Override
    public ResultBody getCode(SaasClientPay saasClientPay,HttpServletRequest request) throws Exception {
        SaasClientPay modelByObject = findByObject(saasClientPay);
        if(modelByObject!=null){
            String str="";
            Map<String, String> wxCode = this.getWxCode(modelByObject, request);
            if(wxCode!=null){
                saasClientPay.setPrepayId(wxCode.get("prepay_id"));
                str=wxCode.get("code_url");
            }
            String aliCode = this.getaliPayCode(modelByObject);
            List<String> list=new ArrayList<>();
            list.add(str);
            list.add(aliCode);
            Map<String,Object> map=new HashMap<>();
            map.put("list",list);
            double v = Double.parseDouble(modelByObject.getPayPrice().toString())/100;
            map.put("payPrice",v);
            return ResultBody.ok().data(map);
        }
        saasClientPay.setId(UUID.randomUUID().toString().replace("-",""));
        saasClientPay.setOutTradeNo(UUID.randomUUID().toString().replace("-",""));
        PayManager payManager = new PayManager();
        payManager.setDepartmentId(Long.valueOf(saasClientPay.getCreateHospitalDepartid()));
        payManager.setHospitalId(saasClientPay.getCreateHospitalId());
        payManager.setIsDel(false);
        payManager.setEnabled(true);
        PayManager byObject = payManagerService.findByObject(payManager);
        if(byObject==null){
            return ResultBody.failed().msg("该部门或医院没有开通单次支付");
        }
 
        saasClientPay.setPayPrice(Double.valueOf(byObject.getPayPrice()).intValue());
//        saasClientPay.setPayPrice(1);
        saasClientPay.setPayKidgrow(Double.valueOf(byObject.getPayKidgrow()).intValue());
        saasClientPay.setPayCustom(Double.valueOf(byObject.getPayCustom()).intValue());
        saasClientPay.setPayStatus(0);
        String str="";
        Map<String, String> wxCode = this.getWxCode(saasClientPay, request);
        if(wxCode!=null){
            saasClientPay.setPrepayId(wxCode.get("prepay_id"));
            str=wxCode.get("code_url");
        }
        boolean b = this.saveOrUpdate(saasClientPay);
        String aliCode = this.getaliPayCode(saasClientPay);
        List<String> list=new ArrayList<>();
        list.add(str);
        list.add(aliCode);
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        double v = byObject.getPayPrice() / 100;
        map.put("payPrice",v);
        return ResultBody.ok().data(map);
    }
    /**
     * 获取微信的二维码
     * @param saasClientPay
     * @return
     */
    public  Map<String,String> getWxCode(SaasClientPay saasClientPay, HttpServletRequest request) throws Exception{
        return wxService.getCode(saasClientPay,request);
 
    }
    /**
     * 获取支付宝的二维码
     * @param saasClientPay
     * @return
     */
    public String getaliPayCode(SaasClientPay saasClientPay) throws Exception{
        return alipayService.getCode(saasClientPay);
    }
}