main.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql"
  6. "fmt"
  7. "io/ioutil"
  8. "strings"
  9. "time"
  10. basedao "dashoo.cn/micro/app/dao/base"
  11. contractdao "dashoo.cn/micro/app/dao/contract"
  12. projao "dashoo.cn/micro/app/dao/proj"
  13. basemodel "dashoo.cn/micro/app/model/base"
  14. model "dashoo.cn/micro/app/model/contract"
  15. "github.com/gogf/gf/database/gdb"
  16. "github.com/gogf/gf/frame/g"
  17. "github.com/gogf/gf/util/gconv"
  18. "github.com/gogf/gf/os/gtime"
  19. "github.com/xuri/excelize/v2"
  20. )
  21. func main() {
  22. // contract()
  23. // product()
  24. // collectionplan()
  25. // collection()
  26. // dist()
  27. // prepareuser()
  28. // prepare()
  29. // fmt.Println(Usermap)
  30. // fmt.Println(MapProvince)
  31. // distContact()
  32. // projectRelate()
  33. // updateclloect()
  34. // updateContractIncharge()
  35. // updateDist()
  36. }
  37. func updateDist() {
  38. // select id, nbo_name,nbo_type, distributor_id , distributor_name from proj_business where (distributor_id=0 or distributor_id is null) and distributor_name != '' and distributor_name is not null
  39. distdao := basedao.NewBaseDistributorDao("prod")
  40. dist, err := distdao.All()
  41. if err != nil {
  42. panic(err)
  43. }
  44. projao := projao.NewProjBusinessDao("prod")
  45. proj, err := projao.All()
  46. if err != nil {
  47. panic(err)
  48. }
  49. distmap := map[string]*basemodel.BaseDistributor{}
  50. for _, d := range dist {
  51. distmap[d.DistName] = d
  52. }
  53. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  54. for _, p := range proj {
  55. if p.DistributorName == "" {
  56. continue
  57. }
  58. if p.DistributorId != 0 {
  59. continue
  60. }
  61. d := distmap[p.DistributorName]
  62. if d == nil {
  63. fmt.Println("not found", p.DistributorName)
  64. continue
  65. }
  66. fmt.Println(p.DistributorName, d.Id, d.DistName)
  67. _, err = tx.Update("proj_business",
  68. map[string]interface{}{
  69. "distributor_id": d.Id,
  70. }, "id = ?", p.Id)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. // return fmt.Errorf("测试")
  76. return nil
  77. })
  78. if txerr != nil {
  79. panic(txerr)
  80. }
  81. }
  82. func updateContractIncharge() {
  83. prepareuser()
  84. contractdao := contractdao.NewCtrContractDao("prod")
  85. contract, err := contractdao.All()
  86. if err != nil {
  87. panic(err)
  88. }
  89. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  90. for _, c := range contract {
  91. if c.InchargeId != 0 {
  92. continue
  93. }
  94. if Usermap[c.InchargeName].Id == 0 {
  95. panic(c.InchargeName + ":seller")
  96. }
  97. _, err = tx.Update("ctr_contract",
  98. map[string]interface{}{
  99. "incharge_id": Usermap[c.InchargeName].Id,
  100. }, "id = ?", c.Id)
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. // return fmt.Errorf("测试")
  106. return nil
  107. })
  108. if txerr != nil {
  109. panic(txerr)
  110. }
  111. }
  112. func updateclloect() {
  113. contractdao := contractdao.NewCtrContractDao("prod")
  114. contract, err := contractdao.All()
  115. if err != nil {
  116. panic(err)
  117. }
  118. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  119. for _, c := range contract {
  120. v, err := tx.GetValue("select sum(collection_amount) from ctr_contract_collection where contract_id=? and appro_status='20' and deleted_time is null", c.Id)
  121. if err != nil {
  122. return err
  123. }
  124. amount := v.Float64()
  125. _, err = tx.Update("ctr_contract",
  126. map[string]interface{}{
  127. "collected_amount": amount,
  128. }, "id = ?", c.Id)
  129. if err != nil {
  130. return err
  131. }
  132. }
  133. // return fmt.Errorf("测试")
  134. return nil
  135. })
  136. if txerr != nil {
  137. panic(txerr)
  138. }
  139. }
  140. func projectRelate() {
  141. projao := projao.NewProjBusinessDao("prod")
  142. contractdao := contractdao.NewCtrContractDao("prod")
  143. fileb, err := ioutil.ReadFile("/home/lai/项目关联.xlsx")
  144. if err != nil {
  145. panic(err)
  146. }
  147. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  148. if err != nil {
  149. panic(err)
  150. }
  151. coderow, err := e.GetRows("Sheet1")
  152. if err != nil {
  153. panic(err)
  154. }
  155. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  156. for rown, row := range coderow[1:] {
  157. projcode := strings.TrimSpace(row[0])
  158. contrcode := strings.TrimSpace(row[1])
  159. fmt.Println(rown, projcode, contrcode)
  160. nbo, err := projao.Where("nbo_code = ?", projcode).One()
  161. if err != nil {
  162. return err
  163. }
  164. if nbo == nil {
  165. return fmt.Errorf("项目不存在")
  166. }
  167. contract, err := contractdao.Where("contract_code = ?", contrcode).One()
  168. if err != nil {
  169. return err
  170. }
  171. if contract == nil {
  172. return fmt.Errorf("合同不存在")
  173. }
  174. _, err = tx.Update("ctr_contract", map[string]interface{}{
  175. "cust_id": nbo.CustId,
  176. "cust_name": nbo.CustName,
  177. "nbo_id": nbo.Id,
  178. "nbo_name": nbo.NboName,
  179. "is_big": nbo.IsBig,
  180. "product_line": nbo.ProductLine,
  181. "cust_province_id": nbo.CustProvinceId,
  182. "cust_province": nbo.CustProvince,
  183. "cust_city_id": nbo.CustCityId,
  184. "cust_city": nbo.CustCity,
  185. }, "contract_code = ?", contrcode)
  186. if err != nil {
  187. return err
  188. }
  189. }
  190. // return fmt.Errorf("测试")
  191. return nil
  192. })
  193. if txerr != nil {
  194. panic(txerr)
  195. }
  196. }
  197. func distContact() {
  198. dao := basedao.NewBaseDistributorDao("prod")
  199. distMap := map[string]*basemodel.BaseDistributor{}
  200. ent, err := dao.All()
  201. if err != nil {
  202. panic(err)
  203. }
  204. for _, i := range ent {
  205. distMap[i.DistName] = i
  206. }
  207. fmt.Println(distMap)
  208. fileb, err := ioutil.ReadFile("/home/lai/渠道信息联系方式.xlsx")
  209. if err != nil {
  210. panic(err)
  211. }
  212. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  213. if err != nil {
  214. panic(err)
  215. }
  216. contact, err := e.GetRows("Sheet1")
  217. if err != nil {
  218. panic(err)
  219. }
  220. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  221. for rown, row := range contact[1:] {
  222. distname := strings.TrimSpace(row[2])
  223. name := strings.TrimSpace(row[3])
  224. position := strings.TrimSpace(row[5])
  225. phone := strings.TrimSpace(row[6])
  226. email := strings.TrimSpace(row[7])
  227. wechat := strings.TrimSpace(row[8])
  228. business := strings.TrimSpace(row[9])
  229. fmt.Println(rown, distname, name, position, phone, email, wechat, business, distMap[distname].Id)
  230. p := basemodel.BaseDistributorContact{
  231. DistId: distMap[distname].Id,
  232. Name: name,
  233. Post: position,
  234. Phone: phone,
  235. Wechat: wechat,
  236. Mail: email,
  237. Honorific: "未知",
  238. Territory: business,
  239. Remark: "",
  240. CreatedBy: 1000,
  241. CreatedName: "dashoo",
  242. CreatedTime: gtime.Now(),
  243. UpdatedBy: 1000,
  244. UpdatedName: "dashoo",
  245. UpdatedTime: gtime.Now(),
  246. }
  247. _, err = tx.Insert("base_distributor_contact", p)
  248. if err != nil {
  249. return err
  250. }
  251. }
  252. // return fmt.Errorf("测试")
  253. return nil
  254. })
  255. if txerr != nil {
  256. panic(txerr)
  257. }
  258. }
  259. type User struct {
  260. Id int
  261. NickName string
  262. UserName string
  263. }
  264. var Usermap = map[string]User{}
  265. func prepareuser() {
  266. users := []User{}
  267. err := g.DB("prod").Table("sys_user").Structs(&users)
  268. if err != nil {
  269. panic(err)
  270. }
  271. for _, u := range users {
  272. Usermap[u.NickName] = u
  273. }
  274. }
  275. func dist() {
  276. prepare()
  277. prepareuser()
  278. var custtypMap = map[string]string{
  279. "医院": "10",
  280. "科研单位": "20",
  281. "高校": "30",
  282. "疾控": "40",
  283. "细胞公司": "50",
  284. "生物药企": "60",
  285. "其他": "90",
  286. }
  287. fileb, err := ioutil.ReadFile("/home/lai/渠道信息台账0523(1)(1).xlsx")
  288. if err != nil {
  289. panic(err)
  290. }
  291. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  292. if err != nil {
  293. panic(err)
  294. }
  295. proxy, err := e.GetRows("代理商列表详情")
  296. if err != nil {
  297. panic(err)
  298. }
  299. dist, err := e.GetRows("经销商列表详情")
  300. if err != nil {
  301. panic(err)
  302. }
  303. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  304. for rowtype, distlist := range [][][]string{dist, proxy} {
  305. distType := "10"
  306. if rowtype == 1 {
  307. distType = "20"
  308. }
  309. for rown, row := range distlist[2:] {
  310. createdStr := strings.TrimSpace(row[1])
  311. seller := strings.TrimSpace(row[2])
  312. name := strings.TrimSpace(row[3])
  313. province := strings.TrimSpace(row[4])
  314. var sellerNumStr string
  315. var product string
  316. var hisCust string
  317. var custTypStr string
  318. var proxydistrict string
  319. var startStr string
  320. var endStr string
  321. var business string
  322. var capitalStr string
  323. var register string
  324. if len(row) > 5 {
  325. business = strings.TrimSpace(row[5])
  326. capitalStr = strings.TrimSpace(row[6])
  327. register = strings.TrimSpace(row[7])
  328. if len(row) > 8 {
  329. sellerNumStr = strings.TrimSpace(row[8])
  330. if len(row) > 9 {
  331. product = strings.TrimSpace(row[9])
  332. if len(row) > 10 {
  333. hisCust = strings.TrimSpace(row[10])
  334. if len(row) > 11 {
  335. if distType == "10" {
  336. custTypStr = strings.TrimSpace(row[11])
  337. } else {
  338. // fmt.Println(row)
  339. proxydistrict = strings.TrimSpace(row[12])
  340. startStr = strings.TrimSpace(row[13])
  341. endStr = strings.TrimSpace(row[14])
  342. custTypStr = strings.TrimSpace(row[15])
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. created, err := time.Parse("2006/01/02 15:04:05", createdStr)
  350. if err != nil {
  351. panic(fmt.Errorf("%s, %s", createdStr, err.Error()))
  352. }
  353. start, err := time.Parse("2006/01/02", startStr)
  354. if err != nil {
  355. if distType == "10" {
  356. start = time.Time{}
  357. } else {
  358. panic(fmt.Errorf("%s, %s", startStr, err.Error()))
  359. }
  360. }
  361. end, err := time.Parse("2006/01/02", endStr)
  362. if err != nil {
  363. if distType == "10" {
  364. start = time.Time{}
  365. } else {
  366. panic(fmt.Errorf("%s, %s", endStr, err.Error()))
  367. }
  368. }
  369. sellerNum := gconv.Int(sellerNumStr)
  370. capital := gconv.Float64(capitalStr)
  371. var custypes string
  372. if custTypStr == "" {
  373. custypes = ""
  374. } else {
  375. custTypeslice := strings.Split(custTypStr, ",")
  376. custypeslice := []string{}
  377. for _, t := range custTypeslice {
  378. if t == "企业" {
  379. continue
  380. }
  381. if t == "医疗" {
  382. continue
  383. }
  384. if t == "科研" {
  385. continue
  386. }
  387. if t == "细胞企业" {
  388. continue
  389. }
  390. if t == "生物药企、医院" {
  391. custypeslice = append(custypeslice, custtypMap["生物药企"])
  392. custypeslice = append(custypeslice, custtypMap["医院"])
  393. continue
  394. }
  395. if custtypMap[t] == "" {
  396. panic(t + ":custypeslice")
  397. }
  398. custypeslice = append(custypeslice, custtypMap[t])
  399. }
  400. custypes = strings.Join(custypeslice, ",")
  401. if Usermap[seller].Id == 0 {
  402. panic(seller + ":seller")
  403. }
  404. }
  405. fmt.Println(rown, distType, created, seller, name, province, MapProvince[province], business, capital, register, sellerNum, product, hisCust, custypes, proxydistrict, start, end)
  406. p := basemodel.BaseDistributor{
  407. DistCode: "",
  408. DistName: name,
  409. AbbrName: "",
  410. DistDesc: "",
  411. DistBoss: "",
  412. DistBossPhone: "",
  413. ProvinceId: MapProvince[province],
  414. ProvinceDesc: province,
  415. BusinessScope: business,
  416. BelongSaleId: Usermap[seller].Id,
  417. BelongSale: seller,
  418. Capital: capital,
  419. SaleNum: sellerNum,
  420. InvoiceHeader: "",
  421. DistType: distType,
  422. CustomerType: custypes,
  423. ExistedProduct: product,
  424. AssistantSaleId: "",
  425. AssistantSale: "",
  426. RegisterDistrict: register,
  427. HistoryCustomer: hisCust,
  428. ProxyStartTime: gtime.NewFromTime(start),
  429. ProxyEndTime: gtime.NewFromTime(end),
  430. ProxyDistrict: proxydistrict,
  431. ContractUrl: "",
  432. Remark: "",
  433. CreatedBy: 1000,
  434. CreatedName: "dashoo",
  435. CreatedTime: gtime.Now(),
  436. UpdatedBy: 1000,
  437. UpdatedName: "dashoo",
  438. UpdatedTime: gtime.Now(),
  439. }
  440. _, err = tx.Insert("base_distributor", p)
  441. if err != nil {
  442. return err
  443. }
  444. }
  445. }
  446. // return fmt.Errorf("测试")
  447. return nil
  448. })
  449. if txerr != nil {
  450. panic(txerr)
  451. }
  452. }
  453. func collection() {
  454. var typMap = map[string]string{
  455. "电汇": "10",
  456. "承兑": "20",
  457. "现金": "30",
  458. }
  459. var contractMap = map[string]*model.CtrContract{}
  460. fileb, err := ioutil.ReadFile("/home/lai/OMS合同管理17-23合同信息0519(1).xlsx")
  461. if err != nil {
  462. panic(err)
  463. }
  464. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  465. if err != nil {
  466. panic(err)
  467. }
  468. rows2021_2023, err := e.GetRows("2021-2023年回款管理")
  469. if err != nil {
  470. panic(err)
  471. }
  472. rows2017_2020, err := e.GetRows("2017-2020年回款管理")
  473. if err != nil {
  474. panic(err)
  475. }
  476. rows := append(rows2021_2023[1:], rows2017_2020[1:]...)
  477. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  478. for rown, row := range rows {
  479. custname := strings.TrimSpace(row[0])
  480. contractcode := strings.TrimSpace(row[1])
  481. amountStr := strings.TrimSpace(row[2])
  482. typ := strings.TrimSpace(row[3])
  483. var ctimeStr string
  484. if len(row) > 5 {
  485. ctimeStr = strings.TrimSpace(row[5])
  486. }
  487. if ctimeStr == "" {
  488. ctimeStr = "0001/1/2"
  489. }
  490. ctime, err := time.Parse("2006/1/2", ctimeStr)
  491. if err != nil {
  492. ctime, err = time.Parse("01-02-06", ctimeStr)
  493. }
  494. if err != nil {
  495. ctime, err = time.Parse("2006.1.2", ctimeStr)
  496. }
  497. if err != nil {
  498. if ctimeStr == "" {
  499. ctime = time.Time{}
  500. } else {
  501. panic(fmt.Errorf("%s, %s", ctimeStr, err.Error()))
  502. }
  503. }
  504. amount := gconv.Float64(amountStr)
  505. fmt.Println(rown, custname, contractcode, amount, typ, ctime)
  506. if _, ok := contractMap[contractcode]; !ok {
  507. c := &model.CtrContract{}
  508. err := tx.GetStruct(c, "select * from ctr_contract where contract_code = ?", contractcode)
  509. if err != nil {
  510. fmt.Println(c.ContractCode)
  511. return err
  512. }
  513. contractMap[contractcode] = c
  514. }
  515. p := model.CtrContractCollection{
  516. PlanId: 0,
  517. CustId: 0,
  518. CustName: custname,
  519. ContractId: contractMap[contractcode].Id,
  520. ContractCode: contractMap[contractcode].ContractCode,
  521. CollectionDatetime: gtime.NewFromTime(ctime),
  522. CollectionAmount: amount,
  523. CollectionType: typMap[typ],
  524. ApproStatus: "20",
  525. ContractAmount: contractMap[contractcode].ContractAmount,
  526. Remark: "",
  527. CreatedBy: 1000,
  528. CreatedName: "dashoo",
  529. CreatedTime: gtime.Now(),
  530. UpdatedBy: 1000,
  531. UpdatedName: "dashoo",
  532. UpdatedTime: gtime.Now(),
  533. }
  534. _, err = tx.Insert("ctr_contract_collection", p)
  535. if err != nil {
  536. return err
  537. }
  538. }
  539. return nil
  540. // return fmt.Errorf("测试")
  541. })
  542. fmt.Println(contractMap)
  543. if txerr != nil {
  544. panic(txerr)
  545. }
  546. }
  547. func collectionplan() {
  548. var statusMap = map[string]string{
  549. "已回款": "30",
  550. "未回款": "10",
  551. }
  552. var contractMap = map[string]*model.CtrContract{}
  553. fileb, err := ioutil.ReadFile("/home/lai/OMS合同管理17-23合同信息0519(1).xlsx")
  554. if err != nil {
  555. panic(err)
  556. }
  557. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  558. if err != nil {
  559. panic(err)
  560. }
  561. rows2021_2023, err := e.GetRows("2021-2023年回款计划")
  562. if err != nil {
  563. panic(err)
  564. }
  565. rows := rows2021_2023[1:]
  566. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  567. for rown, row := range rows {
  568. custname := strings.TrimSpace(row[0])
  569. contractcode := strings.TrimSpace(row[1])
  570. amountStr := strings.TrimSpace(row[2])
  571. status := strings.TrimSpace(row[3])
  572. planTimeStr := strings.TrimSpace(row[4])
  573. scaleStr := strings.TrimSpace(row[5])
  574. var criteria string
  575. if len(row) > 6 {
  576. criteria = strings.TrimSpace(row[6])
  577. }
  578. if planTimeStr == "" {
  579. planTimeStr = "0001/1/2"
  580. }
  581. planTime, err := time.Parse("2006/1/2", planTimeStr)
  582. if err != nil {
  583. planTime, err = time.Parse("01-02-06", planTimeStr)
  584. }
  585. if err != nil {
  586. if planTimeStr == "" {
  587. planTime = time.Time{}
  588. } else {
  589. panic(fmt.Errorf("%s, %s", planTimeStr, err.Error()))
  590. }
  591. }
  592. amount := gconv.Float64(amountStr)
  593. scale := gconv.Float64(strings.Replace(scaleStr, "%", "", -1))
  594. var CashedAmount float64
  595. if status == "已回款" {
  596. CashedAmount = amount
  597. }
  598. fmt.Println(rown, custname, contractcode, amount, status, scale, criteria)
  599. if _, ok := contractMap[contractcode]; !ok {
  600. c := &model.CtrContract{}
  601. err := tx.GetStruct(c, "select * from ctr_contract where contract_code = ?", contractcode)
  602. if err != nil {
  603. fmt.Println(c.ContractCode)
  604. return err
  605. }
  606. contractMap[contractcode] = c
  607. }
  608. p := model.CtrContractCollectionPlan{
  609. CustId: 0,
  610. CustName: custname,
  611. ContractId: contractMap[contractcode].Id,
  612. ContractCode: contractMap[contractcode].ContractCode,
  613. ContractStatus: statusMap[status],
  614. PlanAmount: amount,
  615. PlanDatetime: gtime.NewFromTime(planTime),
  616. PlanScale: scale,
  617. PlanCondition: criteria,
  618. CashedAmount: CashedAmount,
  619. CashedDatetime: nil,
  620. Remark: "",
  621. CreatedBy: 1000,
  622. CreatedName: "dashoo",
  623. CreatedTime: gtime.Now(),
  624. UpdatedBy: 1000,
  625. UpdatedName: "dashoo",
  626. UpdatedTime: gtime.Now(),
  627. }
  628. _, err = tx.Insert("ctr_contract_collection_plan", p)
  629. if err != nil {
  630. return err
  631. }
  632. }
  633. // return fmt.Errorf("测试")
  634. return nil
  635. })
  636. fmt.Println(contractMap)
  637. if txerr != nil {
  638. panic(txerr)
  639. }
  640. }
  641. func product() {
  642. var ProdClassMap = map[string]string{
  643. "BIOBANK": "10",
  644. "Biobank": "10",
  645. "CELLSOP": "20",
  646. "CellSOP": "20",
  647. "CellsSOP": "20",
  648. "LIMS": "30",
  649. "LIMS+基因": "30",
  650. "MCS": "60",
  651. "咨询服务": "70",
  652. "外购": "90",
  653. "智能硬件": "40",
  654. "液氮罐": "50",
  655. "设备": "",
  656. }
  657. var productMap = map[string]*basemodel.BaseProduct{}
  658. var contractMap = map[string]*model.CtrContract{}
  659. fileb, err := ioutil.ReadFile("/home/lai/OMS合同管理17-23合同信息0519(1).xlsx")
  660. if err != nil {
  661. panic(err)
  662. }
  663. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  664. if err != nil {
  665. panic(err)
  666. }
  667. rows2021_2023, err := e.GetRows("2021-2023年产品信息")
  668. if err != nil {
  669. panic(err)
  670. }
  671. rows2017_2020, err := e.GetRows("2017-2020年产品信息")
  672. if err != nil {
  673. panic(err)
  674. }
  675. rows := append(rows2021_2023[1:], rows2017_2020[1:]...)
  676. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  677. for rown, row := range rows {
  678. contractcode := strings.TrimSpace(row[0])
  679. ProdClass := strings.TrimSpace(row[1])
  680. ProdName := strings.TrimSpace(row[2])
  681. ProdCode := strings.TrimSpace(row[3])
  682. priceStr := strings.TrimSpace(row[4])
  683. numStr := strings.TrimSpace(row[5])
  684. sumStr := strings.TrimSpace(row[6])
  685. var zhibaoStr string
  686. var maintainStr string
  687. var maintainTerm string
  688. var acceptTimeStr string
  689. if len(row) > 7 {
  690. zhibaoStr = strings.TrimSpace(row[7])
  691. if len(row) > 8 {
  692. maintainStr = strings.TrimSpace(row[8])
  693. if len(row) > 9 {
  694. maintainTerm = strings.TrimSpace(row[9])
  695. if len(row) > 10 {
  696. acceptTimeStr = strings.TrimSpace(row[10])
  697. }
  698. }
  699. }
  700. }
  701. acceptTime, err := time.Parse("2006/1/2", acceptTimeStr)
  702. if err != nil {
  703. acceptTime, err = time.Parse("01-02-06", acceptTimeStr)
  704. }
  705. if err != nil {
  706. acceptTime, err = time.Parse("2006年1月2日", acceptTimeStr)
  707. }
  708. if err != nil {
  709. if acceptTimeStr == "" {
  710. acceptTime = time.Time{}
  711. } else {
  712. panic(fmt.Errorf("%s, %s", acceptTimeStr, err.Error()))
  713. }
  714. }
  715. price := gconv.Float64(priceStr)
  716. num := gconv.Int(numStr)
  717. sum := gconv.Float64(sumStr)
  718. zhibao := gconv.Int(zhibaoStr)
  719. maintain := gconv.Int(maintainStr)
  720. fmt.Println(rown, contractcode, ProdClass, ProdName, ProdCode, price,
  721. num, sum, zhibao, maintain, maintainTerm, acceptTime)
  722. p := basemodel.BaseProduct{
  723. ProdCode: ProdCode,
  724. ProdName: ProdName,
  725. ProdClass: ProdClassMap[ProdClass],
  726. GuidPrice: 0,
  727. DistPrice: 0,
  728. AgentPrice: 0,
  729. MarketPrice: 0,
  730. Remark: "",
  731. CreatedBy: 1000,
  732. CreatedName: "dashoo",
  733. CreatedTime: gtime.Now(),
  734. UpdatedBy: 1000,
  735. UpdatedName: "dashoo",
  736. UpdatedTime: gtime.Now(),
  737. DeletedTime: gtime.Now(),
  738. }
  739. if _, ok := productMap[p.ProdName]; !ok {
  740. dbexistp := &basemodel.BaseProduct{}
  741. err := tx.GetStruct(dbexistp, "select * from base_product where prod_name = ?", p.ProdName)
  742. if err == sql.ErrNoRows {
  743. id, err := tx.InsertAndGetId("base_product", p)
  744. if err != nil {
  745. return err
  746. }
  747. p.Id = int(id)
  748. productMap[p.ProdName] = &p
  749. } else {
  750. if err != nil {
  751. return err
  752. }
  753. productMap[p.ProdName] = dbexistp
  754. }
  755. }
  756. if _, ok := contractMap[contractcode]; !ok {
  757. c := &model.CtrContract{}
  758. err := tx.GetStruct(c, "select * from ctr_contract where contract_code = ?", contractcode)
  759. if err != nil {
  760. fmt.Println(c.ContractCode)
  761. return err
  762. }
  763. contractMap[contractcode] = c
  764. }
  765. cp := model.CtrContractProduct{
  766. ContractId: contractMap[contractcode].Id,
  767. ContractCode: contractMap[contractcode].ContractCode,
  768. ProdId: productMap[p.ProdName].Id,
  769. ProdCode: productMap[p.ProdName].ProdCode,
  770. ProdName: productMap[p.ProdName].ProdName,
  771. ProdClass: productMap[p.ProdName].ProdClass,
  772. ProdNum: num,
  773. MaintTerm: 0,
  774. SugSalesPrice: 0,
  775. TranPrice: price,
  776. ContractPrive: contractMap[contractcode].ContractAmount,
  777. PurchaseCost: 0,
  778. DevCost: 0,
  779. MaintainCost: 0,
  780. DirectCost: 0,
  781. MaintainPeriod: zhibao * 365,
  782. WarrantPeriod: maintain * 365,
  783. MaintainStartTime: nil,
  784. MaintainRemark: maintainTerm,
  785. AcceptTime: gtime.NewFromTime(acceptTime),
  786. Remark: "",
  787. CreatedBy: 1000,
  788. CreatedName: "dashoo",
  789. CreatedTime: gtime.Now(),
  790. UpdatedBy: 1000,
  791. UpdatedName: "dashoo",
  792. UpdatedTime: gtime.Now(),
  793. }
  794. _, err = tx.Insert("ctr_contract_product", cp)
  795. if err != nil {
  796. return err
  797. }
  798. }
  799. // return fmt.Errorf("测试")
  800. return nil
  801. })
  802. fmt.Println(contractMap)
  803. fmt.Println(productMap)
  804. fmt.Println(ProdClassMap)
  805. if txerr != nil {
  806. panic(txerr)
  807. }
  808. }
  809. func contract() {
  810. prepare()
  811. contract := parse()
  812. fmt.Println(MapProductLine)
  813. txerr := g.DB("prod").Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  814. _, err := tx.Insert("ctr_contract", contract)
  815. return err
  816. })
  817. if txerr != nil {
  818. panic(txerr)
  819. }
  820. }
  821. var MapProvince = map[string]int{}
  822. var MapCity = map[string]int{}
  823. var MapProductLine = map[string]string{
  824. "BIOBANK": "10",
  825. "CELLSOP": "20",
  826. "LIMS+基因": "30",
  827. "MCS": "60",
  828. "咨询服务": "70",
  829. "外购": "90",
  830. "智能硬件": "40",
  831. "石油": "80",
  832. }
  833. var Mapcontracttype = map[string]string{
  834. "销售合同": "XS",
  835. "技术合同": "JS",
  836. }
  837. var MapSignatoryType = map[string]string{
  838. "终端用户": "10",
  839. "经销商": "20",
  840. "代理商": "30",
  841. }
  842. func prepare() {
  843. dao := basedao.NewBaseDistrictDao("prod")
  844. province, err := dao.Where("dist_level = 1").All()
  845. if err != nil {
  846. panic(err)
  847. }
  848. for _, p := range province {
  849. MapProvince[p.DistName] = p.Id
  850. }
  851. city, err := dao.Where("dist_level = 2").All()
  852. if err != nil {
  853. panic(err)
  854. }
  855. for _, c := range city {
  856. MapCity[c.DistName] = c.Id
  857. }
  858. }
  859. func parse() []model.CtrContract {
  860. fileb, err := ioutil.ReadFile("/home/lai/OMS合同管理17-23合同信息0519(1).xlsx")
  861. if err != nil {
  862. panic(err)
  863. }
  864. e, err := excelize.OpenReader(bytes.NewBuffer(fileb))
  865. if err != nil {
  866. panic(err)
  867. }
  868. rows2021_2023, err := e.GetRows("2021-2023年合同基本信息")
  869. if err != nil {
  870. panic(err)
  871. }
  872. rows2017_2020, err := e.GetRows("2017-2020年合同基本信息")
  873. if err != nil {
  874. panic(err)
  875. }
  876. rows := append(rows2021_2023[1:], rows2017_2020[1:]...)
  877. contract := []model.CtrContract{}
  878. for rown, row := range rows {
  879. code := strings.TrimSpace(row[0])
  880. name := strings.TrimSpace(row[1])
  881. custname := strings.TrimSpace(row[2])
  882. nobname := strings.TrimSpace(row[3])
  883. province := strings.TrimSpace(row[4])
  884. city := strings.TrimSpace(row[5])
  885. amountstr := strings.TrimSpace(row[6])
  886. InchargeName := strings.TrimSpace(row[7])
  887. ContractType := strings.TrimSpace(row[8])
  888. ContractSignTimestr := strings.TrimSpace(row[9])
  889. ContractEndTimestr := strings.TrimSpace(row[10])
  890. SignatoryName := strings.TrimSpace(row[11])
  891. ProductLine := strings.TrimSpace(row[12])
  892. SignatoryUnit := strings.TrimSpace(row[13])
  893. SignatoryType := strings.TrimSpace(row[14])
  894. if ContractEndTimestr == "" {
  895. ContractEndTimestr = "0001/1/2"
  896. }
  897. if ContractSignTimestr == "" {
  898. ContractSignTimestr = "0001/1/2"
  899. }
  900. amount := gconv.Float64(amountstr)
  901. ContractSignTime, err := time.Parse("2006/1/2", ContractSignTimestr)
  902. if err != nil {
  903. ContractSignTime, err = time.Parse("01-02-06", ContractSignTimestr)
  904. }
  905. if err != nil {
  906. panic(fmt.Errorf("%s, %s", ContractSignTimestr, err.Error()))
  907. }
  908. ContractEndTime, err := time.Parse("2006/1/2", ContractEndTimestr)
  909. if err != nil {
  910. ContractEndTime, err = time.Parse("01-02-06", ContractEndTimestr)
  911. }
  912. if err != nil {
  913. panic(fmt.Errorf("%s, %s", ContractEndTimestr, err.Error()))
  914. }
  915. provinceId := MapProvince[province]
  916. cityId := MapCity[city]
  917. var DistributorName string
  918. if SignatoryType == "经销商" || SignatoryType == "代理商" {
  919. DistributorName = SignatoryUnit
  920. }
  921. fmt.Println(rown, code, name, custname, nobname, province, city,
  922. amount, InchargeName, ContractType, ContractSignTime,
  923. ContractEndTime, SignatoryName, ProductLine, SignatoryType)
  924. contract = append(contract, model.CtrContract{
  925. ContractCode: code,
  926. ContractName: name,
  927. CustId: 0,
  928. CustName: custname,
  929. ProductLine: MapProductLine[ProductLine],
  930. IsBig: "20",
  931. CustProvinceId: provinceId,
  932. CustProvince: province,
  933. CustCityId: cityId,
  934. CustCity: city,
  935. NboId: 0,
  936. NboName: nobname,
  937. ApproStatus: "30",
  938. ContractType: Mapcontracttype[ContractType],
  939. ContractAmount: amount,
  940. InvoiceAmount: 0,
  941. CollectedAmount: 0,
  942. ContractStartTime: gtime.NewFromTime(ContractSignTime),
  943. ContractEndTime: gtime.NewFromTime(ContractEndTime),
  944. ContractSignTime: gtime.NewFromTime(ContractSignTime),
  945. InchargeId: 0,
  946. InchargeName: InchargeName,
  947. SignatoryId: 0,
  948. SignatoryName: SignatoryName,
  949. SignatoryType: MapSignatoryType[SignatoryType],
  950. SignatoryUnit: SignatoryUnit,
  951. EarnestMoney: 0,
  952. CustSignatoryId: 0,
  953. CustSignatoryName: "",
  954. DistributorId: 0,
  955. DistributorName: DistributorName,
  956. ServiceFeeAgreement: "",
  957. Remark: "",
  958. CreatedBy: 1000,
  959. CreatedName: "dashoo",
  960. CreatedTime: gtime.Now(),
  961. UpdatedBy: 1000,
  962. UpdatedName: "dashoo",
  963. UpdatedTime: gtime.Now(),
  964. })
  965. }
  966. return contract
  967. }