from typing import List
from lib.all import *


# Assume these classes exist and mimic the Go structs
class ConsumeHistory:
    def __init__(self, ct=None, change=None, s_type=None, type_id=None, uid=None):
        self.ct = ct
        self.change = change
        self.s_type = s_type
        self.type_id = type_id
        self.uid = uid

    def GetChange(self):
        return self.change

    def GetSType(self):
        return self.s_type

    def GetTypeId(self):
        return self.type_id

    def GetCt(self):
        return self.ct

    def GetUid(self):
        return self.uid


class CHSt:
    def __init__(self, ct=None, desc=None, change=None):
        self.ct = ct
        self.desc = desc
        self.change = change


# Assume these constants are defined similarly to the Go constants
CHTypeCost = 1  # 消费明细(金币)
CHTypeCharge = 2  # 充值明细(金币)
CHTypeIncome = 3  # 收入明细(钻石)
CHTypeWithdraw = 4  # 提现明细(钻石)

CHSTypeCostContact = 10001  # 消费明细,联系方式
CHSTypeCostRefundContactWechat = 10002  # 消费明细,微信联系方式退款
CHSTypeCostMembership = 10003  # 消费明细,会员资格解锁(伪金币记录,会员资格解锁中间无转金币过程)
CHSTypeCostRefundMembership = 10004  # 消费明细,会员资格解锁退款(伪金币记录,会员资格解锁中间无转金币过程)

CHSTypeChargeUser = 20001  # 充值明细,用户自己冲
CHSTypeChargeOp = 20002  # 充值明细,OP充值
CHSTypeChargeRefundCoins = 20003  # 充值明细,金币退款
CHSTypeChargeRefundMembership = 20004  # 充值明细,会员退款
CHSTypeChargeMembership = 20005  # 充值明细,会员充值
CHSTypeChargeRefundContactWechat = 20006  # 充值明细,微信金币退款
CHSTypeChargeZoneMoment = 20007  # 充值明细,动态解锁
CHSTypeChargeZoneAdmission = 20008  # 充值明细,空间会员
CHSTypeChargeZoneSuperfanship = 20009  # 充值明细,空间超粉
CHSTypeChargeZoneRefundAdmission = 20010  # 充值明细,空间普通会员退款
CHSTypeChargeZoneRefundMoment = 20011  # 充值明细,空间动态退款
CHSTypeChargeZoneRefundSuperfanship = 20012  # 充值明细,空间动态退款

CHSTypeIncomeContact = 30001  # 收入明细,联系方式
CHSTypeIncomeInvite = 30002  # 收入明细,邀请分成
CHSTypeIncomeRefundMembership = 30003  # 收入明细,会员退款
CHSTypeIncomeRefundContactWechat = 30004  # 收入明细,微信退款
CHSTypeIncomeThirdPartner = 30005  # 收入明细,代运营
CHSTypeIncomeCollaborator = 30006  # 收入明细,协作者
CHSTypeIncomeZoneStreamer = 30007  # 收入明细,主播空间收益
CHSTypeIncomeMembership = 30008  # 收入明细,会员
CHSTypeIncomeRefundZoneAdmission = 30009  # 收入明细,空间普通会员分成退款
CHSTypeIncomeRefundThirdPartner = 30010  # 收入明细,代运营
CHSTypeIncomeRefundCollaborator = 30011  # 收入明细,协作者
CHSTypeIncomeRefundZoneStreamer = 30012  # 收入明细,主播空间收益

CHSTypeWithdrawDiamondAuto = 40001  # 自动提现明细
CHSTypeWithdrawDiamondHvyogo = 40002  # 慧用工提现明细
CHSTypeWithdrawDiamondReversal = 40003  # 提现冲正明细

ProductIdOpCoin = "op_coin"  # op充值的金币

ProductIdH5CustomCoin = "h5_custom_coin"  # H5 自定义金币
ProductIdH5Coin500 = "h5_coin_500"  # H5 500币
ProductIdH5Coin1000 = "h5_coin_1000"  # H5 1000币
ProductIdH5Coin3000 = "h5_coin_3000"  # H5 3000币
ProductIdH5Coin5000 = "h5_coin_5000"  # H5 5000币
ProductIdH5Coin8000 = "h5_coin_8000"  # H5 8000币
ProductIdH5Coin10000 = "h5_coin_10000"  # H5 10000币
ProductIdH5Coin15000 = "h5_coin_15000"  # H5 15000币
ProductIdH5Coin18000 = "h5_coin_18000"  # H5 18000币
ProductIdH5Coin20000 = "h5_coin_20000"  # H5 20000币

