package cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel;

import cn.iocoder.yudao.module.system.dal.dataobject.biz.BalanceDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.BalanceRecordDO;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = false)
public class BalanceExportExcelVO {

    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    @ExcelProperty("长者姓名")
    private String elderName;

    @ExcelProperty("床位信息")
    private String bedName;

    @ExcelProperty("入住状态")
    private Integer inStatus;

    @ExcelProperty("余额")
    private BigDecimal amount;

    @ExcelProperty("余额明细")
    private String balanceDetail;

    public static BalanceExportExcelVO fromBalance(BalanceDO balance) {
        return BalanceExportExcelVO.builder()
                .elderName(balance.getElderName())
                .bedName(balance.getBedName())
                .inStatus(balance.getInStatus())
                .amount(balance.getAmount() == null ? BigDecimal.ZERO : balance.getAmount())
                .balanceDetail(formatBalanceDetail(balance.getBalanceRecord()))
                .build();
    }

    private static String formatBalanceDetail(List<BalanceRecordDO> balanceRecords) {
        if (balanceRecords == null || balanceRecords.isEmpty()) {
            return "";
        }
        return balanceRecords.stream()
                .map(BalanceExportExcelVO::formatBalanceRecord)
                .collect(Collectors.joining(System.lineSeparator()));
    }

    private static String formatBalanceRecord(BalanceRecordDO record) {
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME_PATTERN);
        String createdTime = record.getCreatedTime() == null ? "" : formatter.format(record.getCreatedTime());
        String type = formatType(record.getType());
        String payType = formatPayType(record.getPayType());
        String remarks = record.getRemarks() == null ? "" : record.getRemarks();
        String createdBy = record.getCreatedBy() == null ? "" : record.getCreatedBy();
        String changeAmount = record.getChangeAmount() == null ? BigDecimal.ZERO.toPlainString() : record.getChangeAmount().toPlainString();
        return String.format("时间:%s; 类型:%s; 支付方式:%s; 变动金额:%s; 备注:%s; 操作人:%s",
                createdTime, type, payType, changeAmount, remarks, createdBy);
    }

    private static String formatType(Integer type) {
        if (type == null) {
            return "";
        }
        switch (type) {
            case 1:
                return "充值";
            case 2:
                return "退款";
            case 3:
                return "消费";
            default:
                return String.valueOf(type);
        }
    }

    private static String formatPayType(String payType) {
        if (payType == null || payType.trim().isEmpty()) {
            return "";
        }
        switch (payType.trim()) {
            case "1":
                return "支付宝";
            case "2":
                return "微信";
            default:
                return payType;
        }
    }
}
