material.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego/logs"
  6. "kpt.xdmy/apiserver/config"
  7. "regexp"
  8. "strconv"
  9. "time"
  10. "github.com/pkg/errors"
  11. log "github.com/sirupsen/logrus"
  12. _ "github.com/go-sql-driver/mysql"
  13. "kpt.xdmy/apiserver/model"
  14. "kpt.xdmy/apiserver/model/http"
  15. // "kpt.xdmy/pkg/log"
  16. )
  17. var partChan chan error
  18. // 定时从sap拉取物料(备件等)数据
  19. // 预留参数时间、备件编码,给手动调用接口
  20. func (s *Service) SapMaterial(t time.Time, code string) {
  21. partChan = make(chan error, 10)
  22. r := new(http.MaterialReq)
  23. rp := new(http.MaterialResp)
  24. var e error
  25. r.Dest.DestID = "EQMAN"
  26. r.Dest.BussTp = "MM002"
  27. //r.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial/"
  28. r.Dest.Url = fmt.Sprintf("%s/Common/MM002/QueryMaterial/", config.Conf.Http.Routing)
  29. if t.IsZero() {
  30. t = time.Now()
  31. }
  32. r.Data.BudatB = t.Format("20060102")
  33. r.Data.BudatE = t.Format("20060102")
  34. //r.Data.BudatB = "20221104"
  35. //r.Data.BudatE = "20221028"
  36. r.Data.Type = http.MTART{MTART: "XD08"}
  37. // r.Data.Group = []http.MATKL{{MATKL: "14010101"}}
  38. // r.Data.Group = []http.MATKL{{MATKL: "14"}}
  39. if code != "" {
  40. // r.Data.Codes = []http.MATNR{{Code: "14.01.06.02.000216"}}
  41. }
  42. rbyte, _ := json.Marshal(r)
  43. fmt.Println(string(rbyte))
  44. if e = s.SyncSap(r, rp, rbyte); e == nil {
  45. if rp.Dest.Status == "S" {
  46. log.Infof("sap material success:len=%d", len(rp.Data.Master))
  47. } else {
  48. e = errors.Errorf("sap material fail", rp.Dest.MessText)
  49. }
  50. } else {
  51. e = errors.Wrapf(e, "sap material error")
  52. }
  53. var update error
  54. var count int
  55. for _, v := range rp.Data.Master {
  56. update = nil
  57. s.UpdatePart(&v)
  58. update = <-partChan
  59. if update != nil {
  60. log.Error(update)
  61. count++
  62. l := model.SapDetailLog{Name: "part", Code: v.Code, ErrorText: update.Error()}
  63. if e := s.d.DB.Create(&l); e != nil {
  64. log.Errorf("add sapdetail log: %v", e)
  65. }
  66. }
  67. }
  68. if e != nil {
  69. log.Error(e)
  70. } else if update != nil {
  71. e = errors.Errorf("material update fail sum:%d", count)
  72. } else {
  73. log.Infof("material update success sum:%d", len(rp.Data.Master)-count)
  74. }
  75. s.AddSapLog(r, e)
  76. return
  77. }
  78. // 物料数据更新
  79. func (s *Service) UpdatePart(p *http.MaterialDetail) {
  80. part := new(model.Parts)
  81. if len(p.ModifyTime) >= 8 {
  82. p.ModifyTime = p.ModifyTime[:4] + "-" + p.ModifyTime[4:6] + "-" + p.ModifyTime[6:8]
  83. } else {
  84. partChan <- errors.New("ModifyTime is invalid:" + p.ModifyTime)
  85. }
  86. if t, e := time.ParseInLocation("2006-01-02", p.ModifyTime, time.Local); e != nil {
  87. partChan <- errors.Wrapf(e, "time:%s", p.ModifyTime)
  88. } else {
  89. part.ModifyTime = t
  90. }
  91. if p.Dflag == "" {
  92. part.Enable = 1
  93. part.Statue = 1
  94. }
  95. part.PartCode = p.Code
  96. part.Note = p.Description
  97. part.Category = p.Type
  98. part.Unit = p.Unit
  99. part.Name = p.Name
  100. part.Specification = p.Model
  101. if e := s.d.DB.Where(model.Parts{PartCode: p.Code}).Assign(*part).FirstOrCreate(part).Error; e != nil {
  102. partChan <- errors.Wrapf(e, "code:%s", p.Code)
  103. } else {
  104. log.Infof("sap part update success:%s", p.Code)
  105. partChan <- nil
  106. }
  107. }
  108. func Exec(rows [][]string) {
  109. for i, v := range rows {
  110. fmt.Println(i)
  111. if i == 0 {
  112. continue
  113. }
  114. fmt.Println(v[1], v[2], v[10])
  115. s.d.DB.Exec(` update emp set tel = ? where empCode = ? `, v[10], v[1])
  116. }
  117. //precontractId := model.PartRepertory{}
  118. //err := s.d.DB.Raw(`select precontractId from part_repertory where precontractId is not null order by precontractId `).First(&precontractId).Error
  119. //if err != nil {
  120. // log.Error(err)
  121. // //continue
  122. //}
  123. //contractId := model.PartRepertory{}
  124. //err = s.d.DB.Raw(`select (contractId) contractId from part_repertory order by contractId `).First(&contractId).Error
  125. //if err != nil {
  126. // log.Error(err)
  127. // //continue
  128. //}
  129. //for i, v := range rows {
  130. // fmt.Println(i)
  131. // if i == 0 {
  132. // continue
  133. // }
  134. // /*
  135. // pasture := model.Pasture{}
  136. // err := s.d.DB.Where(&model.Pasture{Name: v[1]}).First(&pasture).Error
  137. // if err != nil {
  138. // fmt.Println(err)
  139. // return
  140. // }
  141. // now := time.Now().Format("2006-01-02")
  142. // contractId, partCode := v[13], v[4]
  143. // partLaid := model.PartLaid{}
  144. // err = s.d.DB.Raw(` select p.id from bigpartlaid bp join partlaid p on p.bigid = bp.id
  145. // where bp.pastureid = ? and p.contractId = ? and p.partCode = ? and DATE_FORMAT(bp.creatDate,'%Y-%m-%d') = ?`, pasture.ID, contractId, partCode, now).First(&partLaid).Error
  146. // if err != nil {
  147. // fmt.Println(err)
  148. // //return
  149. // }
  150. // partuse := model.PartUse{}
  151. // err = s.d.DB.Raw(` select p.id from bigpartuse bp join partuse p on p.bigid = bp.id
  152. // where bp.pastureid = ? and p.contractId = ? and p.partCode = ? and DATE_FORMAT(bp.creatDate,'%Y-%m-%d') = ?`, pasture.ID, contractId, partCode, now).First(&partuse).Error
  153. // if err != nil {
  154. // fmt.Println(err)
  155. // //return
  156. // }
  157. // if partuse.ID > 0 || partLaid.ID > 0 {
  158. // continue
  159. // }
  160. //
  161. // err = s.d.DB.Exec(` update part_repertory ps set reportery = ? where id = ? `, v[15], v[0]).Error
  162. // if err != nil {
  163. // fmt.Println(err)
  164. // return
  165. // }
  166. // */
  167. //
  168. // //if v[14] == "新增" {
  169. // //
  170. // // //1983
  171. //
  172. // //
  173. // //if v[1] == "M017" || v[1] == "N302" || i <= 47 {
  174. // // continue
  175. // //}
  176. // id := v[0]
  177. // ///*
  178. // if id == "" && v[11] == "无合同" && v[14] == "M017" {
  179. //
  180. // parts := model.Parts{}
  181. // err := s.d.DB.Where(&model.Parts{PartCode: v[3]}).First(&parts).Error
  182. // if err != nil {
  183. // log.Error(err)
  184. // continue
  185. // }
  186. //
  187. // pasture := model.Pasture{}
  188. // err = s.d.DB.Where(&model.Pasture{FactoryCode: "M005"}).First(&pasture).Error
  189. // if err != nil {
  190. // fmt.Println(err)
  191. // continue
  192. // }
  193. //
  194. // w := model.Warehouse{}
  195. // err = s.d.DB.Where(&model.Warehouse{PastureID: pasture.ID, Status: 1}).First(&w).Error
  196. // if err != nil {
  197. // fmt.Println(err)
  198. // continue
  199. // }
  200. //
  201. // reportery := v[16]
  202. // price := 1
  203. // //reportery := v[2]
  204. // providerid := "1975"
  205. // providerName := "北京京东"
  206. // contractId := fmt.Sprintf("-%d%s%d", pasture.ID, providerid, parts.ID)
  207. // //
  208. // //// PrecontractID, err := strconv.ParseInt(precontractId.PrecontractID, 10, 64)
  209. // err = s.d.DB.Exec(` insert into part_repertory(specification,pastureId,partId,partName,partCode,location,locationId,reportery,
  210. // contractId,brandId,unit,price,precontractId,providerNames,providerId)
  211. // values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  212. // parts.Specification, pasture.ID, parts.ID, parts.Name, parts.PartCode, w.WarehoseCode, w.ID, reportery, contractId, 0, parts.Unit,
  213. // price, contractId, providerName, providerid).Error
  214. // }
  215. // //*/
  216. // //if err != nil {
  217. // // fmt.Println(err)
  218. // // return
  219. // //}
  220. //
  221. // //}
  222. // //else {
  223. // //reportery := v[16]
  224. // //id := v[0]
  225. // //if id != "" {
  226. // // err := s.d.DB.Exec(` update part_repertory ps set reportery = ? where id = ?`, reportery, id).Error
  227. // // if err != nil {
  228. // // fmt.Println(err)
  229. // // return
  230. // // }
  231. // //}
  232. // //} else {
  233. // // err := s.d.DB.Exec(` delete from part_repertory where id = ?`, id).Error
  234. // // if err != nil {
  235. // // fmt.Println(err)
  236. // // return
  237. // // }
  238. // //}
  239. // //}
  240. //}
  241. }
  242. func DepartmentExec(rows [][]string) {
  243. for i, v := range rows {
  244. //fmt.Println(i, v)
  245. if i == 0 {
  246. continue
  247. }
  248. id := v[0]
  249. name := v[1]
  250. parentid := v[4]
  251. costCenter_code := v[15]
  252. costCenter_code2 := v[17]
  253. fmt.Println(id, name, parentid, costCenter_code, costCenter_code2)
  254. if id != "新增" {
  255. fmt.Println(id, name, parentid, costCenter_code, costCenter_code2)
  256. if costCenter_code2 != "" {
  257. err := s.d.DB.Exec(` update department ps set costCenter_code = ? where id = ? `, costCenter_code2, id).Error
  258. if err != nil {
  259. fmt.Println(err)
  260. return
  261. }
  262. }
  263. if v[18] == "无此部门" || v[18] == "未上线牧场" || v[18] == "重复" {
  264. err := s.d.DB.Exec(` delete from department where id = ? `, id).Error
  265. if err != nil {
  266. fmt.Println(err)
  267. return
  268. }
  269. }
  270. } else {
  271. fmt.Println(id, name, parentid, costCenter_code, costCenter_code2)
  272. if costCenter_code2 != "" {
  273. err := s.d.DB.Exec(` insert into department(name,pastureId,isPasture,costCenter_code) values(?,?,?,?) `, name, parentid, 0, costCenter_code2).Error
  274. if err != nil {
  275. fmt.Println(err)
  276. return
  277. }
  278. }
  279. }
  280. }
  281. }
  282. func ProviderExec(rows [][]string) {
  283. //for i, v := range rows {
  284. // if i == 0 {
  285. // continue
  286. // }
  287. // id := v[0]
  288. // //sapcode := v[4]
  289. // //providername := v[5]
  290. // status := v[6]
  291. //
  292. // if status == "删除" || v[4] == "" {
  293. // err := s.d.DB.Exec(` delete from provider where id = ? `, id).Error
  294. // if err != nil {
  295. // fmt.Println(err)
  296. // return
  297. // }
  298. // }
  299. //}
  300. //for i, v := range rows {
  301. // if i == 0 {
  302. // continue
  303. // }
  304. // id := v[0]
  305. // sapcode := v[4]
  306. // providername := v[5]
  307. // status := v[6]
  308. //
  309. // if status != "删除" {
  310. // err := s.d.DB.Exec(` update provider set providerIntro = ? , sapcode = ? where id = ? `, providername, sapcode, id).Error
  311. // if err != nil {
  312. // fmt.Println(err)
  313. // return
  314. // }
  315. // }
  316. //}
  317. //for _, v := range rows {
  318. //if i == 0 {
  319. // continue
  320. //}
  321. //id := v[0]
  322. //sapcode := v[4]
  323. //providername := v[1]
  324. //providerIntro := v[5]
  325. //status := v[6]
  326. //
  327. //if status != "删除" {
  328. // err := s.d.DB.Exec(` update provider set providerIntro = ? , sapcode = ? where id = ? `, providername, sapcode, id).Error
  329. // if err != nil {
  330. // fmt.Println(err)
  331. // return
  332. // }
  333. //}
  334. // err := s.d.DB.Exec(` insert into provider(providerName,providerIntro,providerNumber,sapcode) values(?,?,?,?)`,
  335. // providername, providerIntro, sapcode, sapcode).Error
  336. // if err != nil {
  337. // fmt.Println(err)
  338. // return
  339. // }
  340. //}
  341. }
  342. func EquipmentExec(rows [][]string, pastureId int) error {
  343. errList := make([]map[string]interface{}, 0)
  344. for i, v := range rows {
  345. financeCode := v[13]
  346. if financeCode == "" || i == 0 {
  347. continue
  348. }
  349. data := make(map[string]interface{})
  350. fmt.Println(i)
  351. eqCode := v[4]
  352. eqName := v[3]
  353. specification := v[5]
  354. //用途
  355. purpose := v[8]
  356. //purchaseDate, _ := ExcelIntDate(v[10])
  357. //purchaseDate := v[10]
  358. //entranceDate, _ := ExcelIntDate(v[11])
  359. //entranceDate := v[11]
  360. assetCode := v[2]
  361. depreciation := v[12]
  362. yuanzhi := v[14]
  363. salvage := v[15]
  364. subtractvalue := v[16]
  365. upkeepgrade := v[17]
  366. if upkeepgrade == "A" {
  367. upkeepgrade = "113"
  368. } else if upkeepgrade == "B" {
  369. upkeepgrade = "114"
  370. } else if upkeepgrade == "C" {
  371. upkeepgrade = "115"
  372. }
  373. yearUpkeepCost := v[18]
  374. yearMaintainCost := v[19]
  375. baseHours := v[20]
  376. employeName := v[22]
  377. inputUser := v[23]
  378. var leaveDate interface{}
  379. if v[24] != "" {
  380. leaveDate = v[24]
  381. //leaveDate, _ = ExcelIntDate(v[24])
  382. }
  383. brand := v[6]
  384. proName := v[7]
  385. maintenance := v[27]
  386. var onepushTime, twopushTime, threepushTime interface{}
  387. if v[29] == "" {
  388. onepushTime = nil
  389. } else {
  390. onepushTime = v[29]
  391. }
  392. if v[31] == "" {
  393. twopushTime = nil
  394. } else {
  395. twopushTime = v[31]
  396. }
  397. if v[33] == "" {
  398. threepushTime = nil
  399. } else {
  400. threepushTime = v[33]
  401. }
  402. levelone := v[28]
  403. leveltwo := v[30]
  404. levelthree := v[32]
  405. //license := v[34]
  406. //duration := v[35]
  407. brandInfo := model.Brand{}
  408. if brand != "" {
  409. err := s.d.DB.Where(&model.Brand{BrandName: brand}).First(&brandInfo).Error
  410. if err != nil {
  411. fmt.Println(err)
  412. //return err
  413. }
  414. }
  415. provider := model.Provider{}
  416. if proName != "" {
  417. err := s.d.DB.Where(&model.Provider{ProviderName: proName}).First(&provider).Error
  418. if err != nil {
  419. fmt.Println(err)
  420. //return err
  421. }
  422. }
  423. pasture := model.Pasture{}
  424. err := s.d.DB.Where(&model.Pasture{Name: v[0]}).First(&pasture).Error
  425. if err != nil {
  426. data["pasture"] = v[0]
  427. data["eqName"] = eqName
  428. data["eqCode"] = eqCode
  429. data["financeCode"] = financeCode
  430. data["err"] = err.Error()
  431. errList = append(errList, data)
  432. fmt.Println(err)
  433. return err
  434. }
  435. //if pastureId != pasture.ID {
  436. // continue
  437. //}
  438. err = s.d.DB.Exec(` insert into equipment (assetCode,pastureId,pastureName,eqCode,specification,purpose,depreciation,yuanzhi, salvage,subtractvalue, upkeepgrade,yearUpkeepCost,
  439. yearMaintainCost,baseHours,employeName,employeeId,inputUser,leaveDate,brand,brandId,proId,proName,eqClassName,eqClassId,eqName,
  440. maintenance,levelone,leveltwo,levelthree,leveloneTime,leveltwoTime,levelthreeTime)
  441. values(?,?,?,if(? = '',null,?),?,?,?,?,?,?,?,?,?,?,(select empname from emp where replace(empname,' ','') = ? and pastureId = ? and enable = 1 ),
  442. (select id from emp where replace(empname,' ','') = ? and pastureId = ? and enable = 1 ),?,?,?,?,?,?,?,
  443. (select id from eqclass where typeName = ?),?,(select GROUP_CONCAT(id) from emp where FIND_IN_SET(replace(empname,' ',''),replace(?,' ',''))
  444. and pastureId = ? and enable = 1 ),
  445. (select id from emp where replace(empname,' ','') = replace(?,' ','') and pastureId = ? and enable = 1 ),
  446. (select id from emp where replace(empname,' ','') = replace(?,' ','') and pastureId = ? and enable = 1 ),
  447. (select id from emp where replace(empname,' ','') = replace(?,' ','') and pastureId = ? and enable = 1 ),?,?,?) ON DUPLICATE KEY UPDATE assetCode = ?`,
  448. assetCode, pasture.ID, pasture.Name, eqCode, eqCode, specification, purpose, depreciation, yuanzhi,
  449. salvage, subtractvalue, upkeepgrade, yearUpkeepCost, yearMaintainCost, baseHours, employeName, pasture.ID, employeName, pasture.ID, inputUser, leaveDate,
  450. brandInfo.BrandName, brandInfo.ID, provider.ID, provider.ProviderName, eqName, v[1], v[3], maintenance, pasture.ID, levelone, pasture.ID,
  451. leveltwo, pasture.ID, levelthree, pasture.ID, onepushTime, twopushTime, threepushTime, assetCode).Error
  452. //err = s.d.DB.Exec(` update equipment set eqCode = if(? = '',null,?),specification = ?,purpose = ?
  453. //,depreciation=?,
  454. // yuanzhi=?, salvage =?,subtractvalue =?, upkeepgrade= ?,yearUpkeepCost=? ,yearMaintainCost=?,baseHours=?,employeName=(select empname from emp where replace(empname,' ','') = ? and pastureId = ? and enable = 1 ),
  455. // employeeId = (select id from emp where replace(empname,' ','') = ? and pastureId = ? and enable = 1 ),inputUser=?,leaveDate=? ,
  456. // brand = ?, brandId = ? ,proId= ?,proName= ? ,eqClassName = ?,eqClassId = (select id from eqclass where typeName = ?),eqName = ?,
  457. // maintenance= (select GROUP_CONCAT(id) from emp where FIND_IN_SET(replace(empname,' ',''),replace(?,' ','')) and pastureId = ? and enable = 1 ),levelone=(select id from emp where replace(empname,' ','') = replace(?,' ','') and pastureId = ? and enable = 1 ) ,
  458. //leveltwo = (select id from emp where replace(empname,' ','') = replace(?,' ','') and pastureId = ? and enable = 1 ),levelthree=(select id from emp where replace(empname,' ','') = replace(?,' ','') and pastureId = ? and enable = 1 )
  459. //,leveloneTime = ? ,leveltwoTime = ?,levelthreeTime= ? ,pushStatus = 2 where financeCode = ? `,
  460. // eqCode, eqCode, specification, purpose, depreciation, yuanzhi,
  461. // salvage, subtractvalue, upkeepgrade, yearUpkeepCost, yearMaintainCost, baseHours, employeName, pasture.ID, employeName, pasture.ID, inputUser, leaveDate,
  462. // brandInfo.BrandName, brandInfo.ID, provider.ID, provider.ProviderName, eqName, v[1], v[3], maintenance, pasture.ID, levelone, pasture.ID,
  463. // leveltwo, pasture.ID, levelthree, pasture.ID, onepushTime, twopushTime, threepushTime, financeCode).Error
  464. if err != nil {
  465. data["pasture"] = v[0]
  466. data["eqName"] = eqName
  467. data["eqCode"] = eqCode
  468. data["financeCode"] = financeCode
  469. data["err"] = err.Error()
  470. errList = append(errList, data)
  471. fmt.Println(err)
  472. //return err
  473. continue
  474. }
  475. eqclass := model.EqClass{}
  476. err = s.d.DB.Table("eqclass").Where("typeName = ?", v[1]).First(&eqclass).Error
  477. if err != nil {
  478. data["pasture"] = v[0]
  479. data["eqName"] = eqName
  480. data["eqCode"] = eqCode
  481. data["financeCode"] = financeCode
  482. data["err"] = err.Error()
  483. errList = append(errList, data)
  484. fmt.Println(err)
  485. //return err
  486. continue
  487. }
  488. if eqclass.ID == 0 {
  489. continue
  490. }
  491. if v[2] == "" {
  492. err = s.d.DB.Exec(` update equipment set assetCode = (select cerateEQNumber((select typeCode from eqclass where typeName = ?),?)) where financeCode = ? `,
  493. v[1], pasture.ID, financeCode).Error
  494. if err != nil {
  495. data["pasture"] = v[0]
  496. data["eqName"] = eqName
  497. data["eqCode"] = eqCode
  498. data["financeCode"] = financeCode
  499. data["err"] = err.Error()
  500. errList = append(errList, data)
  501. fmt.Println(err)
  502. //return err
  503. }
  504. }
  505. }
  506. return nil
  507. }
  508. func ExcelIntDate(dateStr string) (dt time.Time, err error) {
  509. var dateValue float64
  510. matched, err := regexp.MatchString(`^\d+$`, dateStr)
  511. if err != nil {
  512. return
  513. }
  514. if !matched {
  515. err = errors.New("not excel time")
  516. return
  517. }
  518. dateValue, err = strconv.ParseFloat(dateStr, 64)
  519. if err != nil {
  520. return
  521. }
  522. epoch := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC) // UTC 1899/12/30 00:00:00
  523. dt = epoch.Add(time.Duration(dateValue) * 24 * time.Hour)
  524. return
  525. }
  526. func EquipmentExecAdd(rows [][]string) error {
  527. for i, v := range rows {
  528. financeCode := fmt.Sprintf("%s-%s-%s", v[0], v[1], v[2])
  529. fmt.Println(financeCode, v[11], i)
  530. //financeCode := v[13]
  531. if i == 0 || (v[0] != "1042" && v[0] != "1002") {
  532. continue
  533. }
  534. err := s.d.DB.Exec(` update equipment set pastureid = (SELECT pastureid FROM department WHERE costCenter_code = ? ) where financeCode = ? `, v[11], financeCode).Error
  535. fmt.Println(err)
  536. //var purchaseDate, entranceDate string
  537. //if v[9] != "" {
  538. // date, _ := ExcelIntDate(v[9])
  539. // purchaseDate = date.Format("2006-01-02")
  540. //}
  541. //if v[10] != "" {
  542. // date, _ := ExcelIntDate(v[10])
  543. // entranceDate = date.Format("2006-01-02")
  544. //}
  545. //a1, _ := strconv.Atoi()
  546. //a1, _ := strconv.ParseFloat(v[12][:3], 64)
  547. //n := strings.Index(v[12][3:], "6")
  548. //if n > 0 {
  549. // a1 += 0.5
  550. //}
  551. }
  552. return nil
  553. }
  554. func ContractExec(rows [][]string) {
  555. for i, v := range rows {
  556. if i < 6 {
  557. continue
  558. }
  559. partCode := v[1]
  560. providerSapCode := v[3]
  561. pastureCode := v[6]
  562. price := v[8]
  563. //fmt.Println(partCode, providerSapCode, pastureCode, price)
  564. pasture := model.Pasture{}
  565. err := s.d.DB.Where(model.Pasture{FactoryCode: pastureCode}).First(&pasture).Error
  566. if err != nil {
  567. fmt.Println(err)
  568. //return
  569. continue
  570. }
  571. //fmt.Println(pasture, err)
  572. provider := model.Provider{}
  573. err = s.d.DB.Where(model.Provider{SapCode: providerSapCode}).First(&provider).Error
  574. if err != nil {
  575. fmt.Println(err)
  576. continue
  577. }
  578. contract := model.Contract{}
  579. s.d.DB.Raw(`SELECT
  580. c.id
  581. FROM
  582. contract c
  583. JOIN bigcontract bc ON bc.id = c.bigid
  584. WHERE
  585. c.partCode = ?
  586. AND ? IN (
  587. SELECT
  588. pb.pasture_id
  589. FROM
  590. bigcontract bct
  591. JOIN pasture_bigcontract pb ON pb.bigcontract_id = bct.id
  592. WHERE
  593. bct.contractCode LIKE concat( "%", LEFT ( bc.contractCode, 12 ), "%" )
  594. )
  595. AND bc.providerId = ? `, partCode, pasture.ID, provider.ID).First(&contract)
  596. fmt.Println(pasture.Name, provider.ProviderName, price, pastureCode, i)
  597. if contract.ID > 0 {
  598. s.d.DB.Exec(" update contract set price = ? where id = ? ", price, contract.ID)
  599. }
  600. }
  601. }
  602. func (s *Service) EditPartpurchase(reqList []*model.EditPartpurchaseReq) error {
  603. tx := s.d.DB.Begin()
  604. var bigId, employeId int64
  605. idList := []int64{}
  606. for _, req := range reqList {
  607. bigId = req.BigId
  608. break
  609. }
  610. var total int64
  611. s.d.DB.Debug().Model(&model.BigPartPurchase{}).Where("id = ? ", bigId).Where("statue >= 3 ").Count(&total)
  612. if total > 0 {
  613. return errors.New("单号已合单,请返回开始页面重新申购!")
  614. }
  615. for _, req := range reqList {
  616. var partPurchaseCount int64
  617. err := tx.Model(&model.PartPurchase{}).Where(" reject = 0 ").Where(" bigId = ? ", req.BigId).Where("partCode = ? ", req.PartCode).Where(" contractId = ? ", req.ContractId).Count(&partPurchaseCount).Error
  618. if err != nil {
  619. tx.Rollback()
  620. logs.Error("EditPartpurchase-0:" + err.Error())
  621. return err
  622. }
  623. if partPurchaseCount > 0 {
  624. return errors.New(fmt.Sprintf("该单号中已存在相同配件%s %s,请调整后重新保存", req.PartCode, req.PartName))
  625. }
  626. }
  627. //appG.Response(http.StatusOK, e.ERROR, )
  628. for _, req := range reqList {
  629. //bigId = req.BigId
  630. employeId = req.EmployeId
  631. if req.Id == 0 {
  632. if req.Reject == "" {
  633. req.Reject = "0"
  634. }
  635. part := &model.PartPurchase{
  636. BigId: req.BigId,
  637. PartId: req.PartId,
  638. PartName: req.PartName,
  639. PartCode: req.PartCode,
  640. Amount: req.Amount,
  641. Specification: req.Specification,
  642. Purpose: req.Purpose,
  643. Unit: req.Unit,
  644. BrandId: req.BrandId,
  645. StorageAmount: req.StorageAmount,
  646. Price: req.Price,
  647. ContractId: req.ContractId,
  648. LifeCycle: req.LifeCycle,
  649. ContractVarianceItem: req.ContractVarianceItem,
  650. Reject: req.Reject,
  651. DepartmentId: req.DepartmentId,
  652. Explain: req.Explain,
  653. EmployeId: req.EmployeId,
  654. Date: time.Now().Format("2006-01-02 15:04:05"),
  655. }
  656. resp := tx.Debug().Create(&part)
  657. if resp.Error != nil {
  658. tx.Rollback()
  659. logs.Error("EditPartpurchase-0:" + resp.Error.Error())
  660. return resp.Error
  661. }
  662. idList = append(idList, part.Id)
  663. } else {
  664. err := tx.Exec(` update partpurchase set reject = 0 ,amount = ? ,purpose = ?,bigId = ?,date = now() where id = ? `, req.Amount, req.Purpose, req.BigId, req.Id).Error
  665. if err != nil {
  666. tx.Rollback()
  667. logs.Error("EditPartpurchase-1:" + err.Error())
  668. return err
  669. }
  670. idList = append(idList, req.Id)
  671. }
  672. }
  673. if bigId > 0 {
  674. err := tx.Debug().Exec(` delete from partpurchase where id in((select pl.id from partpurchase pl
  675. join bigpartpurchase bpp on bpp.id = pl.bigId where bpp.purchase_type = (select purchase_type from bigpartpurchase where id = ? )
  676. and pl.reject = 1 and pl.id not in(?))) and employeId = ? `, bigId, idList, employeId).Error
  677. if err != nil {
  678. tx.Rollback()
  679. logs.Error("EditPartpurchase-2:" + err.Error())
  680. return err
  681. }
  682. }
  683. err := tx.Commit().Error
  684. if err != nil {
  685. tx.Rollback()
  686. logs.Error("EditPartpurchase-3:" + err.Error())
  687. return err
  688. }
  689. return nil
  690. }