ProductIdIosCoin70 = "70_gold"
ProductIdIosCoin350 = "350_gold"
ProductIdIosCoin686 = "686_gold"
ProductIdIosCoin2086 = "2086_gold"
ProductIdIosCoin4886 = "4886_gold"
ProductIdIosCoin6986 = "6986_gold"
ProductIdIosCoin9086 = "9086_gold"
ProductIdIosCoin13986 = "13986_gold"
ProductIdIosCoin20986 = "20986_gold"

ProductIdContactWechat = "contact_wechat"  # 微信联系方式
ProductIdH5ContactWechat = "h5_contact_wechat"  # h5的联系方式,rmb直接解锁
ProductIdSuperfanGiftContactWechat = "superfan_gift_contact_wechat"  # 超粉赠送的联系方式

ProductIdMembership = "membership"  # 会员
ProductIdH5Membership = "h5_membership"  # 会员

ProductIdH5ZoneMoment = "h5_zone_moment"  # 空间动态
ProductIdH5ZoneAdmission = "h5_zone_admission"  # 空间普通会员
ProductIdH5ZoneIronfanship = "h5_zone_ironfanship"  # 空间铁粉
ProductIdH5ZoneSuperfanship = "h5_zone_superfanship"  # 空间超粉

ProductIdOuterRaven = "outer_raven"  # 瑞文测试


def get_type_id_desc(type_id):  # Placeholder function
    return f" (Type ID: {type_id})"


