main.go 25 KB

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