sunshine.go 50 KB

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