java110
2020-06-04 09c25c250ba885c9b5b23429ce08d255a5d8f980
加入费用出账问题
3个文件已修改
2个文件已添加
462 ■■■■■ 已修改文件
java110-core/src/main/java/com/java110/core/factory/GenerateCodeFactory.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-db/src/main/resources/mapper/fee/FeeConfigServiceDaoImplMapper.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-utils/src/main/java/com/java110/utils/exception/TaskTemplateException.java 189 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service-job/src/main/java/com/java110/job/quartz/TaskSystemQuartz.java 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service-job/src/main/java/com/java110/job/task/fee/GenerateRoomBillTemplate.java 232 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-core/src/main/java/com/java110/core/factory/GenerateCodeFactory.java
@@ -127,7 +127,7 @@
    public static final String CODE_PREFIX_junkRequirementId = "69";
    public static final String CODE_PREFIX_returnFeeId = "70";
    public static final String CODE_PREFIX_weChatId = "71";
    public static final String CODE_PREFIX_oweId = "72";
java110-db/src/main/resources/mapper/fee/FeeConfigServiceDaoImplMapper.xml
@@ -29,10 +29,10 @@
        <if test="computingFormula !=null and computingFormula != ''">
            and t.computing_formula= #{computingFormula}
        </if>
        <if test="additionalAmount !=null and additionalAmount != ''">
        <if test="additionalAmount !=null and additionalAmount != '' or additionalAmount == 0">
            and t.additional_amount= #{additionalAmount}
        </if>
        <if test="squarePrice !=null and squarePrice != ''">
        <if test="squarePrice !=null and squarePrice != '' or squarePrice == 0">
            and t.square_price= #{squarePrice}
        </if>
        <if test="isDefault !=null and isDefault != ''">
java110-utils/src/main/java/com/java110/utils/exception/TaskTemplateException.java
New file
@@ -0,0 +1,189 @@
package com.java110.utils.exception;
import com.alibaba.fastjson.JSONObject;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
 * 系统不支持异常
 * Created by wuxw on 2018/4/14.
 */
