qiyeku.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package audit
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "gopkg.in/mgo.v2/bson"
  6. . "jy/admin"
  7. "jy/clear"
  8. . "jy/mongodbutil"
  9. "jy/util"
  10. "log"
  11. "qfw/util/elastic"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. /**
  17. 企业库
  18. */
  19. func init() {
  20. //页面
  21. Admin.GET("/audit/qiyeku_info", func(c *gin.Context) {
  22. c.HTML(200, "qiyekuinfo.html", gin.H{})
  23. })
  24. //save
  25. Admin.POST("/audit/qiyeku_info/save", func(c *gin.Context) {
  26. _id := c.PostForm("_id")
  27. company_name := c.PostForm("company_name")
  28. alias := c.PostForm("alias")
  29. history_name := strings.ReplaceAll(c.PostForm("history_name"),";",";")
  30. area_code := c.PostForm("area_code")
  31. province := c.PostForm("province")
  32. city := c.PostForm("city")
  33. district := c.PostForm("district")
  34. capital := c.PostForm("capital")
  35. company_address := c.PostForm("company_address")
  36. business_scope := c.PostForm("business_scope")
  37. wechat_accounts := strings.ReplaceAll(c.PostForm("wechat_accounts"),";",";")
  38. website := c.PostForm("website")
  39. contact := c.PostForm("contact")
  40. contacts := make([]map[string]interface{},0)
  41. jsonerr :=json.Unmarshal([]byte(contact),&contacts)
  42. if strings.TrimSpace(company_name) == "" {
  43. c.JSON(200, gin.H{"rep": 400})
  44. return
  45. }
  46. //金额转换
  47. capitalfloat := clear.ObjToMoney([]interface{}{capital, ""})[0]
  48. e := make(map[string]interface{})
  49. e["company_name"] = company_name
  50. e["alias"] = alias
  51. if len(history_name)>0{
  52. e["history_name"] = strings.Split(history_name,";")
  53. }else {
  54. e["history_name"] =[]string{}
  55. }
  56. e["area_code"] = area_code
  57. e["province"] = province
  58. e["city"] = city
  59. e["district"] = district
  60. e["capital"] = capitalfloat
  61. e["company_address"] = company_address
  62. e["business_scope"] = business_scope
  63. if len(wechat_accounts)>0{
  64. e["wechat_accounts"] = strings.Split(wechat_accounts,";")
  65. }else {
  66. e["wechat_accounts"] = []string{}
  67. }
  68. e["website"] = website
  69. if jsonerr != nil {
  70. e["contact"] = []map[string]interface{}{}
  71. }else {
  72. for k,v := range contacts{
  73. if v["updatetime"] == nil{
  74. contacts[k]["updatetime"]=time.Now().Unix()
  75. }
  76. }
  77. e["contact"] = contacts
  78. }
  79. var sid string
  80. if bson.IsObjectIdHex(_id) {
  81. //更新
  82. sid = _id
  83. delete(e,"_id")
  84. delete(e,"company_name")
  85. //转换失败不更新
  86. if jsonerr!= nil {
  87. delete(e,"contact")
  88. }
  89. tmpb := Mgo.Update(util.ElasticClientDB, bson.M{"_id": bson.ObjectIdHex(_id)}, bson.M{"$set": e}, false, false)
  90. //更新es
  91. if tmpb {
  92. escon := elastic.GetEsConn()
  93. defer elastic.DestoryEsConn(escon)
  94. _, err := escon.Update().Index(util.ElasticClientIndex).Type(util.ElasticClientType).
  95. Id(sid).Doc(e).Refresh(true).Do()
  96. if err != nil {
  97. log.Println("update qyk err:", err)
  98. c.JSON(200, gin.H{"rep": 500, "err":"更新es错误"})
  99. return
  100. }
  101. }else {
  102. c.JSON(200, gin.H{"rep": 500, "err":"更新mongo错误"})
  103. return
  104. }
  105. c.JSON(200, gin.H{"rep": 200, "updateid": sid})
  106. } else {
  107. //不存在直接保存新数据
  108. sid = Mgo.Save(util.ElasticClientDB, e)
  109. if sid == ""{
  110. c.JSON(200, gin.H{"rep": 500,"err":"保存mongo出错"})
  111. return
  112. }else {
  113. delete(e, "_id")
  114. qykredis := util.QykRedisPool.Conn()
  115. defer qykredis.Close()
  116. if saveRedis:=qykredis.Set(company_name,sid,0);saveRedis.Err()!= nil{
  117. log.Println("保存redis 错误",saveRedis.Err())
  118. c.JSON(200, gin.H{"rep": 500,"err":"保存reids出错"})
  119. return
  120. }
  121. escon := elastic.GetEsConn()
  122. defer elastic.DestoryEsConn(escon)
  123. _, err := escon.Index().Index(util.ElasticClientIndex).Type(util.ElasticClientType).Id(sid).BodyJson(e).Refresh(true).Do()
  124. if err != nil {
  125. log.Println("save qyk err:", err)
  126. c.JSON(200, gin.H{"rep": 500, "err": "更新es错误"})
  127. return
  128. }
  129. }
  130. c.JSON(200, gin.H{"rep": 200, "saveid": sid})
  131. }
  132. })
  133. //列表查询
  134. Admin.POST("/audit/query_qyk/list", func(c *gin.Context) {
  135. search, _ := c.GetPostForm("search[value]")
  136. startstr, _ := c.GetPostForm("start")
  137. limitstr, _ := c.GetPostForm("length")
  138. start, _ := strconv.Atoi(startstr)
  139. limit, _ := strconv.Atoi(limitstr)
  140. if limit < 1 {
  141. limit = 10
  142. }
  143. if search == "" {
  144. c.JSON(200, gin.H{"data": []map[string]interface{}{}, "recordsFiltered": 0, "recordsTotal": 0})
  145. } else {
  146. //log.Println(util.ElasticClientIndex, util.ElasticClientType, search)
  147. //查询es
  148. escon := elastic.GetEsConn()
  149. defer elastic.DestoryEsConn(escon)
  150. res, err := escon.Search(util.ElasticClientIndex).
  151. Type(util.ElasticClientType).Source(`{"query": {"match_phrase":{"company_name":"`+search+`"}}}`).
  152. //Query( elastic.NewMatchPhraseQuery("company_name", search)).
  153. Size(limit).
  154. From(start).
  155. Do()
  156. if err != nil {
  157. log.Println(err)
  158. c.JSON(500, gin.H{"data": []map[string]interface{}{}, "recordsFiltered": 0, "recordsTotal": 0})
  159. return
  160. }
  161. if res.Hits != nil{
  162. tmps := make([]map[string]interface{}, 0)
  163. for _, v := range res.Hits.Hits {
  164. tmp := make(map[string]interface{})
  165. err := json.Unmarshal(*v.Source, &tmp)
  166. if err != nil {
  167. log.Println(err)
  168. continue
  169. }
  170. tmp["_id"] = v.Id
  171. //log.Println(tmp)
  172. tmps = append(tmps, tmp)
  173. }
  174. //count := Mgo.Count("enterprise_qyxy", bson.M{"company_name": bson.M{"$regex": bson.RegEx{search, "i"}}})
  175. //data, _ := Mgo.Find("enterprise_qyxy", bson.M{"company_name": bson.M{"$regex": bson.RegEx{search, "i"}}}, `{"_id":-1}`, nil, false, start, limit)
  176. c.JSON(200, gin.H{"data": tmps, "recordsFiltered": res.Hits.TotalHits, "recordsTotal": res.Hits.TotalHits})
  177. }else {
  178. c.JSON(200, gin.H{"data": []map[string]interface{}{}, "recordsFiltered": 0, "recordsTotal": 0})
  179. }
  180. }
  181. })
  182. //delete
  183. Admin.POST("/audit/qiyeku_info/deleteQyk", func(c *gin.Context) {
  184. _id := c.PostForm("_id")
  185. company_name := c.PostForm("company_name")
  186. if bson.IsObjectIdHex(_id) {
  187. delisok := Mgo.Del(util.ElasticClientDB, bson.M{"_id": bson.ObjectIdHex(_id)})
  188. if !delisok{
  189. c.JSON(200, gin.H{"rep": 500,"err":"删除mongo错误"})
  190. return
  191. }
  192. qykredis := util.QykRedisPool.Conn()
  193. defer qykredis.Close()
  194. if del := qykredis.Del(company_name);del.Err()!=nil{
  195. log.Println("delete qyk err:", del.Err(),company_name)
  196. c.JSON(200, gin.H{"rep": 500,"err":"删除redis错误"})
  197. return
  198. }
  199. escon := elastic.GetEsConn()
  200. defer elastic.DestoryEsConn(escon)
  201. _, err := escon.Delete().Index(util.ElasticClientIndex).Type(util.ElasticClientType).Id(_id).Refresh(true).Do()
  202. if err != nil {
  203. log.Println("delete qyk err:", err)
  204. c.JSON(200, gin.H{"rep": 500,"err":"删除es错误"})
  205. return
  206. }
  207. c.JSON(200, gin.H{"rep": 200, "data": delisok})
  208. } else {
  209. c.JSON(200, gin.H{"rep": 400,"err":"参数错误"})
  210. }
  211. })
  212. //queryById
  213. Admin.POST("/audit/query_qyk/ById", func(c *gin.Context) {
  214. _id := c.PostForm("_id")
  215. q_field := c.PostForm("q_field")
  216. if bson.IsObjectIdHex(_id) && strings.TrimSpace(q_field) != "" {
  217. data, _ := Mgo.FindById(util.ElasticClientDB, _id, bson.M{q_field: 1})
  218. c.JSON(200, gin.H{"rep": 200, "data": data})
  219. } else {
  220. c.JSON(200, gin.H{"rep": 400})
  221. }
  222. })
  223. //updateIndustrys 更新行业类型
  224. Admin.POST("/audit/query_qyk/UpdateIndustrys", func(c *gin.Context) {
  225. _id := c.PostForm("_id")
  226. industrys := c.PostFormArray("industry")
  227. //log.Println(_id,industrys)
  228. if bson.IsObjectIdHex(_id) {
  229. b := Mgo.Update(util.ElasticClientDB, bson.M{"_id": bson.ObjectIdHex(_id)}, bson.M{"$set": bson.M{"industry": industrys}}, false, false)
  230. if b {
  231. escon := elastic.GetEsConn()
  232. defer elastic.DestoryEsConn(escon)
  233. _, err := escon.Update().Index(util.ElasticClientIndex).Type(util.ElasticClientType).Id(_id).Doc(map[string]interface{}{
  234. "industry": industrys,
  235. }).Refresh(true).Do()
  236. if err != nil {
  237. log.Println("update yqk industry err :", err)
  238. }
  239. }
  240. c.JSON(200, gin.H{"rep": 200, "data": b})
  241. } else {
  242. c.JSON(200, gin.H{"rep": 400})
  243. }
  244. })
  245. //updateTels 更新联系方式
  246. Admin.POST("/audit/query_qyk/UpdateTels", func(c *gin.Context) {
  247. _id := c.PostForm("_id")
  248. //log.Println(_id)
  249. contact_persons := c.PostFormArray("contact_persons")
  250. contact_types := c.PostFormArray("contact_types")
  251. phones := c.PostFormArray("phones")
  252. topscopeclasss := c.PostFormArray("topscopeclasss")
  253. if bson.IsObjectIdHex(_id) && len(contact_persons) == len(contact_types) && len(phones) == len(topscopeclasss) && len(phones) == len(contact_persons) {
  254. contacts := make([]map[string]interface{}, 0)
  255. for _, v := range contact_persons {
  256. tmp := make(map[string]interface{})
  257. tmp["contact_person"] = v
  258. contacts = append(contacts, tmp)
  259. }
  260. for i, v := range contact_types {
  261. contacts[i]["contact_type"] = v
  262. }
  263. for i, v := range phones {
  264. contacts[i]["phone"] = v
  265. }
  266. for i, v := range topscopeclasss {
  267. contacts[i]["topscopeclass"] = v
  268. contacts[i]["updatetime"] = time.Now().Unix()
  269. }
  270. //for k,v := range contacts{
  271. // log.Println(k,v)
  272. //}
  273. b := Mgo.Update(util.ElasticClientDB, bson.M{"_id": bson.ObjectIdHex(_id)}, bson.M{"$set": bson.M{"contact": contacts}}, false, false)
  274. if b {
  275. escon := elastic.GetEsConn()
  276. defer elastic.DestoryEsConn(escon)
  277. _, err := escon.Update().Index(util.ElasticClientIndex).Type(util.ElasticClientType).Id(_id).Doc(map[string]interface{}{
  278. "contact": contacts,
  279. }).Refresh(true).Do()
  280. if err != nil {
  281. log.Println("update yqk contact err :", err)
  282. }
  283. }
  284. c.JSON(200, gin.H{"rep": 200, "data": b})
  285. } else {
  286. c.JSON(200, gin.H{"rep": 400})
  287. }
  288. })
  289. }