InsureContorll.java 68.5 KB
Newer Older
翁国栋 committed
1 2
package cn.timer.api.controller.insure;

翁国栋 committed
3
import cn.timer.api.bean.insure.*;
翁国栋 committed
4
import cn.timer.api.bean.kqmk.KqglAssoLeaveRules;
翁国栋 committed
5 6 7 8
import cn.timer.api.bean.qyzx.QyzxEntInfoM;
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
9
import cn.timer.api.controller.insure.enums.PlanEnum;
翁国栋 committed
10
import cn.timer.api.dao.insure.InsureLogMapper;
翁国栋 committed
11 12 13 14
import cn.timer.api.dao.insure.InsurePolicyMapper;
import cn.timer.api.dao.insure.InsureUserMapper;
import cn.timer.api.dao.yggl.YgglMainEmpMapper;
import cn.timer.api.dto.insure.InsureDto;
翁国栋 committed
15
import cn.timer.api.dto.insure.InsureUserDto;
翁国栋 committed
16
import cn.timer.api.dto.insure.PlansDto;
翁国栋 committed
17
import cn.timer.api.dto.insure.PolicyDto;
翁国栋 committed
18 19
import cn.timer.api.utils.*;
import com.alibaba.druid.util.Base64;
翁国栋 committed
20
import com.aliyun.oss.common.utils.StringUtils;
翁国栋 committed
21
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
翁国栋 committed
22
import com.beust.jcommander.internal.Lists;
翁国栋 committed
23
import com.google.common.base.Strings;
翁国栋 committed
24
import com.google.common.collect.Maps;
25
import com.google.gson.JsonObject;
翁国栋 committed
26 27 28
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
翁国栋 committed
29 30 31 32
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
翁国栋 committed
33 34
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.jetty.util.StringUtil;
翁国栋 committed
35 36
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
翁国栋 committed
37
import org.springframework.beans.factory.annotation.Autowired;
284718418@qq.com committed
38
import org.springframework.beans.factory.annotation.Value;
翁国栋 committed
39 40 41 42 43
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
翁国栋 committed
44 45 46
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
翁国栋 committed
47 48
import java.text.ParseException;
import java.text.SimpleDateFormat;
翁国栋 committed
49 50
import java.time.LocalDate;
import java.time.LocalDateTime;
翁国栋 committed
51
import java.time.ZoneId;
翁国栋 committed
52 53
import java.time.format.DateTimeFormatter;
import java.util.*;
翁国栋 committed
54
import java.util.stream.Collectors;
翁国栋 committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Description TODO
 * @Author wgd
 * @Date 2022/3/1 11:34
 */
@Api(tags = "8.0保险列表")
@RestController
@Transactional
@RequestMapping(value = "/insure", produces = {"application/json"})
public class InsureContorll {
284718418@qq.com committed
72 73 74

    @Value("${BASE_API_URL}")
    private String base_api_url;
翁国栋 committed
75
    private static final Logger log = LoggerFactory.getLogger(InsureContorll.class);
翁国栋 committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    @Value("${insure.appid}")
    private String appid;
    @Value("${insure.secret}")
    private String secret;
    @Value("${insure.appidq}")
    private String appidq;
    @Value("${insure.secretq}")
    private String secretq;
    @Value("${insure.uploadUrl}")
    private String uploadUrl;
    @Value("${insure.insuredUrl}")
    private String insuredUrl;
    @Value("${insure.uploadUrlq}")
    private String uploadUrl2;
    @Value("${insure.batchUrl}")
    private String batchUrl;
93 94
    @Value("${insure.policyDetail}")
    private String policyDetailUrl;
翁国栋 committed
95

翁国栋 committed
96
    private static SimpleDateFormat dtf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
翁国栋 committed
97

翁国栋 committed
98 99 100 101 102 103 104 105 106
    @Autowired
    private YgglMainEmpMapper ygglMainEmpMapper;

    @Autowired
    private InsureUserMapper insureUserMapper;

    @Autowired
    private InsurePolicyMapper insurePolicyMapper;

翁国栋 committed
107 108 109
    @Autowired
    private InsureLogMapper insureLogMapper;

翁国栋 committed
110 111 112

    /**
     * 设置请求参数
翁国栋 committed
113
     *
翁国栋 committed
114 115 116
     * @param sign
     * @return
     */
翁国栋 committed
117
    private Map setParams(String sign, String appid, String secret) {
翁国栋 committed
118
        /*当前时间戳*/
翁国栋 committed
119 120
        long timestamp = System.currentTimeMillis() / 1000;
        log.info("时间戳" + timestamp);
翁国栋 committed
121
        Map paramsMap = Maps.newHashMap();
翁国栋 committed
122 123 124
        paramsMap.put("pid", appid);
        paramsMap.put("timestamp", String.valueOf(timestamp));
        paramsMap.put("trace_id", appid + timestamp + new Random().nextInt((9999 - 100) + 1) + 10);
翁国栋 committed
125 126
//        paramsMap.put("sign",Md5.md5(appid+secret+timestamp+sign.trim()));

翁国栋 committed
127 128
        String value = appid + secret + timestamp + sign;
        log.info("body参数======" + sign);
翁国栋 committed
129
        paramsMap.put("sign", DigestUtils.md5Hex(value));
翁国栋 committed
130
        log.info("params参数======" + JSONObject.toJSONString(paramsMap));
翁国栋 committed
131 132 133 134
        return paramsMap;
    }