public class TaskTemplateException extends RuntimeException {
    private Result result;
    private Throwable cause = this;
    public TaskTemplateException(){}
    /**
     * 构造方法
     * @param result 返回值
     * @param cause  异常堆栈
     */
    public TaskTemplateException(Result result, Throwable cause) {
        super(result.getMsg(), cause);
        this.result = result;
    }
    /**
     * 构造方法
     * @param code 返回码
     * @param msg  错误消息
     */
    public TaskTemplateException(int code, String msg) {
        super(msg);
        this.result = new Result(code, msg);
    }
    public TaskTemplateException(String code, String msg) {
        super(msg);
        this.result = new Result(code, msg);
    }
    /**
     * 构造方法
     * @param result 返回值
     * @param detail 具体的返回消息
     */
    public TaskTemplateException(Result result, String detail) {
        super(result.getMsg() + "," + detail);
        this.result = new Result(result.getCode(), result.getMsg() + "," + detail);
    }
    /**
     * 构造方法
     * @param result 返回值
     * @param detail 具体的返回消息
     * @param cause  异常堆栈
     */
    public TaskTemplateException(Result result, String detail, Throwable cause) {
        super(result.getMsg() + "," + detail, cause);
        this.result = new Result(result.getCode(), result.getMsg() + "," + detail);
    }
    /**
     * 构造方法
     * @param code    返回码
     * @param msg    返回消息
     * @param cause 异常堆栈
     */
    public TaskTemplateException(int code, String msg, Throwable cause) {
        super(msg, cause);
        if(cause != null) {
            if(cause.getCause() != null) {
                msg += " cause:" + ExceptionUtils.populateExecption(cause.getCause(), 500);
            }
            msg += " StackTrace:"+ExceptionUtils.populateExecption(cause, 500);
        }
        this.result = new Result(code, msg);
    }
    /**
     * 构造方法
     * @param code    返回码
     * @param cause    异常堆栈
     */
    public TaskTemplateException(int code, Throwable cause) {
        super(cause);
        String msg = "";
        if(cause != null) {
            if(cause.getCause() != null) {
                msg += " cause:" + ExceptionUtils.populateExecption(cause.getCause(), 500);
            }
            msg += " StackTrace:"+ExceptionUtils.populateExecption(cause, 500);
        }
        this.result = new Result(code, msg);
    }
    /**
     *
     * TODO 简单描述该方法的实现功能(可选).
     * @see Throwable#getCause()
     */
    public synchronized Throwable getCause() {
        return (cause==this ? super.getCause() : cause);
    }
    /**
     * 返回异常消息
     * @return 异常消息
     */
    @Override
    public String getMessage() {
        return ExceptionUtils.buildMessage(super.getMessage(), getCause());
    }
    /**
     * 异常
     * @return
     */
    public String toJsonString() {
        JSONObject exceptionJson = JSONObject.parseObject("{\"exception\":{}");
        JSONObject exceptionJsonObj = exceptionJson.getJSONObject("exception");
        if (getResult() != null)
            exceptionJsonObj.putAll(JSONObject.parseObject(result.toString()));
        exceptionJsonObj.put("exceptionTrace",getMessage());
        return exceptionJsonObj.toString();
    }
    @Override
    public void printStackTrace(PrintStream ps) {
        ps.print("<exception>");
        if (getResult() != null) {
            ps.print(result.toString());
        }
        ps.append("<exceptionTrace>");
        Throwable cause = getCause();
        if (cause == null) {
            super.printStackTrace(ps);
        } else {
            ps.println(this);
            ps.print("Caused by: ");
            cause.printStackTrace(ps);
        }
        ps.append("</exceptionTrace>");
        ps.println("</exception>");
    }
    @Override
    public void printStackTrace(PrintWriter pw) {
        pw.print("<exception>");
        if (getResult() != null) {
            pw.print(result.toString());
        }
        pw.append("<exceptionTrace>");
        Throwable cause = getCause();
        if (cause == null) {
            super.printStackTrace(pw);
        } else {
            pw.println(this);
            pw.print("Caused by: ");
            cause.printStackTrace(pw);
        }
        pw.append("</exceptionTrace>");
        pw.println("</exception>");
    }
    /**
     * 返回异常值
     * @return    异常值对象
     */
    public Result getResult() {
        return result;
    }
    public void setResult(Result result) {
        this.result = result;
    }
}
service-job/src/main/java/com/java110/job/quartz/TaskSystemQuartz.java
@@ -1,6 +1,9 @@
package com.java110.job.quartz;
import com.java110.core.smo.community.ICommunityInnerServiceSMO;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.task.TaskDto;
import com.java110.dto.taskAttr.TaskAttrDto;
import com.java110.job.dao.ITaskServiceDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -19,6 +22,9 @@
    @Autowired
    private ITaskServiceDao taskServiceDaoImpl;
    @Autowired
    private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;
    public void initTask() {
@@ -93,4 +99,33 @@
    protected void after(TaskDto taskDto) {
    }
    /**
     * 查询小区信息
     *
     * @return
     */
    protected List<CommunityDto> getAllCommunity() {
        CommunityDto communityDto = new CommunityDto();
        communityDto.setState("1100"); //审核过的小区
        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
        return communityDtos;
    }
    /**
     * 获取当前属性
     * @param taskDto
     * @param specCd
     * @return
     */
    protected TaskAttrDto getCurTaskAttr(TaskDto taskDto, String specCd) {
        List<TaskAttrDto> taskAttrDtos = taskDto.getTaskAttr();
        for (TaskAttrDto taskAttrDto : taskAttrDtos) {
            if (specCd.equals(taskAttrDto.getSpecCd())) {
                return taskAttrDto;
            }
        }
        return null;
    }
}
service-job/src/main/java/com/java110/job/task/fee/GenerateRoomBillTemplate.java
New file
@@ -0,0 +1,232 @@
package com.java110.job.task.fee;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.smo.fee.IFeeConfigInnerServiceSMO;
import com.java110.core.smo.fee.IFeeDetailInnerServiceSMO;
import com.java110.core.smo.fee.IFeeInnerServiceSMO;
import com.java110.core.smo.room.IRoomInnerServiceSMO;
import com.java110.dto.RoomDto;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.fee.BillDto;
import com.java110.dto.fee.BillOweFeeDto;
import com.java110.dto.fee.FeeConfigDto;
import com.java110.dto.fee.FeeDetailDto;
import com.java110.dto.fee.FeeDto;
import com.java110.dto.task.TaskDto;
import com.java110.job.quartz.TaskSystemQuartz;
import com.java110.utils.constant.ResponseConstant;
import com.java110.utils.exception.TaskTemplateException;
import com.java110.utils.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GenerateOwnerBillTemplate
 * @Description TODO  房屋费用账单生成
 * @Author wuxw
 * @Date 2020/6/4 8:33
 * @Version 1.0
 * add by wuxw 2020/6/4
 **/
