company.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hongze_task/models"
  7. "hongze/hongze_task/utils"
  8. "os"
  9. "strings"
  10. "time"
  11. )
  12. //客户
  13. func ImportCompany() {
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("err:", err.Error())
  18. }
  19. }()
  20. fileName := "./company_0717.xlsx"
  21. xlFile, err := xlsx.OpenFile(fileName)
  22. if err != nil {
  23. fmt.Printf("open failed: %s\n", err)
  24. }
  25. n := 0
  26. startDate := ""
  27. endDate := ""
  28. dateType := 3
  29. permissionMap := make(map[string]int)
  30. permissionMap["宏观经济"] = 1
  31. permissionMap["有色"] = 2
  32. permissionMap["原油"] = 3
  33. permissionMap["橡胶"] = 4
  34. permissionMap["钢材"] = 5
  35. permissionMap["煤炭"] = 6
  36. permissionMap["甲醇"] = 7
  37. permissionMap["沥青"] = 8
  38. permissionMap["PTA"] = 9
  39. permissionMap["MEG"] = 10
  40. permissionMap["聚烯烃"] = 11
  41. permissionMap["铁矿"] = 12
  42. permissionMap["苯乙烯"] = 13
  43. permissionMap["织造终端"] = 14
  44. for _, sheet := range xlFile.Sheet {
  45. rows := sheet.MaxRow
  46. for i := 0; i < rows; i++ {
  47. if i > 2 {
  48. row := sheet.Row(i)
  49. cells := row.Cells
  50. realName := cells[1].String()
  51. realName = strings.Trim(realName, " ")
  52. realName = strings.Replace(realName, " ", "", -1)
  53. mobile := cells[2].String()
  54. mobile = strings.Trim(mobile, " ")
  55. mobile = strings.Replace(mobile, " ", "", -1)
  56. fmt.Println(mobile)
  57. companyName := cells[6].String()
  58. fmt.Println(companyName)
  59. companyName = strings.Trim(companyName, " ")
  60. companyName = strings.Replace(companyName, " ", "", -1)
  61. company, err := models.GetCompanyByName(companyName)
  62. if err != nil {
  63. if utils.ErrNoRow() == err.Error() {
  64. utils.FileLog.Info("客户不存在%s", companyName)
  65. fmt.Println("GetCompanyByName Err:" + err.Error())
  66. return
  67. } else {
  68. fmt.Println("GetCompanyByName Err:" + err.Error())
  69. return
  70. }
  71. }
  72. var companyId int
  73. if company == nil {
  74. sellsId := 0
  75. sellsName := ""
  76. if sellsId <= 0 {
  77. fmt.Println("sellsId <= 0")
  78. return
  79. }
  80. if startDate == "" || endDate == "" {
  81. fmt.Println("startDate,endDate is empty")
  82. return
  83. }
  84. fmt.Println("客户不存在,", companyName)
  85. utils.FileLog.Info("客户不存在 %s", companyName)
  86. newCompany := new(models.Company)
  87. newCompany.CompanyName = companyName
  88. newCompany.CompanyType = 2
  89. newCompany.Enabled = 1
  90. newCompany.CreatedTime = time.Now()
  91. newCompany.LastUpdatedTime = time.Now()
  92. newCompany.Sort = 1
  93. newCompany.SellsId = sellsId
  94. newCompany.Seller = sellsName
  95. newCompany.CompanyBelong = "ficc"
  96. newCompany.StartDate = startDate
  97. newCompany.EndDate = endDate
  98. newCompany.FirstStartDate = startDate
  99. newCompany.FirstEndDate = endDate
  100. newCompany.DateType = dateType
  101. newId, err := models.AddCompany(newCompany)
  102. if err != nil {
  103. fmt.Println("新增公司失败,Err:" + err.Error())
  104. return
  105. }
  106. companyId = int(newId)
  107. //新增权限
  108. return
  109. }
  110. companyId = company.CompanyId
  111. if companyId > 0 {
  112. wxUser, err := models.GetWxUserByMobile(mobile)
  113. if err != nil && err.Error() != utils.ErrNoRow() {
  114. fmt.Println("GetWxUserByMobile Err:" + err.Error())
  115. return
  116. }
  117. if wxUser == nil {
  118. //用户不存在,新增
  119. fmt.Println("AddWxUser")
  120. email := ""
  121. fmt.Println(company.CompanyId, mobile, realName)
  122. err = models.AddWxUser(company.CompanyId, mobile, realName, email)
  123. if err != nil {
  124. fmt.Println("AddWxUser Err:" + err.Error())
  125. return
  126. }
  127. } else {
  128. fmt.Println("用户存在:mobile:", mobile)
  129. fmt.Println(company.CompanyId, mobile, realName)
  130. }
  131. fmt.Println(companyName, company.CompanyId)
  132. } else {
  133. fmt.Println("companyId<=0")
  134. }
  135. n++
  136. }
  137. }
  138. }
  139. fmt.Println("len:", n)
  140. }
  141. //客户
  142. func ImportCompanyUsers() {
  143. var err error
  144. defer func() {
  145. if err != nil {
  146. fmt.Println("err:", err.Error())
  147. }
  148. }()
  149. fileName := "./company_user_1127.xlsx"
  150. xlFile, err := xlsx.OpenFile(fileName)
  151. if err != nil {
  152. fmt.Printf("open failed: %s\n", err)
  153. }
  154. n := 0
  155. for _, sheet := range xlFile.Sheet {
  156. rows := sheet.MaxRow
  157. for i := 0; i < rows; i++ {
  158. if i > 2 {
  159. row := sheet.Row(i)
  160. cells := row.Cells
  161. realName := cells[1].String()
  162. realName = strings.Trim(realName, " ")
  163. realName = strings.Replace(realName, " ", "", -1)
  164. mobile := cells[2].String()
  165. mobile = strings.Trim(mobile, " ")
  166. mobile = strings.Replace(mobile, " ", "", -1)
  167. email := cells[5].String()
  168. email = strings.Trim(email, " ")
  169. email = strings.Replace(email, " ", "", -1)
  170. fmt.Println(mobile)
  171. var companyId int
  172. companyId = 5140
  173. if companyId > 0 {
  174. wxUser, err := models.GetWxUserByMobile(mobile)
  175. if err != nil && err.Error() != utils.ErrNoRow() {
  176. fmt.Println("GetWxUserByMobile Err:" + err.Error())
  177. return
  178. }
  179. if wxUser == nil {
  180. //用户不存在,新增
  181. fmt.Println("AddWxUser")
  182. err = models.AddWxUser(companyId, mobile, realName, email)
  183. if err != nil {
  184. fmt.Println("AddWxUser Err:" + err.Error())
  185. return
  186. }
  187. } else {
  188. fmt.Println("用户存在:mobile:", mobile, realName)
  189. }
  190. } else {
  191. fmt.Println("companyId<=0")
  192. }
  193. n++
  194. }
  195. }
  196. }
  197. fmt.Println("len:", n)
  198. }
  199. // 导入客户信用码
  200. func ImportCompanyCreditCode() {
  201. var err error
  202. defer func() {
  203. if err != nil {
  204. fmt.Println("err:", err.Error())
  205. }
  206. }()
  207. fileName := "./客户社会信用码.xlsx"
  208. xlFile, err := xlsx.OpenFile(fileName)
  209. if err != nil {
  210. fmt.Printf("open failed: %s\n", err)
  211. }
  212. n := 0
  213. for _, sheet := range xlFile.Sheet {
  214. rows := sheet.MaxRow
  215. for i := 0; i < rows; i++ {
  216. if i > 2 {
  217. row := sheet.Row(i)
  218. cells := row.Cells
  219. companyName := cells[0].String()
  220. companyName = strings.Trim(companyName, " ")
  221. companyName = strings.Replace(companyName, " ", "", -1)
  222. creditCode := cells[4].String()
  223. creditCode = strings.Trim(creditCode, " ")
  224. creditCode = strings.Replace(creditCode, " ", "", -1)
  225. industryName := cells[5].String()
  226. industryName = strings.Trim(industryName, " ")
  227. industryName = strings.Replace(industryName, " ", "", -1)
  228. companyItem, err := models.GetCompanyByName(companyName)
  229. if err != nil {
  230. fmt.Println("GetCompanyByName Err:" + err.Error())
  231. continue
  232. }
  233. if companyItem == nil {
  234. fmt.Println("companyItem is nil")
  235. }
  236. count, err := models.GetCompanyCountByCreditCode(creditCode)
  237. if err != nil {
  238. fmt.Println("GetCompanyCountByCreditCode Err:" + err.Error())
  239. continue
  240. }
  241. if count > 0 {
  242. fmt.Println("creditCode exist")
  243. continue
  244. } else {
  245. industryItem, err := models.GetCompanyIndustryByName(industryName)
  246. if err != nil {
  247. fmt.Println("GetCompanyIndustryByName Err:" + err.Error())
  248. return
  249. }
  250. fmt.Println(industryItem.IndustryId, industryItem.IndustryName)
  251. err = models.ModifyCompanyCreditCode(companyItem.CompanyId, creditCode)
  252. if err != nil {
  253. fmt.Println("Err:" + err.Error())
  254. return
  255. }
  256. err = models.ModifyCompanyIndustry(industryItem.IndustryId, companyItem.CompanyId, industryName)
  257. if err != nil {
  258. fmt.Println("Err:" + err.Error())
  259. return
  260. }
  261. }
  262. fmt.Println(companyName, creditCode, industryName)
  263. }
  264. }
  265. }
  266. fmt.Println("len:", n)
  267. }
  268. // IncrementCompany 增量客户
  269. func IncrementCompany(cont context.Context) (err error) {
  270. defer func() {
  271. if err != nil {
  272. fmt.Println("crete IncrementCompany err:", err.Error())
  273. utils.FileLog.Info(fmt.Sprintf("crete IncrementCompany err: %s", err.Error()))
  274. }
  275. }()
  276. //endTime := time.Now().Format(utils.FormatDateTime)
  277. startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDate)
  278. endTime := time.Now().Format(utils.FormatDate)
  279. _, items, err := models.GetIncrementCompanyProductUpdateLogList(startTime, endTime)
  280. if err != nil {
  281. return
  282. }
  283. title := "增量客户记录"
  284. //创建excel
  285. file := xlsx.NewFile()
  286. sheet, err := file.AddSheet(title)
  287. if err != nil {
  288. return err
  289. }
  290. //标头
  291. rowTitle := sheet.AddRow()
  292. cellA := rowTitle.AddCell()
  293. cellA.Value = "客户名称"
  294. cellB := rowTitle.AddCell()
  295. cellB.Value = "社会信用码"
  296. cellC := rowTitle.AddCell()
  297. cellC.Value = "客户类型"
  298. cellD := rowTitle.AddCell()
  299. cellD.Value = "客户状态"
  300. cellE := rowTitle.AddCell()
  301. cellE.Value = "所属销售"
  302. for _, item := range items {
  303. row := sheet.AddRow()
  304. cellA := row.AddCell()
  305. cellA.Value = item.CompanyName
  306. cellB := row.AddCell()
  307. cellB.Value = item.CreditCode
  308. cellC := row.AddCell()
  309. productName := ``
  310. switch item.ProductId {
  311. case 1:
  312. productName = "ficc"
  313. case 2:
  314. productName = `权益`
  315. }
  316. cellC.Value = productName
  317. cellD := row.AddCell()
  318. cellD.Value = item.CurrStatus
  319. cellE := row.AddCell()
  320. cellE.Value = item.SellerName
  321. }
  322. savePath := "increment_company_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  323. err = file.Save("./" + savePath)
  324. if err != nil {
  325. return
  326. }
  327. //发送邮件
  328. fmt.Println("start send email")
  329. //收取邮件的人
  330. emailUser := "kwhuang@hzinsights.com;csun@hzinsights.com;sphuang@hzinsights.com;pyan@hzinsights.com"
  331. sendResult := utils.SendEmailByHongze(title, "增量客户记录,你好,上周的增量客户记录列表见附件。", emailUser, savePath, title+".xlsx")
  332. if sendResult {
  333. os.Remove(savePath)
  334. }
  335. //fmt.Println("send result:", sendResult)
  336. fmt.Println("end send email")
  337. return nil
  338. }