1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912 |
- package api
- import (
- "fmt"
- "io/ioutil"
- "log"
- "math"
- "net/http"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- "../../utils"
- "../../pkg/app"
- "../../pkg/e"
- "../../routers/restful"
- "github.com/Anderson-Lu/gofasion/gofasion"
- "github.com/gin-gonic/gin"
- "github.com/kptyun/KPTCOMM/pkg/util"
- "github.com/tealeg/xlsx"
- )
- func GetProductAreas(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- parammaps := fsion.Get("parammaps")
- productname := parammaps.Get("productname").ValueStr()
- classids := parammaps.Get("productclassid").ValueStr()
- var recordcount int64 = 0
- queryData := make([]map[string]interface{}, 1)
- var err error
- tx := restful.Engine.NewSession()
- defer tx.Close()
- {
- sqlstr := `SELECT t1.id,t1.productname,t1.spec FROM productinfo t1
- INNER JOIN prounit t2 ON t1.prounit = t2.id where 1=1 and t1.enable = 1 `
- if len(classids) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productclass in ('%s')", classids)
- }
- if len(productname) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productname like ('%s')", "%"+productname+"%")
- }
- tt, err := tx.SQL(sqlstr).Query().Count()
- if err != nil {
- log.Println("GetProductAreas-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- recordcount = int64(tt)
- }
- if pagecount == 0 {
- sqlstr := `SELECT t1.id as productid,t1.productname,ifnull(t1.spec,"") as specifications,ifnull(t2.unitname,"") as unit
- FROM productinfo t1
- INNER JOIN prounit t2 ON t1.prounit = t2.id where 1=1 and t1.enable = 1 `
- if len(classids) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productclass in ('%s')", classids)
- }
- if len(productname) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productname like ('%s')", "%"+productname+"%")
- }
- query := tx.SQL(sqlstr)
- queryData, err = query.Query().List()
- if err != nil {
- log.Println("GetProductAreas-err2: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- } else {
- sqlstr := `SELECT t1.id as productid,t1.productname,ifnull(t1.spec,"") as specifications,ifnull(t2.unitname,"") as unit
- FROM productinfo t1
- INNER JOIN prounit t2 ON t1.prounit = t2.id where 1=1 and t1.enable = 1 `
- if len(classids) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productclass in ('%s')", classids)
- }
- if len(productname) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productname like ('%s')", "%"+productname+"%")
- }
- queryData, err = tx.SQL(sqlstr).Limit(pagecount, (offset-1)*pagecount).Query().List()
- if err != nil {
- log.Println("GetProductAreas-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- }
- headlist := make([]map[string]interface{}, 0)
- areaData := make([]map[string]interface{}, 0)
- {
- sqlstr := `SELECT
- t1.id AS productareaid,
- t1.AreaId as areaid,
- t2.name,
- t1.Productid as productid
- FROM
- productarea t1
- INNER JOIN canton t2 ON t1.AreaId = t2.ID order by areaid`
- areaData, err = tx.SQL(sqlstr).Query().List()
- if err != nil {
- log.Println("GetProductAreas-err4: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- areaMap := make(map[interface{}]interface{})
- contactlistMap := make(map[interface{}][]interface{})
- for _, area := range areaData {
- areaMap[area["areaid"]] = area["name"]
- contactlistMap[area["productid"]] = append(contactlistMap[area["productid"]], area["areaid"])
- }
- areaids := []int64{}
- for areaid, _ := range areaMap {
- areaids = append(areaids, areaid.(int64))
- }
- utils.QuickSort(areaids, 0, int64(len(areaids)-1))
- for _, v := range areaids {
- head := make(map[string]interface{})
- head["column_name"] = fmt.Sprintf("column%v", v)
- head["column_title"] = areaMap[v]
- head["column_id"] = v
- headlist = append(headlist, head)
- }
- for _, data := range queryData {
- for id, _ := range areaMap {
- data[fmt.Sprintf("column%v", id)] = 0
- }
- if _, ok := contactlistMap[data["productid"]]; ok {
- for _, areaid := range contactlistMap[data["productid"]] {
- data[fmt.Sprintf("column%v", areaid)] = 1
- }
- }
- }
- }
- data := make(map[string]interface{})
- data["headlist"] = headlist
- data["list"] = queryData
- data["total"] = recordcount
- data["code"] = 0
- data["message"] = "ok"
- if pagecount > 0 {
- data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
- data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
- }
- data["pageSize"] = pagecount
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- //查看发布价格
- func GetPublishPrice(c *gin.Context) {
- areaids := getroleareaid(c.GetHeader("token"))
- user, _ := util.ParseToken(c.GetHeader("token"))
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- parammaps := fsion.Get("parammaps")
- productclass := parammaps.Get("productclassid").ValueInt64()
- date := parammaps.Get("starttime").ValueStr()
- productname := parammaps.Get("productname").ValueStr()
- data := make(map[string]interface{})
- var err error
- queryData := make([]map[string]interface{}, 0)
- var recordcount int
- tx := restful.Engine.NewSession()
- defer tx.Close()
- {
- sqlstr := `select if(t5.code= "",0,1) as coedisnull ,ifnull(t5.code,"") as productcode ,t3.classname,t1.id as productid,t1.productname,t1.productclass,ifnull(t1.spec,"") as specifications ,ifnull(t2.unitname,"") as unit from productinfo t1
- left join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass join productarea t4 on t1.id = t4.Productid
- `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
- // args = append(args, areaids)
- }
- var args []interface{}
- args = append(args, user.Username)
- sqlstr += "left join productusercode t5 on t5.userid = ? and t5.productid = t1.id"
- sqlstr += ` where 1=1 and t1.enable = 1 `
- if productclass > 0 {
- sqlstr += " and t1.productclass = ?"
- args = append(args, productclass)
- }
- if len(productname) > 0 {
- sqlstr += " and t1.productname like CONCAT('%',?,'%')"
- args = append(args, productname)
- }
- sqlstr += " group by t1.id order by coedisnull desc ,t5.code +0 , t3.ordernum,t3.id,t1.ordernum,t1.id asc "
- if pagecount > 0 {
- sqlstr += " limit ?,?"
- args = append(args, (offset-1)*pagecount, pagecount)
- }
- productrow := tx.SQL(sqlstr, args...)
- defer productrow.Close()
- queryData, err = productrow.Query().List()
- if err != nil {
- log.Println("GetPublishPrice-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- // var args1 []interface{}
- // sqlstr1 := `select t1.id from productinfo t1
- // join prounit t2 on t2.id = t1.prounit where 1=1 and t1.enable = 1 `
- // if productclass > 0 {
- // sqlstr1 += " and t1.productclass = ?"
- // args1 = append(args1, productclass)
- // }
- // if len(productname) > 0 {
- // sqlstr1 += " and t1.productname like CONCAT('%',?,'%')"
- // args1 = append(args1, productname)
- // }
- // row := tx.SQL(sqlstr1, args1...)
- // defer row.Close()
- // count, err := row.Query().Count()
- // if err != nil {
- // log.Println("GetPublishPrice-err2: ", err)
- // appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- // return
- // }
- // recordcount = count
- }
- productidlist := []string{"0"}
- for _, data := range queryData {
- productidlist = append(productidlist, strconv.FormatInt(data["productid"].(int64), 10))
- }
- type areaid struct {
- areaid int64
- name string
- parentid int64
- layer int64
- sort int64
- }
- headlist := make([]map[string]interface{}, 0)
- {
- // sqlstr := `select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1
- // join (select DISTINCT(areaid) as areaid,t2.name from productarea t1 join canton t2 on t2.id = t1.areaid
- // where productid in (%s)) t2 on (t1.id = t2.areaid or t1.PARENT_ID = t2.areaid)`
- sqlstr := ` select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1 where layer >0 `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and (parent_id in (%s) or id in (%s))", areaids, areaids)
- }
- arearow := tx.SQL(sqlstr)
- areaData, err := arearow.Query().List()
- if err != nil {
- log.Println("GetPublishPrice-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- bigarea := make([]*areaid, 0)
- wg := sync.WaitGroup{}
- wg.Add(len(areaData))
- for _, area := range areaData {
- go func(area map[string]interface{}) {
- if area["layer"].(int64) == 1 {
- bigarea = append(bigarea, &areaid{
- areaid: area["id"].(int64),
- name: area["name"].(string),
- parentid: area["parentid"].(int64),
- layer: area["layer"].(int64),
- sort: area["ord"].(int64),
- })
- }
- wg.Done()
- }(area)
- }
- wg.Wait()
- sort.Slice(bigarea, func(i, j int) bool {
- return bigarea[i].areaid < bigarea[j].areaid // 升序
- })
- sort.Slice(bigarea, func(i, j int) bool {
- return bigarea[i].sort < bigarea[j].sort // 升序
- })
- for _, big := range bigarea {
- head := make(map[string]interface{})
- head["column_id"] = big.areaid
- head["column_name"] = fmt.Sprintf("column_%d", big.areaid)
- head["column_title"] = big.name
- smallarea := make([]*areaid, 0)
- for _, area := range areaData {
- if area["parentid"].(int64) == big.areaid {
- smallarea = append(smallarea, &areaid{
- areaid: area["id"].(int64),
- name: area["name"].(string),
- parentid: area["parentid"].(int64),
- layer: area["layer"].(int64),
- sort: area["ord"].(int64),
- })
- }
- }
- sort.Slice(smallarea, func(i, j int) bool {
- return smallarea[i].areaid < smallarea[j].areaid // 升序
- })
- sort.Slice(smallarea, func(i, j int) bool {
- return smallarea[i].sort < smallarea[j].sort // 升序
- })
- smallhead := make([]map[string]interface{}, 0)
- for _, small := range smallarea {
- data1 := make(map[string]interface{})
- data1["column_id"] = small.areaid
- data1["column_name"] = fmt.Sprintf("column_%d", small.areaid)
- data1["column_title"] = small.name
- smallhead = append(smallhead, data1)
- }
- head["headlist"] = smallhead
- headlist = append(headlist, head)
- }
- productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,price FROM (
- SELECT * FROM productprice WHERE productid in (%s) `
- productpricesql = fmt.Sprintf(productpricesql, strings.Join(productidlist, ","))
- if date != "" {
- start := fmt.Sprintf("%s 00:00:00", date)
- end := fmt.Sprintf("%s 23:59:59", date)
- productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", start, end)
- }
- productpricesql += " ORDER BY pricedate DESC limit 1000000000000000) AS t where 1=1"
- if len(areaids) > 0 {
- productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- productpricesql += " group by productid, marketid "
- productpricerow := tx.SQL(productpricesql)
- defer productpricerow.Close()
- productpriceData, err := productpricerow.Query().List()
- if err != nil {
- log.Println(err)
- return
- }
- type priceStruct struct {
- marketid int64
- price float64
- }
- priceMap := make(map[int64][]priceStruct)
- for _, productprice := range productpriceData {
- priceintface, _ := strconv.ParseFloat(productprice["price"].(string), 64)
- priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
- marketid: productprice["smallareaid"].(int64),
- price: priceintface,
- })
- }
- wg.Add(len(queryData))
- for _, query := range queryData {
- go func(query map[string]interface{}) {
- for _, area := range areaData {
- if area["layer"].(int64) == 2 {
- query[fmt.Sprintf("column_%v", area["id"])] = 0
- }
- }
- query["Edit"] = false
- query["NoEdit"] = true
- query["isCreate"] = false
- query["isUpdate"] = true
- query["isUpdateSave"] = false
- wg.Done()
- }(query)
- }
- wg.Wait()
- wg.Add(len(queryData))
- for _, query := range queryData {
- go func(query map[string]interface{}) {
- for _, price := range priceMap[query["productid"].(int64)] {
- query[fmt.Sprintf("column_%d", price.marketid)] = price.price
- }
- wg.Done()
- }(query)
- }
- wg.Wait()
- data["list"] = queryData
- }
- data["headlist"] = headlist
- data["total"] = recordcount
- data["code"] = 0
- data["message"] = "ok"
- if pagecount > 0 {
- data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
- data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
- }
- data["pageSize"] = pagecount
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- //修改发布价格信息
- func UpdatePublishPrice(c *gin.Context) {
- appG := app.Gin{C: c}
- user, _ := util.ParseToken(c.GetHeader("token"))
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- params := fsion.Get("params").Array()
- tx := restful.Engine.NewSession()
- defer tx.Close()
- areamap := make([]map[string]interface{}, 0)
- pricemap := make([]map[string]interface{}, 0)
- var err error
- if len(params) > 0 {
- areaidlist := []string{"0"}
- for _, key := range params[0].Keys() {
- if strings.Index(key, "column_") > -1 {
- areaidlist = append(areaidlist, key[len("column_"):])
- }
- }
- areamap, err = tx.SQL(fmt.Sprintf(`select t1.id,t1.PARENT_ID as parentid from canton t1
- join canton t2 on t1.PARENT_ID = t2.id where t1.id in (%s)
- order by t2.ORD ,t1.ORD,t1.id`, strings.Join(areaidlist, ","))).Query().List()
- if err != nil {
- log.Println("UpdatePublishPrice-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- productidlist := []string{}
- for _, param := range params {
- productidlist = append(productidlist, param.Get("productid").ValueStr())
- }
- pricemap, err = tx.SQL(fmt.Sprintf(`SELECT t.marketid ,t.marketclass,t.price , t.pricedate,id,productid FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
- ) AS t
- GROUP BY t.marketid ,productid `, strings.Join(productidlist, ","))).Query().List()
- if err != nil {
- log.Println("UpdatePublishPrice-err2: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- }
- var wg sync.WaitGroup
- for _, param := range params {
- productid := param.Get("productid").ValueInt64()
- productclass := param.Get("productclass").ValueStr()
- productcode := param.Get("productcode").ValueStr()
- for _, area := range areamap {
- pricenum := param.Get(fmt.Sprintf("column_%v", area["id"])).ValueFloat64()
- isupdateprice := false
- exist := false
- for _, price := range pricemap {
- if area["id"].(int64) == price["marketid"].(int64) && productid == price["productid"].(int64) {
- priceintface, _ := strconv.ParseFloat(price["price"].(string), 64)
- if priceintface == pricenum {
- isupdateprice = true
- }
- exist = true
- break
- }
- }
- if (!isupdateprice && (exist)) || (!isupdateprice && !exist && pricenum != 0) {
- var args []interface{}
- args = append(args, productid, area["id"], pricenum, area["parentid"], productclass)
- _, err := tx.SQL("insert into productprice(productid,marketid,price,marketclass,productclass,pricedate)values(?,?,?,?,?,now())", args...).Execute()
- if err != nil {
- log.Println("UpdatePublishPrice-err4: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- }
- }
- wg.Add(1)
- go func(user *util.Claims, productcode string, productid int64) {
- defer wg.Done()
- _, err := tx.SQL(`insert into productusercode(userid,code,productid)value(?,?,?)
- ON DUPLICATE KEY UPDATE code = ?`, user.Username, productcode, productid, productcode).Execute()
- if err != nil {
- log.Println("UpdatePublishPrice-err5: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- }(user, productcode, productid)
- }
- wg.Wait()
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- //查看产品信息 GetProductInfo
- func GetProductInfo(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- parammaps := fsion.Get("parammaps")
- smallclassid := parammaps.Get("smallclassid").ValueStr()
- productname := parammaps.Get("productname").ValueStr()
- var args []interface{}
- tx := restful.Engine.NewSession()
- defer tx.Close()
- sqlstr := ` select t1.id as productid ,t1.productname, IFNULL(t1.spec,'') as specifications ,t2.unitname as unit,t1.prounit as unitid ,t1.enable,t1.ordernum as sort , t3.productclass ,t3.id as smallclassid
- from productinfo t1
- inner join prounit t2 on t2.id = t1.prounit
- inner join (select t1.classname as smallname,t2.classname as bigname,t1.id,CONCAT(t2.classname,'-',t1.classname)as productclass ,t1.ordernum as smallsort ,t2.ordernum as bigsort
- from productclass t1 join productclass t2 on t2.id = t1.parentid where t1.leavel = 2) t3 on t3.id = t1.productclass
- where 1=1 `
- if smallclassid != "" {
- sqlstr += " and t1.productclass = ? "
- args = append(args, smallclassid)
- }
- if productname != "" {
- sqlstr += " and t1.productname like CONCAT('%',?,'%') "
- args = append(args, productname)
- }
- sqlstr += " order by t3.bigsort,t3.smallsort ,t1.ordernum"
- arearow := tx.SQL(sqlstr, args...)
- defer arearow.Close()
- queryData := make([]map[string]interface{}, 0)
- var err error
- if pagecount > 0 {
- queryData, err = arearow.Limit(pagecount, (offset-1)*pagecount).Query().List()
- if err != nil {
- log.Println("GetProductInfo-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- } else {
- queryData, err = arearow.Query().List()
- if err != nil {
- log.Println("GetProductInfo-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- }
- sqlstrcount := `select t1.id
- from productinfo t1
- inner join prounit t2 on t2.id = t1.prounit
- inner join (select t1.classname as smallname,t2.classname as bigname,t1.id,CONCAT(t2.classname,'-',t1.classname)as productclass ,t1.ordernum as smallsort ,t2.ordernum as bigsort
- from productclass t1 join productclass t2 on t2.id = t1.parentid where t1.leavel = 2) t3 on t3.id = t1.productclass
- where 1=1 and t1.enable = 1 `
- if smallclassid != "" {
- sqlstrcount += " and t1.productclass = ? "
- }
- if productname != "" {
- sqlstrcount += " and t1.productname like CONCAT('%',?,'%') "
- }
- countrow := tx.SQL(sqlstr, args...)
- recordcount, err := countrow.Query().Count()
- if err != nil {
- log.Println("GetProductInfo-err2: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- productids := []string{"0"}
- for _, query := range queryData {
- productids = append(productids, fmt.Sprintf("%v", query["productid"]))
- }
- pricemap, err := tx.SQL(fmt.Sprintf(`SELECT productid,price FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
- ) AS t group by productid `, strings.Join(productids, ","))).Query().List()
- if err != nil {
- log.Println("GetProductInfo-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, query := range queryData {
- productid := query["productid"].(int64)
- for _, price := range pricemap {
- if productid == price["productid"].(int64) {
- query["price"] = price["price"]
- }
- }
- }
- data := make(map[string]interface{})
- data["list"] = queryData
- data["total"] = recordcount
- data["code"] = 0
- data["message"] = "ok"
- if pagecount > 0 {
- data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
- data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
- }
- data["pageSize"] = pagecount
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- func GetAveragePrice(c *gin.Context) {
- areaids := getroleareaid(c.GetHeader("token"))
- user, _ := util.ParseToken(c.GetHeader("token"))
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- parammaps := fsion.Get("parammaps")
- productclass := parammaps.Get("productclassid").ValueStr()
- productid := parammaps.Get("productid").ValueInt64()
- starttime := parammaps.Get("starttime").ValueStr()
- endtime := parammaps.Get("endtime").ValueStr()
- productname := parammaps.Get("productname").ValueStr()
- userid := parammaps.Get("userid").ValueStr()
- var productidlist []string
- if productid != 0 {
- productidlist = append(productidlist, fmt.Sprintf("%d", productid))
- }
- data := make(map[string]interface{})
- var err error
- queryData := make([]map[string]interface{}, 0)
- var recordcount int
- tx := restful.Engine.NewSession()
- defer tx.Close()
- if userid != "" {
- productidmap, err := tx.SQL("select Productid as productid from attention where userid = ?", userid).Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, product := range productidmap {
- productidlist = append(productidlist, fmt.Sprintf("%v", product["productid"]))
- }
- if len(productidlist) == 0 {
- appG.Response(http.StatusOK, e.SUCCESS, data)
- return
- }
- }
- {
- // sqlstr := `select t1.id as productid,t1.productname,t1.productclass,ifnull(t1.spec,"") as specifications ,ifnull(t2.unitname,"") as unit from productinfo t1
- // join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass where 1=1 and t1.enable = 1 `
- sqlstr := `select if(t5.code= "",0,1) as coedisnull, ifnull(t5.code,"") productcode,t3.classname,t1.id as productid,t1.productname,t1.productclass,ifnull(t1.spec,"") as specifications ,ifnull(t2.unitname,"") as unit from productinfo t1
- left join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass join productarea t4 on t1.id = t4.Productid
- `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
- }
- sqlstr += " left join productusercode t5 on t5.userid = ? and t5.productid = t1.id"
- var args []interface{}
- args = append(args, user.Username)
- sqlstr += ` where 1=1 and t1.enable = 1 `
- // var args []interface{}
- if len(productclass) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productclass in (%s)", productclass)
- }
- if len(productname) > 0 {
- sqlstr += " and t1.productname like CONCAT('%',?,'%')"
- args = append(args, productname)
- }
- // if productid != 0 {
- // sqlstr += " and t1.id in (?) "
- // args = append(args, strings.Join(productidlist, ","))
- // }
- if len(productidlist) > 0 {
- sqlstr += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
- }
- sqlstr += "group by t1.id order by coedisnull desc ,t5.code +0 , t3.ordernum,t3.id,t1.ordernum,t1.id"
- if pagecount > 0 && productid == 0 {
- sqlstr += " limit ?,?"
- args = append(args, (offset-1)*pagecount, pagecount)
- }
- productrow := tx.SQL(sqlstr, args...)
- defer productrow.Close()
- queryData, err = productrow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- if productid == 0 {
- var args1 []interface{}
- sqlstr1 := `select t1.id from productinfo t1
- left join prounit t2 on t2.id = t1.prounit join productarea t4 on t1.id = t4.Productid where 1=1 and t1.enable = 1 `
- // var args []interface{}
- if len(areaids) > 0 {
- // sqlstr1 += " and t4.Areaid in (?)"
- sqlstr1 += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
- // args1 = append(args, areaids)
- }
- if len(productclass) > 0 {
- sqlstr1 += fmt.Sprintf(" and t1.productclass in (%s) ", productclass)
- }
- if len(productname) > 0 {
- sqlstr1 += " and t1.productname like CONCAT('%',?,'%')"
- args1 = append(args1, productname)
- }
- if len(productidlist) > 0 {
- sqlstr1 += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
- }
- sqlstr1 += " group by t1.id"
- row := tx.SQL(sqlstr1, args1...)
- defer row.Close()
- count, err := row.Query().Count()
- if err != nil {
- log.Println("GetAveragePrice-err2: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- recordcount = count
- defer row.Close()
- }
- defer productrow.Close()
- }
- // productidlist := []string{"0"}
- for _, data := range queryData {
- productidlist = append(productidlist, strconv.FormatInt(data["productid"].(int64), 10))
- }
- type areaid struct {
- areaid int64
- name string
- parentid int64
- layer int64
- sort int64
- }
- headlist := make([]map[string]interface{}, 0)
- if len(productidlist) > 0 {
- // sqlstr := `select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1
- // join (select DISTINCT(areaid) as areaid,t2.name from productarea t1 join canton t2 on t2.id = t1.areaid
- // where productid in (%s)) t2 on (t1.id = t2.areaid or t1.PARENT_ID = t2.areaid)`
- sqlstr := ` select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1 where layer >0 `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and PARENT_ID in (%s) or t1.id in(%s)", areaids, areaids)
- }
- arearow := tx.SQL(sqlstr)
- areaData, err := arearow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- bigarea := make([]*areaid, 0)
- for _, area := range areaData {
- if area["layer"].(int64) == 1 {
- bigarea = append(bigarea, &areaid{
- areaid: area["id"].(int64),
- name: area["name"].(string),
- parentid: area["parentid"].(int64),
- layer: area["layer"].(int64),
- sort: area["ord"].(int64),
- })
- }
- }
- sort.Slice(bigarea, func(i, j int) bool {
- return bigarea[i].areaid < bigarea[j].areaid // 升序
- })
- sort.Slice(bigarea, func(i, j int) bool {
- return bigarea[i].sort < bigarea[j].sort // 升序
- })
- for _, big := range bigarea {
- head := make(map[string]interface{})
- head["column_id"] = big.areaid
- head["column_name"] = fmt.Sprintf("column_%d", big.areaid)
- head["column_title"] = big.name
- smallarea := make([]*areaid, 0)
- for _, area := range areaData {
- if area["parentid"].(int64) == big.areaid {
- smallarea = append(smallarea, &areaid{
- areaid: area["id"].(int64),
- name: area["name"].(string),
- parentid: area["parentid"].(int64),
- layer: area["layer"].(int64),
- sort: area["ord"].(int64),
- })
- }
- }
- sort.Slice(smallarea, func(i, j int) bool {
- return smallarea[i].areaid < smallarea[j].areaid // 升序
- })
- sort.Slice(smallarea, func(i, j int) bool {
- return smallarea[i].sort < smallarea[j].sort // 升序
- })
- smallhead := make([]map[string]interface{}, 0)
- for _, small := range smallarea {
- data1 := make(map[string]interface{})
- data1["column_id"] = small.areaid
- data1["column_name"] = fmt.Sprintf("column_%d", small.areaid)
- data1["column_title"] = small.name
- smallhead = append(smallhead, data1)
- }
- head["headlist"] = smallhead
- headlist = append(headlist, head)
- }
- type priceStruct struct {
- marketid int64
- price float64
- pricedate string
- }
- priceMap := make(map[int64][]priceStruct)
- if productid == 0 {
- productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,pricedate FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
- ) AS t where 1=1 `
- if starttime != "" && endtime != "" {
- productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
- }
- if len(areaids) > 0 {
- productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- productpricesql += " group by productid, marketid "
- if offset != 0 || pagecount != 0 {
- productpricesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
- }
- productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
- defer productpricerow.Close()
- productpriceData, err := productpricerow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err4: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, productprice := range productpriceData {
- price, err := strconv.ParseFloat(productprice["price"].(string), 64)
- if err != nil {
- log.Println("GetAveragePrice-err6: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
- marketid: productprice["smallareaid"].(int64),
- price: utils.Round(price, 2),
- })
- }
- for _, query := range queryData {
- for _, area := range areaData {
- if area["layer"].(int64) == 2 {
- query[fmt.Sprintf("column_%v", area["id"])] = 0
- }
- }
- query["Edit"] = false
- query["NoEdit"] = true
- query["isCreate"] = false
- query["isUpdate"] = true
- query["isUpdateSave"] = false
- }
- for _, query := range queryData {
- for _, price := range priceMap[query["productid"].(int64)] {
- query[fmt.Sprintf("column_%d", price.marketid)] = price.price
- }
- }
- data["list"] = queryData
- } else {
- timesql := `SELECT date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate desc limit 10000000000000000
- ) AS t where 1=1 `
- if len(areaids) > 0 {
- timesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- timesql += " group by date_format(pricedate, '%%Y-%%c-%%d') ORDER BY pricedate desc"
- count, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().Count()
- if err != nil {
- log.Println("GetAveragePrice-err4: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- recordcount = int(count)
- dates := []string{}
- if offset != 0 || pagecount != 0 {
- timesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
- }
- timeData, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err5: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
- ) AS t where 1=1 `
- if len(areaids) > 0 {
- productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- if len(timeData) > 0 {
- starttime = fmt.Sprintf("%s 00:00:00", timeData[len(timeData)-1]["pricedate"].(string))
- endtime = fmt.Sprintf("%s 23:59:59", timeData[0]["pricedate"].(string))
- productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
- }
- productpricesql += " group by marketid,date_format(pricedate, '%%Y-%%c-%%d') "
- productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
- defer productpricerow.Close()
- productpriceData, err := productpricerow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err6: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, productprice := range productpriceData {
- dates = append(dates, productprice["pricedate"].(string))
- price, err := strconv.ParseFloat(productprice["price"].(string), 64)
- if err != nil {
- log.Println("GetAveragePrice-err7: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
- marketid: productprice["smallareaid"].(int64),
- price: utils.Round(price, 2),
- pricedate: productprice["pricedate"].(string),
- })
- }
- datelist := utils.RemoveRepeatedElement(dates)
- sort.Sort(sort.Reverse(sort.StringSlice(datelist)))
- productmap := make([]map[string]interface{}, 0)
- for _, date := range datelist {
- product := make(map[string]interface{}, 0)
- product["productid"] = productid
- for _, query := range queryData {
- if query["productid"].(int64) == productid {
- product["productclass"] = query["productclass"]
- product["productname"] = query["productname"]
- product["unit"] = query["unit"]
- product["specifications"] = query["specifications"]
- break
- }
- }
- for _, area := range areaData {
- if area["layer"].(int64) == 2 {
- product[fmt.Sprintf("column_%v", area["id"])] = 0
- }
- }
- product["Edit"] = false
- product["NoEdit"] = true
- product["isCreate"] = false
- product["isUpdate"] = true
- product["isUpdateSave"] = false
- product["pricedate"] = date
- for _, price := range priceMap[product["productid"].(int64)] {
- if date == price.pricedate {
- product[fmt.Sprintf("column_%d", price.marketid)] = price.price
- }
- }
- productmap = append(productmap, product)
- }
- data["list"] = productmap
- }
- }
- data["headlist"] = headlist
- data["total"] = recordcount
- data["code"] = 0
- data["message"] = "ok"
- if pagecount > 0 {
- data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
- data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
- }
- data["pageSize"] = pagecount
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- //查看平均价格图标信息
- func GetQuotationChart(c *gin.Context) {
- // areaids := getroleareaid(c.GetHeader("token"))
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- parammaps := fsion.Get("parammaps")
- productid := parammaps.Get("productid").ValueInt64()
- areaids := parammaps.Get("areaids").ValueStr()
- tx := restful.Engine.NewSession()
- defer tx.Close()
- areasql := `select name from canton t1 join productarea t2 on t2.productid = ? and t1.PARENT_ID = t2.areaid where layer = 2 `
- if len(areaids) > 0 {
- areasql += fmt.Sprintf(" and t1.id in (%s)", areaids)
- }
- areamap, err := tx.SQL(areasql, productid).Query().List()
- if err != nil {
- log.Println("GetQuotationChart-err0: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- sqlstr := `select avg(t1.price) as price , date_format(t1.pricedate, '%Y-%c-%d') as date ,t2.name from productprice t1
- join canton t2 on t2.id = t1.marketid where t1.productid = ? `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and t1.marketid in (%s)", areaids)
- }
- sqlstr += " group by date,name order by date desc"
- priceData, err := tx.SQL(sqlstr, productid).Query().List()
- if err != nil {
- log.Println("GetQuotationChart-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- datelist := []string{}
- daylist := []interface{}{}
- for _, price := range priceData {
- datelist = append(datelist, price["date"].(string))
- }
- days := utils.RemoveRepeatedElement(datelist)
- sort.Sort(sort.Reverse(sort.StringSlice(days)))
- for _, day := range days {
- daylist = append(daylist, day)
- }
- var list [][]interface{}
- namelist := []interface{}{}
- for _, area := range areamap {
- namelist = append(namelist, area["name"].(string))
- }
- list = append(list, namelist)
- if len(daylist) == 0 {
- now := time.Now()
- for i := 0; i < 7; i++ {
- daylist = append(daylist, now.AddDate(0, 0, -i).Format("2006-01-02"))
- }
- }
- list = append(list, daylist)
- for _, name := range namelist {
- pricelist := []interface{}{}
- for _, day := range daylist {
- exist := false
- for _, pricemap := range priceData {
- if pricemap["name"].(string) == name.(string) && day.(string) == pricemap["date"].(string) {
- price, err := strconv.ParseFloat(pricemap["price"].(string), 64)
- if err != nil {
- log.Println("GetAveragePrice-err6: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- pricelist = append(pricelist, utils.Round(price, 2))
- exist = true
- }
- }
- if !exist {
- pricelist = append(pricelist, 0)
- }
- }
- if len(pricelist) == 0 {
- for i := 0; i < 7; i++ {
- pricelist = append(pricelist, 0)
- }
- } else {
- list = append(list, pricelist)
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, list)
- }
- func AttentionProduce(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- userid := fsion.Get("userid").ValueStr()
- params := fsion.Get("params").Array()
- tx := restful.Engine.NewSession()
- defer tx.Close()
- productids := []string{}
- for _, product := range params {
- productids = append(productids, product.ValueStr())
- exist, err := tx.SQL("select id from attention where userid = ? and productid = ? ", userid, product.ValueInt64()).Exist()
- if err != nil {
- log.Println("AttentionProduce-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- if !exist {
- _, err := tx.SQL("insert into attention(userid,productid)values(?,?)", userid, product.ValueInt64()).Execute()
- if err != nil {
- log.Println("AttentionProduce-err2: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- }
- }
- if len(productids) == 0 {
- productids = append(productids, "0")
- }
- _, err := tx.SQL(fmt.Sprintf("delete from attention where userid = ? and productid not in(%s)", strings.Join(productids, ",")), userid).Execute()
- if err != nil {
- log.Println("AttentionProduce-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- func GetAveragePriceExcel(c *gin.Context) {
- areaids := getroleareaid(c.GetHeader("token"))
- user, _ := util.ParseToken(c.GetHeader("token"))
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- parammaps := fsion.Get("parammaps")
- productclass := parammaps.Get("productclassid").ValueStr()
- productid := parammaps.Get("productid").ValueInt64()
- starttime := parammaps.Get("starttime").ValueStr()
- endtime := parammaps.Get("endtime").ValueStr()
- productname := parammaps.Get("productname").ValueStr()
- userid := parammaps.Get("userid").ValueStr()
- username := parammaps.Get("empname").ValueStr()
- // var productidlist []string
- // if productid != 0 {
- // productidlist = append(productidlist, fmt.Sprintf("%d", productid))
- // }
- // data := make(map[string]interface{})
- // var err error
- // queryData := make([]map[string]interface{}, 0)
- var productidlist []string
- if productid != 0 {
- productidlist = append(productidlist, fmt.Sprintf("%d", productid))
- }
- data := make(map[string]interface{})
- var err error
- queryData := make([]map[string]interface{}, 0)
- // var recordcount int
- tx := restful.Engine.NewSession()
- defer tx.Close()
- if userid != "" {
- productidmap, err := tx.SQL("select Productid as productid from attention where userid = ?", userid).Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, product := range productidmap {
- productidlist = append(productidlist, fmt.Sprintf("%v", product["productid"]))
- }
- if len(productidlist) == 0 {
- appG.Response(http.StatusOK, e.SUCCESS, data)
- return
- }
- }
- {
- // sqlstr := `select t3.classname,t1.id as productid,t1.productname,t1.productclass,ifnull(t1.spec,"") as specifications ,ifnull(t2.unitname,"") as unit from productinfo t1
- // join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass where 1=1 and t1.enable = 1 `
- sqlstr := `select if(t5.code= "",0,1) as coedisnull,ifnull(t5.code,"")as productcode ,t3.classname,t1.id as productid,t1.productname,t1.productclass,ifnull(t1.spec,"") as specifications ,ifnull(t2.unitname,"") as unit from productinfo t1
- left join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass join productarea t4 on t1.id = t4.Productid
- `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
- }
- sqlstr += " left join productusercode t5 on t5.userid = ? and t5.productid = t1.id "
- var args []interface{}
- args = append(args, user.Username)
- sqlstr += ` where 1=1 and t1.enable = 1 `
- // var args []interface{}
- if len(productclass) > 0 {
- sqlstr += fmt.Sprintf(" and t1.productclass in (%s)", productclass)
- }
- if len(productname) > 0 {
- sqlstr += " and t1.productname like CONCAT('%',?,'%')"
- args = append(args, productname)
- }
- // if productid != 0 {
- // sqlstr += " and t1.id in (?) "
- // args = append(args, strings.Join(productidlist, ","))
- // }
- if len(productidlist) > 0 {
- sqlstr += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
- }
- sqlstr += " group by t1.id order by coedisnull desc, t5.code +0,t3.ordernum,t3.id,t1.ordernum,t1.id"
- // if pagecount > 0 && productid == 0 {
- // sqlstr += " limit ?,?"
- // args = append(args, (offset-1)*pagecount, pagecount)
- // }
- productrow := tx.SQL(sqlstr, args...)
- defer productrow.Close()
- queryData, err = productrow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err1: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- // if productid == 0 {
- // var args1 []interface{}
- // sqlstr1 := `select t1.id from productinfo t1
- // join prounit t2 on t2.id = t1.prounit where 1=1 and t1.enable = 1 `
- // if len(productclass) > 0 {
- // sqlstr1 += fmt.Sprintf(" and t1.productclass in (%s) ", productclass)
- // }
- // if len(productname) > 0 {
- // sqlstr1 += " and t1.productname like CONCAT('%',?,'%')"
- // args1 = append(args1, productname)
- // }
- // if len(productidlist) > 0 {
- // sqlstr1 += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
- // }
- // row := tx.SQL(sqlstr1, args1...)
- // defer row.Close()
- // count, err := row.Query().Count()
- // if err != nil {
- // log.Println("GetAveragePrice-err2: ", err)
- // appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- // return
- // }
- // recordcount = count
- // defer row.Close()
- // }
- defer productrow.Close()
- }
- // productidlist := []string{"0"}
- for _, data := range queryData {
- productidlist = append(productidlist, strconv.FormatInt(data["productid"].(int64), 10))
- }
- type areaid struct {
- areaid int64
- name string
- parentid int64
- layer int64
- sort int64
- }
- headlist := make([]map[string]interface{}, 0)
- if len(productidlist) > 0 {
- // sqlstr := `select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1
- // join (select DISTINCT(areaid) as areaid,t2.name from productarea t1 join canton t2 on t2.id = t1.areaid
- // where productid in (%s)) t2 on (t1.id = t2.areaid or t1.PARENT_ID = t2.areaid)`
- sqlstr := ` select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1 where layer >0 `
- if len(areaids) > 0 {
- sqlstr += fmt.Sprintf(" and PARENT_ID in (%s) or id in (%s)", areaids, areaids)
- }
- arearow := tx.SQL(sqlstr)
- areaData, err := arearow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- bigarea := make([]*areaid, 0)
- for _, area := range areaData {
- if area["layer"].(int64) == 1 {
- bigarea = append(bigarea, &areaid{
- areaid: area["id"].(int64),
- name: area["name"].(string),
- parentid: area["parentid"].(int64),
- layer: area["layer"].(int64),
- sort: area["ord"].(int64),
- })
- }
- }
- sort.Slice(bigarea, func(i, j int) bool {
- return bigarea[i].areaid < bigarea[j].areaid // 升序
- })
- sort.Slice(bigarea, func(i, j int) bool {
- return bigarea[i].sort < bigarea[j].sort // 升序
- })
- for _, big := range bigarea {
- head := make(map[string]interface{})
- head["column_id"] = big.areaid
- head["column_name"] = fmt.Sprintf("column_%d", big.areaid)
- head["column_title"] = big.name
- smallarea := make([]*areaid, 0)
- for _, area := range areaData {
- if area["parentid"].(int64) == big.areaid {
- smallarea = append(smallarea, &areaid{
- areaid: area["id"].(int64),
- name: area["name"].(string),
- parentid: area["parentid"].(int64),
- layer: area["layer"].(int64),
- sort: area["ord"].(int64),
- })
- }
- }
- sort.Slice(smallarea, func(i, j int) bool {
- return smallarea[i].areaid < smallarea[j].areaid // 升序
- })
- sort.Slice(smallarea, func(i, j int) bool {
- return smallarea[i].sort < smallarea[j].sort // 升序
- })
- smallhead := make([]map[string]interface{}, 0)
- for _, small := range smallarea {
- data1 := make(map[string]interface{})
- data1["column_id"] = small.areaid
- data1["column_name"] = fmt.Sprintf("column_%d", small.areaid)
- data1["column_title"] = small.name
- smallhead = append(smallhead, data1)
- }
- head["headlist"] = smallhead
- headlist = append(headlist, head)
- }
- type priceStruct struct {
- marketid int64
- price float64
- pricedate string
- }
- priceMap := make(map[int64][]priceStruct)
- if productid == 0 {
- productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,pricedate FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
- ) AS t where 1=1 `
- if starttime != "" && endtime != "" {
- productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
- }
- if len(areaids) > 0 {
- productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- productpricesql += " group by productid, marketid "
- if offset != 0 || pagecount != 0 {
- productpricesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
- }
- productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
- defer productpricerow.Close()
- productpriceData, err := productpricerow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err4: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, productprice := range productpriceData {
- price, err := strconv.ParseFloat(productprice["price"].(string), 64)
- if err != nil {
- log.Println("GetAveragePrice-err6: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
- marketid: productprice["smallareaid"].(int64),
- price: utils.Round(price, 2),
- })
- }
- for _, query := range queryData {
- for _, area := range areaData {
- if area["layer"].(int64) == 2 {
- query[fmt.Sprintf("column_%v", area["id"])] = 0
- }
- }
- query["Edit"] = false
- query["NoEdit"] = true
- query["isCreate"] = false
- query["isUpdate"] = true
- query["isUpdateSave"] = false
- }
- for _, query := range queryData {
- for _, price := range priceMap[query["productid"].(int64)] {
- query[fmt.Sprintf("column_%d", price.marketid)] = price.price
- }
- }
- data["list"] = queryData
- } else {
- timesql := `SELECT date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate desc limit 10000000000000000
- ) AS t where 1=1 `
- if len(areaids) > 0 {
- timesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- timesql += "group by date_format(pricedate, '%%Y-%%c-%%d') ORDER BY pricedate desc"
- // count, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().Count()
- // if err != nil {
- // log.Println("GetAveragePrice-err4: ", err)
- // appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- // return
- // }
- // recordcount = int(count)
- dates := []string{}
- if offset != 0 || pagecount != 0 {
- timesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
- }
- timeData, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err5: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
- SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
- ) AS t where 1=1 `
- if len(areaids) > 0 {
- productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
- }
- if len(timeData) > 0 {
- starttime = fmt.Sprintf("%s 00:00:00", timeData[len(timeData)-1]["pricedate"].(string))
- endtime = fmt.Sprintf("%s 23:59:59", timeData[0]["pricedate"].(string))
- productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
- }
- productpricesql += " group by marketid,date_format(pricedate, '%%Y-%%c-%%d') "
- productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
- defer productpricerow.Close()
- productpriceData, err := productpricerow.Query().List()
- if err != nil {
- log.Println("GetAveragePrice-err6: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- for _, productprice := range productpriceData {
- dates = append(dates, productprice["pricedate"].(string))
- price, err := strconv.ParseFloat(productprice["price"].(string), 64)
- if err != nil {
- log.Println("GetAveragePrice-err7: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
- marketid: productprice["smallareaid"].(int64),
- price: utils.Round(price, 2),
- pricedate: productprice["pricedate"].(string),
- })
- }
- datelist := utils.RemoveRepeatedElement(dates)
- sort.Sort(sort.Reverse(sort.StringSlice(datelist)))
- productmap := make([]map[string]interface{}, 0)
- for _, date := range datelist {
- product := make(map[string]interface{}, 0)
- product["productid"] = productid
- for _, query := range queryData {
- if query["productid"].(int64) == productid {
- product["productclass"] = query["productclass"]
- product["productname"] = query["productname"]
- product["unit"] = query["unit"]
- product["specifications"] = query["specifications"]
- product["classname"] = query["classname"]
- break
- }
- }
- for _, area := range areaData {
- if area["layer"].(int64) == 2 {
- product[fmt.Sprintf("column_%v", area["id"])] = 0
- }
- }
- product["Edit"] = false
- product["NoEdit"] = true
- product["isCreate"] = false
- product["isUpdate"] = true
- product["isUpdateSave"] = false
- product["pricedate"] = date
- for _, price := range priceMap[product["productid"].(int64)] {
- if date == price.pricedate {
- product[fmt.Sprintf("column_%d", price.marketid)] = price.price
- }
- }
- productmap = append(productmap, product)
- }
- data["list"] = productmap
- }
- }
- var file *xlsx.File
- var sheet *xlsx.Sheet
- var row *xlsx.Row
- var cell *xlsx.Cell
- style := xlsx.NewStyle()
- style.ApplyBorder = true
- border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
- style.Border = border
- style.Font.Size = 15
- style.Alignment = xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- ShrinkToFit: true,
- }
- style.ApplyBorder = true
- file = xlsx.NewFile()
- sheet, _ = file.AddSheet("Sheet1")
- row = sheet.AddRow()
- cell = row.AddCell()
- classnum := make(map[string]int64)
- datamap := make(map[string][]interface{})
- classlist := []string{}
- for _, query := range data["list"].([]map[string]interface{}) {
- exist := false
- for _, class := range classlist {
- if query["classname"].(string) == class {
- exist = true
- }
- }
- if !exist {
- classlist = append(classlist, query["classname"].(string))
- }
- classnum[query["classname"].(string)]++
- datamap[query["classname"].(string)] = append(datamap[query["classname"].(string)], query)
- }
- var datatitle []string
- for _, data := range headlist {
- headlist := data["headlist"].([]map[string]interface{})
- for _, head := range headlist {
- datatitle = append(datatitle, head["column_name"].(string))
- }
- }
- exceltitle := "品种: "
- for _, v := range classlist {
- exceltitle += v + ","
- }
- exceltitle = exceltitle[:len(exceltitle)-1]
- exceltitle = fmt.Sprintf("%s 用户: %s", exceltitle, username)
- cell.Value = exceltitle
- cell.Merge(len(datatitle)+6, 2)
- cell.SetStyle(style)
- num := len(datatitle) + 6
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- style = xlsx.NewStyle()
- style.Border = border
- style.ApplyBorder = true
- style.Alignment = xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- // WrapText: true,
- // ShrinkToFit: true,
- }
- style.Font.Size = 13
- row = sheet.AddRow()
- cell = row.AddCell()
- cell.Value = "分类"
- cell.Merge(0, 1)
- cell.SetStyle(style)
- cell = row.AddCell()
- cell.Value = "序号"
- cell.SetStyle(style)
- cell.Merge(0, 1)
- cell = row.AddCell()
- cell.Value = "产品ID"
- cell.SetStyle(style)
- cell.Merge(0, 1)
- cell = row.AddCell()
- cell.Value = "品名"
- cell.SetStyle(style)
- cell.Merge(0, 1)
- cell = row.AddCell()
- cell.Value = "编码"
- cell.SetStyle(style)
- cell.Merge(0, 1)
- cell = row.AddCell()
- cell.Value = "规格"
- cell.SetStyle(style)
- cell.Merge(0, 1)
- cell = row.AddCell()
- cell.Value = "单位"
- cell.SetStyle(style)
- cell.Merge(0, 1)
- if data["list"] == nil {
- _ = file.Write(c.Writer)
- return
- }
- v := 0
- for _, data := range headlist {
- for i := 0; i < v; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Value = ""
- }
- cell = row.AddCell()
- cell.Value = data["column_title"].(string)
- cell.SetStyle(style)
- head := data["headlist"].([]map[string]interface{})
- hcells := len(head) - 1
- cell.Merge(hcells, 0)
- v = hcells
- }
- for i := 0; i < v; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Value = ""
- }
- row = sheet.AddRow()
- for i := 0; i < 7; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Value = ""
- }
- // var datatitle []string
- for _, data := range headlist {
- headlist := data["headlist"].([]map[string]interface{})
- for _, head := range headlist {
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Value = head["column_title"].(string)
- // datatitle = append(datatitle, head["column_name"].(string))
- }
- }
- style = xlsx.NewStyle()
- style.ApplyBorder = true
- style.Alignment = xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- // WrapText: true,
- // ShrinkToFit: true,
- }
- style.Border = border
- style.Font.Size = 10
- i := 0
- for _, v := range classlist {
- style = xlsx.NewStyle()
- style.ApplyBorder = true
- style.Border = border
- style.Alignment = xlsx.Alignment{
- Horizontal: "center",
- Vertical: "top",
- // WrapText: true,
- // ShrinkToFit: true,
- }
- style.Font.Size = 10
- row = sheet.AddRow()
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Value = v
- cell.Merge(0, int(classnum[v])-1)
- n := len(datamap[v]) - 1
- style = xlsx.NewStyle()
- style.ApplyBorder = true
- style.Border = border
- style.Alignment = xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- // WrapText: true,
- // ShrinkToFit: true,
- }
- style.Font.Size = 10
- for m, data := range datamap[v] {
- avgdata := data.(map[string]interface{})
- i++
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.SetValue(i)
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.SetValue(avgdata["productid"])
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.SetValue(avgdata["productname"])
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.SetValue(avgdata["productcode"])
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.SetValue(avgdata["specifications"])
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.SetValue(avgdata["unit"])
- for _, title := range datatitle {
- var value float64
- if _, ok := avgdata[title].(float64); ok {
- value = avgdata[title].(float64)
- }
- cell = row.AddCell()
- cell.SetStyle(style)
- // value := avgdata[title].(float64)
- cell.SetValue(value)
- }
- if n != m {
- row = sheet.AddRow()
- cell.SetStyle(style)
- cell = row.AddCell()
- }
- }
- }
- style = xlsx.NewStyle()
- style.ApplyBorder = true
- style.Border = border
- style.Alignment = xlsx.Alignment{}
- style.Font.Size = 10
- row = sheet.AddRow()
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Merge(len(datatitle)+6, 1)
- cell.Value = "备注:1、本网站市场价格为市场现金交易价,不含增值税专用发票。配送价为配送到酒店的价格(含运输费,发票,15天帐期)。"
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Merge(len(datatitle)+6, 1)
- cell.Value = " 2、本网站蔬菜类价格主要是市场上大量采购及供货商拿货时间段(凌晨4:00-6:00)的价格。"
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- cell.SetStyle(style)
- cell.Merge(len(datatitle)+6, 1)
- cell.Value = " 3、会员企业可根据市场价格上浮一定比例给供应商定价,部分加工品种可根据加工程度另调上浮比例来定价。"
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- row = sheet.AddRow()
- cell = row.AddCell()
- cell.SetStyle(style)
- for i := 0; i < num; i++ {
- cell = row.AddCell()
- cell.SetStyle(style)
- }
- c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = file.Write(c.Writer)
- // appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- func UpdateRoleArea(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- params := fsion.Get("params").Array()
- roleid := fsion.Get("id").ValueStr()
- arealist := []string{}
- for _, arg := range params {
- arealist = append(arealist, arg.ValueStr())
- }
- tx := restful.Engine.NewSession()
- defer tx.Close()
- _, err := tx.SQL("update role set areaid = ? where id = ?", strings.Join(arealist, ","), roleid).Execute()
- if err != nil {
- log.Println("UpdateRoleArea-err3: ", err)
- appG.Response(http.StatusInternalServerError, e.ERROR, nil)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- func getroleareaid(token string) string {
- user, _ := util.ParseToken(token)
- tx := restful.Engine.NewSession()
- defer tx.Close()
- sqlstr := `select areaid from user t1
- join role t3 on t3.id = t1.roleid
- where t1.username = ?`
- data, err := tx.SQL(sqlstr, user.Username).Query().List()
- if err != nil {
- log.Println("getroleareaid-err3: ", err)
- return ""
- }
- if _, ok := data[0]["areaid"]; ok {
- return data[0]["areaid"].(string)
- }
- return ""
- }
- type areaStruct struct {
- AreaID int64 `xorm:"id"`
- Name string `xorm:"name"`
- }
- func GetAreaInfo(c *gin.Context) {
- appG := app.Gin{C: c}
- areaids := getroleareaid(c.GetHeader("token"))
- arealist := make([]*areaStruct, 0)
- tx := restful.Engine.NewSession()
- err := tx.SQL(fmt.Sprintf(`select id,name from canton where PARENT_ID in (%s)`, areaids)).Find(&arealist)
- if err != nil {
- log.Println("GetAreaInfo-err: ", err)
- appG.Response(http.StatusOK, e.ERROR, nil)
- return
- }
- data := make(map[string]interface{})
- data["list"] = arealist
- data["total"] = len(arealist)
- data["code"] = 0
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
|