@Component
public class GenerateRoomBillTemplate extends TaskSystemQuartz {
    private static final String TASK_ATTR_BILL_TYPE = "10002"; // 出账类型
    private static final String TASK_ATTR_VALUE_MONTH = "002"; //按月出账
    private static final String TASK_ATTR_VALUE_DAY = "003"; //按日出账
    private static final String TASK_ATTR_VALUE_YEAR = "001"; //按年出账
    private static final String TASK_ATTR_VALUE_ONCE_MONTH = "005"; //一次性按月出账
    @Autowired
    private IFeeConfigInnerServiceSMO feeConfigInnerServiceSMOImpl;
    @Autowired
    private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
    @Autowired
    private IFeeDetailInnerServiceSMO feeDetailInnerServiceSMOImpl;
    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
    @Override
    protected void process(TaskDto taskDto) throws Exception {
        // 获取小区
        List<CommunityDto> communityDtos = getAllCommunity();
        for (CommunityDto communityDto : communityDtos) {
            GenerateBill(taskDto, communityDto);
        }
    }
    /**
     * 根据小区生成账单
     *
     * @param communityDto
     */
    private void GenerateBill(TaskDto taskDto, CommunityDto communityDto) {
        //查询费用项
        FeeConfigDto feeConfigDto = new FeeConfigDto();
        feeConfigDto.setCommunityId(communityDto.getCommunityId());
        feeConfigDto.setBillType(getCurTaskAttr(taskDto, TASK_ATTR_BILL_TYPE).getValue());
        List<FeeConfigDto> feeConfigDtos = feeConfigInnerServiceSMOImpl.queryFeeConfigs(feeConfigDto);
        for (FeeConfigDto tmpFeeConfigDto : feeConfigDtos) {
            try {
                GenerateBillByFeeConfig(taskDto, tmpFeeConfigDto);
            } catch (Exception e) {
                logger.error("费用出账失败" + tmpFeeConfigDto.getConfigId(), e);
            }
        }
    }
    /**
     * 按费用项来出账
     *
     * @param taskDto
     * @param feeConfigDto
     */
    private void GenerateBillByFeeConfig(TaskDto taskDto, FeeConfigDto feeConfigDto) throws Exception {
        //当前费用项是否
        BillDto billDto = new BillDto();
        billDto.setCurBill("T");
        billDto.setConfigId(feeConfigDto.getConfigId());
        billDto.setCommunityId(feeConfigDto.getCommunityId());
        List<BillDto> billDtos = feeInnerServiceSMOImpl.queryBills(billDto);
        //Assert.listOnlyOne(billDtos, "当前存在多个有效账单" + feeConfigDto.getConfigId());
        if (billDtos != null && billDtos.size() > 1) {
            throw new TaskTemplateException(ResponseConstant.RESULT_CODE_ERROR, "当前存在多个有效账单");
        }
        Date startTime = billDtos == null ? getDefaultStartTime(feeConfigDto.getBillType()) : DateUtil.getDateFromString(billDto.getBillTime(), DateUtil.DATE_FORMATE_STRING_A);
        FeeDto feeDto = new FeeDto();
        feeDto.setConfigId(feeConfigDto.getConfigId());
        feeDto.setCommunityId(feeConfigDto.getCommunityId());
        List<FeeDto> feeDtos = feeInnerServiceSMOImpl.queryFees(feeDto);
        //没有关联费用不做出账
        if (feeDto == null || feeDtos.size() < 1) {
            return;
        }
        for (FeeDto tmpFeeDto : feeDtos) {
            generateFee(startTime, tmpFeeDto);
        }
    }
    /**
     * 生成 费用
     *
     * @param feeDto
     */
    private void generateFee(Date startTime, FeeDto feeDto) {
        Date billEndTime = DateUtil.getCurrentDate();
        if ("2009001".equals(feeDto.getState())) { //判断是否缴费结束
            FeeDetailDto feeDetailDto = new FeeDetailDto();
            feeDetailDto.setCommunityId(feeDto.getCommunityId());
            feeDetailDto.setFeeId(feeDto.getFeeId());
            List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
            if (feeDetailDtos == null || feeDetailDtos.size() < 1) {
                return;//这种数据有问题 不做出账处理
            }
            Date detailTime = feeDetailDtos.get(0).getCreateTime();
            if (detailTime.getTime() < startTime.getTime()) {
                //说明上次出账已经出国 无需再出
                return;
            }
        }
        if (feeDto.getEndTime().getTime() > billEndTime.getTime()) { //当期没有欠费
            return;
        }
        computeFeePriceByRoom(feeDto);
        if (feeDto.getFeePrice() <= 0) {
            return ;//这个没有欠费可算
        }
        BillOweFeeDto billOweFeeDto = new BillOweFeeDto();
        billOweFeeDto.setOweId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_oweId));
        billOweFeeDto.setFeeId(feeDto.getFeeId());
        billOweFeeDto.setBillId("-1");
        billOweFeeDto.setAmountOwed();
    }
    private Date getDefaultStartTime(String billType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (billType.equals(TASK_ATTR_VALUE_MONTH)) {
            calendar.add(Calendar.MONTH, -1);
            return calendar.getTime();
        }
        if (billType.equals(TASK_ATTR_VALUE_DAY)) {
            calendar.add(Calendar.DATE, -1);
            return calendar.getTime();
        }
        if (billType.equals(TASK_ATTR_VALUE_DAY)) {
            calendar.add(Calendar.DATE, -1);
            return calendar.getTime();
        }
        if (billType.equals(TASK_ATTR_VALUE_YEAR)) {
            calendar.add(Calendar.YEAR, -1);
            return calendar.getTime();
        }
        return calendar.getTime();
    }
    /**
     * 根据房屋来算单价
     *
     * @param feeDto
     */
    private void computeFeePriceByRoom(FeeDto feeDto) {
        RoomDto roomDto = new RoomDto();
        roomDto.setCommunityId(feeDto.getCommunityId());
        roomDto.setRoomId(feeDto.getPayerObjId());
        List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
        if (roomDtos == null || roomDtos.size() < 1) { //数据有问题
            return;
        }
        String computingFormula = feeDto.getComputingFormula();
        double feePrice = 0.00;
        if ("1001".equals(computingFormula)) { //面积*单价+附加费
            BigDecimal squarePrice = new BigDecimal(Double.parseDouble(feeDto.getSquarePrice()));
            BigDecimal builtUpArea = new BigDecimal(Double.parseDouble(roomDtos.get(0).getBuiltUpArea()));
            BigDecimal additionalAmount = new BigDecimal(Double.parseDouble(feeDto.getAdditionalAmount()));
            feePrice = squarePrice.multiply(builtUpArea).add(additionalAmount).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
        } else if ("2002".equals(computingFormula)) { // 固定费用
            BigDecimal additionalAmount = new BigDecimal(Double.parseDouble(feeDto.getAdditionalAmount()));
            feePrice = additionalAmount.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
        } else {
            feePrice = -1.00;
        }
        feeDto.setFeePrice(feePrice);
    }
}