CustomerConvert.java 3.98 KB
package cn.com.glsx.entmicro.convert.outbound;

import cn.com.glsx.entmicro.enums.outbound.EndTypeReasonEnum;
import cn.com.glsx.entmicro.model.response.outbound.OutBoundTaskCustomerExportResponse;
import cn.com.glsx.entmicro.model.response.outbound.OutBoundTaskCustomerRpcResponse;
import cn.com.glsx.entmicro.response.outbound.OutBoundTaskCustomerRopResponse;
import cn.com.glsx.entmicro.utils.Pagination;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yelq
 * @Version 1.0
 * @Description:
 * @Date 2023/6/6 11:08
 */
public class CustomerConvert {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

    /**
     * 分页转化
     * @param page
     * @return
     */
    public static Pagination<OutBoundTaskCustomerRopResponse> toPage(RpcPagination<OutBoundTaskCustomerRpcResponse> page){
        Pagination<OutBoundTaskCustomerRopResponse> pagination = new Pagination<>();
        pagination.setPageNum(page.getPageNum());
        pagination.setPageSize(page.getPageSize());
        pagination.setData(toOutBoundTaskCustomerRopResponses(page.getList()));
        pagination.setCount((int)page.getTotal());
        pagination.setPageSize(page.getPageSize());
        return pagination;
    }

    /**
     * 列表转化
     *
     * @param list
     * @return
     */
    public static List<OutBoundTaskCustomerRopResponse> toOutBoundTaskCustomerRopResponses(List<OutBoundTaskCustomerRpcResponse> list) {
        if (list == null) {
            return null;
        } else if (list.isEmpty()) {
            return new ArrayList<>();
        }
        return list.stream().map(CustomerConvert::toOutBoundTaskCustomerRopResponse).collect(Collectors.toList());
    }

    /**
     * 客户返回对象转化为导出对象
     *
     * @param source
     * @return
     */
    public static OutBoundTaskCustomerRopResponse toOutBoundTaskCustomerRopResponse(OutBoundTaskCustomerRpcResponse source) {
        if (source == null) {
            return null;
        }
        return OutBoundTaskCustomerRopResponse.builder()
                .phone(source.getPhone())
                .answer(source.getTalkingTimeLen() != null && source.getTalkingTimeLen() > 0 ? "已接听" : source.getTalkingTimeLen() == null ? null : ("未接听(" + EndTypeReasonEnum.getMsg(source.getEndTypeReason()) + ")"))
                .startTime(createStartTime(source.getStartTime()))
                .reCallTimes(source.getCallTimes() == null || source.getCallTimes() == 0 ? 0 : source.getCallTimes() - 1)
                .talkingTimeLen(source.getTalkingTimeLen() == null ? "0s" : source.getTalkingTimeLen() + "s")
                .talkingTimeLeng(source.getTalkingTimeLeng() == null ? "0分钟" : source.getTalkingTimeLeng() + "分钟")
                .intent(StringUtils.isBlank(source.getIntent()) ? "否" : "是")
                .carNumber(source.getCarNumber())
                .lastInsuranceCompany(source.getLastInsuranceCompany())
                .insuranceEndTime(changeDate(source.getInsuranceEndTime()))
                .build();
    }

    private static String changeDate(Date insuranceEndTime) {
        if (insuranceEndTime == null) {
            return null;
        }
        try {
            return sdf.format(insuranceEndTime);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 转换开始时间
     *
     * @param source
     * @return
     */
    private static String createStartTime(Long source) {
        if (source == null) {
            return null;
        }
        try {
            return simpleDateFormat.format(new Date(source));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}