Created
October 13, 2020 12:03
-
-
Save AmyrAhmady/b25466cf6ceaeacc16b0cafe9be6a463 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"encoding/json" | |
//"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
"io/ioutil" | |
"strconv" | |
) | |
type PeykData struct { | |
ID *int `json:"id"` | |
Name *string `json:"name"` | |
LastName *string `json:"lastname"` | |
LeftDate *string `json:"left"` | |
BackDate *string `json:"back"` | |
ComPercent *int `json:"percent"` | |
} | |
type TransactionInfo struct { | |
Amount *int `json:"amount"` | |
} | |
type WalletData struct { | |
keraye int | |
all_keraye int | |
commission int | |
} | |
type PeykInfo struct { | |
id int | |
name string | |
lastname string | |
back string | |
left string | |
wallet WalletData | |
} | |
func ProcessCommission(db *sql.DB, peykData PeykData) (bool, int, int, int) { | |
peykKeraye := 0 | |
peykAllKeraye := 0 | |
peykCommission := 0 | |
results, err := db.Query("SELECT amount FROM transaction " + | |
"WHERE " + | |
"request_status = 6" + | |
" AND type = 1" + | |
" AND type_name = 'COMM'" + | |
" AND peyk_id = " + strconv.Itoa(*peykData.ID) + | |
" AND receiver <> 'OFFICE'") | |
if err != nil { | |
panic(err.Error()) | |
} | |
for results.Next() { | |
var transData TransactionInfo | |
err = results.Scan(&transData.Amount) | |
if err != nil { | |
panic(err.Error()) | |
} | |
if transData.Amount != nil { | |
peykKeraye = peykKeraye + *transData.Amount | |
} | |
} | |
peykAllKeraye = peykKeraye | |
peykKeraye = *peykData.ComPercent / 100 * peykKeraye | |
results, err = db.Query("SELECT amount FROM transaction " + | |
"WHERE " + | |
"from_user = " + strconv.Itoa(*peykData.ID) + | |
" AND to_user = 35004" + | |
" AND type_name = 'COMM'") | |
if err != nil { | |
panic(err.Error()) | |
} | |
for results.Next() { | |
var transData TransactionInfo | |
err = results.Scan(&transData.Amount) | |
if err != nil { | |
panic(err.Error()) | |
} | |
if transData.Amount != nil { | |
peykKeraye = peykKeraye - *transData.Amount | |
} | |
} | |
peykCommission = *peykData.ComPercent / 100 * peykAllKeraye | |
return true, peykKeraye, peykAllKeraye, peykCommission | |
} | |
func main() { | |
var Peyks []PeykInfo | |
db, err := sql.Open("mysql", "root@tcp(127.0.0.1:3306)/etemad") | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer db.Close() | |
results, err := db.Query("SELECT `id`, `name`, `lastname`, `left`, `back`, `percent` FROM peyk") | |
if err != nil { | |
panic(err.Error()) | |
} | |
for results.Next() { | |
var tag PeykData | |
err = results.Scan(&tag.ID, &tag.Name, &tag.LastName, &tag.LeftDate, &tag.BackDate, &tag.ComPercent) | |
if err != nil { | |
panic(err.Error()) | |
} | |
emptyString := "" | |
if tag.Name == nil { | |
tag.Name = &emptyString | |
} | |
if tag.LastName == nil { | |
tag.LastName = &emptyString | |
} | |
if tag.LeftDate == nil { | |
tag.LeftDate = &emptyString | |
} | |
if tag.BackDate == nil { | |
tag.BackDate = &emptyString | |
} | |
if tag.ComPercent == nil { | |
zeroPercentage := 0 | |
tag.ComPercent = &zeroPercentage | |
} | |
processRes, peykKeraye, peykAllKeraye, peykCommission := ProcessCommission(db, tag) | |
if processRes { | |
//fmt.Println("Process was successful") | |
Test := PeykInfo{ | |
id: *tag.ID, | |
name: *tag.Name, | |
lastname: *tag.LastName, | |
left: *tag.LeftDate, | |
back: *tag.BackDate, | |
wallet: WalletData{ | |
keraye: peykKeraye, | |
all_keraye: peykAllKeraye, | |
commission: peykCommission, | |
}, | |
} | |
Peyks = append(Peyks, Test) | |
} | |
} | |
//for i := 0; i < len(Peyks); i++ { | |
// fmt.Println(Peyks[i].name) | |
//} | |
file, err := json.MarshalIndent(Peyks, "", " ") | |
if err != nil { | |
panic(err.Error()) | |
} | |
err = ioutil.WriteFile("test.json", file, 0644) | |
if err != nil { | |
panic(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment