material.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. depreciation := v[12]
  361. yuanzhi := v[14]
  362. salvage := v[15]
  363. subtractvalue := v[16]
  364. upkeepgrade := v[17]
  365. if upkeepgrade == "A" {
  366. upkeepgrade = "113"
  367. } else if upkeepgrade == "B" {
  368. upkeepgrade = "114"
  369. } else if upkeepgrade == "C" {
  370. upkeepgrade = "115"
  371. }
  372. yearUpkeepCost := v[18]
  373. yearMaintainCost := v[19]
  374. baseHours := v[20]
  375. employeName := v[22]
  376. inputUser := v[23]
  377. var leaveDate interface{}
  378. if v[24] != "" {
  379. leaveDate = v[24]
  380. //leaveDate, _ = ExcelIntDate(v[24])
  381. }
  382. brand := v[6]
  383. proName := v[7]
  384. maintenance := v[27]
  385. var onepushTime, twopushTime, threepushTime interface{}
  386. if v[29] == "" {
  387. onepushTime = nil
  388. } else {
  389. onepushTime = v[29]
  390. }
  391. if v[31] == "" {
  392. twopushTime = nil
  393. } else {
  394. twopushTime = v[31]
  395. }
  396. if v[33] == "" {
  397. threepushTime = nil
  398. } else {
  399. threepushTime = v[33]
  400. }
  401. levelone := v[28]
  402. leveltwo := v[30]
  403. levelthree := v[32]
  404. //license := v[34]
  405. //duration := v[35]
  406. brandInfo := model.Brand{}
  407. if brand != "" {
  408. err := s.d.DB.Where(&model.Brand{BrandName: brand}).First(&brandInfo).Error
  409. if err != nil {
  410. fmt.Println(err)
  411. //return err
  412. }
  413. }
  414. provider := model.Provider{}
  415. if proName != "" {
  416. err := s.d.DB.Where(&model.Provider{ProviderName: proName}).First(&provider).Error
  417. if err != nil {
  418. fmt.Println(err)
  419. //return err
  420. }
  421. }
  422. pasture := model.Pasture{}
  423. err := s.d.DB.Where(&model.Pasture{Name: v[0]}).First(&pasture).Error
  424. if err != nil {
  425. data["pasture"] = v[0]
  426. data["eqName"] = eqName
  427. data["eqCode"] = eqCode
  428. data["financeCode"] = financeCode
  429. data["err"] = err.Error()
  430. errList = append(errList, data)
  431. fmt.Println(err)
  432. return err
  433. }
  434. if pastureId != pasture.ID {
  435. continue
  436. }
  437. err = s.d.DB.Exec(` update equipment set eqCode = if(? = '',null,?),specification = ?,purpose = ?
  438. ,depreciation=?,
  439. yuanzhi=?, salvage =?,subtractvalue =?, upkeepgrade= ?,yearUpkeepCost=? ,yearMaintainCost=?,baseHours=?,employeName=(select empname from emp where replace(empname,' ','') = ? and pastureId = ? and enable = 1 ),
  440. employeeId = (select id from emp where replace(empname,' ','') = ? and pastureId = ? and enable = 1 ),inputUser=?,leaveDate=? ,
  441. brand = ?, brandId = ? ,proId= ?,proName= ? ,eqClassName = ?,eqClassId = (select id from eqclass where typeName = ?),eqName = ?,
  442. 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 ) ,
  443. 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 )
  444. ,leveloneTime = ? ,leveltwoTime = ?,levelthreeTime= ? ,pushStatus = 2 where financeCode = ? `,
  445. eqCode, eqCode, specification, purpose, depreciation, yuanzhi,
  446. salvage, subtractvalue, upkeepgrade, yearUpkeepCost, yearMaintainCost, baseHours, employeName, pasture.ID, employeName, pasture.ID, inputUser, leaveDate,
  447. brandInfo.BrandName, brandInfo.ID, provider.ID, provider.ProviderName, eqName, v[1], v[3], maintenance, pasture.ID, levelone, pasture.ID,
  448. leveltwo, pasture.ID, levelthree, pasture.ID, onepushTime, twopushTime, threepushTime, financeCode).Error
  449. if err != nil {
  450. data["pasture"] = v[0]
  451. data["eqName"] = eqName
  452. data["eqCode"] = eqCode
  453. data["financeCode"] = financeCode
  454. data["err"] = err.Error()
  455. errList = append(errList, data)
  456. fmt.Println(err)
  457. //return err
  458. continue
  459. }
  460. eqclass := model.EqClass{}
  461. err = s.d.DB.Table("eqclass").Where("typeName = ?", v[1]).First(&eqclass).Error
  462. if err != nil {
  463. data["pasture"] = v[0]
  464. data["eqName"] = eqName
  465. data["eqCode"] = eqCode
  466. data["financeCode"] = financeCode
  467. data["err"] = err.Error()
  468. errList = append(errList, data)
  469. fmt.Println(err)
  470. //return err
  471. continue
  472. }
  473. if eqclass.ID == 0 {
  474. continue
  475. }
  476. if v[2] == "" {
  477. err = s.d.DB.Exec(` update equipment set assetCode = (select cerateEQNumber((select typeCode from eqclass where typeName = ?),?)) where financeCode = ? `,
  478. v[1], pasture.ID, financeCode).Error
  479. if err != nil {
  480. data["pasture"] = v[0]
  481. data["eqName"] = eqName
  482. data["eqCode"] = eqCode
  483. data["financeCode"] = financeCode
  484. data["err"] = err.Error()
  485. errList = append(errList, data)
  486. fmt.Println(err)
  487. //return err
  488. }
  489. }
  490. }
  491. return nil
  492. }
  493. func ExcelIntDate(dateStr string) (dt time.Time, err error) {
  494. var dateValue float64
  495. matched, err := regexp.MatchString(`^\d+$`, dateStr)
  496. if err != nil {
  497. return
  498. }
  499. if !matched {
  500. err = errors.New("not excel time")
  501. return
  502. }
  503. dateValue, err = strconv.ParseFloat(dateStr, 64)
  504. if err != nil {
  505. return
  506. }
  507. epoch := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC) // UTC 1899/12/30 00:00:00
  508. dt = epoch.Add(time.Duration(dateValue) * 24 * time.Hour)
  509. return
  510. }
  511. func EquipmentExecAdd(rows [][]string) error {
  512. for i, v := range rows {
  513. financeCode := fmt.Sprintf("%s-%s-%s", v[0], v[1], v[2])
  514. fmt.Println(financeCode, v[11], i)
  515. //financeCode := v[13]
  516. if i == 0 || (v[0] != "1042" && v[0] != "1002") {
  517. continue
  518. }
  519. err := s.d.DB.Exec(` update equipment set pastureid = (SELECT pastureid FROM department WHERE costCenter_code = ? ) where financeCode = ? `, v[11], financeCode).Error
  520. fmt.Println(err)
  521. //var purchaseDate, entranceDate string
  522. //if v[9] != "" {
  523. // date, _ := ExcelIntDate(v[9])
  524. // purchaseDate = date.Format("2006-01-02")
  525. //}
  526. //if v[10] != "" {
  527. // date, _ := ExcelIntDate(v[10])
  528. // entranceDate = date.Format("2006-01-02")
  529. //}
  530. //a1, _ := strconv.Atoi()
  531. //a1, _ := strconv.ParseFloat(v[12][:3], 64)
  532. //n := strings.Index(v[12][3:], "6")
  533. //if n > 0 {
  534. // a1 += 0.5
  535. //}
  536. }
  537. return nil
  538. }
  539. func ContractExec(rows [][]string) {
  540. for i, v := range rows {
  541. if i < 6 {
  542. continue
  543. }
  544. partCode := v[1]
  545. providerSapCode := v[3]
  546. pastureCode := v[6]
  547. price := v[8]
  548. //fmt.Println(partCode, providerSapCode, pastureCode, price)
  549. pasture := model.Pasture{}
  550. err := s.d.DB.Where(model.Pasture{FactoryCode: pastureCode}).First(&pasture).Error
  551. if err != nil {
  552. fmt.Println(err)
  553. //return
  554. continue
  555. }
  556. //fmt.Println(pasture, err)
  557. provider := model.Provider{}
  558. err = s.d.DB.Where(model.Provider{SapCode: providerSapCode}).First(&provider).Error
  559. if err != nil {
  560. fmt.Println(err)
  561. continue
  562. }
  563. contract := model.Contract{}
  564. s.d.DB.Raw(`SELECT
  565. c.id
  566. FROM
  567. contract c
  568. JOIN bigcontract bc ON bc.id = c.bigid
  569. WHERE
  570. c.partCode = ?
  571. AND ? IN (
  572. SELECT
  573. pb.pasture_id
  574. FROM
  575. bigcontract bct
  576. JOIN pasture_bigcontract pb ON pb.bigcontract_id = bct.id
  577. WHERE
  578. bct.contractCode LIKE concat( "%", LEFT ( bc.contractCode, 12 ), "%" )
  579. )
  580. AND bc.providerId = ? `, partCode, pasture.ID, provider.ID).First(&contract)
  581. fmt.Println(pasture.Name, provider.ProviderName, price, pastureCode, i)
  582. if contract.ID > 0 {
  583. s.d.DB.Exec(" update contract set price = ? where id = ? ", price, contract.ID)
  584. }
  585. }
  586. }
  587. func (s *Service) EditPartpurchase(reqList []*model.EditPartpurchaseReq) error {
  588. tx := s.d.DB.Begin()
  589. var bigId, employeId int64
  590. idList := []int64{}
  591. for _, req := range reqList {
  592. bigId = req.BigId
  593. break
  594. }
  595. var total int64
  596. s.d.DB.Debug().Model(&model.BigPartPurchase{}).Where("id = ? ", bigId).Where("statue >= 3 ").Count(&total)
  597. if total > 0 {
  598. return errors.New("单号已合单,请返回开始页面重新申购!")
  599. }
  600. for _, req := range reqList {
  601. var partPurchaseCount int64
  602. err := tx.Model(&model.PartPurchase{}).Where(" reject = 0 ").Where(" bigId = ? ", req.BigId).Where("partCode = ? ", req.PartCode).Where(" contractId = ? ", req.ContractId).Count(&partPurchaseCount).Error
  603. if err != nil {
  604. tx.Rollback()
  605. logs.Error("EditPartpurchase-0:" + err.Error())
  606. return err
  607. }
  608. if partPurchaseCount > 0 {
  609. return errors.New(fmt.Sprintf("该单号中已存在相同配件%s %s,请调整后重新保存", req.PartCode, req.PartName))
  610. }
  611. }
  612. //appG.Response(http.StatusOK, e.ERROR, )
  613. for _, req := range reqList {
  614. //bigId = req.BigId
  615. employeId = req.EmployeId
  616. if req.Id == 0 {
  617. if req.Reject == "" {
  618. req.Reject = "0"
  619. }
  620. part := &model.PartPurchase{
  621. BigId: req.BigId,
  622. PartId: req.PartId,
  623. PartName: req.PartName,
  624. PartCode: req.PartCode,
  625. Amount: req.Amount,
  626. Specification: req.Specification,
  627. Purpose: req.Purpose,
  628. Unit: req.Unit,
  629. BrandId: req.BrandId,
  630. StorageAmount: req.StorageAmount,
  631. Price: req.Price,
  632. ContractId: req.ContractId,
  633. LifeCycle: req.LifeCycle,
  634. ContractVarianceItem: req.ContractVarianceItem,
  635. Reject: req.Reject,
  636. DepartmentId: req.DepartmentId,
  637. Explain: req.Explain,
  638. EmployeId: req.EmployeId,
  639. Date: time.Now().Format("2006-01-02 15:04:05"),
  640. }
  641. resp := tx.Debug().Create(&part)
  642. if resp.Error != nil {
  643. tx.Rollback()
  644. logs.Error("EditPartpurchase-0:" + resp.Error.Error())
  645. return resp.Error
  646. }
  647. idList = append(idList, part.Id)
  648. } else {
  649. err := tx.Exec(` update partpurchase set reject = 0 ,amount = ? ,purpose = ?,bigId = ?,date = now() where id = ? `, req.Amount, req.Purpose, req.BigId, req.Id).Error
  650. if err != nil {
  651. tx.Rollback()
  652. logs.Error("EditPartpurchase-1:" + err.Error())
  653. return err
  654. }
  655. idList = append(idList, req.Id)
  656. }
  657. }
  658. if bigId > 0 {
  659. err := tx.Debug().Exec(` delete from partpurchase where id in((select pl.id from partpurchase pl
  660. join bigpartpurchase bpp on bpp.id = pl.bigId where bpp.purchase_type = (select purchase_type from bigpartpurchase where id = ? )
  661. and pl.reject = 1 and pl.id not in(?))) and employeId = ? `, bigId, idList, employeId).Error
  662. if err != nil {
  663. tx.Rollback()
  664. logs.Error("EditPartpurchase-2:" + err.Error())
  665. return err
  666. }
  667. }
  668. err := tx.Commit().Error
  669. if err != nil {
  670. tx.Rollback()
  671. logs.Error("EditPartpurchase-3:" + err.Error())
  672. return err
  673. }
  674. return nil
  675. }