    @PostMapping(value = "/insured_add")
135
    @ApiOperation(value = "1.投保申请", httpMethod = "POST", notes = "投保申请")
翁国栋 committed
136
    public Result<Object> insured_add(@RequestBody InsureDto insureDto) throws ParseException {
翁国栋 committed
137
        InsureProduct insureProduct = InsureProduct.builder().id(Integer.parseInt(insureDto.getProductId())).build().selectById();
翁国栋 committed
138
        if (insureProduct == null) {
翁国栋 committed
139 140
            return ResultUtil.error("产品不存在");
        }
翁国栋 committed
141
        QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(Integer.parseInt(insureDto.getOrgCode())).build().selectById(); // 企业信息
翁国栋 committed
142 143
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime now = LocalDateTime.now();
翁国栋 committed
144
        Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
翁国栋 committed
145 146
        Map bodyMap = Maps.newHashMap();
        ArrayList<Map> quotationsArry = new ArrayList<Map>();
翁国栋 committed
147 148
        bodyMap.put("is_ss", 0);
        Map quotations = Maps.newHashMap();
翁国栋 committed
149 150 151
        insureDto.getPlans().forEach(i -> {
            i.setPrice(getPrice(i.getBenefitBasicPlan(), i.getBenefitOccupationCategory()));
            insureDto.setTotalPrice(i.getPrice() * i.getUserIds().length + (insureDto.getTotalPrice() == null ? 0 : insureDto.getTotalPrice()));
翁国栋 committed
152
        });
翁国栋 committed
153 154 155 156
        quotations.put("trans_id", "MB_" + dtf2.format(now));/*报价请求流水号*/
        quotations.put("applicant_type", "2");/*投保人类型:2-企业*/
        quotations.put("quotation_type", 3);/*报价类型:3-投保申请*/
        quotations.put("currency", "5");/*币种:5人民币*/
翁国栋 committed
157

翁国栋 committed
158 159 160 161 162 163
        quotations.put("source_type", "");//*币种:5人民币*//*
        quotations.put("cps_email", "");//*币种:5人民币*//*
        quotations.put("cps_tel", "");//*币种:5人民币*//*
        quotations.put("last_serial_number", "");//*币种:5人民币*//*
        quotations.put("from_source", "");//*币种:5人民币*//*
        quotations.put("operator_name", "");//*币种:5人民币
翁国栋 committed
164

翁国栋 committed
165 166
        /*企业信息*/
        InsureApplicant insureApplicant = InsureApplicant.builder().id(1).build().selectById();
翁国栋 committed
167
        if (insureApplicant == null) {
翁国栋 committed
168 169 170
            return ResultUtil.error("未设置投保人");
        }
        Map companyApplicantMap = Maps.newHashMap();
翁国栋 committed
171 172
        companyApplicantMap.put("applicant_e_name", insureApplicant.getApplicantEName());/*企业/网点名称*/
        companyApplicantMap.put("applicant_e_address", insureApplicant.getApplicantEAddress());/*企业/网点地址*/
翁国栋 committed
173 174 175 176 177
        companyApplicantMap.put("applicant_e_contacts", insureApplicant.getApplicantEContacts());/*企业授权操作人姓名*/
        companyApplicantMap.put("applicant_e_phone", insureApplicant.getApplicantEPhone());/*操作人的联系电话*/
        companyApplicantMap.put("applicant_e_no_type", insureApplicant.getApplicantENoType());/*企业证件类型*/
        companyApplicantMap.put("applicant_e_no", insureApplicant.getApplicantENo());/*企业证件号码*/
        companyApplicantMap.put("applicant_e_mail", insureApplicant.getApplicantEMail());/*企业邮箱*/
翁国栋 committed
178
        companyApplicantMap.put("applicant_province_city", "21024");/*省市区*/
翁国栋 committed
179
        /*人员清单填被保人员*/
翁国栋 committed
180
        String url = creaXSSFWorkbook(insureDto.getPlans(), insureDto.getPolicyDateStart());
翁国栋 committed
181
        if (!StringUtil.isNotBlank(url)) {
翁国栋 committed
182 183
            return ResultUtil.error("文件错误");
        }
翁国栋 committed
184 185
        companyApplicantMap.put("applicant_employee_list", url);
        quotations.put("company_applicant", companyApplicantMap);
翁国栋 committed
186 187


翁国栋 committed
188
        Map basicMap = Maps.newHashMap();
翁国栋 committed
189
        basicMap.put("partner_pid", appid);/*分销商pid*/
翁国栋 committed
190 191
        basicMap.put("plan_code_id", insureProduct.getPlanCodeId());/*计划代码,选项值:61b85207b73d2ab09fb2959 (生产环境) 61b85207b73d2ab09fb2959 (沙箱环境)*/
        basicMap.put("product_code_id", insureProduct.getProductCodeId());/*产品代码,选项值:61b85207a9f87da19fb5986 (生产环境) 61b85207a9f87da19fb5986 (沙箱环境)*/
翁国栋 committed
192
        basicMap.put("policy_date_start", insureDto.getPolicyDateStart());
翁国栋 committed
193
        basicMap.put("policy_date_end", insureDto.getPolicyDateEnd());
翁国栋 committed
194
        basicMap.put("channel", "");
翁国栋 committed
195 196 197 198
        basicMap.put("fee", "");
        basicMap.put("partner_user_rate", "");
        basicMap.put("promotion_fee", "");
        basicMap.put("agent_id", "");
翁国栋 committed
199
        basicMap.put("policy_insure_notify", "");
翁国栋 committed
200
        basicMap.put("promotion_id", 0);
翁国栋 committed
201
        basicMap.put("policy_number_last_year", "");
翁国栋 committed
202 203
        quotations.put("basic", basicMap);
        quotations.put("individual_applicant", Maps.newHashMap());
翁国栋 committed
204

翁国栋 committed
205

翁国栋 committed
206 207 208 209


        /*被保人员 填被保企业信息*/
        ArrayList<Map> insuredArray = new ArrayList<>();
翁国栋 committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        insureDto.getPlans().forEach(p -> {
            Map insured = Maps.newHashMap();
            insured.put("trans_id", "PI_" + qyzxEntInfoM.getId() + "_" + dtf2.format(now) + "_" + UUID.randomUUID().toString().replace("-", ""));
            insured.put("price", p.getUserIds().length * p.getPrice());/*根据基础计划生成*/
            insured.put("insured_applicant_relations", "36960");
            insured.put("insured_mobile", insureApplicant.getApplicantEPhone());
            insured.put("insured_e_contact", insureApplicant.getApplicantEContacts());
            insured.put("insured_name", qyzxEntInfoM.getName());
            insured.put("insured_no_type", "3306");
            insured.put("insured_no", qyzxEntInfoM.getCreditCode());
            insured.put("insured_zm_openid", "");
            insured.put("benefit_basic_plan", p.getBenefitBasicPlan());/*基础计划写死*/
            insured.put("benefit_occupation_category", p.getBenefitOccupationCategory());
            insured.put("benefit_EL_employee_number_t", p.getUserIds().length);
            insuredArray.add(insured);
翁国栋 committed
225
        });
翁国栋 committed
226 227
        quotations.put("insured", insuredArray);
        quotations.put("file", Lists.newArrayList());
翁国栋 committed
228 229 230
        quotations.put("specifically_agreed_file", Lists.newArrayList());
        quotations.put("policy_no", "");
        quotationsArry.add(quotations);
翁国栋 committed
231
        bodyMap.put("quotations", quotationsArry);
翁国栋 committed
232
        log.info(JSONObject.toJSONString(bodyMap));
翁国栋 committed
233 234
        String data = HttpUtils.sendPost(insuredUrl, setParams(JSONObject.toJSONString(bodyMap), appid, secret), bodyMap);
        Map dataMap = JSONObject.parseObject(data, Map.class);
翁国栋 committed
235
        if (dataMap != null) {
翁国栋 committed
236 237 238 239 240 241 242 243
            if ((dataMap.get("errcode").toString().equals("suc")) || (dataMap.get("errcode").toString().equals("e25"))) {
                Map policyMap = Maps.newHashMap();
                List<Map> batchMap = Lists.newArrayList();
                if (dataMap.get("errcode").toString().equals("suc")) {
                    Object o = dataMap.get("data");
                    Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(o));
                    policyMap = JSONObject.parseObject(JSONObject.toJSONString(map.get("policy")), Map.class);
                    batchMap = JSONObject.parseObject(JSONObject.toJSONString(policyMap.get("batch")), List.class);
翁国栋 committed
244
                }
翁国栋 committed
245
                InsurePolicy insurePolicy = InsurePolicy.builder().build();
翁国栋 committed
246 247 248 249 250 251
                insurePolicy.setPolicyDateStart(dtf3.parse(insureDto.getPolicyDateStart()));
                insurePolicy.setPolicyDateEnd(dtf3.parse(insureDto.getPolicyDateEnd()));
                if (dataMap.get("errcode").toString().equals("suc")) {
                    insurePolicy.setPolicyNo(policyMap.get("policy_no").toString());
                    insurePolicy.setPolicyFile(policyMap.get("policy_file").toString());
                    insurePolicy.setKitUrl(policyMap.get("kit_url").toString());
翁国栋 committed
252
                }
翁国栋 committed
253 254 255 256 257 258
                insurePolicy.setType(2);/*年单或者月单*/
                insurePolicy.setCurrency("5");
                if (dataMap.get("errcode").toString().equals("suc")) {
                    insurePolicy.setStatus("1");
                } else {
                    insurePolicy.setStatus("2");
翁国栋 committed
259
                }
翁国栋 committed
260 261 262 263
                insurePolicy.setOrgCode(qyzxEntInfoM.getId());
                insurePolicy.setCreateTime(date);
                insurePolicy.setUpdateTime(date);
                insurePolicy.setSchemeName(insureProduct.getName());/*先写死*/
翁国栋 committed
264
                insurePolicy.setTotalPremium(String.valueOf(insureDto.getTotalPrice()));
翁国栋 committed
265 266 267 268 269
                insurePolicy.setProductId(insureProduct.getId());
                insurePolicy.insert();
                InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(1)
                        .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(JSONObject.toJSONString(dataMap)).requestPath(insuredUrl)
                        .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(insurePolicy.getId()).fileUrl(url).build().insert();
翁国栋 committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
                for (PlansDto p : insureDto.getPlans()) {
                    List<YgglMainEmp> ygglMainEmpList = ygglMainEmpMapper.selectListByIds(p.getUserIds());
                    InsureUser insureUser;
                    for (YgglMainEmp y : ygglMainEmpList) {
                        insureUser = InsureUser.builder().build();
                        insureUser.setUserId(y.getId());
                        insureUser.setInsuredName(qyzxEntInfoM.getName());
                        insureUser.setPrice(String.valueOf(p.getPrice()));/*前端获取的保费*/
                        insureUser.setTransId("P_" + qyzxEntInfoM.getId() + "_" + dtf2.format(now));
                        insureUser.setPremium(String.valueOf(insureDto.getTotalPrice()));
                        if (dataMap.get("errcode").toString().equals("suc")) {
                            insureUser.setBatchNo(batchMap.get(0).get("batch_no").toString());
                            insureUser.setPolicyNo(policyMap.get("policy_no").toString());
                        }
                        insureUser.setBenefitBasicPlan(p.getBenefitBasicPlan());
                        insureUser.setUserId(y.getId());
                        insureUser.setOrgCode(y.getOrgCode());
                        insureUser.setBenefitOccupationCategory(p.getBenefitOccupationCategory());
                        insureUser.setInsuredMobile(y.getPhone());
                        insureUser.setInsuredNo(y.getZjNum());
                        insureUser.setInsuredEContact(y.getName());
                        insureUser.setApplicantEmployeeList(url);
                        insureUser.setTricycleFrameNumber(p.getTricycleFrameNumber());
                        if (dataMap.get("errcode").toString().equals("suc")) {
                            insureUser.setInsureStatus(1);
                            insureUser.setStatus("1");
                        } else {
                            insureUser.setInsureStatus(3);
                            insureUser.setStatus("2");
                        }
                        insureUser.setPolicyDateStart(dtf3.parse(insureDto.getPolicyDateStart()));
                        insureUser.setPolicyDateEnd(dtf3.parse(insureDto.getPolicyDateEnd()));
                        insureUser.setApplyType(2);//投保类型
                        insureUser.setPolicyId(insurePolicy.getId());

                        insureUser.insert();
                        if (dataMap.get("errcode").toString().equals("suc")) {
                            y.setIsInsure(1);
                            y.insertOrUpdate();
翁国栋 committed
309
                        }
翁国栋 committed
310
                    }
翁国栋 committed
311
                }
翁国栋 committed
312
                return ResultUtil.success(dataMap.get("errmsg").toString());
翁国栋 committed
313
            }
翁国栋 committed
314
            return ResultUtil.error(dataMap.get("errmsg").toString());
翁国栋 committed
315
        }
翁国栋 committed
316
        return ResultUtil.error("网络异常");
翁国栋 committed
317

翁国栋 committed
318
    }
