ApplyRoomDiscountApi.java 39.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
package com.java110.fee.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.DataFlowContext;
import com.java110.core.factory.CommunitySettingFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.room.ApplyRoomDiscountDto;
import com.java110.dto.room.ApplyRoomDiscountTypeDto;
import com.java110.dto.fee.FeeDetailDto;
import com.java110.dto.fee.FeeDiscountRuleDto;
import com.java110.fee.bmo.account.IUpdateAccountBMO;
import com.java110.fee.bmo.applyRoomDiscount.IAuditApplyRoomDiscountBMO;
import com.java110.fee.bmo.applyRoomDiscount.IDeleteApplyRoomDiscountBMO;
import com.java110.fee.bmo.applyRoomDiscount.IGetApplyRoomDiscountBMO;
import com.java110.fee.bmo.applyRoomDiscount.ISaveApplyRoomDiscountBMO;
import com.java110.fee.bmo.applyRoomDiscount.IUpdateApplyRoomDiscountBMO;
import com.java110.fee.bmo.applyRoomDiscountType.IDeleteApplyRoomDiscountTypeBMO;
import com.java110.fee.bmo.applyRoomDiscountType.IGetApplyRoomDiscountTypeBMO;
import com.java110.fee.bmo.applyRoomDiscountType.ISaveApplyRoomDiscountTypeBMO;
import com.java110.fee.bmo.applyRoomDiscountType.IUpdateApplyRoomDiscountTypeBMO;
import com.java110.intf.common.IFileRelInnerServiceSMO;
import com.java110.intf.fee.IApplyRoomDiscountInnerServiceSMO;
import com.java110.intf.fee.IFeeDetailInnerServiceSMO;
import com.java110.intf.fee.IFeeDiscountRuleInnerServiceSMO;
import com.java110.po.room.ApplyRoomDiscountPo;
import com.java110.po.room.ApplyRoomDiscountTypePo;
import com.java110.po.file.FileRelPo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/applyRoomDiscount")
public class ApplyRoomDiscountApi {

    private static final String SPEC_RATE = "89002020980013"; // 打折率

    @Autowired
    private ISaveApplyRoomDiscountBMO saveApplyRoomDiscountBMOImpl;

    @Autowired
    private IUpdateApplyRoomDiscountBMO updateApplyRoomDiscountBMOImpl;

    @Autowired
    private IDeleteApplyRoomDiscountBMO deleteApplyRoomDiscountBMOImpl;

    @Autowired
    private IGetApplyRoomDiscountBMO getApplyRoomDiscountBMOImpl;

    @Autowired
    private IAuditApplyRoomDiscountBMO auditApplyRoomDiscountBMOImpl;

    @Autowired
    private ISaveApplyRoomDiscountTypeBMO saveApplyRoomDiscountTypeBMOImpl;

    @Autowired
    private IUpdateApplyRoomDiscountTypeBMO updateApplyRoomDiscountTypeBMOImpl;

    @Autowired
    private IDeleteApplyRoomDiscountTypeBMO deleteApplyRoomDiscountTypeBMOImpl;

    @Autowired
    private IGetApplyRoomDiscountTypeBMO getApplyRoomDiscountTypeBMOImpl;

    @Autowired
    private IApplyRoomDiscountInnerServiceSMO applyRoomDiscountInnerServiceSMOImpl;

    @Autowired
    private IFeeDiscountRuleInnerServiceSMO feeDiscountRuleInnerServiceSMOImpl;

    @Autowired
    private IFeeDetailInnerServiceSMO feeDetailInnerServiceSMOImpl;

    @Autowired
    private IUpdateAccountBMO updateAccountBMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    //域
    public static final String DOMAIN_COMMON = "DOMAIN.COMMON";

    //键
    public static final String TOTAL_FEE_PRICE = "TOTAL_FEE_PRICE";

