package canal

import (
	"github.com/go-mysql-org/go-mysql/canal"
	. "proxy/MsgDef"
	mdb "proxy/Nmysql"
	"proxy/logger"
)

func updateRedisTable(c *RecordChange) {
	if c == nil {
		return
	}
	var synType int
	if c.EventType == canal.InsertAction {
		synType = 1
	} else if c.EventType == canal.DeleteAction {
		synType = 3
	} else {
		synType = 2
	}
	switch c.TableName {
	case TbAcctInfo:
		for _, row := range c.ChgAcctInfo {
			logger.CanalLog.Infof("entry updateRedisTable: TbAcctInfo, type[%d]", synType)
			mdb.UpdateRedisAcctTable(synType, &row)
		}
	case TbOfrDetail:
		for _, row := range c.ChgOfrDetail {
			logger.CanalLog.Infof("entry updateRedisTable: TbOfrDetail, type[%d]", synType)
			mdb.UpdateRedisAcctTableOfr(synType, &row)
		}
	case TbTariff:
		for _, row := range c.ChgTariff {
			logger.CanalLog.Infof("entry updateRedisTable: TbTariff, type[%d]", synType)
			mdb.UpdateRedisTariffTable(synType, &row)
		}
	case TbOfrInfo:
		for _, row := range c.ChgOfrInfo {
			logger.CanalLog.Infof("entry updateRedisTable: TbOfrInfo, type[%d]", synType)
			mdb.UpdateRedisTariffTableByOfr(synType, &row)
		}
	case TbPricingStrategy:
		for _, row := range c.ChgPricingStrategy {
			logger.CanalLog.Infof("entry updateRedisTable: TbPricingStrategy, type[%d]", synType)
			mdb.UpdateRedisTariffTableByStrategy(synType, &row)
		}
	case TbConfigArea:// update prefix table, while update config_area table
		for _, row := range c.ChgConfigArea {
			logger.CanalLog.Infof("entry updateRedisTable: TbConfigArea, type[%d]", synType)
			if synType == 3 {
				mdb.UpdateRedisPrefixTableByConfigArea(synType, &row)
			} else {
				mdb.UpdateRedisPrefixTableByConfigArea(synType, &row)
			}
		}
	case TbPricingArea:
		for _, row := range c.ChgPricingArea {
			logger.CanalLog.Infof("entry updateRedisTable: TbPricingArea, type[%d]", synType)
			if synType == 3 {
				mdb.UpdateRedisPrefixTable(synType, &row)
			} else {
				mdb.UpdateRedisPrefixTable(synType, &row)
			}
		}
	case TbRr:
		for _, row := range c.ChgRr {
			logger.CanalLog.Infof("entry updateRedisTable: TbRr, type[%d]", synType)
			//if row.Id > 0 {
			if synType == 1 {
				mdb.UpdateRedisRrTable(synType, &row)
			} else if synType == 3 {
				mdb.UpdateRedisRrTable(synType, &row)
			} else {
				mdb.UpdateRedisRrTable(synType, &row)
			}

			/*} else {
				if ofrId > 0 {// ofr: CALC_PRIORITY
					mdb.UpdateRdbRrOfrPriority(ofrId, tariffSeq)
				} else if strategyId > 0 {// strategy: EVENT_PRIORITY
					mdb.UpdateRdbRrStrategyPriority(strategyId, tariffSeq)
				}
			}*/
		}
	case TbHoliday:
		for _, row := range c.ChgHoliday {
			logger.CanalLog.Infof("entry updateRedisTable: TbHoliday, type[%d]", synType)
			if synType == 1 {
				mdb.UpdateRedisHolidayDisTableByTariffId(synType, &row)
			} else if synType == 3 {
				mdb.UpdateRedisHolidayDisTableByTariffId(synType, &row)
			} else {
				mdb.UpdateRedisHolidayDisTableByTariffId(synType, &row)
			}
		}
	case TbBilHoliday:
		for _, row := range c.ChgBilHoliday {
			logger.CanalLog.Infof("entry updateRedisTable: TbBilHoliday, type[%d]", synType)
			mdb.UpdateRedisHolidayDisTableByHolidayId(synType, &row)
		}
	case TbSmsInfo:
		for _, row := range c.ChgAlertSms {
			logger.CanalLog.Infof("entry updateRedisTable: TbSmsInfo, type[%d]", synType)
			mdb.UpdateRedisAlertSms(synType, &row)
		}
	case TbSyncMobile:
		/*for _, row := range c.ChgSyncMobile {
			logger.CanalLog.Infof("entry updateRedisTable: TbSyncMobile, type[%d]", synType)
			mdb.UpdateRedisSyncMobile(synType, &row)
		}*/
	default:
	}
}
