CallBackContorll.java 7.38 KB
Newer Older
翁国栋 committed
1 2 3
package cn.timer.api.controller.insure;

import cn.timer.api.bean.insure.CallBack;
翁国栋 committed
4
import cn.timer.api.bean.insure.InsureLog;
翁国栋 committed
5 6 7 8 9
import cn.timer.api.bean.insure.InsurePolicy;
import cn.timer.api.bean.insure.InsureUser;
import cn.timer.api.bean.yggl.YgglMainEmp;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
翁国栋 committed
10
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
翁国栋 committed
11 12
import com.google.common.collect.Maps;
import com.mysql.cj.util.StringUtils;
翁国栋 committed
13 14
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
翁国栋 committed
15
import org.apache.commons.codec.digest.DigestUtils;
翁国栋 committed
16 17
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
翁国栋 committed
18
import org.springframework.beans.factory.annotation.Value;
翁国栋 committed
19 20 21
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

翁国栋 committed
22 23 24 25 26 27
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
翁国栋 committed
28 29 30 31 32 33 34 35 36 37 38 39

/**
 * @Description TODO
 * @Author wgd
 * @Date 2022/3/3 8:45
 */
@Api(tags = "8.0回调接口")
@RestController
@Transactional
@RequestMapping(value = "/callBack/policy", produces = {"application/json"})
public class CallBackContorll {
    private static final Logger log = LoggerFactory.getLogger(CallBackContorll.class);
翁国栋 committed
40
    /*保全测试用*/
翁国栋 committed
41 42 43 44 45 46 47
    @Value("${insure.appidq}")
    private String appidq;
    @Value("${insure.secretq}")
    private String secretq;

    @Value("${BASE_API_URL}")
    private String base_api_url;
翁国栋 committed
48 49

    @PostMapping(value = "/insuredCallBack")
284718418@qq.com committed
50
    @ApiOperation(value = "6.投保申请回调", httpMethod = "POST", notes = "投保申请回调")
翁国栋 committed
51
    @ApiOperationSupport(order = 2)
翁国栋 committed
52 53
    private Map insuredCallBack(HttpServletRequest request, @RequestParam String pid, @RequestParam String sign, @RequestParam String timestamp) throws IOException {
        Map map = Maps.newHashMap();
翁国栋 committed
54 55
        map.put("status", "error");
        if (StringUtils.isNullOrEmpty(pid) || StringUtils.isNullOrEmpty(sign) && StringUtils.isNullOrEmpty(timestamp)) {
翁国栋 committed
56 57
            return map;
        }
翁国栋 committed
58
        if (!pid.equals(appidq)) {
翁国栋 committed
59 60 61 62 63 64
            return map;
        }
        InputStream is = null;
        is = request.getInputStream();
        StringBuilder sb = new StringBuilder();
        byte[] b = new byte[4096];
翁国栋 committed
65
        for (int n; (n = is.read(b)) != -1; ) {
翁国栋 committed
66 67 68
            sb.append(new String(b, 0, n));
        }
        String value = DigestUtils.md5Hex(appidq + secretq + timestamp + sb.toString());
翁国栋 committed
69
        if (!value.equals(sign)) {
翁国栋 committed
70 71
            return map;
        }
翁国栋 committed
72
        CallBack callBack = JSONObject.parseObject(sb.toString(), CallBack.class);
翁国栋 committed
73
        Map trueMap = Maps.newHashMap();
翁国栋 committed
74
        trueMap.put("status", "1");
翁国栋 committed
75
        return trueMap;
翁国栋 committed
76 77 78
    }