    /**
     * 优惠申请
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/saveApplyRoomDiscount
     * @path /app/applyRoomDiscount/saveApplyRoomDiscount
     */
    @RequestMapping(value = "/saveApplyRoomDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> saveApplyRoomDiscount(@RequestBody JSONObject reqJson) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "roomId", "请求报文中未包含roomId");
        Assert.hasKeyAndValue(reqJson, "roomName", "请求报文中未包含roomName");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含startTime");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含endTime");
        Assert.hasKeyAndValue(reqJson, "applyType", "请求报文中未包含applyType");
        if (!StringUtil.isEmpty(reqJson.getString("startTime"))) {
            String startTime = reqJson.getString("startTime") + " 00:00:00";
            reqJson.put("startTime", startTime);
        }
        if (!StringUtil.isEmpty(reqJson.getString("endTime"))) {
            String endTime = reqJson.getString("endTime") + " 23:59:59";
            reqJson.put("endTime", endTime);
        }
        ApplyRoomDiscountPo applyRoomDiscountPo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountPo.class);
        ApplyRoomDiscountDto applyRoomDiscountDto = new ApplyRoomDiscountDto();
        applyRoomDiscountDto.setCommunityId(applyRoomDiscountPo.getCommunityId());
        applyRoomDiscountDto.setRoomId(applyRoomDiscountPo.getRoomId());
        applyRoomDiscountDto.setFeeId(applyRoomDiscountPo.getFeeId());
        //查询折扣申请表中该房屋下符合条件的最新的一条数据
        List<ApplyRoomDiscountDto> applyRoomDiscountDtos = applyRoomDiscountInnerServiceSMOImpl.queryFirstApplyRoomDiscounts(applyRoomDiscountDto);
        //获取本次填写的开始时间
        Date startDate = simpleDateFormat.parse(applyRoomDiscountPo.getStartTime());
        if (applyRoomDiscountDtos.size() == 0) {
            //空置房优惠可用
            applyRoomDiscountPo.setInUse("0");
            applyRoomDiscountPo.setArdId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_ardId));
            applyRoomDiscountPo.setState(ApplyRoomDiscountDto.STATE_APPLY);
            saveFile(applyRoomDiscountPo);
            return saveApplyRoomDiscountBMOImpl.save(applyRoomDiscountPo);
        } else if (applyRoomDiscountDtos.size() > 0) {
            //取出结束时间
            String endTime = applyRoomDiscountDtos.get(0).getEndTime();
            Date finishTime = simpleDateFormat.parse(endTime);
            if (startDate.getTime() - finishTime.getTime() >= 0) {
                //空置房优惠可用
                applyRoomDiscountPo.setInUse("0");
                applyRoomDiscountPo.setArdId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_ardId));
                applyRoomDiscountPo.setState(ApplyRoomDiscountDto.STATE_APPLY);
                saveFile(applyRoomDiscountPo);
                return saveApplyRoomDiscountBMOImpl.save(applyRoomDiscountPo);
            } else {
                throw new UnsupportedOperationException("该时间段已经申请过空置房,请重新输入空置房申请开始和结束时间");
            }
        } else {
            throw new UnsupportedOperationException("信息错误");
        }
    }

    /**
     * 上传图片
     *
     * @param applyRoomDiscountPo
     */
    public void saveFile(ApplyRoomDiscountPo applyRoomDiscountPo) {
        //获取图片
        List<String> photos = applyRoomDiscountPo.getPhotos();
        FileRelPo fileRelPo = new FileRelPo();
        fileRelPo.setFileRelId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_relId));
        fileRelPo.setObjId(applyRoomDiscountPo.getArdId());
        //table表示表存储 ftp表示ftp文件存储
        fileRelPo.setSaveWay("ftp");
        fileRelPo.setCreateTime(new Date());
        //图片上传
        if (photos != null && photos.size() > 0) {
            //19000表示装修图片
            fileRelPo.setRelTypeCd("19000");
            for (String photo : photos) {
                fileRelPo.setFileRealName(photo);
                fileRelPo.setFileSaveName(photo);
                fileRelInnerServiceSMOImpl.saveFileRel(fileRelPo);
            }
        }
    }

    /**
     * 验房接口
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/updateApplyRoomDiscount
     * @path /app/applyRoomDiscount/updateApplyRoomDiscount
     */
    @RequestMapping(value = "/updateApplyRoomDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> updateApplyRoomDiscount(@RequestBody JSONObject reqJson, @RequestHeader(value = "user-id") String userId) {
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含验房状态");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含开始时间");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含结束时间");
        Assert.hasKeyAndValue(reqJson, "checkRemark", "请求报文中未包含验房说明");
        Assert.hasKeyAndValue(reqJson, "ardId", "ardId不能为空");
        ApplyRoomDiscountDto applyRoomDiscountDto = new ApplyRoomDiscountDto();
        applyRoomDiscountDto.setArdId(reqJson.getString("ardId"));
        //查询房屋优惠申请信息
        List<ApplyRoomDiscountDto> applyRoomDiscountDtos = applyRoomDiscountInnerServiceSMOImpl.queryApplyRoomDiscounts(applyRoomDiscountDto);
        Assert.listOnlyOne(applyRoomDiscountDtos, "查询房屋优惠信息错误!");
        //获取房屋优惠审核状态
        String state = applyRoomDiscountDtos.get(0).getState();
        if (!StringUtil.isEmpty(state) && !state.equals("1")) {
            throw new IllegalArgumentException("该房屋已验过房,无法再次进行验房!");
        }
        reqJson.put("checkUserId", userId);
        ApplyRoomDiscountPo applyRoomDiscountPo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountPo.class);
        return updateApplyRoomDiscountBMOImpl.update(applyRoomDiscountPo);
    }

    /**
     * 修改优惠申请
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/editApplyRoomDiscount
     * @path /app/applyRoomDiscount/editApplyRoomDiscount
     */
    @RequestMapping(value = "/editApplyRoomDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> editApplyRoomDiscount(@RequestBody JSONObject reqJson, @RequestHeader(value = "user-id") String userId) {
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含验房状态");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含开始时间");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含结束时间");
        Assert.hasKeyAndValue(reqJson, "ardId", "ardId不能为空");
        ApplyRoomDiscountPo applyRoomDiscountPo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountPo.class);
        return updateApplyRoomDiscountBMOImpl.update(applyRoomDiscountPo);
    }

    /**
     * 审批接口
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/updateReviewApplyRoomDiscount
     * @path /app/applyRoomDiscount/updateReviewApplyRoomDiscount
     */
    @RequestMapping(value = "/updateReviewApplyRoomDiscount", method = RequestMethod.POST)
    @Java110Transactional
    public ResponseEntity<String> updateReviewApplyRoomDiscount(@RequestBody JSONObject reqJson, @RequestHeader(value = "user-id") String userId, DataFlowContext dataFlowContext) {
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含验房状态");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含开始时间");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含结束时间");
        Assert.hasKeyAndValue(reqJson, "reviewRemark", "请求报文中未包含验房说明");
        Assert.hasKeyAndValue(reqJson, "ardId", "ardId不能为空");
        ApplyRoomDiscountDto applyRoomDiscountDto = new ApplyRoomDiscountDto();
        applyRoomDiscountDto.setArdId(reqJson.getString("ardId"));
        //查询房屋优惠申请信息
        List<ApplyRoomDiscountDto> applyRoomDiscountDtos = applyRoomDiscountInnerServiceSMOImpl.queryApplyRoomDiscounts(applyRoomDiscountDto);
        Assert.listOnlyOne(applyRoomDiscountDtos, "查询房屋优惠信息错误!");
        //获取房屋优惠审核状态
        String state = applyRoomDiscountDtos.get(0).getState();
        String stateNow = reqJson.getString("state");
        String returnWay = reqJson.getString("returnWay");
        if (!StringUtil.isEmpty(state) && state.equals("4")) {
            throw new IllegalArgumentException("该房屋已审核过,无法再次进行审核!");
        }
        if (reqJson.containsKey("selectedFees") && !StringUtil.isEmpty(reqJson.getString("selectedFees")) && stateNow.equals("4") && "1002".equals(returnWay)) {
            //获取前端传来的缴费历史
            String selectedFees = reqJson.getString("selectedFees");
            JSONArray feeDetailIds = JSON.parseArray(selectedFees);
            //获取优惠
            JSONArray discounts = reqJson.getJSONArray("discounts");
            BigDecimal cashBackAmount = new BigDecimal("0.00");//返现总金额
            for (int i = 0; i < discounts.size(); i++) {
                JSONObject discountObject = discounts.getJSONObject(i);
                if (!reqJson.getString("discountId").equals(discountObject.getString("discountId"))) {
                    continue;
                }
                JSONArray feeDiscountSpecs = discountObject.getJSONArray("feeDiscountSpecs");
                //获取规则id
                String ruleId = discounts.getJSONObject(i).getString("ruleId");
                FeeDiscountRuleDto feeDiscountRuleDto = new FeeDiscountRuleDto();
                feeDiscountRuleDto.setRuleId(ruleId);
                List<FeeDiscountRuleDto> feeDiscountRuleDtos = feeDiscountRuleInnerServiceSMOImpl.queryFeeDiscountRules(feeDiscountRuleDto);
                Assert.listOnlyOne(feeDiscountRuleDtos, "查询折扣规则错误!");
                //获取折扣类型(1: 打折  2:减免  3:滞纳金  4:空置房打折  5:空置房减免)
                String discountSmallType = feeDiscountRuleDtos.get(0).getDiscountSmallType();
                //获取规则
                //String specValue = feeDiscountSpecs.getJSONObject(1).getString("specValue");
                String specValue = getRateSpecValueByFeeDiscountSpecs(feeDiscountSpecs);
                if (!StringUtil.isEmpty(discountSmallType) && (discountSmallType.equals("1") || discountSmallType.equals("4"))) { //打折
                    for (int index = 0; index < feeDetailIds.size(); index++) {
                        String feeDetailId = String.valueOf(feeDetailIds.get(index));
                        FeeDetailDto feeDetailDto = new FeeDetailDto();
                        feeDetailDto.setDetailId(feeDetailId);
                        List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
                        Assert.listOnlyOne(feeDetailDtos, "查询费用明细表错误!");

                        BigDecimal receivedAmount = new BigDecimal(feeDetailDtos.get(0).getReceivedAmount());//获取实收金额
                        BigDecimal spec = new BigDecimal(specValue);//折扣
                        //计算打折后的实收金额
                        BigDecimal money = receivedAmount.multiply(spec);
                        cashBackAmount = cashBackAmount.add(receivedAmount.subtract(money)); //计算优惠的金额
                    }
                } else if (!StringUtil.isEmpty(discountSmallType) && (discountSmallType.equals("2") || discountSmallType.equals("5"))) { //减免
                    for (int index = 0; index < feeDetailIds.size(); index++) {
                        String feeDetailId = String.valueOf(feeDetailIds.get(index));
                        FeeDetailDto feeDetailDto = new FeeDetailDto();
                        feeDetailDto.setDetailId(feeDetailId);
                        List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
                        Assert.listOnlyOne(feeDetailDtos, "查询费用明细表错误!");

                        BigDecimal spec = new BigDecimal(specValue);//减免金額
                        cashBackAmount = cashBackAmount.add(spec); //计算优惠的金额
                    }
                }
                //1表示四舍五入后保留小数点后两位;2表示四舍五入后保留小数点后一位;3表示向上取整;4 表示向下取整;5表示 四舍五入后取整 其他情况默认四舍五入后保留小数点后两位
                String val = CommunitySettingFactory.getValue(reqJson.getString("communityId"), TOTAL_FEE_PRICE);
                if (StringUtil.isEmpty(val)) {
                    //1表示四舍五入后保留小数点后两位;2表示四舍五入后保留小数点后一位;3表示向上取整;4 表示向下取整;5表示 四舍五入后取整 其他情况默认四舍五入后保留小数点后两位
                    val = MappingCache.getValue(DOMAIN_COMMON, TOTAL_FEE_PRICE);
                }
                DecimalFormat df = new DecimalFormat("0.00");
                if (StringUtil.isEmpty(reqJson.getString("refundAmount"))) {
                    if (!StringUtil.isEmpty(val) && val.equals("2")) { //2表示四舍五入后保留小数点后一位
                        BigDecimal bigDecimal = cashBackAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("3")) { //3表示向上取整
                        BigDecimal bigDecimal = cashBackAmount.setScale(0, BigDecimal.ROUND_UP);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("4")) { //4 表示向下取整
                        BigDecimal bigDecimal = cashBackAmount.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("5")) { //5表示 四舍五入后取整
                        BigDecimal bigDecimal = cashBackAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        BigDecimal bigDecimal1 = bigDecimal.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal1));
                    }
                    if ((!val.equals("2") && !val.equals("3") && !val.equals("4") && !val.equals("5")) || StringUtil.isEmpty(val)) {
                        BigDecimal bigDecimal = cashBackAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                } else {
                    if (!StringUtil.isEmpty(val) && val.equals("2")) { //2表示四舍五入后保留小数点后一位
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("3")) { //3表示向上取整
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(0, BigDecimal.ROUND_UP);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("4")) { //4 表示向下取整
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("5")) { //5表示 四舍五入后取整
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        BigDecimal bigDecimal1 = bigDecimal.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal1));
                    }
                    if ((!val.equals("2") && !val.equals("3") && !val.equals("4") && !val.equals("5")) || StringUtil.isEmpty(val)) {
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("cashBackAmount", String.valueOf(bigDecimal));
                    }
                }
                //处理账户返现
                JSONArray businesses = new JSONArray();
                updateAccountBMOImpl.cashBackAccount(reqJson, dataFlowContext, businesses);
                reqJson.put("inUse", 1);
                if (StringUtil.isEmpty(reqJson.getString("refundAmount"))) {
                    if (!StringUtil.isEmpty(val) && val.equals("2")) { //2表示四舍五入后保留小数点后一位
                        BigDecimal bigDecimal = cashBackAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("3")) { //3表示向上取整
                        BigDecimal bigDecimal = cashBackAmount.setScale(0, BigDecimal.ROUND_UP);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("4")) { //4 表示向下取整
                        BigDecimal bigDecimal = cashBackAmount.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("5")) { //5表示 四舍五入后取整
                        BigDecimal bigDecimal = cashBackAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        BigDecimal bigDecimal1 = bigDecimal.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal1));
                    }
                    if ((!val.equals("2") && !val.equals("3") && !val.equals("4") && !val.equals("5")) || StringUtil.isEmpty(val)) {
                        BigDecimal bigDecimal = cashBackAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                } else {
                    if (!StringUtil.isEmpty(val) && val.equals("2")) { //2表示四舍五入后保留小数点后一位
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("3")) { //3表示向上取整
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(0, BigDecimal.ROUND_UP);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("4")) { //4 表示向下取整
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                    if (!StringUtil.isEmpty(val) && val.equals("5")) { //5表示 四舍五入后取整
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(1, BigDecimal.ROUND_HALF_UP);
                        BigDecimal bigDecimal1 = bigDecimal.setScale(0, BigDecimal.ROUND_DOWN);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal1));
                    }
                    if ((!val.equals("2") && !val.equals("3") && !val.equals("4") && !val.equals("5")) || StringUtil.isEmpty(val)) {
                        BigDecimal refundAmount = new BigDecimal(reqJson.getString("refundAmount"));
                        BigDecimal bigDecimal = refundAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
                        reqJson.put("returnAmount", String.valueOf(bigDecimal));
                    }
                }
            }
        } else {
            reqJson.put("inUse", 0);
        }
        reqJson.put("reviewUserId", userId);
        ApplyRoomDiscountPo applyRoomDiscountPo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountPo.class);
        return updateApplyRoomDiscountBMOImpl.update(applyRoomDiscountPo);
    }

    /**
     * 处理返还金额
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/dealWithRefundAmount
     * @path /app/applyRoomDiscount/dealWithRefundAmount
     */
    @RequestMapping(value = "/dealWithRefundAmount", method = RequestMethod.POST)
    @Java110Transactional
    public JSONObject dealWithRefundAmount(@RequestBody JSONObject reqJson, @RequestHeader(value = "user-id") String userId, DataFlowContext dataFlowContext) {
        JSONArray fees = reqJson.getJSONArray("fees");
        if (fees != null && fees.size() > 0) {
            JSONObject fee = null;
            BigDecimal cashBackAmount = new BigDecimal("0.00");//返现总金额
            for (int index = 0; index < fees.size(); index++) {
                fee = fees.getJSONObject(index);
                if (fee.containsKey("checked") && !StringUtil.isEmpty(fee.getString("checked")) && fee.getString("checked").equals("true")) {
                    //获取优惠
                    JSONArray discounts = reqJson.getJSONArray("discounts");
                    for (int i = 0; i < discounts.size(); i++) {
                        JSONObject discountObject = discounts.getJSONObject(i);
                        if (!reqJson.getString("discountId").equals(discountObject.getString("discountId"))) {
                            continue;
                        }
                        JSONArray feeDiscountSpecs = discountObject.getJSONArray("feeDiscountSpecs");
                        //获取规则id
                        String ruleId = discounts.getJSONObject(i).getString("ruleId");
                        FeeDiscountRuleDto feeDiscountRuleDto = new FeeDiscountRuleDto();
                        feeDiscountRuleDto.setRuleId(ruleId);
                        List<FeeDiscountRuleDto> feeDiscountRuleDtos = feeDiscountRuleInnerServiceSMOImpl.queryFeeDiscountRules(feeDiscountRuleDto);
                        Assert.listOnlyOne(feeDiscountRuleDtos, "查询折扣规则错误!");
                        //获取折扣类型(1: 打折  2:减免  3:滞纳金  4:空置房打折  5:空置房减免)
                        String discountSmallType = feeDiscountRuleDtos.get(0).getDiscountSmallType();
                        String specValue = getRateSpecValueByFeeDiscountSpecs(feeDiscountSpecs);
                        if (!StringUtil.isEmpty(discountSmallType) && (discountSmallType.equals("1") || discountSmallType.equals("4"))) { //打折
                            FeeDetailDto feeDetailDto = new FeeDetailDto();
                            feeDetailDto.setDetailId(fee.getString("detailId"));
                            List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
                            Assert.listOnlyOne(feeDetailDtos, "查询费用明细表错误!");
                            BigDecimal receivedAmount = new BigDecimal(feeDetailDtos.get(0).getReceivedAmount());//获取实收金额
                            BigDecimal spec = new BigDecimal(specValue);//折扣
                            //计算打折后的实收金额
                            BigDecimal money = receivedAmount.multiply(spec);
                            cashBackAmount = cashBackAmount.add(receivedAmount.subtract(money)); //计算优惠的金额
                        } else if (!StringUtil.isEmpty(discountSmallType) && (discountSmallType.equals("2") || discountSmallType.equals("5"))) { //减免
                            FeeDetailDto feeDetailDto = new FeeDetailDto();
                            feeDetailDto.setDetailId(fee.getString("detailId"));
                            List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
                            Assert.listOnlyOne(feeDetailDtos, "查询费用明细表错误!");
                            BigDecimal spec = new BigDecimal(specValue);//减免金額
                            cashBackAmount = cashBackAmount.add(spec); //计算优惠的金额
                        }
                    }
                }
            }
            DecimalFormat df = new DecimalFormat("0.00");
            reqJson.put("returnAmount", df.format(cashBackAmount));
        }
        return reqJson;
    }

    /**
     * 89002020980001	102020001	月份
     * 89002020980002	102020001	打折率
     * 89002020980003	102020002	月份
     * 89002020980004	102020002	减免金额
     * 89002020980005	102020003	滞纳金
     * 89002020980006	102020004	滞纳金
     * 89002020980007	102020005	月份
     * 89002020980008	102020005	打折率
     * 89002020980009	102020005	欠费时长
     * 89002020980010	102020006	月份
     * 89002020980011	102020006	减免金额
     * 89002020980012	102020007	月份
     * 89002020980013	102020007	打折率
     *
     * @param feeDiscountSpecs
     * @return
     */
    private String getRateSpecValueByFeeDiscountSpecs(JSONArray feeDiscountSpecs) {

        for (int specIndex = 0; specIndex < feeDiscountSpecs.size(); specIndex++) {
            if (SPEC_RATE.equals(feeDiscountSpecs.getJSONObject(specIndex).getString("specId"))) {
                return feeDiscountSpecs.getJSONObject(specIndex).getString("specValue");
            }
            if ("89002020980002".equals(feeDiscountSpecs.getJSONObject(specIndex).getString("specId"))) {
                return feeDiscountSpecs.getJSONObject(specIndex).getString("specValue");
            }
            if ("89002020980004".equals(feeDiscountSpecs.getJSONObject(specIndex).getString("specId"))) {
                return feeDiscountSpecs.getJSONObject(specIndex).getString("specValue");
            }
            if ("89002020980008".equals(feeDiscountSpecs.getJSONObject(specIndex).getString("specId"))) {
                return feeDiscountSpecs.getJSONObject(specIndex).getString("specValue");
            }
            if ("89002020980011".equals(feeDiscountSpecs.getJSONObject(specIndex).getString("specId"))) {
                return feeDiscountSpecs.getJSONObject(specIndex).getString("specValue");
            }
        }

        throw new IllegalArgumentException("未找到 打折系数");
    }

    /**
     * 验房接口
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/auditApplyRoomDiscount
     * @path /app/applyRoomDiscount/auditApplyRoomDiscount
     */
    @RequestMapping(value = "/auditApplyRoomDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> auditApplyRoomDiscount(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含小区信息");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含验房状态");
        Assert.hasKeyAndValue(reqJson, "reviewRemark", "请求报文中未包含审核说明");
        Assert.hasKeyAndValue(reqJson, "ardId", "ardId不能为空");
        ApplyRoomDiscountPo applyRoomDiscountPo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountPo.class);
        return auditApplyRoomDiscountBMOImpl.audit(applyRoomDiscountPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/deleteApplyRoomDiscount
     * @path /app/applyRoomDiscount/deleteApplyRoomDiscount
     */
    @RequestMapping(value = "/deleteApplyRoomDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> deleteApplyRoomDiscount(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");
        Assert.hasKeyAndValue(reqJson, "ardId", "ardId不能为空");
        ApplyRoomDiscountPo applyRoomDiscountPo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountPo.class);
        return deleteApplyRoomDiscountBMOImpl.delete(applyRoomDiscountPo);
    }

    /**
     * 查询优惠申请
     *
     * @param communityId 小区ID
     * @return
     * @serviceCode /applyRoomDiscount/queryApplyRoomDiscount
     * @path /app/applyRoomDiscount/queryApplyRoomDiscount
     */
    @RequestMapping(value = "/queryApplyRoomDiscount", method = RequestMethod.GET)
    public ResponseEntity<String> queryApplyRoomDiscount(@RequestParam(value = "communityId") String communityId,
                                                         @RequestParam(value = "ardId", required = false) String ardId,
                                                         @RequestParam(value = "roomName", required = false) String roomName,
                                                         @RequestParam(value = "roomId", required = false) String roomId,
                                                         @RequestParam(value = "state", required = false) String state,
                                                         @RequestParam(value = "applyType", required = false) String applyType,
                                                         @RequestParam(value = "feeId", required = false) String feeId,
                                                         @RequestParam(value = "page") int page,
                                                         @RequestParam(value = "row") int row) {
        ApplyRoomDiscountDto applyRoomDiscountDto = new ApplyRoomDiscountDto();
        applyRoomDiscountDto.setArdId(ardId);
        applyRoomDiscountDto.setPage(page);
        applyRoomDiscountDto.setRow(row);
        applyRoomDiscountDto.setCommunityId(communityId);
        applyRoomDiscountDto.setRoomName(roomName);
        applyRoomDiscountDto.setRoomId(roomId);
        applyRoomDiscountDto.setState(state);
        applyRoomDiscountDto.setApplyType(applyType);
        applyRoomDiscountDto.setFeeId(feeId);
        return getApplyRoomDiscountBMOImpl.get(applyRoomDiscountDto);
    }

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/saveApplyRoomDiscountType
     * @path /app/applyRoomDiscount/saveApplyRoomDiscountType
     */
    @RequestMapping(value = "/saveApplyRoomDiscountType", method = RequestMethod.POST)
    public ResponseEntity<String> saveApplyRoomDiscountType(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "typeName", "请求报文中未包含typeName");
        ApplyRoomDiscountTypePo applyRoomDiscountTypePo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountTypePo.class);
        return saveApplyRoomDiscountTypeBMOImpl.save(applyRoomDiscountTypePo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/updateApplyRoomDiscountType
     * @path /app/applyRoomDiscount/updateApplyRoomDiscountType
     */
    @RequestMapping(value = "/updateApplyRoomDiscountType", method = RequestMethod.POST)
    public ResponseEntity<String> updateApplyRoomDiscountType(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
        Assert.hasKeyAndValue(reqJson, "typeName", "请求报文中未包含typeName");
        Assert.hasKeyAndValue(reqJson, "applyType", "applyType不能为空");
        ApplyRoomDiscountTypePo applyRoomDiscountTypePo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountTypePo.class);
        return updateApplyRoomDiscountTypeBMOImpl.update(applyRoomDiscountTypePo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /applyRoomDiscount/deleteApplyRoomDiscountType
     * @path /app/applyRoomDiscount/deleteApplyRoomDiscountType
     */
    @RequestMapping(value = "/deleteApplyRoomDiscountType", method = RequestMethod.POST)
    public ResponseEntity<String> deleteApplyRoomDiscountType(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");
        Assert.hasKeyAndValue(reqJson, "applyType", "applyType不能为空");
        ApplyRoomDiscountTypePo applyRoomDiscountTypePo = BeanConvertUtil.covertBean(reqJson, ApplyRoomDiscountTypePo.class);
        return deleteApplyRoomDiscountTypeBMOImpl.delete(applyRoomDiscountTypePo);
    }

    /**
     * 微信删除消息模板
     *
     * @param communityId 小区ID
     * @return
     * @serviceCode /applyRoomDiscount/queryApplyRoomDiscountType
     * @path /app/applyRoomDiscount/queryApplyRoomDiscountType
     */
    @RequestMapping(value = "/queryApplyRoomDiscountType", method = RequestMethod.GET)
    public ResponseEntity<String> queryApplyRoomDiscountType(@RequestParam(value = "communityId") String communityId,
                                                             @RequestParam(value = "applyType", required = false) String applyType,
                                                             @RequestParam(value = "typeName", required = false) String typeName,
                                                             @RequestParam(value = "page") int page,
                                                             @RequestParam(value = "row") int row) {
        ApplyRoomDiscountTypeDto applyRoomDiscountTypeDto = new ApplyRoomDiscountTypeDto();
        applyRoomDiscountTypeDto.setPage(page);
        applyRoomDiscountTypeDto.setRow(row);
        applyRoomDiscountTypeDto.setCommunityId(communityId);
        applyRoomDiscountTypeDto.setApplyType(applyType);
        applyRoomDiscountTypeDto.setTypeName(typeName);
        return getApplyRoomDiscountTypeBMOImpl.get(applyRoomDiscountTypeDto);
    }
}