sunshine.go 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. package api
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "math"
  7. "net/http"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "../../utils"
  14. "../../pkg/app"
  15. "../../pkg/e"
  16. "../../routers/restful"
  17. "github.com/Anderson-Lu/gofasion/gofasion"
  18. "github.com/gin-gonic/gin"
  19. "github.com/kptyun/KPTCOMM/pkg/util"
  20. "github.com/tealeg/xlsx"
  21. )
  22. func GetProductAreas(c *gin.Context) {
  23. appG := app.Gin{C: c}
  24. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  25. fsion := gofasion.NewFasion(string(dataByte))
  26. offset := fsion.Get("offset").ValueDefaultInt(0)
  27. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  28. parammaps := fsion.Get("parammaps")
  29. productname := parammaps.Get("productname").ValueStr()
  30. classids := parammaps.Get("productclassid").ValueStr()
  31. var recordcount int64 = 0
  32. queryData := make([]map[string]interface{}, 1)
  33. var err error
  34. tx := restful.Engine.NewSession()
  35. defer tx.Close()
  36. {
  37. sqlstr := `SELECT t1.id,t1.productname,t1.spec FROM productinfo t1
  38. INNER JOIN prounit t2 ON t1.prounit = t2.id where 1=1 and t1.enable = 1 `
  39. if len(classids) > 0 {
  40. sqlstr += fmt.Sprintf(" and t1.productclass in ('%s')", classids)
  41. }
  42. if len(productname) > 0 {
  43. sqlstr += fmt.Sprintf(" and t1.productname like ('%s')", "%"+productname+"%")
  44. }
  45. tt, err := tx.SQL(sqlstr).Query().Count()
  46. if err != nil {
  47. log.Println("GetProductAreas-err1: ", err)
  48. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  49. return
  50. }
  51. recordcount = int64(tt)
  52. }
  53. if pagecount == 0 {
  54. sqlstr := `SELECT t1.id as productid,t1.productname,ifnull(t1.spec,"") as specifications,ifnull(t2.unitname,"") as unit
  55. FROM productinfo t1
  56. INNER JOIN prounit t2 ON t1.prounit = t2.id where 1=1 and t1.enable = 1 `
  57. if len(classids) > 0 {
  58. sqlstr += fmt.Sprintf(" and t1.productclass in ('%s')", classids)
  59. }
  60. if len(productname) > 0 {
  61. sqlstr += fmt.Sprintf(" and t1.productname like ('%s')", "%"+productname+"%")
  62. }
  63. query := tx.SQL(sqlstr)
  64. queryData, err = query.Query().List()
  65. if err != nil {
  66. log.Println("GetProductAreas-err2: ", err)
  67. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  68. return
  69. }
  70. } else {
  71. sqlstr := `SELECT t1.id as productid,t1.productname,ifnull(t1.spec,"") as specifications,ifnull(t2.unitname,"") as unit
  72. FROM productinfo t1
  73. INNER JOIN prounit t2 ON t1.prounit = t2.id where 1=1 and t1.enable = 1 `
  74. if len(classids) > 0 {
  75. sqlstr += fmt.Sprintf(" and t1.productclass in ('%s')", classids)
  76. }
  77. if len(productname) > 0 {
  78. sqlstr += fmt.Sprintf(" and t1.productname like ('%s')", "%"+productname+"%")
  79. }
  80. queryData, err = tx.SQL(sqlstr).Limit(pagecount, (offset-1)*pagecount).Query().List()
  81. if err != nil {
  82. log.Println("GetProductAreas-err3: ", err)
  83. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  84. return
  85. }
  86. }
  87. headlist := make([]map[string]interface{}, 0)
  88. areaData := make([]map[string]interface{}, 0)
  89. {
  90. sqlstr := `SELECT
  91. t1.id AS productareaid,
  92. t1.AreaId as areaid,
  93. t2.name,
  94. t1.Productid as productid
  95. FROM
  96. productarea t1
  97. INNER JOIN canton t2 ON t1.AreaId = t2.ID order by areaid`
  98. areaData, err = tx.SQL(sqlstr).Query().List()
  99. if err != nil {
  100. log.Println("GetProductAreas-err4: ", err)
  101. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  102. return
  103. }
  104. areaMap := make(map[interface{}]interface{})
  105. contactlistMap := make(map[interface{}][]interface{})
  106. for _, area := range areaData {
  107. areaMap[area["areaid"]] = area["name"]
  108. contactlistMap[area["productid"]] = append(contactlistMap[area["productid"]], area["areaid"])
  109. }
  110. areaids := []int64{}
  111. for areaid, _ := range areaMap {
  112. areaids = append(areaids, areaid.(int64))
  113. }
  114. utils.QuickSort(areaids, 0, int64(len(areaids)-1))
  115. for _, v := range areaids {
  116. head := make(map[string]interface{})
  117. head["column_name"] = fmt.Sprintf("column%v", v)
  118. head["column_title"] = areaMap[v]
  119. head["column_id"] = v
  120. headlist = append(headlist, head)
  121. }
  122. for _, data := range queryData {
  123. for id, _ := range areaMap {
  124. data[fmt.Sprintf("column%v", id)] = 0
  125. }
  126. if _, ok := contactlistMap[data["productid"]]; ok {
  127. for _, areaid := range contactlistMap[data["productid"]] {
  128. data[fmt.Sprintf("column%v", areaid)] = 1
  129. }
  130. }
  131. }
  132. }
  133. data := make(map[string]interface{})
  134. data["headlist"] = headlist
  135. data["list"] = queryData
  136. data["total"] = recordcount
  137. data["code"] = 0
  138. data["message"] = "ok"
  139. if pagecount > 0 {
  140. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  141. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  142. }
  143. data["pageSize"] = pagecount
  144. appG.Response(http.StatusOK, e.SUCCESS, data)
  145. }
  146. //查看发布价格
  147. func GetPublishPrice(c *gin.Context) {
  148. areaids := getroleareaid(c.GetHeader("token"))
  149. user, _ := util.ParseToken(c.GetHeader("token"))
  150. appG := app.Gin{C: c}
  151. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  152. fsion := gofasion.NewFasion(string(dataByte))
  153. offset := fsion.Get("offset").ValueDefaultInt(0)
  154. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  155. parammaps := fsion.Get("parammaps")
  156. productclass := parammaps.Get("productclassid").ValueInt64()
  157. date := parammaps.Get("starttime").ValueStr()
  158. productname := parammaps.Get("productname").ValueStr()
  159. data := make(map[string]interface{})
  160. var err error
  161. queryData := make([]map[string]interface{}, 0)
  162. var recordcount int
  163. tx := restful.Engine.NewSession()
  164. defer tx.Close()
  165. {
  166. 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
  167. 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
  168. `
  169. if len(areaids) > 0 {
  170. sqlstr += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
  171. // args = append(args, areaids)
  172. }
  173. var args []interface{}
  174. args = append(args, user.Username)
  175. sqlstr += "left join productusercode t5 on t5.userid = ? and t5.productid = t1.id"
  176. sqlstr += ` where 1=1 and t1.enable = 1 `
  177. if productclass > 0 {
  178. sqlstr += " and t1.productclass = ?"
  179. args = append(args, productclass)
  180. }
  181. if len(productname) > 0 {
  182. sqlstr += " and t1.productname like CONCAT('%',?,'%')"
  183. args = append(args, productname)
  184. }
  185. sqlstr += " group by t1.id order by coedisnull desc ,t5.code +0 , t3.ordernum,t3.id,t1.ordernum,t1.id asc "
  186. if pagecount > 0 {
  187. sqlstr += " limit ?,?"
  188. args = append(args, (offset-1)*pagecount, pagecount)
  189. }
  190. productrow := tx.SQL(sqlstr, args...)
  191. defer productrow.Close()
  192. queryData, err = productrow.Query().List()
  193. if err != nil {
  194. log.Println("GetPublishPrice-err1: ", err)
  195. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  196. return
  197. }
  198. // var args1 []interface{}
  199. // sqlstr1 := `select t1.id from productinfo t1
  200. // join prounit t2 on t2.id = t1.prounit where 1=1 and t1.enable = 1 `
  201. // if productclass > 0 {
  202. // sqlstr1 += " and t1.productclass = ?"
  203. // args1 = append(args1, productclass)
  204. // }
  205. // if len(productname) > 0 {
  206. // sqlstr1 += " and t1.productname like CONCAT('%',?,'%')"
  207. // args1 = append(args1, productname)
  208. // }
  209. // row := tx.SQL(sqlstr1, args1...)
  210. // defer row.Close()
  211. // count, err := row.Query().Count()
  212. // if err != nil {
  213. // log.Println("GetPublishPrice-err2: ", err)
  214. // appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  215. // return
  216. // }
  217. // recordcount = count
  218. }
  219. productidlist := []string{"0"}
  220. for _, data := range queryData {
  221. productidlist = append(productidlist, strconv.FormatInt(data["productid"].(int64), 10))
  222. }
  223. type areaid struct {
  224. areaid int64
  225. name string
  226. parentid int64
  227. layer int64
  228. sort int64
  229. }
  230. headlist := make([]map[string]interface{}, 0)
  231. {
  232. // sqlstr := `select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1
  233. // join (select DISTINCT(areaid) as areaid,t2.name from productarea t1 join canton t2 on t2.id = t1.areaid
  234. // where productid in (%s)) t2 on (t1.id = t2.areaid or t1.PARENT_ID = t2.areaid)`
  235. sqlstr := ` select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1 where layer >0 `
  236. if len(areaids) > 0 {
  237. sqlstr += fmt.Sprintf(" and (parent_id in (%s) or id in (%s))", areaids, areaids)
  238. }
  239. arearow := tx.SQL(sqlstr)
  240. areaData, err := arearow.Query().List()
  241. if err != nil {
  242. log.Println("GetPublishPrice-err3: ", err)
  243. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  244. return
  245. }
  246. bigarea := make([]*areaid, 0)
  247. wg := sync.WaitGroup{}
  248. wg.Add(len(areaData))
  249. for _, area := range areaData {
  250. go func(area map[string]interface{}) {
  251. if area["layer"].(int64) == 1 {
  252. bigarea = append(bigarea, &areaid{
  253. areaid: area["id"].(int64),
  254. name: area["name"].(string),
  255. parentid: area["parentid"].(int64),
  256. layer: area["layer"].(int64),
  257. sort: area["ord"].(int64),
  258. })
  259. }
  260. wg.Done()
  261. }(area)
  262. }
  263. wg.Wait()
  264. sort.Slice(bigarea, func(i, j int) bool {
  265. return bigarea[i].areaid < bigarea[j].areaid // 升序
  266. })
  267. sort.Slice(bigarea, func(i, j int) bool {
  268. return bigarea[i].sort < bigarea[j].sort // 升序
  269. })
  270. for _, big := range bigarea {
  271. head := make(map[string]interface{})
  272. head["column_id"] = big.areaid
  273. head["column_name"] = fmt.Sprintf("column_%d", big.areaid)
  274. head["column_title"] = big.name
  275. smallarea := make([]*areaid, 0)
  276. for _, area := range areaData {
  277. if area["parentid"].(int64) == big.areaid {
  278. smallarea = append(smallarea, &areaid{
  279. areaid: area["id"].(int64),
  280. name: area["name"].(string),
  281. parentid: area["parentid"].(int64),
  282. layer: area["layer"].(int64),
  283. sort: area["ord"].(int64),
  284. })
  285. }
  286. }
  287. sort.Slice(smallarea, func(i, j int) bool {
  288. return smallarea[i].areaid < smallarea[j].areaid // 升序
  289. })
  290. sort.Slice(smallarea, func(i, j int) bool {
  291. return smallarea[i].sort < smallarea[j].sort // 升序
  292. })
  293. smallhead := make([]map[string]interface{}, 0)
  294. for _, small := range smallarea {
  295. data1 := make(map[string]interface{})
  296. data1["column_id"] = small.areaid
  297. data1["column_name"] = fmt.Sprintf("column_%d", small.areaid)
  298. data1["column_title"] = small.name
  299. smallhead = append(smallhead, data1)
  300. }
  301. head["headlist"] = smallhead
  302. headlist = append(headlist, head)
  303. }
  304. productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,price FROM (
  305. SELECT * FROM productprice WHERE productid in (%s) `
  306. productpricesql = fmt.Sprintf(productpricesql, strings.Join(productidlist, ","))
  307. if date != "" {
  308. start := fmt.Sprintf("%s 00:00:00", date)
  309. end := fmt.Sprintf("%s 23:59:59", date)
  310. productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", start, end)
  311. }
  312. productpricesql += " ORDER BY pricedate DESC limit 1000000000000000) AS t where 1=1"
  313. if len(areaids) > 0 {
  314. productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  315. }
  316. productpricesql += " group by productid, marketid "
  317. productpricerow := tx.SQL(productpricesql)
  318. defer productpricerow.Close()
  319. productpriceData, err := productpricerow.Query().List()
  320. if err != nil {
  321. log.Println(err)
  322. return
  323. }
  324. type priceStruct struct {
  325. marketid int64
  326. price float64
  327. }
  328. priceMap := make(map[int64][]priceStruct)
  329. for _, productprice := range productpriceData {
  330. priceintface, _ := strconv.ParseFloat(productprice["price"].(string), 64)
  331. priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
  332. marketid: productprice["smallareaid"].(int64),
  333. price: priceintface,
  334. })
  335. }
  336. wg.Add(len(queryData))
  337. for _, query := range queryData {
  338. go func(query map[string]interface{}) {
  339. for _, area := range areaData {
  340. if area["layer"].(int64) == 2 {
  341. query[fmt.Sprintf("column_%v", area["id"])] = 0
  342. }
  343. }
  344. query["Edit"] = false
  345. query["NoEdit"] = true
  346. query["isCreate"] = false
  347. query["isUpdate"] = true
  348. query["isUpdateSave"] = false
  349. wg.Done()
  350. }(query)
  351. }
  352. wg.Wait()
  353. wg.Add(len(queryData))
  354. for _, query := range queryData {
  355. go func(query map[string]interface{}) {
  356. for _, price := range priceMap[query["productid"].(int64)] {
  357. query[fmt.Sprintf("column_%d", price.marketid)] = price.price
  358. }
  359. wg.Done()
  360. }(query)
  361. }
  362. wg.Wait()
  363. data["list"] = queryData
  364. }
  365. data["headlist"] = headlist
  366. data["total"] = recordcount
  367. data["code"] = 0
  368. data["message"] = "ok"
  369. if pagecount > 0 {
  370. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  371. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  372. }
  373. data["pageSize"] = pagecount
  374. appG.Response(http.StatusOK, e.SUCCESS, data)
  375. }
  376. //修改发布价格信息
  377. func UpdatePublishPrice(c *gin.Context) {
  378. appG := app.Gin{C: c}
  379. user, _ := util.ParseToken(c.GetHeader("token"))
  380. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  381. fsion := gofasion.NewFasion(string(dataByte))
  382. params := fsion.Get("params").Array()
  383. tx := restful.Engine.NewSession()
  384. defer tx.Close()
  385. areamap := make([]map[string]interface{}, 0)
  386. pricemap := make([]map[string]interface{}, 0)
  387. var err error
  388. if len(params) > 0 {
  389. areaidlist := []string{"0"}
  390. for _, key := range params[0].Keys() {
  391. if strings.Index(key, "column_") > -1 {
  392. areaidlist = append(areaidlist, key[len("column_"):])
  393. }
  394. }
  395. areamap, err = tx.SQL(fmt.Sprintf(`select t1.id,t1.PARENT_ID as parentid from canton t1
  396. join canton t2 on t1.PARENT_ID = t2.id where t1.id in (%s)
  397. order by t2.ORD ,t1.ORD,t1.id`, strings.Join(areaidlist, ","))).Query().List()
  398. if err != nil {
  399. log.Println("UpdatePublishPrice-err1: ", err)
  400. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  401. return
  402. }
  403. productidlist := []string{}
  404. for _, param := range params {
  405. productidlist = append(productidlist, param.Get("productid").ValueStr())
  406. }
  407. pricemap, err = tx.SQL(fmt.Sprintf(`SELECT t.marketid ,t.marketclass,t.price , t.pricedate,id,productid FROM (
  408. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
  409. ) AS t
  410. GROUP BY t.marketid ,productid `, strings.Join(productidlist, ","))).Query().List()
  411. if err != nil {
  412. log.Println("UpdatePublishPrice-err2: ", err)
  413. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  414. return
  415. }
  416. }
  417. var wg sync.WaitGroup
  418. for _, param := range params {
  419. productid := param.Get("productid").ValueInt64()
  420. productclass := param.Get("productclass").ValueStr()
  421. productcode := param.Get("productcode").ValueStr()
  422. for _, area := range areamap {
  423. pricenum := param.Get(fmt.Sprintf("column_%v", area["id"])).ValueFloat64()
  424. isupdateprice := false
  425. exist := false
  426. for _, price := range pricemap {
  427. if area["id"].(int64) == price["marketid"].(int64) && productid == price["productid"].(int64) {
  428. priceintface, _ := strconv.ParseFloat(price["price"].(string), 64)
  429. if priceintface == pricenum {
  430. isupdateprice = true
  431. }
  432. exist = true
  433. break
  434. }
  435. }
  436. if (!isupdateprice && (exist)) || (!isupdateprice && !exist && pricenum != 0) {
  437. var args []interface{}
  438. args = append(args, productid, area["id"], pricenum, area["parentid"], productclass)
  439. _, err := tx.SQL("insert into productprice(productid,marketid,price,marketclass,productclass,pricedate)values(?,?,?,?,?,now())", args...).Execute()
  440. if err != nil {
  441. log.Println("UpdatePublishPrice-err4: ", err)
  442. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  443. return
  444. }
  445. }
  446. }
  447. wg.Add(1)
  448. go func(user *util.Claims, productcode string, productid int64) {
  449. defer wg.Done()
  450. _, err := tx.SQL(`insert into productusercode(userid,code,productid)value(?,?,?)
  451. ON DUPLICATE KEY UPDATE code = ?`, user.Username, productcode, productid, productcode).Execute()
  452. if err != nil {
  453. log.Println("UpdatePublishPrice-err5: ", err)
  454. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  455. return
  456. }
  457. }(user, productcode, productid)
  458. }
  459. wg.Wait()
  460. appG.Response(http.StatusOK, e.SUCCESS, nil)
  461. }
  462. //查看产品信息 GetProductInfo
  463. func GetProductInfo(c *gin.Context) {
  464. appG := app.Gin{C: c}
  465. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  466. fsion := gofasion.NewFasion(string(dataByte))
  467. offset := fsion.Get("offset").ValueDefaultInt(0)
  468. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  469. parammaps := fsion.Get("parammaps")
  470. smallclassid := parammaps.Get("smallclassid").ValueStr()
  471. productname := parammaps.Get("productname").ValueStr()
  472. var args []interface{}
  473. tx := restful.Engine.NewSession()
  474. defer tx.Close()
  475. 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
  476. from productinfo t1
  477. inner join prounit t2 on t2.id = t1.prounit
  478. 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
  479. from productclass t1 join productclass t2 on t2.id = t1.parentid where t1.leavel = 2) t3 on t3.id = t1.productclass
  480. where 1=1 `
  481. if smallclassid != "" {
  482. sqlstr += " and t1.productclass = ? "
  483. args = append(args, smallclassid)
  484. }
  485. if productname != "" {
  486. sqlstr += " and t1.productname like CONCAT('%',?,'%') "
  487. args = append(args, productname)
  488. }
  489. sqlstr += " order by t3.bigsort,t3.smallsort ,t1.ordernum"
  490. arearow := tx.SQL(sqlstr, args...)
  491. defer arearow.Close()
  492. queryData := make([]map[string]interface{}, 0)
  493. var err error
  494. if pagecount > 0 {
  495. queryData, err = arearow.Limit(pagecount, (offset-1)*pagecount).Query().List()
  496. if err != nil {
  497. log.Println("GetProductInfo-err1: ", err)
  498. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  499. return
  500. }
  501. } else {
  502. queryData, err = arearow.Query().List()
  503. if err != nil {
  504. log.Println("GetProductInfo-err1: ", err)
  505. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  506. return
  507. }
  508. }
  509. sqlstrcount := `select t1.id
  510. from productinfo t1
  511. inner join prounit t2 on t2.id = t1.prounit
  512. 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
  513. from productclass t1 join productclass t2 on t2.id = t1.parentid where t1.leavel = 2) t3 on t3.id = t1.productclass
  514. where 1=1 and t1.enable = 1 `
  515. if smallclassid != "" {
  516. sqlstrcount += " and t1.productclass = ? "
  517. }
  518. if productname != "" {
  519. sqlstrcount += " and t1.productname like CONCAT('%',?,'%') "
  520. }
  521. countrow := tx.SQL(sqlstr, args...)
  522. recordcount, err := countrow.Query().Count()
  523. if err != nil {
  524. log.Println("GetProductInfo-err2: ", err)
  525. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  526. return
  527. }
  528. productids := []string{"0"}
  529. for _, query := range queryData {
  530. productids = append(productids, fmt.Sprintf("%v", query["productid"]))
  531. }
  532. pricemap, err := tx.SQL(fmt.Sprintf(`SELECT productid,price FROM (
  533. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
  534. ) AS t group by productid `, strings.Join(productids, ","))).Query().List()
  535. if err != nil {
  536. log.Println("GetProductInfo-err3: ", err)
  537. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  538. return
  539. }
  540. for _, query := range queryData {
  541. productid := query["productid"].(int64)
  542. for _, price := range pricemap {
  543. if productid == price["productid"].(int64) {
  544. query["price"] = price["price"]
  545. }
  546. }
  547. }
  548. data := make(map[string]interface{})
  549. data["list"] = queryData
  550. data["total"] = recordcount
  551. data["code"] = 0
  552. data["message"] = "ok"
  553. if pagecount > 0 {
  554. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  555. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  556. }
  557. data["pageSize"] = pagecount
  558. appG.Response(http.StatusOK, e.SUCCESS, data)
  559. }
  560. func GetAveragePrice(c *gin.Context) {
  561. areaids := getroleareaid(c.GetHeader("token"))
  562. user, _ := util.ParseToken(c.GetHeader("token"))
  563. appG := app.Gin{C: c}
  564. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  565. fsion := gofasion.NewFasion(string(dataByte))
  566. offset := fsion.Get("offset").ValueDefaultInt(0)
  567. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  568. parammaps := fsion.Get("parammaps")
  569. productclass := parammaps.Get("productclassid").ValueStr()
  570. productid := parammaps.Get("productid").ValueInt64()
  571. starttime := parammaps.Get("starttime").ValueStr()
  572. endtime := parammaps.Get("endtime").ValueStr()
  573. productname := parammaps.Get("productname").ValueStr()
  574. userid := parammaps.Get("userid").ValueStr()
  575. var productidlist []string
  576. if productid != 0 {
  577. productidlist = append(productidlist, fmt.Sprintf("%d", productid))
  578. }
  579. data := make(map[string]interface{})
  580. var err error
  581. queryData := make([]map[string]interface{}, 0)
  582. var recordcount int
  583. tx := restful.Engine.NewSession()
  584. defer tx.Close()
  585. if userid != "" {
  586. productidmap, err := tx.SQL("select Productid as productid from attention where userid = ?", userid).Query().List()
  587. if err != nil {
  588. log.Println("GetAveragePrice-err1: ", err)
  589. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  590. return
  591. }
  592. for _, product := range productidmap {
  593. productidlist = append(productidlist, fmt.Sprintf("%v", product["productid"]))
  594. }
  595. if len(productidlist) == 0 {
  596. appG.Response(http.StatusOK, e.SUCCESS, data)
  597. return
  598. }
  599. }
  600. {
  601. // sqlstr := `select t1.id as productid,t1.productname,t1.productclass,ifnull(t1.spec,"") as specifications ,ifnull(t2.unitname,"") as unit from productinfo t1
  602. // join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass where 1=1 and t1.enable = 1 `
  603. 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
  604. 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
  605. `
  606. if len(areaids) > 0 {
  607. sqlstr += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
  608. }
  609. sqlstr += " left join productusercode t5 on t5.userid = ? and t5.productid = t1.id"
  610. var args []interface{}
  611. args = append(args, user.Username)
  612. sqlstr += ` where 1=1 and t1.enable = 1 `
  613. // var args []interface{}
  614. if len(productclass) > 0 {
  615. sqlstr += fmt.Sprintf(" and t1.productclass in (%s)", productclass)
  616. }
  617. if len(productname) > 0 {
  618. sqlstr += " and t1.productname like CONCAT('%',?,'%')"
  619. args = append(args, productname)
  620. }
  621. // if productid != 0 {
  622. // sqlstr += " and t1.id in (?) "
  623. // args = append(args, strings.Join(productidlist, ","))
  624. // }
  625. if len(productidlist) > 0 {
  626. sqlstr += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
  627. }
  628. sqlstr += "group by t1.id order by coedisnull desc ,t5.code +0 , t3.ordernum,t3.id,t1.ordernum,t1.id"
  629. if pagecount > 0 && productid == 0 {
  630. sqlstr += " limit ?,?"
  631. args = append(args, (offset-1)*pagecount, pagecount)
  632. }
  633. productrow := tx.SQL(sqlstr, args...)
  634. defer productrow.Close()
  635. queryData, err = productrow.Query().List()
  636. if err != nil {
  637. log.Println("GetAveragePrice-err1: ", err)
  638. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  639. return
  640. }
  641. if productid == 0 {
  642. var args1 []interface{}
  643. sqlstr1 := `select t1.id from productinfo t1
  644. left join prounit t2 on t2.id = t1.prounit join productarea t4 on t1.id = t4.Productid where 1=1 and t1.enable = 1 `
  645. // var args []interface{}
  646. if len(areaids) > 0 {
  647. // sqlstr1 += " and t4.Areaid in (?)"
  648. sqlstr1 += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
  649. // args1 = append(args, areaids)
  650. }
  651. if len(productclass) > 0 {
  652. sqlstr1 += fmt.Sprintf(" and t1.productclass in (%s) ", productclass)
  653. }
  654. if len(productname) > 0 {
  655. sqlstr1 += " and t1.productname like CONCAT('%',?,'%')"
  656. args1 = append(args1, productname)
  657. }
  658. if len(productidlist) > 0 {
  659. sqlstr1 += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
  660. }
  661. sqlstr1 += " group by t1.id"
  662. row := tx.SQL(sqlstr1, args1...)
  663. defer row.Close()
  664. count, err := row.Query().Count()
  665. if err != nil {
  666. log.Println("GetAveragePrice-err2: ", err)
  667. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  668. return
  669. }
  670. recordcount = count
  671. defer row.Close()
  672. }
  673. defer productrow.Close()
  674. }
  675. // productidlist := []string{"0"}
  676. for _, data := range queryData {
  677. productidlist = append(productidlist, strconv.FormatInt(data["productid"].(int64), 10))
  678. }
  679. type areaid struct {
  680. areaid int64
  681. name string
  682. parentid int64
  683. layer int64
  684. sort int64
  685. }
  686. headlist := make([]map[string]interface{}, 0)
  687. if len(productidlist) > 0 {
  688. // sqlstr := `select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1
  689. // join (select DISTINCT(areaid) as areaid,t2.name from productarea t1 join canton t2 on t2.id = t1.areaid
  690. // where productid in (%s)) t2 on (t1.id = t2.areaid or t1.PARENT_ID = t2.areaid)`
  691. sqlstr := ` select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1 where layer >0 `
  692. if len(areaids) > 0 {
  693. sqlstr += fmt.Sprintf(" and PARENT_ID in (%s) or t1.id in(%s)", areaids, areaids)
  694. }
  695. arearow := tx.SQL(sqlstr)
  696. areaData, err := arearow.Query().List()
  697. if err != nil {
  698. log.Println("GetAveragePrice-err3: ", err)
  699. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  700. return
  701. }
  702. bigarea := make([]*areaid, 0)
  703. for _, area := range areaData {
  704. if area["layer"].(int64) == 1 {
  705. bigarea = append(bigarea, &areaid{
  706. areaid: area["id"].(int64),
  707. name: area["name"].(string),
  708. parentid: area["parentid"].(int64),
  709. layer: area["layer"].(int64),
  710. sort: area["ord"].(int64),
  711. })
  712. }
  713. }
  714. sort.Slice(bigarea, func(i, j int) bool {
  715. return bigarea[i].areaid < bigarea[j].areaid // 升序
  716. })
  717. sort.Slice(bigarea, func(i, j int) bool {
  718. return bigarea[i].sort < bigarea[j].sort // 升序
  719. })
  720. for _, big := range bigarea {
  721. head := make(map[string]interface{})
  722. head["column_id"] = big.areaid
  723. head["column_name"] = fmt.Sprintf("column_%d", big.areaid)
  724. head["column_title"] = big.name
  725. smallarea := make([]*areaid, 0)
  726. for _, area := range areaData {
  727. if area["parentid"].(int64) == big.areaid {
  728. smallarea = append(smallarea, &areaid{
  729. areaid: area["id"].(int64),
  730. name: area["name"].(string),
  731. parentid: area["parentid"].(int64),
  732. layer: area["layer"].(int64),
  733. sort: area["ord"].(int64),
  734. })
  735. }
  736. }
  737. sort.Slice(smallarea, func(i, j int) bool {
  738. return smallarea[i].areaid < smallarea[j].areaid // 升序
  739. })
  740. sort.Slice(smallarea, func(i, j int) bool {
  741. return smallarea[i].sort < smallarea[j].sort // 升序
  742. })
  743. smallhead := make([]map[string]interface{}, 0)
  744. for _, small := range smallarea {
  745. data1 := make(map[string]interface{})
  746. data1["column_id"] = small.areaid
  747. data1["column_name"] = fmt.Sprintf("column_%d", small.areaid)
  748. data1["column_title"] = small.name
  749. smallhead = append(smallhead, data1)
  750. }
  751. head["headlist"] = smallhead
  752. headlist = append(headlist, head)
  753. }
  754. type priceStruct struct {
  755. marketid int64
  756. price float64
  757. pricedate string
  758. }
  759. priceMap := make(map[int64][]priceStruct)
  760. if productid == 0 {
  761. productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,pricedate FROM (
  762. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
  763. ) AS t where 1=1 `
  764. if starttime != "" && endtime != "" {
  765. productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
  766. }
  767. if len(areaids) > 0 {
  768. productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  769. }
  770. productpricesql += " group by productid, marketid "
  771. if offset != 0 || pagecount != 0 {
  772. productpricesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
  773. }
  774. productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
  775. defer productpricerow.Close()
  776. productpriceData, err := productpricerow.Query().List()
  777. if err != nil {
  778. log.Println("GetAveragePrice-err4: ", err)
  779. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  780. return
  781. }
  782. for _, productprice := range productpriceData {
  783. price, err := strconv.ParseFloat(productprice["price"].(string), 64)
  784. if err != nil {
  785. log.Println("GetAveragePrice-err6: ", err)
  786. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  787. return
  788. }
  789. priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
  790. marketid: productprice["smallareaid"].(int64),
  791. price: utils.Round(price, 2),
  792. })
  793. }
  794. for _, query := range queryData {
  795. for _, area := range areaData {
  796. if area["layer"].(int64) == 2 {
  797. query[fmt.Sprintf("column_%v", area["id"])] = 0
  798. }
  799. }
  800. query["Edit"] = false
  801. query["NoEdit"] = true
  802. query["isCreate"] = false
  803. query["isUpdate"] = true
  804. query["isUpdateSave"] = false
  805. }
  806. for _, query := range queryData {
  807. for _, price := range priceMap[query["productid"].(int64)] {
  808. query[fmt.Sprintf("column_%d", price.marketid)] = price.price
  809. }
  810. }
  811. data["list"] = queryData
  812. } else {
  813. timesql := `SELECT date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
  814. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate desc limit 10000000000000000
  815. ) AS t where 1=1 `
  816. if len(areaids) > 0 {
  817. timesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  818. }
  819. timesql += " group by date_format(pricedate, '%%Y-%%c-%%d') ORDER BY pricedate desc"
  820. count, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().Count()
  821. if err != nil {
  822. log.Println("GetAveragePrice-err4: ", err)
  823. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  824. return
  825. }
  826. recordcount = int(count)
  827. dates := []string{}
  828. if offset != 0 || pagecount != 0 {
  829. timesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
  830. }
  831. timeData, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().List()
  832. if err != nil {
  833. log.Println("GetAveragePrice-err5: ", err)
  834. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  835. return
  836. }
  837. productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
  838. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
  839. ) AS t where 1=1 `
  840. if len(areaids) > 0 {
  841. productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  842. }
  843. if len(timeData) > 0 {
  844. starttime = fmt.Sprintf("%s 00:00:00", timeData[len(timeData)-1]["pricedate"].(string))
  845. endtime = fmt.Sprintf("%s 23:59:59", timeData[0]["pricedate"].(string))
  846. productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
  847. }
  848. productpricesql += " group by marketid,date_format(pricedate, '%%Y-%%c-%%d') "
  849. productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
  850. defer productpricerow.Close()
  851. productpriceData, err := productpricerow.Query().List()
  852. if err != nil {
  853. log.Println("GetAveragePrice-err6: ", err)
  854. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  855. return
  856. }
  857. for _, productprice := range productpriceData {
  858. dates = append(dates, productprice["pricedate"].(string))
  859. price, err := strconv.ParseFloat(productprice["price"].(string), 64)
  860. if err != nil {
  861. log.Println("GetAveragePrice-err7: ", err)
  862. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  863. return
  864. }
  865. priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
  866. marketid: productprice["smallareaid"].(int64),
  867. price: utils.Round(price, 2),
  868. pricedate: productprice["pricedate"].(string),
  869. })
  870. }
  871. datelist := utils.RemoveRepeatedElement(dates)
  872. sort.Sort(sort.Reverse(sort.StringSlice(datelist)))
  873. productmap := make([]map[string]interface{}, 0)
  874. for _, date := range datelist {
  875. product := make(map[string]interface{}, 0)
  876. product["productid"] = productid
  877. for _, query := range queryData {
  878. if query["productid"].(int64) == productid {
  879. product["productclass"] = query["productclass"]
  880. product["productname"] = query["productname"]
  881. product["unit"] = query["unit"]
  882. product["specifications"] = query["specifications"]
  883. break
  884. }
  885. }
  886. for _, area := range areaData {
  887. if area["layer"].(int64) == 2 {
  888. product[fmt.Sprintf("column_%v", area["id"])] = 0
  889. }
  890. }
  891. product["Edit"] = false
  892. product["NoEdit"] = true
  893. product["isCreate"] = false
  894. product["isUpdate"] = true
  895. product["isUpdateSave"] = false
  896. product["pricedate"] = date
  897. for _, price := range priceMap[product["productid"].(int64)] {
  898. if date == price.pricedate {
  899. product[fmt.Sprintf("column_%d", price.marketid)] = price.price
  900. }
  901. }
  902. productmap = append(productmap, product)
  903. }
  904. data["list"] = productmap
  905. }
  906. }
  907. data["headlist"] = headlist
  908. data["total"] = recordcount
  909. data["code"] = 0
  910. data["message"] = "ok"
  911. if pagecount > 0 {
  912. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  913. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  914. }
  915. data["pageSize"] = pagecount
  916. appG.Response(http.StatusOK, e.SUCCESS, data)
  917. }
  918. //查看平均价格图标信息
  919. func GetQuotationChart(c *gin.Context) {
  920. // areaids := getroleareaid(c.GetHeader("token"))
  921. appG := app.Gin{C: c}
  922. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  923. fsion := gofasion.NewFasion(string(dataByte))
  924. parammaps := fsion.Get("parammaps")
  925. productid := parammaps.Get("productid").ValueInt64()
  926. areaids := parammaps.Get("areaids").ValueStr()
  927. tx := restful.Engine.NewSession()
  928. defer tx.Close()
  929. areasql := `select name from canton t1 join productarea t2 on t2.productid = ? and t1.PARENT_ID = t2.areaid where layer = 2 `
  930. if len(areaids) > 0 {
  931. areasql += fmt.Sprintf(" and t1.id in (%s)", areaids)
  932. }
  933. areamap, err := tx.SQL(areasql, productid).Query().List()
  934. if err != nil {
  935. log.Println("GetQuotationChart-err0: ", err)
  936. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  937. return
  938. }
  939. sqlstr := `select avg(t1.price) as price , date_format(t1.pricedate, '%Y-%c-%d') as date ,t2.name from productprice t1
  940. join canton t2 on t2.id = t1.marketid where t1.productid = ? `
  941. if len(areaids) > 0 {
  942. sqlstr += fmt.Sprintf(" and t1.marketid in (%s)", areaids)
  943. }
  944. sqlstr += " group by date,name order by date desc"
  945. priceData, err := tx.SQL(sqlstr, productid).Query().List()
  946. if err != nil {
  947. log.Println("GetQuotationChart-err1: ", err)
  948. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  949. return
  950. }
  951. datelist := []string{}
  952. daylist := []interface{}{}
  953. for _, price := range priceData {
  954. datelist = append(datelist, price["date"].(string))
  955. }
  956. days := utils.RemoveRepeatedElement(datelist)
  957. sort.Sort(sort.Reverse(sort.StringSlice(days)))
  958. for _, day := range days {
  959. daylist = append(daylist, day)
  960. }
  961. var list [][]interface{}
  962. namelist := []interface{}{}
  963. for _, area := range areamap {
  964. namelist = append(namelist, area["name"].(string))
  965. }
  966. list = append(list, namelist)
  967. if len(daylist) == 0 {
  968. now := time.Now()
  969. for i := 0; i < 7; i++ {
  970. daylist = append(daylist, now.AddDate(0, 0, -i).Format("2006-01-02"))
  971. }
  972. }
  973. list = append(list, daylist)
  974. for _, name := range namelist {
  975. pricelist := []interface{}{}
  976. for _, day := range daylist {
  977. exist := false
  978. for _, pricemap := range priceData {
  979. if pricemap["name"].(string) == name.(string) && day.(string) == pricemap["date"].(string) {
  980. price, err := strconv.ParseFloat(pricemap["price"].(string), 64)
  981. if err != nil {
  982. log.Println("GetAveragePrice-err6: ", err)
  983. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  984. return
  985. }
  986. pricelist = append(pricelist, utils.Round(price, 2))
  987. exist = true
  988. }
  989. }
  990. if !exist {
  991. pricelist = append(pricelist, 0)
  992. }
  993. }
  994. if len(pricelist) == 0 {
  995. for i := 0; i < 7; i++ {
  996. pricelist = append(pricelist, 0)
  997. }
  998. } else {
  999. list = append(list, pricelist)
  1000. }
  1001. }
  1002. appG.Response(http.StatusOK, e.SUCCESS, list)
  1003. }
  1004. func AttentionProduce(c *gin.Context) {
  1005. appG := app.Gin{C: c}
  1006. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1007. fsion := gofasion.NewFasion(string(dataByte))
  1008. userid := fsion.Get("userid").ValueStr()
  1009. params := fsion.Get("params").Array()
  1010. tx := restful.Engine.NewSession()
  1011. defer tx.Close()
  1012. productids := []string{}
  1013. for _, product := range params {
  1014. productids = append(productids, product.ValueStr())
  1015. exist, err := tx.SQL("select id from attention where userid = ? and productid = ? ", userid, product.ValueInt64()).Exist()
  1016. if err != nil {
  1017. log.Println("AttentionProduce-err1: ", err)
  1018. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1019. return
  1020. }
  1021. if !exist {
  1022. _, err := tx.SQL("insert into attention(userid,productid)values(?,?)", userid, product.ValueInt64()).Execute()
  1023. if err != nil {
  1024. log.Println("AttentionProduce-err2: ", err)
  1025. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1026. return
  1027. }
  1028. }
  1029. }
  1030. if len(productids) == 0 {
  1031. productids = append(productids, "0")
  1032. }
  1033. _, err := tx.SQL(fmt.Sprintf("delete from attention where userid = ? and productid not in(%s)", strings.Join(productids, ",")), userid).Execute()
  1034. if err != nil {
  1035. log.Println("AttentionProduce-err3: ", err)
  1036. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1037. return
  1038. }
  1039. appG.Response(http.StatusOK, e.SUCCESS, true)
  1040. }
  1041. func GetAveragePriceExcel(c *gin.Context) {
  1042. areaids := getroleareaid(c.GetHeader("token"))
  1043. user, _ := util.ParseToken(c.GetHeader("token"))
  1044. appG := app.Gin{C: c}
  1045. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1046. fsion := gofasion.NewFasion(string(dataByte))
  1047. offset := fsion.Get("offset").ValueDefaultInt(0)
  1048. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  1049. parammaps := fsion.Get("parammaps")
  1050. productclass := parammaps.Get("productclassid").ValueStr()
  1051. productid := parammaps.Get("productid").ValueInt64()
  1052. starttime := parammaps.Get("starttime").ValueStr()
  1053. endtime := parammaps.Get("endtime").ValueStr()
  1054. productname := parammaps.Get("productname").ValueStr()
  1055. userid := parammaps.Get("userid").ValueStr()
  1056. username := parammaps.Get("empname").ValueStr()
  1057. // var productidlist []string
  1058. // if productid != 0 {
  1059. // productidlist = append(productidlist, fmt.Sprintf("%d", productid))
  1060. // }
  1061. // data := make(map[string]interface{})
  1062. // var err error
  1063. // queryData := make([]map[string]interface{}, 0)
  1064. var productidlist []string
  1065. if productid != 0 {
  1066. productidlist = append(productidlist, fmt.Sprintf("%d", productid))
  1067. }
  1068. data := make(map[string]interface{})
  1069. var err error
  1070. queryData := make([]map[string]interface{}, 0)
  1071. // var recordcount int
  1072. tx := restful.Engine.NewSession()
  1073. defer tx.Close()
  1074. if userid != "" {
  1075. productidmap, err := tx.SQL("select Productid as productid from attention where userid = ?", userid).Query().List()
  1076. if err != nil {
  1077. log.Println("GetAveragePrice-err1: ", err)
  1078. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1079. return
  1080. }
  1081. for _, product := range productidmap {
  1082. productidlist = append(productidlist, fmt.Sprintf("%v", product["productid"]))
  1083. }
  1084. if len(productidlist) == 0 {
  1085. appG.Response(http.StatusOK, e.SUCCESS, data)
  1086. return
  1087. }
  1088. }
  1089. {
  1090. // 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
  1091. // join prounit t2 on t2.id = t1.prounit join productclass t3 on t3.id = t1.productclass where 1=1 and t1.enable = 1 `
  1092. 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
  1093. 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
  1094. `
  1095. if len(areaids) > 0 {
  1096. sqlstr += fmt.Sprintf(" and t4.Areaid in (%s)", areaids)
  1097. }
  1098. sqlstr += " left join productusercode t5 on t5.userid = ? and t5.productid = t1.id "
  1099. var args []interface{}
  1100. args = append(args, user.Username)
  1101. sqlstr += ` where 1=1 and t1.enable = 1 `
  1102. // var args []interface{}
  1103. if len(productclass) > 0 {
  1104. sqlstr += fmt.Sprintf(" and t1.productclass in (%s)", productclass)
  1105. }
  1106. if len(productname) > 0 {
  1107. sqlstr += " and t1.productname like CONCAT('%',?,'%')"
  1108. args = append(args, productname)
  1109. }
  1110. // if productid != 0 {
  1111. // sqlstr += " and t1.id in (?) "
  1112. // args = append(args, strings.Join(productidlist, ","))
  1113. // }
  1114. if len(productidlist) > 0 {
  1115. sqlstr += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
  1116. }
  1117. sqlstr += " group by t1.id order by coedisnull desc, t5.code +0,t3.ordernum,t3.id,t1.ordernum,t1.id"
  1118. // if pagecount > 0 && productid == 0 {
  1119. // sqlstr += " limit ?,?"
  1120. // args = append(args, (offset-1)*pagecount, pagecount)
  1121. // }
  1122. productrow := tx.SQL(sqlstr, args...)
  1123. defer productrow.Close()
  1124. queryData, err = productrow.Query().List()
  1125. if err != nil {
  1126. log.Println("GetAveragePrice-err1: ", err)
  1127. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1128. return
  1129. }
  1130. // if productid == 0 {
  1131. // var args1 []interface{}
  1132. // sqlstr1 := `select t1.id from productinfo t1
  1133. // join prounit t2 on t2.id = t1.prounit where 1=1 and t1.enable = 1 `
  1134. // if len(productclass) > 0 {
  1135. // sqlstr1 += fmt.Sprintf(" and t1.productclass in (%s) ", productclass)
  1136. // }
  1137. // if len(productname) > 0 {
  1138. // sqlstr1 += " and t1.productname like CONCAT('%',?,'%')"
  1139. // args1 = append(args1, productname)
  1140. // }
  1141. // if len(productidlist) > 0 {
  1142. // sqlstr1 += fmt.Sprintf(" and t1.id in (%s) ", strings.Join(productidlist, ","))
  1143. // }
  1144. // row := tx.SQL(sqlstr1, args1...)
  1145. // defer row.Close()
  1146. // count, err := row.Query().Count()
  1147. // if err != nil {
  1148. // log.Println("GetAveragePrice-err2: ", err)
  1149. // appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1150. // return
  1151. // }
  1152. // recordcount = count
  1153. // defer row.Close()
  1154. // }
  1155. defer productrow.Close()
  1156. }
  1157. // productidlist := []string{"0"}
  1158. for _, data := range queryData {
  1159. productidlist = append(productidlist, strconv.FormatInt(data["productid"].(int64), 10))
  1160. }
  1161. type areaid struct {
  1162. areaid int64
  1163. name string
  1164. parentid int64
  1165. layer int64
  1166. sort int64
  1167. }
  1168. headlist := make([]map[string]interface{}, 0)
  1169. if len(productidlist) > 0 {
  1170. // sqlstr := `select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1
  1171. // join (select DISTINCT(areaid) as areaid,t2.name from productarea t1 join canton t2 on t2.id = t1.areaid
  1172. // where productid in (%s)) t2 on (t1.id = t2.areaid or t1.PARENT_ID = t2.areaid)`
  1173. sqlstr := ` select t1.id,t1.name,t1.parent_id as parentid ,t1.layer,t1.ord from canton t1 where layer >0 `
  1174. if len(areaids) > 0 {
  1175. sqlstr += fmt.Sprintf(" and PARENT_ID in (%s) or id in (%s)", areaids, areaids)
  1176. }
  1177. arearow := tx.SQL(sqlstr)
  1178. areaData, err := arearow.Query().List()
  1179. if err != nil {
  1180. log.Println("GetAveragePrice-err3: ", err)
  1181. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1182. return
  1183. }
  1184. bigarea := make([]*areaid, 0)
  1185. for _, area := range areaData {
  1186. if area["layer"].(int64) == 1 {
  1187. bigarea = append(bigarea, &areaid{
  1188. areaid: area["id"].(int64),
  1189. name: area["name"].(string),
  1190. parentid: area["parentid"].(int64),
  1191. layer: area["layer"].(int64),
  1192. sort: area["ord"].(int64),
  1193. })
  1194. }
  1195. }
  1196. sort.Slice(bigarea, func(i, j int) bool {
  1197. return bigarea[i].areaid < bigarea[j].areaid // 升序
  1198. })
  1199. sort.Slice(bigarea, func(i, j int) bool {
  1200. return bigarea[i].sort < bigarea[j].sort // 升序
  1201. })
  1202. for _, big := range bigarea {
  1203. head := make(map[string]interface{})
  1204. head["column_id"] = big.areaid
  1205. head["column_name"] = fmt.Sprintf("column_%d", big.areaid)
  1206. head["column_title"] = big.name
  1207. smallarea := make([]*areaid, 0)
  1208. for _, area := range areaData {
  1209. if area["parentid"].(int64) == big.areaid {
  1210. smallarea = append(smallarea, &areaid{
  1211. areaid: area["id"].(int64),
  1212. name: area["name"].(string),
  1213. parentid: area["parentid"].(int64),
  1214. layer: area["layer"].(int64),
  1215. sort: area["ord"].(int64),
  1216. })
  1217. }
  1218. }
  1219. sort.Slice(smallarea, func(i, j int) bool {
  1220. return smallarea[i].areaid < smallarea[j].areaid // 升序
  1221. })
  1222. sort.Slice(smallarea, func(i, j int) bool {
  1223. return smallarea[i].sort < smallarea[j].sort // 升序
  1224. })
  1225. smallhead := make([]map[string]interface{}, 0)
  1226. for _, small := range smallarea {
  1227. data1 := make(map[string]interface{})
  1228. data1["column_id"] = small.areaid
  1229. data1["column_name"] = fmt.Sprintf("column_%d", small.areaid)
  1230. data1["column_title"] = small.name
  1231. smallhead = append(smallhead, data1)
  1232. }
  1233. head["headlist"] = smallhead
  1234. headlist = append(headlist, head)
  1235. }
  1236. type priceStruct struct {
  1237. marketid int64
  1238. price float64
  1239. pricedate string
  1240. }
  1241. priceMap := make(map[int64][]priceStruct)
  1242. if productid == 0 {
  1243. productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,pricedate FROM (
  1244. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
  1245. ) AS t where 1=1 `
  1246. if starttime != "" && endtime != "" {
  1247. productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
  1248. }
  1249. if len(areaids) > 0 {
  1250. productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  1251. }
  1252. productpricesql += " group by productid, marketid "
  1253. if offset != 0 || pagecount != 0 {
  1254. productpricesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
  1255. }
  1256. productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
  1257. defer productpricerow.Close()
  1258. productpriceData, err := productpricerow.Query().List()
  1259. if err != nil {
  1260. log.Println("GetAveragePrice-err4: ", err)
  1261. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1262. return
  1263. }
  1264. for _, productprice := range productpriceData {
  1265. price, err := strconv.ParseFloat(productprice["price"].(string), 64)
  1266. if err != nil {
  1267. log.Println("GetAveragePrice-err6: ", err)
  1268. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1269. return
  1270. }
  1271. priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
  1272. marketid: productprice["smallareaid"].(int64),
  1273. price: utils.Round(price, 2),
  1274. })
  1275. }
  1276. for _, query := range queryData {
  1277. for _, area := range areaData {
  1278. if area["layer"].(int64) == 2 {
  1279. query[fmt.Sprintf("column_%v", area["id"])] = 0
  1280. }
  1281. }
  1282. query["Edit"] = false
  1283. query["NoEdit"] = true
  1284. query["isCreate"] = false
  1285. query["isUpdate"] = true
  1286. query["isUpdateSave"] = false
  1287. }
  1288. for _, query := range queryData {
  1289. for _, price := range priceMap[query["productid"].(int64)] {
  1290. query[fmt.Sprintf("column_%d", price.marketid)] = price.price
  1291. }
  1292. }
  1293. data["list"] = queryData
  1294. } else {
  1295. timesql := `SELECT date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
  1296. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate desc limit 10000000000000000
  1297. ) AS t where 1=1 `
  1298. if len(areaids) > 0 {
  1299. timesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  1300. }
  1301. timesql += "group by date_format(pricedate, '%%Y-%%c-%%d') ORDER BY pricedate desc"
  1302. // count, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().Count()
  1303. // if err != nil {
  1304. // log.Println("GetAveragePrice-err4: ", err)
  1305. // appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1306. // return
  1307. // }
  1308. // recordcount = int(count)
  1309. dates := []string{}
  1310. if offset != 0 || pagecount != 0 {
  1311. timesql += fmt.Sprintf(" limit %d,%d", (offset-1)*pagecount, pagecount)
  1312. }
  1313. timeData, err := tx.SQL(fmt.Sprintf(timesql, strings.Join(productidlist, ","))).Query().List()
  1314. if err != nil {
  1315. log.Println("GetAveragePrice-err5: ", err)
  1316. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1317. return
  1318. }
  1319. productpricesql := `SELECT productid,marketid as smallareaid , marketclass as bigareaid,avg(price) as price,date_format(pricedate, '%%Y-%%c-%%d') as pricedate FROM (
  1320. SELECT * FROM productprice WHERE productid in (%s) ORDER BY pricedate DESC limit 10000000000000000
  1321. ) AS t where 1=1 `
  1322. if len(areaids) > 0 {
  1323. productpricesql += fmt.Sprintf(" and t.marketclass in (%s)", areaids)
  1324. }
  1325. if len(timeData) > 0 {
  1326. starttime = fmt.Sprintf("%s 00:00:00", timeData[len(timeData)-1]["pricedate"].(string))
  1327. endtime = fmt.Sprintf("%s 23:59:59", timeData[0]["pricedate"].(string))
  1328. productpricesql += fmt.Sprintf(" and pricedate BETWEEN '%s' and '%s' ", fmt.Sprintf("%s 00:00:00", starttime), fmt.Sprintf("%s 23:59:59", endtime))
  1329. }
  1330. productpricesql += " group by marketid,date_format(pricedate, '%%Y-%%c-%%d') "
  1331. productpricerow := tx.SQL(fmt.Sprintf(productpricesql, strings.Join(productidlist, ",")))
  1332. defer productpricerow.Close()
  1333. productpriceData, err := productpricerow.Query().List()
  1334. if err != nil {
  1335. log.Println("GetAveragePrice-err6: ", err)
  1336. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1337. return
  1338. }
  1339. for _, productprice := range productpriceData {
  1340. dates = append(dates, productprice["pricedate"].(string))
  1341. price, err := strconv.ParseFloat(productprice["price"].(string), 64)
  1342. if err != nil {
  1343. log.Println("GetAveragePrice-err7: ", err)
  1344. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1345. return
  1346. }
  1347. priceMap[productprice["productid"].(int64)] = append(priceMap[productprice["productid"].(int64)], priceStruct{
  1348. marketid: productprice["smallareaid"].(int64),
  1349. price: utils.Round(price, 2),
  1350. pricedate: productprice["pricedate"].(string),
  1351. })
  1352. }
  1353. datelist := utils.RemoveRepeatedElement(dates)
  1354. sort.Sort(sort.Reverse(sort.StringSlice(datelist)))
  1355. productmap := make([]map[string]interface{}, 0)
  1356. for _, date := range datelist {
  1357. product := make(map[string]interface{}, 0)
  1358. product["productid"] = productid
  1359. for _, query := range queryData {
  1360. if query["productid"].(int64) == productid {
  1361. product["productclass"] = query["productclass"]
  1362. product["productname"] = query["productname"]
  1363. product["unit"] = query["unit"]
  1364. product["specifications"] = query["specifications"]
  1365. product["classname"] = query["classname"]
  1366. break
  1367. }
  1368. }
  1369. for _, area := range areaData {
  1370. if area["layer"].(int64) == 2 {
  1371. product[fmt.Sprintf("column_%v", area["id"])] = 0
  1372. }
  1373. }
  1374. product["Edit"] = false
  1375. product["NoEdit"] = true
  1376. product["isCreate"] = false
  1377. product["isUpdate"] = true
  1378. product["isUpdateSave"] = false
  1379. product["pricedate"] = date
  1380. for _, price := range priceMap[product["productid"].(int64)] {
  1381. if date == price.pricedate {
  1382. product[fmt.Sprintf("column_%d", price.marketid)] = price.price
  1383. }
  1384. }
  1385. productmap = append(productmap, product)
  1386. }
  1387. data["list"] = productmap
  1388. }
  1389. }
  1390. var file *xlsx.File
  1391. var sheet *xlsx.Sheet
  1392. var row *xlsx.Row
  1393. var cell *xlsx.Cell
  1394. style := xlsx.NewStyle()
  1395. style.ApplyBorder = true
  1396. border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
  1397. style.Border = border
  1398. style.Font.Size = 15
  1399. style.Alignment = xlsx.Alignment{
  1400. Horizontal: "center",
  1401. Vertical: "center",
  1402. WrapText: true,
  1403. ShrinkToFit: true,
  1404. }
  1405. style.ApplyBorder = true
  1406. file = xlsx.NewFile()
  1407. sheet, _ = file.AddSheet("Sheet1")
  1408. row = sheet.AddRow()
  1409. cell = row.AddCell()
  1410. classnum := make(map[string]int64)
  1411. datamap := make(map[string][]interface{})
  1412. classlist := []string{}
  1413. for _, query := range data["list"].([]map[string]interface{}) {
  1414. exist := false
  1415. for _, class := range classlist {
  1416. if query["classname"].(string) == class {
  1417. exist = true
  1418. }
  1419. }
  1420. if !exist {
  1421. classlist = append(classlist, query["classname"].(string))
  1422. }
  1423. classnum[query["classname"].(string)]++
  1424. datamap[query["classname"].(string)] = append(datamap[query["classname"].(string)], query)
  1425. }
  1426. var datatitle []string
  1427. for _, data := range headlist {
  1428. headlist := data["headlist"].([]map[string]interface{})
  1429. for _, head := range headlist {
  1430. datatitle = append(datatitle, head["column_name"].(string))
  1431. }
  1432. }
  1433. exceltitle := "品种: "
  1434. for _, v := range classlist {
  1435. exceltitle += v + ","
  1436. }
  1437. exceltitle = exceltitle[:len(exceltitle)-1]
  1438. exceltitle = fmt.Sprintf("%s 用户: %s", exceltitle, username)
  1439. cell.Value = exceltitle
  1440. cell.Merge(len(datatitle)+6, 2)
  1441. cell.SetStyle(style)
  1442. num := len(datatitle) + 6
  1443. for i := 0; i < num; i++ {
  1444. cell = row.AddCell()
  1445. cell.SetStyle(style)
  1446. }
  1447. row = sheet.AddRow()
  1448. cell = row.AddCell()
  1449. for i := 0; i < num; i++ {
  1450. cell = row.AddCell()
  1451. cell.SetStyle(style)
  1452. }
  1453. row = sheet.AddRow()
  1454. cell = row.AddCell()
  1455. for i := 0; i < num; i++ {
  1456. cell = row.AddCell()
  1457. cell.SetStyle(style)
  1458. }
  1459. style = xlsx.NewStyle()
  1460. style.Border = border
  1461. style.ApplyBorder = true
  1462. style.Alignment = xlsx.Alignment{
  1463. Horizontal: "center",
  1464. Vertical: "center",
  1465. // WrapText: true,
  1466. // ShrinkToFit: true,
  1467. }
  1468. style.Font.Size = 13
  1469. row = sheet.AddRow()
  1470. cell = row.AddCell()
  1471. cell.Value = "分类"
  1472. cell.Merge(0, 1)
  1473. cell.SetStyle(style)
  1474. cell = row.AddCell()
  1475. cell.Value = "序号"
  1476. cell.SetStyle(style)
  1477. cell.Merge(0, 1)
  1478. cell = row.AddCell()
  1479. cell.Value = "产品ID"
  1480. cell.SetStyle(style)
  1481. cell.Merge(0, 1)
  1482. cell = row.AddCell()
  1483. cell.Value = "品名"
  1484. cell.SetStyle(style)
  1485. cell.Merge(0, 1)
  1486. cell = row.AddCell()
  1487. cell.Value = "编码"
  1488. cell.SetStyle(style)
  1489. cell.Merge(0, 1)
  1490. cell = row.AddCell()
  1491. cell.Value = "规格"
  1492. cell.SetStyle(style)
  1493. cell.Merge(0, 1)
  1494. cell = row.AddCell()
  1495. cell.Value = "单位"
  1496. cell.SetStyle(style)
  1497. cell.Merge(0, 1)
  1498. if data["list"] == nil {
  1499. _ = file.Write(c.Writer)
  1500. return
  1501. }
  1502. v := 0
  1503. for _, data := range headlist {
  1504. for i := 0; i < v; i++ {
  1505. cell = row.AddCell()
  1506. cell.SetStyle(style)
  1507. cell.Value = ""
  1508. }
  1509. cell = row.AddCell()
  1510. cell.Value = data["column_title"].(string)
  1511. cell.SetStyle(style)
  1512. head := data["headlist"].([]map[string]interface{})
  1513. hcells := len(head) - 1
  1514. cell.Merge(hcells, 0)
  1515. v = hcells
  1516. }
  1517. for i := 0; i < v; i++ {
  1518. cell = row.AddCell()
  1519. cell.SetStyle(style)
  1520. cell.Value = ""
  1521. }
  1522. row = sheet.AddRow()
  1523. for i := 0; i < 7; i++ {
  1524. cell = row.AddCell()
  1525. cell.SetStyle(style)
  1526. cell.Value = ""
  1527. }
  1528. // var datatitle []string
  1529. for _, data := range headlist {
  1530. headlist := data["headlist"].([]map[string]interface{})
  1531. for _, head := range headlist {
  1532. cell = row.AddCell()
  1533. cell.SetStyle(style)
  1534. cell.Value = head["column_title"].(string)
  1535. // datatitle = append(datatitle, head["column_name"].(string))
  1536. }
  1537. }
  1538. style = xlsx.NewStyle()
  1539. style.ApplyBorder = true
  1540. style.Alignment = xlsx.Alignment{
  1541. Horizontal: "center",
  1542. Vertical: "center",
  1543. // WrapText: true,
  1544. // ShrinkToFit: true,
  1545. }
  1546. style.Border = border
  1547. style.Font.Size = 10
  1548. i := 0
  1549. for _, v := range classlist {
  1550. style = xlsx.NewStyle()
  1551. style.ApplyBorder = true
  1552. style.Border = border
  1553. style.Alignment = xlsx.Alignment{
  1554. Horizontal: "center",
  1555. Vertical: "top",
  1556. // WrapText: true,
  1557. // ShrinkToFit: true,
  1558. }
  1559. style.Font.Size = 10
  1560. row = sheet.AddRow()
  1561. cell = row.AddCell()
  1562. cell.SetStyle(style)
  1563. cell.Value = v
  1564. cell.Merge(0, int(classnum[v])-1)
  1565. n := len(datamap[v]) - 1
  1566. style = xlsx.NewStyle()
  1567. style.ApplyBorder = true
  1568. style.Border = border
  1569. style.Alignment = xlsx.Alignment{
  1570. Horizontal: "center",
  1571. Vertical: "center",
  1572. // WrapText: true,
  1573. // ShrinkToFit: true,
  1574. }
  1575. style.Font.Size = 10
  1576. for m, data := range datamap[v] {
  1577. avgdata := data.(map[string]interface{})
  1578. i++
  1579. cell = row.AddCell()
  1580. cell.SetStyle(style)
  1581. cell.SetValue(i)
  1582. cell = row.AddCell()
  1583. cell.SetStyle(style)
  1584. cell.SetValue(avgdata["productid"])
  1585. cell = row.AddCell()
  1586. cell.SetStyle(style)
  1587. cell.SetValue(avgdata["productname"])
  1588. cell = row.AddCell()
  1589. cell.SetStyle(style)
  1590. cell.SetValue(avgdata["productcode"])
  1591. cell = row.AddCell()
  1592. cell.SetStyle(style)
  1593. cell.SetValue(avgdata["specifications"])
  1594. cell = row.AddCell()
  1595. cell.SetStyle(style)
  1596. cell.SetValue(avgdata["unit"])
  1597. for _, title := range datatitle {
  1598. var value float64
  1599. if _, ok := avgdata[title].(float64); ok {
  1600. value = avgdata[title].(float64)
  1601. }
  1602. cell = row.AddCell()
  1603. cell.SetStyle(style)
  1604. // value := avgdata[title].(float64)
  1605. cell.SetValue(value)
  1606. }
  1607. if n != m {
  1608. row = sheet.AddRow()
  1609. cell.SetStyle(style)
  1610. cell = row.AddCell()
  1611. }
  1612. }
  1613. }
  1614. style = xlsx.NewStyle()
  1615. style.ApplyBorder = true
  1616. style.Border = border
  1617. style.Alignment = xlsx.Alignment{}
  1618. style.Font.Size = 10
  1619. row = sheet.AddRow()
  1620. cell = row.AddCell()
  1621. cell.SetStyle(style)
  1622. cell.Merge(len(datatitle)+6, 1)
  1623. cell.Value = "备注:1、本网站市场价格为市场现金交易价,不含增值税专用发票。配送价为配送到酒店的价格(含运输费,发票,15天帐期)。"
  1624. for i := 0; i < num; i++ {
  1625. cell = row.AddCell()
  1626. cell.SetStyle(style)
  1627. }
  1628. row = sheet.AddRow()
  1629. cell = row.AddCell()
  1630. for i := 0; i < num; i++ {
  1631. cell = row.AddCell()
  1632. cell.SetStyle(style)
  1633. }
  1634. row = sheet.AddRow()
  1635. cell = row.AddCell()
  1636. cell.SetStyle(style)
  1637. cell.Merge(len(datatitle)+6, 1)
  1638. cell.Value = " 2、本网站蔬菜类价格主要是市场上大量采购及供货商拿货时间段(凌晨4:00-6:00)的价格。"
  1639. for i := 0; i < num; i++ {
  1640. cell = row.AddCell()
  1641. cell.SetStyle(style)
  1642. }
  1643. row = sheet.AddRow()
  1644. cell = row.AddCell()
  1645. for i := 0; i < num; i++ {
  1646. cell = row.AddCell()
  1647. cell.SetStyle(style)
  1648. }
  1649. row = sheet.AddRow()
  1650. cell = row.AddCell()
  1651. cell.SetStyle(style)
  1652. cell.Merge(len(datatitle)+6, 1)
  1653. cell.Value = " 3、会员企业可根据市场价格上浮一定比例给供应商定价,部分加工品种可根据加工程度另调上浮比例来定价。"
  1654. for i := 0; i < num; i++ {
  1655. cell = row.AddCell()
  1656. cell.SetStyle(style)
  1657. }
  1658. row = sheet.AddRow()
  1659. cell = row.AddCell()
  1660. cell.SetStyle(style)
  1661. for i := 0; i < num; i++ {
  1662. cell = row.AddCell()
  1663. cell.SetStyle(style)
  1664. }
  1665. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1666. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  1667. c.Header("Content-Transfer-Encoding", "binary")
  1668. _ = file.Write(c.Writer)
  1669. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  1670. }
  1671. func UpdateRoleArea(c *gin.Context) {
  1672. appG := app.Gin{C: c}
  1673. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1674. fsion := gofasion.NewFasion(string(dataByte))
  1675. params := fsion.Get("params").Array()
  1676. roleid := fsion.Get("id").ValueStr()
  1677. arealist := []string{}
  1678. for _, arg := range params {
  1679. arealist = append(arealist, arg.ValueStr())
  1680. }
  1681. tx := restful.Engine.NewSession()
  1682. defer tx.Close()
  1683. _, err := tx.SQL("update role set areaid = ? where id = ?", strings.Join(arealist, ","), roleid).Execute()
  1684. if err != nil {
  1685. log.Println("UpdateRoleArea-err3: ", err)
  1686. appG.Response(http.StatusInternalServerError, e.ERROR, nil)
  1687. return
  1688. }
  1689. appG.Response(http.StatusOK, e.SUCCESS, true)
  1690. }
  1691. func getroleareaid(token string) string {
  1692. user, _ := util.ParseToken(token)
  1693. tx := restful.Engine.NewSession()
  1694. defer tx.Close()
  1695. sqlstr := `select areaid from user t1
  1696. join role t3 on t3.id = t1.roleid
  1697. where t1.username = ?`
  1698. data, err := tx.SQL(sqlstr, user.Username).Query().List()
  1699. if err != nil {
  1700. log.Println("getroleareaid-err3: ", err)
  1701. return ""
  1702. }
  1703. if _, ok := data[0]["areaid"]; ok {
  1704. return data[0]["areaid"].(string)
  1705. }
  1706. return ""
  1707. }
  1708. type areaStruct struct {
  1709. AreaID int64 `xorm:"id"`
  1710. Name string `xorm:"name"`
  1711. }
  1712. func GetAreaInfo(c *gin.Context) {
  1713. appG := app.Gin{C: c}
  1714. areaids := getroleareaid(c.GetHeader("token"))
  1715. arealist := make([]*areaStruct, 0)
  1716. tx := restful.Engine.NewSession()
  1717. err := tx.SQL(fmt.Sprintf(`select id,name from canton where PARENT_ID in (%s)`, areaids)).Find(&arealist)
  1718. if err != nil {
  1719. log.Println("GetAreaInfo-err: ", err)
  1720. appG.Response(http.StatusOK, e.ERROR, nil)
  1721. return
  1722. }
  1723. data := make(map[string]interface{})
  1724. data["list"] = arealist
  1725. data["total"] = len(arealist)
  1726. data["code"] = 0
  1727. appG.Response(http.StatusOK, e.SUCCESS, data)
  1728. }