翁国栋 committed
319

翁国栋 committed
320
    /**
翁国栋 committed
321
     * 生成投保文件
翁国栋 committed
322
     *
翁国栋 committed
323 324 325
     * @param ids
     * @return
     */
翁国栋 committed
326 327
    public String creaXSSFWorkbook(List<PlansDto> plansList, String createDate) {
        if (plansList.size() <= 0) {
翁国栋 committed
328
            return "error";
翁国栋 committed
329
        }
翁国栋 committed
330
        LocalDate localDate = LocalDate.now();
翁国栋 committed
331 332
        Date date = new Date();
        List list = Lists.newArrayList();
翁国栋 committed
333
        plansList.forEach(p -> {
翁国栋 committed
334 335
            List<YgglMainEmp> ygglMainEmpList = ygglMainEmpMapper.selectListByIds(p.getUserIds());
            for (YgglMainEmp y : ygglMainEmpList) {
翁国栋 committed
336
                String[] arr = new String[]{String.valueOf(y.getId()), y.getName(), "身份证", y.getZjNum(), PlanEnum.getEnum(p.getBenefitBasicPlan(), p.getBenefitOccupationCategory()).getName(), createDate, "", StringUtils.isNullOrEmpty(p.getTricycleFrameNumber()) ? "无" : p.getTricycleFrameNumber(), PlanEnum.getEnum(p.getBenefitBasicPlan(), p.getBenefitOccupationCategory()).getType()};
翁国栋 committed
337 338 339
                list.add(arr);
            }
        });
翁国栋 committed
340 341
        String[] rowName = new String[]{"ID", "name", "ID_type", "ID_number", "Scheme_name", "Date_start", "Branch", "Tricycle_frame_number", "benefit_occupation_category"};
        XSSFWorkbook xssfWorkbook = ExcelUtils.exportExcel(localDate + "_投保" + list.size() + "名人员清单.xlsx", rowName, list);
翁国栋 committed
342

翁国栋 committed
343
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
翁国栋 committed
344
        String url = "";
翁国栋 committed
345
        try {
翁国栋 committed
346 347
            xssfWorkbook.write(byteArrayOutputStream);
            byte[] bytes = byteArrayOutputStream.toByteArray();
翁国栋 committed
348
            String fileBase64 = Base64.byteArrayToBase64(bytes);
翁国栋 committed
349 350

            /*body参数 begin*/
翁国栋 committed
351 352 353 354
            Map bodyMap = Maps.newHashMap();
            bodyMap.put("app", "mall");
            bodyMap.put("name", localDate + "_" + list.size() + "名人员清单.xlsx");
            bodyMap.put("file", fileBase64);
翁国栋 committed
355
            /*end*/
翁国栋 committed
356 357 358
            String data = HttpUtils.sendPost(uploadUrl, setParams(JSONObject.toJSONString(bodyMap), appid, secret), bodyMap);
            Map<String, Object> dataMap = JSONObject.parseObject(data);
            /*创建日志*/
翁国栋 committed
359
            if (dataMap.get("errcode").toString().equals("suc")) {
翁国栋 committed
360 361
                Object o = dataMap.get("data");
                Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(o));
翁国栋 committed
362
                url = map.get("url").toString();
翁国栋 committed
363
            }
翁国栋 committed
364 365 366
            InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(4)
                    .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(data).requestPath(uploadUrl)
                    .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).fileUrl(url).build().insert();
翁国栋 committed
367
            /*文件流base64*/
