package service import ( "XT_Admin_Api/models" "time" ) func FindCustomInfo(org_id int64) (admin models.KyyChargeCustom, err error) { err = ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("xt_org_id = ? AND status = 1", org_id).First(&admin).Error return } func CreateCustom(admin *models.KyyChargeCustom) (err error) { err = ChargeWriteDB().Model(&models.KyyChargeCustom{}).Create(&admin).Error return } func SaveCustom(admin *models.KyyChargeCustom) (err error) { err = ChargeWriteDB().Model(&models.KyyChargeCustom{}).Create(&admin).Error return } func SaveCustomTwo(admin models.KyyChargeCustomThree) (err error) { err = ChargeWriteDB().Model(&models.KyyChargeCustomThree{}).Save(&admin).Error return } func GetAllCustoms(types int64) (cus []*models.KyyChargeCustom, err error) { switch types { case 0: err = ChargeReadDB().Model(&models.KyyChargeCustomTwo{}).Where("status = 1").Find(&cus).Error break case 2: currentDate := time.Now() oneMonthLater := currentDate.AddDate(0, 1, 0) err = ChargeReadDB().Model(&models.KyyChargeCustomTwo{}).Joins("LEFT JOIN kyy_charge_renewal as kr ON kyy_charge_custom.id = kr.custom_id").Where("status = 1 and ((kr.end_time BETWEEN ? AND ?) OR (kr.end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kyy_charge_custom.sign_end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kyy_charge_custom.sign_end_time BETWEEN ? AND ?))", currentDate, oneMonthLater, currentDate, oneMonthLater, currentDate, oneMonthLater, currentDate, oneMonthLater).Find(&cus).Error break case 1: currentDate := time.Now() threeMonthsLater := currentDate.AddDate(0, 3, 0) err = ChargeReadDB().Model(&models.KyyChargeCustomTwo{}).Joins("LEFT JOIN kyy_charge_renewal as kr ON kyy_charge_custom.id = kr.custom_id").Where("status = 1 and ((kr.end_time BETWEEN ? AND ?) OR (kr.end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kyy_charge_custom.sign_end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kyy_charge_custom.sign_end_time BETWEEN ? AND ?))", currentDate, threeMonthsLater, currentDate, threeMonthsLater, currentDate, threeMonthsLater, currentDate, threeMonthsLater).Find(&cus).Error break case 3: currentYear := time.Now().Year() err = ChargeReadDB().Model(&models.KyyChargeCustomTwo{}).Joins("JOIN kyy_charge_renewal kcr on kcr.custom_id = kyy_charge_custom.id and YEAR(kcr.start_time) = ?", currentYear).Where("status = 1").Find(&cus).Error break case 4: currentYear := time.Now().Year() err = ChargeReadDB().Model(&models.KyyChargeCustomTwo{}).Where("YEAR(sign_time) = ? and is_implement = 0", currentYear).Find(&cus).Error break case 5: currentYear := time.Now().Year() err = ChargeReadDB().Model(&models.KyyChargeCustomTwo{}).Where("(YEAR(sign_time) = ? and lis_status = 0) or (YEAR(sign_time) = ? and qc_status = 0) or (YEAR(sign_time) = ? and yb_status = 0)", currentYear, currentYear, currentYear).Find(&cus).Error break } return } func GetAllPaymentCollections() (cus []*models.KyyChargePaymentCollection, err error) { err = ChargeReadDB().Model(&models.KyyChargePaymentCollection{}).Where("status = 1").Find(&cus).Error return } func GetAllChargeContacts() (cus []*models.KyyChargeContact, err error) { err = ChargeReadDB().Model(&models.KyyChargeContact{}).Where("status = 1").Find(&cus).Error return } func GetAllChargeContracts() (cus []*models.KyyChargeContract, err error) { err = ChargeReadDB().Model(&models.KyyChargeContract{}).Where("status = 1").Find(&cus).Error return } func GetAllChargeDemand() (cus []*models.KyyChargeDemand, err error) { err = ChargeReadDB().Model(&models.KyyChargeDemand{}).Where("status = 1").Find(&cus).Error return } func GetAllChargeFollow() (cus []*models.KyyChargeFollow, err error) { err = ChargeReadDB().Model(&models.KyyChargeFollow{}).Where("status = 1").Find(&cus).Error return } func GetAllChargeRenewal() (cus []*models.KyyChargeRenewal, err error) { err = ChargeReadDB().Model(&models.KyyChargeRenewal{}).Where("status = 1").Find(&cus).Error return } func FindCustomInfoByID(id int64) (admin models.KyyChargeCustom, err error) { err = ChargeReadDB().Model(&models.KyyChargeCustom{}). Preload("KyyChargePaymentCollection", "status = 1"). Preload("KyyChargeContact", "status = 1"). Preload("KyyChargeContract", "status = 1"). Preload("KyyChargeDemand", "status = 1"). Preload("KyyChargeFollow", "status = 1"). Preload("KyyChargeRenewal", "status = 1"). Where("id = ? AND status = 1", id).First(&admin).Error return } func FindCustomInfoByIDTwo(id int64) (admin models.KyyChargeCustomThree, err error) { err = ChargeReadDB().Model(&models.KyyChargeCustomThree{}). Where("id = ? AND status = 1", id).First(&admin).Error return } func UpdateCustom(custom models.KyyChargeCustom) (err error) { err = ChargeWriteDB().Save(&custom).Error return } func UpdateCustomTwo(custom models.KyyChargeCustomThree) (err error) { err = ChargeWriteDB().Save(&custom).Error return } func CreateChargePaymentCollection(cpc *models.KyyChargePaymentCollection) (err error) { err = ChargeWriteDB().Save(&cpc).Error return } func SaveChargePaymentCollection(cpc *models.KyyChargePaymentCollection) (err error) { err = ChargeWriteDB().Save(&cpc).Error return } func FindPaymentCollectionInfo(id int64) (cpc models.KyyChargePaymentCollection, err error) { err = ChargeReadDB().Model(&models.KyyChargePaymentCollection{}).Where("id = ? AND status = 1", id).First(&cpc).Error return } func SaveChargeContact(chargeContact *models.KyyChargeContact) (err error) { err = ChargeWriteDB().Save(&chargeContact).Error return } func FindChargeContactInfo(id int64) (cpc models.KyyChargeContact, err error) { err = ChargeReadDB().Model(&models.KyyChargeContact{}).Where("id = ? AND status = 1", id).First(&cpc).Error return } func SaveChargeContract(chargeContract *models.KyyChargeContract) (err error) { err = ChargeWriteDB().Save(&chargeContract).Error return } func FindChargeContractInfo(id int64) (cpc models.KyyChargeContract, err error) { err = ChargeReadDB().Model(&models.KyyChargeContract{}).Where("id = ? AND status = 1", id).First(&cpc).Error return } func SaveChargeFollow(chargeFollow *models.KyyChargeFollow) (err error) { err = ChargeWriteDB().Save(&chargeFollow).Error return } func CheckChargeFollowTwo(ids []int64) (err error) { err = ChargeWriteDB().Model(&models.KyyChargeFollow{}).Where("id in (?)", ids).Updates(map[string]interface{}{"is_check": 1}).Error return } func FindChargeFollowInfo(id int64) (cf models.KyyChargeFollow, err error) { err = ChargeReadDB().Model(&models.KyyChargeFollow{}).Where("id = ? AND status = 1", id).First(&cf).Error return } func FindChargeFollowInfoTwo(id int64) (cf []models.KyyChargeFollow, err error) { err = ChargeReadDB().Model(&models.KyyChargeFollow{}).Where("custom_id = ? AND status = 1", id).First(&cf).Error return } func SaveChargeDemand(cd *models.KyyChargeDemand) (err error) { err = ChargeWriteDB().Save(&cd).Error return } func FindChargeDemandInfo(id int64) (cd models.KyyChargeDemand, err error) { err = ChargeReadDB().Model(&models.KyyChargeDemand{}).Where("id = ? AND status = 1", id).First(&cd).Error return } func SaveChargeRenewal(chargeRenewal *models.KyyChargeRenewal) (err error) { err = ChargeWriteDB().Save(&chargeRenewal).Error return } func FindChargeRenewalInfo(id int64) (chargeRenewal models.KyyChargeRenewal, err error) { err = ChargeReadDB().Model(&models.KyyChargeRenewal{}).Where("id = ? AND status = 1", id).First(&chargeRenewal).Error return }