def ch_list_income(ch_list: List[ConsumeHistory]) -> List[CHSt]:
    """
    Translates the provided Go code snippet to Python.

    Processes a list of ConsumeHistory objects and returns a list of CHSt objects.
    """

    list_chst = []
    for ch_db in ch_list:
        item = CHSt(ct=ch_db.ct)
        change_mark = "-"
        if ch_db.GetChange() >= 0:
            change_mark = "+"

        if ch_db.GetSType() == CHSTypeIncomeContact:
            item.desc = "解锁微信"
            item.change = change_mark + f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeInvite:
            item.desc = "邀请收益"
            item.change = change_mark + f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeRefundMembership:
            item.desc = "用户会员退款"
            item.change = f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeRefundContactWechat:
            item.desc = "用户微信退款"
            item.change = f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeThirdPartner:
            item.desc = "代运营收益" + get_type_id_desc(ch_db.GetTypeId())
            if ch_db.GetTypeId() == ProductIdH5ZoneAdmission:
                item.desc = "空间成员-代运营"
            elif ch_db.GetTypeId() == ProductIdH5ZoneMoment:
                item.desc = "空间动态-代运营"
            elif ch_db.GetTypeId() == ProductIdH5ZoneSuperfanship:
                item.desc = "空间超粉-代运营"
            elif ch_db.GetTypeId() == ProductIdContactWechat:
                if ch_db.GetCt() >= 1722497400:
                    item.desc = f"解锁微信({ch_db.GetUid()})-代运营"
            elif ch_db.GetTypeId() == ProductIdSuperfanGiftContactWechat:
                if ch_db.GetCt() >= 1722497400:
                    item.desc = f"超粉送微信({ch_db.GetUid()})-代运营"
            item.change = change_mark + f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeCollaborator:
            item.desc = "协作收益" + get_type_id_desc(ch_db.GetTypeId())
            if ch_db.GetTypeId() == ProductIdH5ZoneAdmission:
                item.desc = "空间成员-合伙人"
            elif ch_db.GetTypeId() == ProductIdH5ZoneMoment:
                item.desc = "空间动态-合伙人"
            elif ch_db.GetTypeId() == ProductIdH5ZoneSuperfanship:
                item.desc = "空间超粉-合伙人"
            elif ch_db.GetTypeId() == ProductIdContactWechat:
                if ch_db.GetCt() >= 1722497400:
                    item.desc = f"解锁微信({ch_db.GetUid()})-合伙人"
            elif ch_db.GetTypeId() == ProductIdSuperfanGiftContactWechat:
                if ch_db.GetCt() >= 1722497400:
                    item.desc = f"超粉送微信({ch_db.GetUid()})-合伙人"
            item.change = change_mark + f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeZoneStreamer:
            item.desc = "空间收益" + get_type_id_desc(ch_db.GetTypeId())
            if ch_db.GetTypeId() == ProductIdH5ZoneAdmission:
                item.desc = "空间成员"
            elif ch_db.GetTypeId() == ProductIdH5ZoneMoment:
                item.desc = "空间动态"
            elif ch_db.GetTypeId() == ProductIdH5ZoneSuperfanship:
                item.desc = "空间超粉"
            item.change = change_mark + f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeMembership:
            item.desc = "永久会员收益" + get_type_id_desc(ch_db.GetTypeId())
            item.change = change_mark + f"{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeRefundZoneAdmission:
            item.desc = "(旧)空间退款" + get_type_id_desc(ch_db.GetTypeId())
            item.change = f"{ch_db.GetChange()}钻石"
            if ch_db.GetChange() == 0:  # This logic seems unusual, but it's from the original code
                item.change = f"-{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeRefundThirdPartner:
            item.desc = "代运营空间退款" + get_type_id_desc(ch_db.GetTypeId())
            if ch_db.GetTypeId() == ProductIdH5ZoneAdmission:
                item.desc = "空间成员退款-代运营"
            elif ch_db.GetTypeId() == ProductIdH5ZoneMoment:
                item.desc = "空间动态退款-代运营"
            elif ch_db.GetTypeId() == ProductIdH5ZoneSuperfanship:
                item.desc = "空间超粉退款-代运营"
            item.change = f"{ch_db.GetChange()}钻石"
            if ch_db.GetChange() == 0:
                item.change = f"-{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeRefundCollaborator:
            item.desc = "合伙人空间退款" + get_type_id_desc(ch_db.GetTypeId())
            if ch_db.GetTypeId() == ProductIdH5ZoneAdmission:
                item.desc = "空间成员退款-合伙人"
            elif ch_db.GetTypeId() == ProductIdH5ZoneMoment:
                item.desc = "空间动态退款-合伙人"
            elif ch_db.GetTypeId() == ProductIdH5ZoneSuperfanship:
                item.desc = "空间动态退款-合伙人"  # This seems like a potential typo (should it be 超粉?)
            item.change = f"{ch_db.GetChange()}钻石"
            if ch_db.GetChange() == 0:
                item.change = f"-{ch_db.GetChange()}钻石"
        elif ch_db.GetSType() == CHSTypeIncomeRefundZoneStreamer:
            item.desc = "空间退款" + get_type_id_desc(ch_db.GetTypeId())
            if ch_db.GetTypeId() == ProductIdH5ZoneAdmission:
                item.desc = "用户退款-空间成员"
            elif ch_db.GetTypeId() == ProductIdH5ZoneMoment:
                item.desc = "用户退款-空间动态"
            elif ch_db.GetTypeId() == ProductIdH5ZoneSuperfanship:
                item.desc = "用户退款-空间超粉"
            item.change = f"{ch_db.GetChange()}钻石"
            if ch_db.GetChange() == 0:
                item.change = f"-{ch_db.GetChange()}钻石"

        list_chst.append(item)

    return list_chst


class S:
    def __init__(self):
        self.mysql_db_vas = Mysql(
            "rm-bp11t1616a1kjvmx5.mysql.rds.aliyuncs.com", 3306, "vas", "root", "Wishpal2024"
        )

    def __del__(self):
        self.mysql_db_vas.close()

    def proc(self, mid, st, et):
        sql = "select * from vas_ch_income where mid=%d and ct>=%d and ct<%d" % (mid, st, et)
        docs = self.mysql_db_vas.query(sql)

        ch_list_db = list()
        for doc in docs:
            # print(doc)
            ch = ConsumeHistory(
                safe_get_int(doc, "ct"),
                safe_get_int(doc, "change"),
                safe_get_int(doc, "stype"),
                safe_get_str(doc, "type_id"),
                safe_get_int(doc, "uid")
            )
            ch_list_db.append(ch)
        ch_list = ch_list_income(ch_list_db)

        csv_w = Csv("352039_11月_主播收益_.csv", ["时间", "钻石变化", "描述"])
        for ch in ch_list:
            d = [
                get_time_str_by_ts(ch.ct), ch.change, ch.desc,
            ]
            csv_w.append([d])


s = S()
s.proc(
    31476,
    get_ts_by_str("2024-11-01 00:00:00"),
    get_ts_by_str("2024-12-01 00:00:00"),
)