翁国栋 committed
368 369
        } catch (IOException e) {
            e.printStackTrace();
翁国栋 committed
370
        } finally {
翁国栋 committed
371
            try {
翁国栋 committed
372 373
                xssfWorkbook.close();
                byteArrayOutputStream.close();
翁国栋 committed
374 375 376 377
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
翁国栋 committed
378

翁国栋 committed
379
        return url;
翁国栋 committed
380

翁国栋 committed
381
    }
翁国栋 committed
382

翁国栋 committed
383

翁国栋 committed
384
    @PostMapping(value = "/policyListAdmin")
385
    @ApiOperation(value = "4.保单列表--运营后台端", httpMethod = "POST", notes = "保单列表--运营后台端")
翁国栋 committed
386
    public Result<Object> policyListAdmin(@RequestBody PolicyDto policyDto) {
翁国栋 committed
387
        Map map = Maps.newHashMap();
翁国栋 committed
388
        List<PolicyDto> list = insurePolicyMapper.policyList(policyDto);
翁国栋 committed
389
        map.put("list", list);
翁国栋 committed
390
        map.put("total", insurePolicyMapper.totalPolicy(policyDto));
翁国栋 committed
391
        return ResultUtil.data(map);
翁国栋 committed
392
    }
翁国栋 committed
393

翁国栋 committed
394
    @GetMapping(value = "/policyDetail")
翁国栋 committed
395
    @ApiOperation(value = "5.保单信息", httpMethod = "GET", notes = "保单信息")
翁国栋 committed
396
    public Result<Object> policyDetail(@RequestParam("policyId") Integer policyId) {
翁国栋 committed
397
//        Map map = Maps.newHashMap();
翁国栋 committed
398
        PolicyDto policyDto = insurePolicyMapper.getPolicyDetail(policyId);
翁国栋 committed
399 400 401 402
        /*获取可选计划*/
//        List<InsureUser> plansList = InsureUser.builder().build().selectList(new QueryWrapper<InsureUser>().lambda().eq(InsureUser::getApplyType,2).eq(InsureUser::getPolicyId,policyId).groupBy(InsureUser::getBenefitBasicPlan));
        /*获取可选类型*/
//        List<InsureUser> categoryList = InsureUser.builder().build().selectList(new QueryWrapper<InsureUser>().lambda().eq(InsureUser::getApplyType,2).eq(InsureUser::getPolicyId,policyId).groupBy(InsureUser::getBenefitOccupationCategory));
翁国栋 committed
403
        if (policyDto == null) {
翁国栋 committed
404 405
            return ResultUtil.error("保单不存在");
        }
翁国栋 committed
406 407 408
//        map.put("policy",policyDto);
//        map.put("plans",plansList.stream().map(InsureUser::getBenefitBasicPlan).toArray());
//        map.put("category",categoryList.stream().map(InsureUser::getBenefitOccupationCategory).toArray());
翁国栋 committed
409 410
        return ResultUtil.data(policyDto);
    }
翁国栋 committed
411

翁国栋 committed
412
    @PostMapping(value = "/joinUser")
413
    @ApiOperation(value = "2.增员", httpMethod = "POST", notes = "增员")
翁国栋 committed
414 415 416 417
    public Result<Object> joinUser(@RequestBody InsureDto insureDto) throws ParseException {

        InsurePolicy insurePolicy = InsurePolicy.builder().id(Integer.parseInt(insureDto.getId())).build().selectById();
        if (insurePolicy == null) {
翁国栋 committed
418 419
            return ResultUtil.error("保单不存在");
        }
翁国栋 committed
420
        LocalDateTime now = LocalDateTime.now();
翁国栋 committed
421
        String url = creaXSSFWorkbook1(insureDto);
翁国栋 committed
422
        log.info("保全增员文件地址", url);
翁国栋 committed
423 424
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        Map bodyMap = Maps.newHashMap();
翁国栋 committed
425 426 427 428 429 430
        bodyMap.put("contract_no", insurePolicy.getPolicyNo());
        bodyMap.put("order_status", "1");
        bodyMap.put("callback_plan", "");
        bodyMap.put("file_url", url);
        bodyMap.put("from_source", "API");
        bodyMap.put("import_review_callback", "");
翁国栋 committed
431
        bodyMap.put("import_callback", base_api_url + "/callBack/policy/CallBack");
翁国栋 committed
432 433 434 435 436 437 438 439
        bodyMap.put("third_uuid", "IS_" + insurePolicy.getOrgCode() + "_" + dtf2.format(now));
        String responData = HttpUtils.sendPost(batchUrl, setParams(JSONObject.toJSONString(bodyMap), appidq, secretq), bodyMap);
        Map<String, Object> dataMap = JSONObject.parseObject(responData);
        if (dataMap.get("errcode").toString().equals("suc")) {
            Map<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(dataMap.get("data")));
            Map<String, Object> orderImportInfo = JSONObject.parseObject(JSONObject.toJSONString(data.get("order_import_info")));
            String status = orderImportInfo.get("status").toString();
            if (status.equals("5") || status.equals("6") || status.equals("7")) {
翁国栋 committed
440 441
                InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(2)
                        .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(responData).requestPath(batchUrl)
翁国栋 committed
442
                        .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(insurePolicy.getId()).transId("IS_" + insurePolicy.getOrgCode() + "_" + dtf2.format(now)).fileUrl(data.get("file_url").toString()).build().insert();
翁国栋 committed
443 444
                return ResultUtil.error("信息填写错误,请查看日志");
            } else {
翁国栋 committed
445 446 447 448 449 450 451
                List<YgglMainEmp> ygglMainEmpList = ygglMainEmpMapper.selectListByIds(insureDto.getUserIds());
                QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(Integer.parseInt(insureDto.getOrgCode())).build().selectById();
                ygglMainEmpList.forEach(y -> {
                    try {
                        InsureUser.builder()
                                .transId("IS_" + insurePolicy.getOrgCode() + "_" + dtf2.format(now))
                                .insuredName(qyzxEntInfoM.getName())
452 453
                                .price(String.valueOf(PlanEnum.getEnum(insureDto.getBenefitBasicPlan(), insureDto.getBenefitOccupationCategory()).getPrice()))/*前端获取的保费*/
                                .premium(String.valueOf(PlanEnum.getEnum(insureDto.getBenefitBasicPlan(), insureDto.getBenefitOccupationCategory()).getPrice() * insureDto.getUserIds().length))
翁国栋 committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
                                .batchNo(data.get("import_uuid").toString())
                                .benefitBasicPlan(insureDto.getBenefitBasicPlan())
                                .policyNo(orderImportInfo.get("contract_no").toString())
                                .userId(y.getId())
                                .orgCode(y.getOrgCode())
                                .insuredMobile(y.getPhone())
                                .benefitOccupationCategory(insureDto.getBenefitOccupationCategory())
                                .insuredNo(y.getZjNum())
                                .tricycleFrameNumber(insureDto.getTricycleFrameNumber())
                                .insuredEContact(y.getName())
                                .applyType(1)
                                .applicantEmployeeList(data.get("file_url").toString())
                                .policyDateStart(dtf3.parse(insureDto.getPolicyDateStart()))
                                .policyDateEnd(dtf3.parse(insureDto.getPolicyDateEnd()))
                                .insureStatus(3).policyId(insurePolicy.getId()).build().insert();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    y.setIsInsure(1);
                    y.insertOrUpdate();
                });
翁国栋 committed
475 476 477
                InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(2)
                        .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(responData).requestPath(batchUrl)
                        .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(insurePolicy.getId()).fileUrl(url).build().insert();
翁国栋 committed
478 479 480
                return ResultUtil.data("核保成功,等待系统更新");
            }
        }
翁国栋 committed
481 482 483 484
        InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(2)
                .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(responData).requestPath(batchUrl)
                .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(insurePolicy.getId()).build().insert();
        return ResultUtil.error(dataMap.get("errmsg").toString());
翁国栋 committed
485 486 487 488
    }

    /**
     * 生成保全增员文件
翁国栋 committed
489
     *
翁国栋 committed
490 491 492
     * @param ids
     * @return
     */
翁国栋 committed
493 494
    public String creaXSSFWorkbook1(InsureDto insureDto) {
        if (insureDto.getUserIds().length <= 0) {
翁国栋 committed
495 496 497
            return "error";
        }
        LocalDate localDate = LocalDate.now();
翁国栋 committed
498
        List<YgglMainEmp> ygglMainEmpList = ygglMainEmpMapper.selectListByIds(insureDto.getUserIds());
翁国栋 committed
499 500
        List list = Lists.newArrayList();
        for (YgglMainEmp y : ygglMainEmpList) {
翁国栋 committed
501
            String[] arr = new String[]{String.valueOf(y.getId()), y.getName(), "身份证", y.getZjNum(), PlanEnum.getEnum(insureDto.getBenefitBasicPlan(), insureDto.getBenefitOccupationCategory()).getName(), insureDto.getPolicyDateStart(), "", StringUtils.isNullOrEmpty(insureDto.getTricycleFrameNumber()) ? "无" : insureDto.getTricycleFrameNumber(), PlanEnum.getEnum(insureDto.getBenefitBasicPlan(), insureDto.getBenefitOccupationCategory()).getType()};
翁国栋 committed
502 503
            list.add(arr);
        }
翁国栋 committed
504
        String[] rowName = new String[]{"ID", "name", "ID_type", "ID_number", "Scheme_name", "Date_start", "Branch", "Tricycle_frame_number", "benefit_occupation_category"};
翁国栋 committed
505

翁国栋 committed
506
        XSSFWorkbook xssfWorkbook = ExcelUtils.exportExcel(localDate + "_补充" + list.size() + "名人员清单.xlsx", rowName, list);
翁国栋 committed
507 508

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
翁国栋 committed
509
        String url = "";
翁国栋 committed
510 511 512
        try {
            xssfWorkbook.write(byteArrayOutputStream);
            byte[] bytes = byteArrayOutputStream.toByteArray();
翁国栋 committed
513
            String fileBase64 = Base64.byteArrayToBase64(bytes);
翁国栋 committed
514 515

            /*body参数 begin*/
翁国栋 committed
516 517
            Map bodyMap = Maps.newHashMap();
            bodyMap.put("media", fileBase64);
翁国栋 committed
518
            /*end*/
翁国栋 committed
519 520
            String data = HttpUtils.sendPost(uploadUrl2, setParams(JSONObject.toJSONString(bodyMap), appidq, secretq), bodyMap);
            Map<String, Object> dataMap = JSONObject.parseObject(data);
翁国栋 committed
521
            if (dataMap.get("errcode").toString().equals("suc")) {
翁国栋 committed
522 523
                Object o = dataMap.get("data");
                Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(o));
翁国栋 committed
524
                url = map.get("file_url").toString();
翁国栋 committed
525
            }
翁国栋 committed
526 527 528
            InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(5)
                    .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(data).requestPath(uploadUrl2)
                    .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).fileUrl(url).build().insert();
翁国栋 committed
529 530 531
            /*文件流base64*/
        } catch (IOException e) {
            e.printStackTrace();
翁国栋 committed
532
        } finally {
翁国栋 committed
533 534 535 536 537 538 539
            try {
                xssfWorkbook.close();
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
翁国栋 committed
540
        return url;
翁国栋 committed
541 542 543
    }

    @GetMapping(value = "/downPolicyFile")
翁国栋 committed
544
    @ApiOperation(value = "7.下载电子保单", httpMethod = "GET", notes = "下载电子保单")
翁国栋 committed
545 546
    public void downPolicyFile(@RequestParam("policyId") String policyId, HttpServletRequest request, HttpServletResponse response) {
        InsurePolicy insurePolicy = InsurePolicy.builder().id(Integer.parseInt(policyId)).build().selectById();
翁国栋 committed
547
        if (insurePolicy == null || StringUtils.isNullOrEmpty(insurePolicy.getPolicyFile())) {
翁国栋 committed
548 549
            return;
        }
翁国栋 committed
550 551
        OutputStream sos = null;
        BufferedInputStream bis = null;
翁国栋 committed
552 553
        try {
            response.setContentType("application/octet-stream");
翁国栋 committed
554
            response.setHeader("content-disposition", "attachment; filename=" + new String((insurePolicy.getPolicyNo() + ".pdf").getBytes("UTF8"), "ISO-8859-1"));
翁国栋 committed
555
            response.setCharacterEncoding("UTF-8");
翁国栋 committed
556 557 558 559 560 561 562 563 564 565 566
            sos = response.getOutputStream();
            String destUrl = insurePolicy.getPolicyFile();
            URL url = new URL(destUrl);
            HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
            //连接指定的网络资源
            httpUrl.connect();
            //获取网络输入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            int b;
            while ((b = bis.read()) != -1) {
                sos.write(b);
翁国栋 committed
567 568 569 570 571 572 573
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
翁国栋 committed
574 575 576 577 578 579
                sos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
翁国栋 committed
580 581 582 583 584 585 586
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping(value = "/replaceUser")
587
    @ApiOperation(value = "6.保全替换", httpMethod = "POST", notes = "保全替换")
翁国栋 committed
588 589 590 591 592
    public Result<Object> replaceUser(@RequestBody InsureDto insureDto) {
        InsurePolicy insurePolicy = InsurePolicy.builder().id(Integer.parseInt(insureDto.getId())).build().selectById();
        if (insurePolicy == null) {
            return ResultUtil.error("保单不存在");
        }
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
        /*单个替换*/
        if(insureDto.getLimit()==1){
            if(!StringUtils.isNullOrEmpty(insureDto.getUserName())&&!StringUtils.isNullOrEmpty(insureDto.getIdCardNo())){
                YgglMainEmp y = YgglMainEmp.builder().build().selectOne(new QueryWrapper<YgglMainEmp>().lambda()
                        .eq(YgglMainEmp::getName,insureDto.getUserName()).eq(YgglMainEmp::getZjNum,insureDto.getIdCardNo()));
                if(y==null){
                    y=new YgglMainEmp();
                    y.setName(insureDto.getUserName());
                    y.setZjType(0);
                    y.setZjNum(insureDto.getIdCardNo());
                    y.setOrgCode(Integer.parseInt(insureDto.getOrgCode()));
                    y.insert();
                }
                insureDto.setUserIds(new String[]{String.valueOf(y.getId())});
            }
        }
翁国栋 committed
609
        List<InsureUser> oldInsureUserList = insureUserMapper.selectListByIds(insureDto.getOldIds());
翁国栋 committed
610 611 612
        String[] oldUser = new String[oldInsureUserList.size()];
        for (int i = 0; i < oldInsureUserList.size(); i++) {
            oldUser[i] = oldInsureUserList.get(i).getUserId().toString();
翁国栋 committed
613
        }
翁国栋 committed
614
        Integer price = getPrice(insureDto.getBenefitBasicPlan(), insureDto.getBenefitOccupationCategory());
翁国栋 committed
615
        LocalDateTime now = LocalDateTime.now();
翁国栋 committed
616
        String url = creaXSSFWorkbook2(insureDto.getUserIds(), insureDto.getPolicyDateStart(), oldUser, insureDto.getBenefitOccupationCategory(), insureDto.getTricycleFrameNumber());
翁国栋 committed
617 618 619 620 621 622 623 624 625
        log.info("保全替换文件地址", url);
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        Map bodyMap = Maps.newHashMap();
        bodyMap.put("contract_no", insurePolicy.getPolicyNo());
        bodyMap.put("order_status", "3");
        bodyMap.put("callback_plan", "");
        bodyMap.put("file_url", url);
        bodyMap.put("from_source", "API");
        bodyMap.put("import_review_callback", "");
翁国栋 committed
626
        bodyMap.put("import_callback", base_api_url + "/callBack/policy/CallBack");
翁国栋 committed
627 628 629 630 631
        bodyMap.put("third_uuid", "RP_" + insurePolicy.getOrgCode() + "_" + dtf2.format(now));
        String responData = HttpUtils.sendPost(batchUrl, setParams(JSONObject.toJSONString(bodyMap), appidq, secretq), bodyMap);
        Map<String, Object> dataMap = JSONObject.parseObject(responData);
        InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(3)
                .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(responData).requestPath(batchUrl)
翁国栋 committed
632
                .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(insurePolicy.getId()).fileUrl(url).build().insert();
翁国栋 committed
633 634 635 636 637
        if (dataMap.get("errcode").toString().equals("suc")) {
            Map<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(dataMap.get("data")));
            Map<String, Object> orderImportInfo = JSONObject.parseObject(JSONObject.toJSONString(data.get("order_import_info")));
            String status = orderImportInfo.get("status").toString();
            if (status.equals("5") || status.equals("6") || status.equals("7")) {
翁国栋 committed
638 639 640
                InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(2)
                        .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(responData).requestPath(batchUrl)
                        .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(insurePolicy.getId()).transId("RP_" + insurePolicy.getOrgCode() + "_" + dtf2.format(now)).fileUrl(data.get("file_url").toString()).build().insert();
翁国栋 committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
                //TODO 写入日志
                return ResultUtil.error("信息填写错误,请查看日志");
            } else {
                List<YgglMainEmp> ygglMainEmpList = ygglMainEmpMapper.selectListByIds(insureDto.getUserIds());
                QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(Integer.parseInt(insureDto.getOrgCode())).build().selectById();
                ygglMainEmpList.forEach(y -> {
                    try {
                        InsureUser.builder()
                                .transId("RP_" + insurePolicy.getOrgCode() + "_" + dtf2.format(now))
                                .insuredName(qyzxEntInfoM.getName())
                                .price(price.toString())/*前端获取的保费*/
                                .premium(String.valueOf(price * ygglMainEmpList.size()))
                                .batchNo(data.get("import_uuid").toString())
                                .benefitBasicPlan(insureDto.getBenefitBasicPlan())
                                .policyNo(orderImportInfo.get("contract_no").toString())
                                .userId(y.getId())
                                .orgCode(y.getOrgCode())
                                .benefitOccupationCategory(insureDto.getBenefitOccupationCategory())
                                .insuredMobile(y.getPhone())
                                .benefitOccupationCategory(insureDto.getBenefitOccupationCategory())
                                .insuredNo(y.getZjNum())
翁国栋 committed
662
                                .tricycleFrameNumber(insureDto.getTricycleFrameNumber())
翁国栋 committed
663 664 665 666
                                .insuredEContact(y.getName())
                                .applicantEmployeeList(data.get("file_url").toString())
                                .policyDateStart(dtf3.parse(insureDto.getPolicyDateStart()))
                                .policyDateEnd(dtf3.parse(insureDto.getPolicyDateEnd()))
翁国栋 committed
667
                                .applyType(3)
翁国栋 committed
668 669 670 671
                                .status("1").insureStatus(3).policyId(insurePolicy.getId()).build().insert();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
翁国栋 committed
672
                    y.setIsInsure(1);
翁国栋 committed
673 674
                    y.insertOrUpdate();
                });
翁国栋 committed
675
                oldInsureUserList.forEach(o -> {
翁国栋 committed
676 677 678 679 680 681 682 683 684
                    o.setReplaceTransId(data.get("import_uuid").toString());
                    o.updateById();
                });

                return ResultUtil.data("核保成功,等待系统更新");
            }
        }
        return ResultUtil.data("成功");
    }
翁国栋 committed
685

翁国栋 committed
686 687 688 689 690 691
    /**
     * 生成保全替换文件
     *
     * @param ids
     * @return
     */
翁国栋 committed
692 693
    public String creaXSSFWorkbook2(String[] ids, String date, String[] oldIds, String type, String tricycleFrameNumber) {
        if (ids.length <= 0 || oldIds.length <= 0) {
翁国栋 committed
694 695 696 697 698
            return "error";
        }
        LocalDate localDate = LocalDate.now();
        List<YgglMainEmp> newList = ygglMainEmpMapper.selectListByIds(ids);
        List<YgglMainEmp> oldList = ygglMainEmpMapper.selectListByIds(oldIds);
翁国栋 committed
699
        if (newList.size() != oldList.size()) {
翁国栋 committed
700 701 702
            return "error";
        }
        List list = Lists.newArrayList();
翁国栋 committed
703
        for (int i = 0; i < newList.size(); i++) {
翁国栋 committed
704 705 706 707
            String[] arr = new String[]{String.valueOf(oldList.get(i).getId()),
                    oldList.get(i).getName(),
                    "身份证",
                    oldList.get(i).getZjNum(),
翁国栋 committed
708
                    date.substring(0, 10),
翁国栋 committed
709 710 711
                    newList.get(i).getName(),
                    "身份证",
                    newList.get(i).getZjNum(),
翁国栋 committed
712
                    "", "", "", "", "", "", StringUtils.isNullOrEmpty(tricycleFrameNumber) ? "无" : tricycleFrameNumber, "1", type.equals("63119") ? "A类" : type.equals("63120") ? "B类" : "C类"};
翁国栋 committed
713 714 715
            list.add(arr);
        }
        String[] rowName = new String[]{"ID", "name", "ID_type", "ID_number", "Update_date", "replace_name", "replace_ID_type",
翁国栋 committed
716 717
                "replace_ID_number", "Date_of_birth", "insured_type", "insured_relationship", "cert_type_related_insured", "insured_relationship_id", "Branch",
                "Tricycle_frame_number", "insured_number", "benefit_occupation_category"};
翁国栋 committed
718

翁国栋 committed
719
        XSSFWorkbook xssfWorkbook = ExcelUtils.exportExcel(localDate + "_替换" + list.size() + "名人员清单.xlsx", rowName, list);
翁国栋 committed
720

翁国栋 committed
721
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
翁国栋 committed
722
        String url = "";
翁国栋 committed
723 724 725 726 727 728 729 730 731 732 733
        try {
            xssfWorkbook.write(byteArrayOutputStream);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            String fileBase64 = Base64.byteArrayToBase64(bytes);

            /*body参数 begin*/
            Map bodyMap = Maps.newHashMap();
            bodyMap.put("media", fileBase64);
            /*end*/
            String data = HttpUtils.sendPost(uploadUrl2, setParams(JSONObject.toJSONString(bodyMap), appidq, secretq), bodyMap);
            Map<String, Object> dataMap = JSONObject.parseObject(data);
翁国栋 committed
734

翁国栋 committed
735 736 737
            if (dataMap.get("errcode").toString().equals("suc")) {
                Object o = dataMap.get("data");
                Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(o));
翁国栋 committed
738
                url = map.get("file_url").toString();
翁国栋 committed
739
            }
翁国栋 committed
740 741 742
            InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(6)
                    .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(data).requestPath(uploadUrl2)
                    .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).fileUrl(url).build().insert();
翁国栋 committed
743 744 745 746 747 748 749 750 751 752 753
            /*文件流base64*/
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                xssfWorkbook.close();
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
翁国栋 committed
754
        return url;
翁国栋 committed
755
    }
翁国栋 committed
756

翁国栋 committed
757
    /*获取基础计划的价格*/
翁国栋 committed
758
    private Integer getPrice(String plan, String type) {
翁国栋 committed
759 760 761 762 763 764 765 766 767
       /*
       基础计划
       30万雇主/3万医疗   36968,
       50万雇主/5万医疗	36969,
       80万雇主/10万医疗	36970,
       80万雇主/20万医疗	36971,
       100万雇主/10万医疗 36972
       */
        /*行业类型
翁国栋 committed
768 769 770 771 772
         * A类	63119
         * B类	63120
         * C类	63121\
         * */
        switch (plan) {
翁国栋 committed
773
            case "36968":
翁国栋 committed
774
                if (type.equals("63119")) {
775
                    return PlanEnum.A_30.getPrice();
翁国栋 committed
776
                } else if (type.equals("63120")) {
777
                    return PlanEnum.B_30.getPrice();
翁国栋 committed
778 779
                }
                break;
翁国栋 committed
780 781
            case "36969":
                if (type.equals("63119")) {
782
                    return PlanEnum.A_30.getPrice();
翁国栋 committed
783
                } else if (type.equals("63120")) {
784
                    return PlanEnum.B_50.getPrice();
翁国栋 committed
785
                } else if (type.equals("63121")) {
786
                    return PlanEnum.C_50.getPrice();
翁国栋 committed
787 788 789
                }
                break;
            case "36970":
翁国栋 committed
790
                if (type.equals("63119")) {
791
                    return PlanEnum.A_80.getPrice();
翁国栋 committed
792
                } else if (type.equals("63120")) {
793
                    return PlanEnum.B_80.getPrice();
翁国栋 committed
794
                } else if (type.equals("63121")) {
795
                    return PlanEnum.C_80.getPrice();
翁国栋 committed
796 797 798
                }
                break;
            case "36971":
翁国栋 committed
799
                if (type.equals("63119")) {
800
                    return PlanEnum.A_80_20.getPrice();
翁国栋 committed
801
                } else if (type.equals("63120")) {
802
                    return PlanEnum.B_80_20.getPrice();
翁国栋 committed
803 804 805
                }
                break;
            case "36972":
翁国栋 committed
806
                if (type.equals("63119")) {
807
                    return PlanEnum.A_100.getPrice();
翁国栋 committed
808
                } else if (type.equals("63120")) {
809
                    return PlanEnum.B_100.getPrice();
翁国栋 committed
810 811 812 813 814
                }
                break;
        }
        return 0;
    }
翁国栋 committed
815

翁国栋 committed
816
    @PostMapping(value = "/policyTotal")
817
    @ApiOperation(value = "11.保单统计列表", httpMethod = "POST", notes = "保单统计列表")
翁国栋 committed
818 819 820 821
    public Result<Object> policyTotal() {
        List<PolicyDto> list = insurePolicyMapper.policyTotalList();
        return ResultUtil.data(list);
    }
翁国栋 committed
822

翁国栋 committed
823
    @GetMapping(value = "/isInsured")
824
    @ApiOperation(value = "10.企业是否已投保", httpMethod = "POST", notes = "企业是否已投保")
翁国栋 committed
825 826 827 828
    public Result<Object> isInsured(@RequestParam("orgCode") String orgCode) {
        InsurePolicy insurePolicy = InsurePolicy.builder().build().selectOne(new QueryWrapper<InsurePolicy>().lambda().eq(InsurePolicy::getOrgCode, orgCode).eq(InsurePolicy::getStatus, 1));
        if (insurePolicy != null) {
            return ResultUtil.error("该企业已经投过保,保单号为" + insurePolicy.getPolicyNo());
翁国栋 committed
829 830 831
        }
        return ResultUtil.success();
    }
翁国栋 committed
832 833

    @GetMapping(value = "/verification")
834
    @ApiOperation(value = "一键校验", httpMethod = "POST", notes = "一键校验")
翁国栋 committed
835
    public Result<Object> verification() {
翁国栋 committed
836
        /*校验投保*/
翁国栋 committed
837 838 839
        List<InsurePolicy> insurePolicies = InsurePolicy.builder().build().selectList(new QueryWrapper<InsurePolicy>().lambda().eq(InsurePolicy::getStatus, 2));
        insurePolicies.forEach(i -> {
            InsureLog insureLog = InsureLog.builder().build().selectOne(new QueryWrapper<InsureLog>().lambda().eq(InsureLog::getPolicyId, i.getId()).eq(InsureLog::getType, 1));
翁国栋 committed
840
            Map bodyMap = JSONObject.parseObject(insureLog.getRequestData(), Map.class);
翁国栋 committed
841 842
            String data = HttpUtils.sendPost(insuredUrl, setParams(JSONObject.toJSONString(bodyMap), appid, secret), bodyMap);
            log.info("校验结果:{}", data);
翁国栋 committed
843
            Map dataMap = JSONObject.parseObject(data, Map.class);
翁国栋 committed
844 845 846
            InsureLog.builder().requestParam(JSONObject.toJSONString(setParams(JSONObject.toJSONString(bodyMap), appid, secret))).type(9)
                    .requestData(JSONObject.toJSONString(bodyMap)).createTime(new Date()).requestType(1).returnBody(JSONObject.toJSONString(dataMap)).requestPath(insuredUrl)
                    .returnCode(dataMap.get("errcode").toString()).returnMsg(dataMap.get("errmsg").toString()).policyId(i.getId()).build().insert();
翁国栋 committed
847 848
            if (dataMap != null) {
                if ((dataMap.get("errcode").toString().equals("suc"))) {
翁国栋 committed
849
                    Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(dataMap.get("data")));
翁国栋 committed
850 851 852 853 854 855 856 857
                    Map policyMap = JSONObject.parseObject(JSONObject.toJSONString(map.get("policy")), Map.class);
                    i.setPolicyNo(policyMap.get("policy_no").toString());
                    i.setPolicyFile(policyMap.get("policy_file").toString());
                    i.setKitUrl(policyMap.get("kit_url").toString());
                    i.setStatus("1");
                    i.updateById();
                    /*批次信息*/
                    List<Map> batchMap = JSONObject.parseObject(JSONObject.toJSONString(policyMap.get("batch")), List.class);
翁国栋 committed
858 859 860
                    List<InsureUser> insureUserList = InsureUser.builder().build().selectList(new QueryWrapper<InsureUser>().lambda().eq(InsureUser::getPolicyId, i.getId())
                            .eq(InsureUser::getApplyType, 2));
                    insureUserList.forEach(user -> {
翁国栋 committed
861 862 863 864 865 866 867 868
                        user.setBatchNo(batchMap.get(0).get("batch_no").toString());
                        user.setPolicyNo(policyMap.get("policy_no").toString());
                        user.setInsureStatus(1);
                        user.setStatus("1");
                        user.updateById();
                        YgglMainEmp.builder().isInsure(1).build().update(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getId, user.getUserId()));
                    });
                }
翁国栋 committed
869

翁国栋 committed
870
            }
翁国栋 committed
871
        });
翁国栋 committed
872 873
        /*校验人员*/

翁国栋 committed
874
        return ResultUtil.success();
翁国栋 committed
875 876
    }

翁国栋 committed
877 878

    @PostMapping(value = "/policyTotalList")
879
    @ApiOperation(value = "11.保单统计列表 -- 8小时端", httpMethod = "POST", notes = "保单统计列表")
翁国栋 committed
880
    public Result<Object> policyTotal(@CurrentUser UserBean userBean, @RequestBody Page page) {
翁国栋 committed
881 882 883 884
        List<InsureUserDto> list = insurePolicyMapper.selectPlansListByorgCode(userBean.getOrgCode());
        return ResultUtil.data(list);
    }

翁国栋 committed
885

翁国栋 committed
886
    @PostMapping(value = "/importUser")
887
    @ApiOperation(value = "导入用户--运营后台", httpMethod = "POST", notes = "导入用户")
翁国栋 committed
888 889 890 891 892 893 894 895 896 897
    public Result<Object> importUser(@RequestParam("orgCode") String orgCode, @RequestParam("file") MultipartFile file) {
        SimpleDateFormat dtf = new SimpleDateFormat("yyyyMMdd");
        QyzxEntInfoM company = QyzxEntInfoM.builder().id(Integer.valueOf(orgCode)).build().selectById();
        if (company == null) {
            return ResultUtil.error("该企业不存在");
        }
        if (file.getSize() <= 0) {
            return ResultUtil.error("请上传文件");
        }
        List<YgglMainEmp> yg = Lists.newArrayList();
翁国栋 committed
898
        List<YgglMainEmp> selectUserList=Lists.newArrayList();
899
        List<YgglMainEmp> allList=Lists.newArrayList();
翁国栋 committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
        try {
            XSSFWorkbook xw = new XSSFWorkbook(file.getInputStream());
            XSSFSheet sheetAt = xw.getSheetAt(0);
            //默认第一行为标题行,i = 0
            XSSFRow titleRow = sheetAt.getRow(0);
            YgglMainEmp y;
            // 循环获取每一行数据
            for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
                XSSFRow row = sheetAt.getRow(i);
                y = YgglMainEmp.builder().build();
                y.setOrgCode(Integer.parseInt(orgCode));
                // 读取每一格内容
                for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
                    XSSFCell titleCell = titleRow.getCell(index);
                    XSSFCell cell = row.getCell(index);
                    cell.setCellType(CellType.STRING);
                    if (cell.getStringCellValue().equals("")) {
                        continue;
                    }
                    switch (ExcelUtils.getString(titleCell)) {
翁国栋 committed
920
                        case "姓名(必填)":
翁国栋 committed
921 922
                            y.setName(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
                            break;
翁国栋 committed
923
                        case "手机号":
翁国栋 committed
924 925 926 927 928 929 930 931
                            y.setPhone(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
                            break;
//                        case "性别":
//                            y.setSex(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
//                            break;
//                        case "证件类型":
//                            y.setZjType(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
//                            break;
翁国栋 committed
932
                        case "证件号(必填)":
翁国栋 committed
933 934
                            String idNo = Optional.ofNullable(ExcelUtils.getString(cell)).orElse("");
                            if (Strings.isNullOrEmpty(idNo) && idNo.length() != 18) {
翁国栋 committed
935
                                return ResultUtil.error(y.getName() + "身份证格式不正确");
翁国栋 committed
936
                            }
翁国栋 committed
937 938
                            if (!idNo.matches("^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$")) {
                                return ResultUtil.error(y.getName() + "身份证格式不正确");
翁国栋 committed
939 940 941
                            }

                            try {
翁国栋 committed
942
                                y.setBirthday(dtf.parse(idNo.substring(6, 14)));
翁国栋 committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            y.setZjNum(idNo);
                            break;
//                        case "工作性质":
//                            y.setJobType(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
//                            break;
//                        case "试用期":
//                            y.setSyq(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
//                            break;
//                        case "入职日期":
//                            y.setRzTime(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
//                            break;
                        default:
                            break;
                    }
                }
翁国栋 committed
961
                if (StringUtils.isNullOrEmpty(y.getName()) || StringUtils.isNullOrEmpty(y.getZjNum())) {
翁国栋 committed
962 963 964 965 966
                    continue;
                }
                yg.add(y);
            }
            List<YgglMainEmp> userList = YgglMainEmp.builder().build().selectList(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getOrgCode, orgCode));
翁国栋 committed
967 968 969
            List<String> idNumList = userList.stream().map(YgglMainEmp::getZjNum).collect(Collectors.toList());
            List<YgglMainEmp> newList = yg.stream().filter(v -> !idNumList.contains(v.getZjNum())).collect(Collectors.toList());
            if (newList.size() > 0) {
翁国栋 committed
970 971
                newList.forEach(u -> u.insert());
            }
972 973
             allList=ygglMainEmpMapper.selectListByIZjNum(yg.stream().map(YgglMainEmp::getZjNum).toArray(String[]::new),company.getId());
             selectUserList=allList.stream().filter(s->s.getIsInsure()==0).collect(Collectors.toList());
翁国栋 committed
974 975 976 977 978 979 980
            //假期规则初始化
            String current_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            List<KqglAssoLeaveRules> rul = KqglAssoLeaveRules.builder().build().selectList(new QueryWrapper<KqglAssoLeaveRules>().lambda().eq(KqglAssoLeaveRules::getOrgCode, orgCode));

        } catch (IOException e) {
            e.printStackTrace();
        }
981 982 983 984 985 986
        List<String> message =allList.stream().filter(s->s.getIsInsure()==1).map(YgglMainEmp::getName).collect(Collectors.toList());
        if(message.size()>0){
            return ResultUtil.data(selectUserList, JSONObject.toJSONString(message)+"已投过保");
        }
        return ResultUtil.data(selectUserList, "导入成功");
     }
翁国栋 committed
987

翁国栋 committed
988
    @PostMapping(value = "/importPolicy")
989
    @ApiOperation(value = "导入保单--运营后台", httpMethod = "POST", notes = "导入用户")
翁国栋 committed
990
    public Result<Object> importPolicy(@RequestParam("file") MultipartFile file, @RequestParam("productId") String productId) {
翁国栋 committed
991 992 993 994 995
        SimpleDateFormat dtf = new SimpleDateFormat("yyyyMMdd");

        if (file.getSize() <= 0) {
            return ResultUtil.error("请上传文件");
        }
翁国栋 committed
996
        InsureProduct insureProduct = InsureProduct.builder().id(Integer.parseInt(productId)).build().selectById();
翁国栋 committed
997
        if (insureProduct == null) {
翁国栋 committed
998 999
            return ResultUtil.error("产品不存在");
        }
翁国栋 committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
        try {
            XSSFWorkbook xw = new XSSFWorkbook(file.getInputStream());
            XSSFSheet sheetAt = xw.getSheetAt(0);
            //默认第一行为标题行,i = 0
            XSSFRow titleRow = sheetAt.getRow(0);
            InsurePolicy policy = InsurePolicy.builder().build();
            QyzxEntInfoM company = null;
            InsureUser user;
            List<InsureUser> userList = Lists.newArrayList();
            // 循环获取每一行数据
            for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
                XSSFRow row = sheetAt.getRow(i);
                user = InsureUser.builder().build();
                // 读取每一格内容
                for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
                    XSSFCell titleCell = titleRow.getCell(index);
                    XSSFCell cell = row.getCell(index);
                    cell.setCellType(CellType.STRING);
                    switch (ExcelUtils.getString(titleCell)) {
                        case "申报日期":
                            Date createTime = null;
                            try {
                                createTime = dtf3.parse(ExcelUtils.getString(cell));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            policy.setCreateTime(createTime);
                            break;
                        case "保单号":
                            policy.setPolicyNo(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
                            user.setPolicyNo(Optional.ofNullable(ExcelUtils.getString(cell)).orElse(""));
                            break;
                        case "申报类型":
                            if (ExcelUtils.getString(cell).equals("新增")) {
                                user.setApplyType(1);
                            } else if (ExcelUtils.getString(cell).equals("批改")) {
                                user.setApplyType(3);
                            }
                            break;
                        case "方案名称":
1040
                            PlanEnum plan = PlanEnum.getEnumOfName(ExcelUtils.getString(cell).trim());
翁国栋 committed
1041
                            user.setBenefitBasicPlan(plan.getPlan());
1042
                            user.setBenefitOccupationCategory(plan.getCategory());
翁国栋 committed
1043
                            break;
1044
                        case "有效期起":
翁国栋 committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
                            Date start = null;
                            try {
                                start = dtf3.parse(ExcelUtils.getString(cell));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            user.setPolicyDateStart(start);
                            policy.setPolicyDateStart(start);
                            break;
                        case "有效期止":
                            Date end = null;
                            try {
                                end = dtf3.parse(ExcelUtils.getString(cell));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
1061 1062
                            user.setPolicyDateEnd(end);
                            policy.setPolicyDateEnd(end);
翁国栋 committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
                            break;
                        case "被保险人姓名":
                            user.setInsuredEContact(ExcelUtils.getString(cell));
                            break;
                        case "证件类型":
                            if (ExcelUtils.getString(cell).equals("身份证")) {

                            }
                            break;
                        case "证件号码":
                            user.setInsuredNo(ExcelUtils.getString(cell));
                            break;
                        case "二、三轮车车架号":
                            user.setTricycleFrameNumber(!ExcelUtils.getString(cell).equals("无") ? ExcelUtils.getString(cell) : null);
                            break;
                        case "职业类别":
                            if (ExcelUtils.getString(cell).equals("A类")) {
                                user.setBenefitOccupationCategory("63119");
                            } else if (ExcelUtils.getString(cell).equals("B类")) {
                                user.setBenefitOccupationCategory("63120");
                            } else if (ExcelUtils.getString(cell).equals("C类")) {
                                user.setBenefitOccupationCategory("63121");
                            }
                            break;
                        case "企业全称":
                            company = QyzxEntInfoM.builder().build().selectOne(new QueryWrapper<QyzxEntInfoM>().lambda().eq(QyzxEntInfoM::getAttestName, ExcelUtils.getString(cell)));
                            if (company == null) {
                                return ResultUtil.error("企业不存在");
                            }
                            user.setOrgCode(company.getId());
1093
                            policy.setOrgCode(company.getId());
翁国栋 committed
1094 1095
                            break;
                        case "总保费":
1096
                            user.setPrice(ExcelUtils.getString(cell));
翁国栋 committed
1097 1098 1099 1100 1101
                            break;
                    }
                }
                userList.add(user);
            }
1102 1103
            //TODO 验证保单的状态
            long timestamp = System.currentTimeMillis() / 1000;
翁国栋 committed
1104 1105 1106 1107 1108
            Map map = Maps.newHashMap();
            map.put("pid", appid);
            map.put("policy_no", policy.getPolicyNo());
            map.put("sign", DigestUtils.md5Hex(appid + secret + timestamp));
            map.put("timestamp", String.valueOf(timestamp));
1109
            String data = HttpUtils.sendGet(policyDetailUrl, map);
翁国栋 committed
1110 1111
            Map returnMap = JSONObject.parseObject(data, Map.class);
            if (returnMap.get("errcode").toString().equals("suc")) {
1112
                Map dataMap = JSONObject.parseObject(JSONObject.toJSONString(returnMap.get("data")), Map.class);
翁国栋 committed
1113
                policy.setProductId(insureProduct.getId());
1114 1115
                policy.setPolicyFile(dataMap.get("policy_file").toString());
                policy.setStatus("1");
翁国栋 committed
1116 1117
            } else {
                return ResultUtil.error("保单不存在,请检查保单号是否正确");
1118
            }
翁国栋 committed
1119
            InsurePolicy oldPolicy = InsurePolicy.builder().build().selectOne(new QueryWrapper<InsurePolicy>().lambda().eq(InsurePolicy::getPolicyNo, policy.getPolicyNo()));
1120

翁国栋 committed
1121
            if (oldPolicy == null) {
翁国栋 committed
1122
                policy.setSchemeName(insureProduct.getName());
翁国栋 committed
1123
                policy.setUpdateTime(new Date());
翁国栋 committed
1124 1125
                policy.insert();
            } else {
1126
                oldPolicy.setPolicyFile(policy.getPolicyFile());
翁国栋 committed
1127
                policy.setUpdateTime(new Date());
翁国栋 committed
1128
                policy = oldPolicy;
1129
                policy.updateById();
翁国栋 committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
            }
            if (userList.size() <= 0) {
                return ResultUtil.error("导入错误,保单已存在");
            }
            for (InsureUser insureUser : userList) {
                /*根据名字,身份,所属企业获取员工*/
                YgglMainEmp ygglMainEmp = YgglMainEmp.builder().build().selectOne(new QueryWrapper<YgglMainEmp>().lambda()
                        .eq(YgglMainEmp::getName, insureUser.getInsuredEContact())
                        .eq(YgglMainEmp::getZjNum, insureUser.getInsuredNo())
                        .eq(YgglMainEmp::getOrgCode, insureUser.getOrgCode()));
                if (ygglMainEmp != null) {
翁国栋 committed
1141
                    insureUser.setInsuredMobile(ygglMainEmp.getPhone());
翁国栋 committed
1142 1143
                } else {
                    /*如果不存在员工就创建一名员工*/
翁国栋 committed
1144
                    ygglMainEmp = new YgglMainEmp();
翁国栋 committed
1145 1146 1147 1148 1149
                    ygglMainEmp.setName(insureUser.getInsuredEContact());
                    ygglMainEmp.setZjType(0);
                    ygglMainEmp.setZjNum(insureUser.getInsuredNo());
                    ygglMainEmp.setOrgCode(insureUser.getOrgCode());
                }
翁国栋 committed
1150
                ygglMainEmp.setIsInsure(1);
翁国栋 committed
1151
                ygglMainEmp.insertOrUpdate();
翁国栋 committed
1152 1153 1154 1155 1156
                insureUser.setInsuredName(company.getAttestName());
                insureUser.setPolicyId(policy.getId());
                insureUser.setInsureStatus(1);
                insureUser.setUserId(ygglMainEmp.getId());
                insureUser.setStatus("1");
翁国栋 committed
1157 1158
                insureUser.insert();
            }
1159 1160
            InsureLog.builder().requestParam(JSONObject.toJSONString(map)).type(6).createTime(new Date()).requestType(2).returnBody(data).requestPath(policyDetailUrl)
                    .returnCode(returnMap.get("errcode").toString()).returnMsg(returnMap.get("errmsg").toString()).policyId(policy.getId()).build().insert();
翁国栋 committed
1161 1162 1163 1164 1165 1166
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultUtil.data("成功导入");
    }

翁国栋 committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    @PostMapping(value = "/importUserPolicy")
    @ApiOperation(value = "导入被保人员--运营后台", httpMethod = "POST", notes = "导入用户")
    public Result<Object> importUserPolicy(@RequestParam("file") MultipartFile file,@RequestParam("orgCode") Integer orgCode) {
        if(file.getSize()<=0){
            return ResultUtil.error("未上传文件");
        }
        if(orgCode==null){
            return ResultUtil.error("企业不存在");
        }
        QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(orgCode).build().selectById();
        if(qyzxEntInfoM==null){
            return ResultUtil.error("企业不存在");
        }
        XSSFWorkbook xw = null;
        Map<String,String> map = Maps.newHashMap();
        try {
            xw = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheetAt = xw.getSheetAt(0);
            //默认第一行为标题行,i = 0
            XSSFRow titleRow = sheetAt.getRow(0);
            InsureUser user;
            List<InsureUser> userList = Lists.newArrayList();
            // 循环获取每一行数据
            for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
                XSSFRow row = sheetAt.getRow(i);
                user = InsureUser.builder().build();
                // 读取每一格内容
                for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
                    XSSFCell titleCell = titleRow.getCell(index);
                    XSSFCell cell = row.getCell(index);
                    cell.setCellType(CellType.STRING);
                    switch (ExcelUtils.getString(titleCell)) {
                        case "被保人姓名(必填)":
                            if(StringUtils.isNullOrEmpty(ExcelUtils.getString(cell))){
                                return ResultUtil.error("第"+i+"行第"+index+"列被保人姓名不能为空");
                            }
                            user.setInsuredEContact(ExcelUtils.getString(cell));
                            break;
                        case "被保人身份证(必填)":
                            if(StringUtils.isNullOrEmpty(ExcelUtils.getString(cell))){
                                return ResultUtil.error("第"+i+"行第"+index+"列被保人身份证不能为空");
                            }
                            user.setInsuredNo(ExcelUtils.getString(cell));
                            break;
                        case "基础计划":
                            if(ExcelUtils.getString(cell).equals("30万意外/3万医疗/扩展24小时")){
                                user.setBenefitBasicPlan("36968");
                            }else if (ExcelUtils.getString(cell).equals("50万意外/5万医疗/扩展24小时")) {
                                user.setBenefitBasicPlan("36969");
                            }else if (ExcelUtils.getString(cell).equals("80万意外/10万医疗/扩展24小时")) {
                                user.setBenefitBasicPlan("36970");
                            }else if (ExcelUtils.getString(cell).equals("80万雇主/20万医疗/扩展24小时")) {
                                user.setBenefitBasicPlan("36971");
                            }else if (ExcelUtils.getString(cell).equals("100万意外/10万医疗/扩展24小时")) {
                                user.setBenefitBasicPlan("36972");
                            }else{
                                return ResultUtil.error("第"+i+"行第"+index+"基础计划不能为空");
                            }
                            break;
                        case "职业类型":
                            if (ExcelUtils.getString(cell).equals("A类")) {
                                user.setBenefitOccupationCategory("63119");
                            } else if (ExcelUtils.getString(cell).equals("B类")) {
                                user.setBenefitOccupationCategory("63120");
                            } else if (ExcelUtils.getString(cell).equals("C类")) {
                                user.setBenefitOccupationCategory("63121");
                            }
                            break;
                        case "二、三轮车车架号":
                            user.setTricycleFrameNumber(StringUtils.isNullOrEmpty(ExcelUtils.getString(cell))==true?"无":ExcelUtils.getString(cell));
                            break;
                    }
                }
                userList.add(user);
            }
            YgglMainEmp y;
            for(InsureUser iu:userList){
                y = YgglMainEmp.builder().build().selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getZjNum,iu.getInsuredNo())
                        .eq(YgglMainEmp::getName,iu.getInsuredEContact()).eq(YgglMainEmp::getOrgCode,qyzxEntInfoM.getId()));
                if(y==null){
                    y = new YgglMainEmp();
                    y.setName(iu.getInsuredEContact());
                    y.setZjType(0);
                    y.setZjNum(iu.getInsuredNo());
                    y.setOrgCode(qyzxEntInfoM.getId());
                    y.setIsInsure(0);
                    y.insert();
                }else{
                    if(y.getIsInsure()==1){
                        return ResultUtil.error(y.getName()+"已经投保");
                    }
                }
                iu.setOrgCode(qyzxEntInfoM.getId());
                iu.setUserId(y.getId());
            }
            return ResultUtil.data(userList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultUtil.error("导入失败");
    }
翁国栋 committed
1268
}