    @PostMapping(value = "/CallBack")
284718418@qq.com committed
79
    @ApiOperation(value = "7.保全增员申请回调", httpMethod = "POST", notes = "投保申请回调")
翁国栋 committed
80 81 82
    @ApiOperationSupport(order = 2)
    private Map callBack(HttpServletRequest request, @RequestParam String pid, @RequestParam String sign, @RequestParam String timestamp) throws IOException {
        Map map = Maps.newHashMap();
翁国栋 committed
83 84
        map.put("status", "error");
        if (StringUtils.isNullOrEmpty(pid) || StringUtils.isNullOrEmpty(sign) && StringUtils.isNullOrEmpty(timestamp)) {
翁国栋 committed
85 86
            return map;
        }
翁国栋 committed
87
        if (!pid.equals(appidq)) {
翁国栋 committed
88 89 90
            return map;
        }
        InputStream is = null;
翁国栋 committed
91 92 93 94 95 96
        is = request.getInputStream();
        StringBuilder sb = new StringBuilder();
        byte[] b = new byte[4096];
        for (int n; (n = is.read(b)) != -1; ) {
            sb.append(new String(b, 0, n));
        }
翁国栋 committed
97
        String value = DigestUtils.md5Hex(appidq + secretq + timestamp + sb.toString());
翁国栋 committed
98
        if (!value.equals(sign)) {
翁国栋 committed
99 100
            return map;
        }
翁国栋 committed
101
        Map paramsMap = Maps.newHashMap();
翁国栋 committed
102 103 104 105 106 107 108
        paramsMap.put("pid", pid);
        paramsMap.put("timestamp", timestamp);
        paramsMap.put("sign", sign);
        CallBack callBack = JSONObject.parseObject(sb.toString(), CallBack.class);
        log.info("callBack=====" + sb.toString());
        if (callBack.getStatus().equals("1")) {
            List<InsureUser> list = InsureUser.builder().build().selectList(new QueryWrapper<InsureUser>().lambda().eq(InsureUser::getBatchNo, callBack.getOrder_import_info().getUuid()).eq(InsureUser::getInsureStatus, 3));
翁国栋 committed
109 110 111 112 113 114
            list.forEach(i -> {
                i.setInsureStatus(1);
                i.updateById();
                YgglMainEmp.builder().isInsure(1).build().update(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getId, i.getUserId()));
            });
            List<InsureUser> oldlist = InsureUser.builder().build().selectList(new QueryWrapper<InsureUser>().lambda().eq(InsureUser::getReplaceTransId, callBack.getOrder_import_info().getUuid()));
翁国栋 committed
115
            if (oldlist != null && oldlist.size() > 0) {
翁国栋 committed
116 117
                oldlist.forEach(i -> {
                    i.setInsureStatus(4);
翁国栋 committed
118
                    i.setStatus("2");
翁国栋 committed
119
                    i.updateById();
翁国栋 committed
120
                    YgglMainEmp.builder().isInsure(0).build().update(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getId, i.getUserId()));
翁国栋 committed
121 122
                });
            }
翁国栋 committed
123 124
            InsurePolicy insurePolicy = InsurePolicy.builder().build().selectOne(new QueryWrapper<InsurePolicy>().lambda().eq(InsurePolicy::getPolicyNo, list.get(0).getPolicyNo()));
            if (oldlist == null || oldlist.size() == 0) {
翁国栋 committed
125 126
                insurePolicy.setTotalPremium(String.valueOf(Double.valueOf(insurePolicy.getTotalPremium()) + Double.valueOf(callBack.getOrder_import_info().getTotal_money())));
            }
翁国栋 committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            insurePolicy.setPolicyFile(callBack.getOrder_import_info().getEndorsement_file());
            insurePolicy.setUpdateTime(new Date());
            insurePolicy.updateById();
            InsureLog.builder().requestParam(JSONObject.toJSONString(paramsMap)).type(7)
                    .requestData(sb.toString()).createTime(new Date()).requestType(1).returnBody(JSONObject.toJSONString(callBack)).requestPath(base_api_url + "/callBack/policy/CallBack")
                    .returnCode(callBack.getStatus()).returnMsg("更新成功").policyId(insurePolicy.getId()).build().insert();
        } else {
            String errorMsg = "";
            InsureLog insureLog = InsureLog.builder().build().selectOne(new QueryWrapper<InsureLog>().lambda().eq(InsureLog::getTransId, callBack.getOrder_import_info().getThird_uuid()));
            List<Map> errMap = callBack.getOrder_import_info().getErr_list();
            if (errMap.size() > 0) {
                for (int i = 0; i < errMap.size(); i++) {
                    errorMsg = errorMsg + ("姓名:"+errMap.get(i).get("name").toString()+",错误:"+errMap.get(i).get("err_content").toString() + ',');
                }
            } else {
                errorMsg = callBack.getErr_msg();
            }
翁国栋 committed
144
            //TODO 写入日志
翁国栋 committed
145 146 147
            InsureLog.builder().requestParam(JSONObject.toJSONString(paramsMap)).type(7)
                    .requestData(sb.toString()).createTime(new Date()).requestType(1).returnBody(JSONObject.toJSONString(callBack)).requestPath(base_api_url + "/callBack/policy/CallBack")
                    .returnCode(callBack.getStatus()).returnMsg(errorMsg).policyId(insureLog.getPolicyId()).build().insert();
翁国栋 committed
148 149
        }
        Map trueMap = Maps.newHashMap();
翁国栋 committed
150 151
        trueMap.put("status", "1");

翁国栋 committed
152
        return trueMap;
翁国栋 committed
153 154
    }
}