ctr_contract.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "time"
  8. basedao "dashoo.cn/micro/app/dao/base"
  9. dao "dashoo.cn/micro/app/dao/contract"
  10. custdao "dashoo.cn/micro/app/dao/cust"
  11. projdao "dashoo.cn/micro/app/dao/proj"
  12. sysdao "dashoo.cn/micro/app/dao/sys"
  13. model "dashoo.cn/micro/app/model/contract"
  14. proj "dashoo.cn/micro/app/model/proj"
  15. "dashoo.cn/opms_libary/micro_srv"
  16. "dashoo.cn/opms_libary/myerrors"
  17. "dashoo.cn/opms_libary/request"
  18. "github.com/gogf/gf/database/gdb"
  19. "github.com/gogf/gf/os/gtime"
  20. "github.com/gogf/gf/util/gvalid"
  21. )
  22. type CtrContractService struct {
  23. Dao *dao.CtrContractDao
  24. ProjBusinessDao *projdao.ProjBusinessDao
  25. CustomerDao *custdao.CustCustomerDao
  26. UserDao *sysdao.SysUserDao
  27. CtrProductDao *dao.CtrContractProductDao
  28. ProductDao *basedao.BaseProductDao
  29. DynamicsDao *dao.CtrContractDynamicsDao
  30. Tenant string
  31. userInfo request.UserInfo
  32. }
  33. func NewCtrContractService(ctx context.Context) (*CtrContractService, error) {
  34. tenant, err := micro_srv.GetTenant(ctx)
  35. if err != nil {
  36. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  37. }
  38. // 获取用户信息
  39. userInfo, err := micro_srv.GetUserInfo(ctx)
  40. if err != nil {
  41. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  42. }
  43. return &CtrContractService{
  44. Dao: dao.NewCtrContractDao(tenant),
  45. ProjBusinessDao: projdao.NewProjBusinessDao(tenant),
  46. CustomerDao: custdao.NewCustCustomerDao(tenant),
  47. UserDao: sysdao.NewSysUserDao(tenant),
  48. CtrProductDao: dao.NewCtrContractProductDao(tenant),
  49. ProductDao: basedao.NewBaseProductDao(tenant),
  50. DynamicsDao: dao.NewCtrContractDynamicsDao(tenant),
  51. Tenant: tenant,
  52. userInfo: userInfo,
  53. }, nil
  54. }
  55. func (s CtrContractService) Get(ctx context.Context, id int) (*model.CtrContractGetRsp, error) {
  56. ent, err := s.Dao.Where("Id = ?", id).One()
  57. if err != nil {
  58. return nil, err
  59. }
  60. if ent == nil {
  61. return nil, myerrors.NewMsgError(nil, "合同不存在")
  62. }
  63. product, err := s.CtrProductDao.Where("contract_id = ?", id).All()
  64. if err != nil {
  65. return nil, err
  66. }
  67. if product == nil {
  68. product = []*model.CtrContractProduct{}
  69. }
  70. return &model.CtrContractGetRsp{
  71. CtrContract: *ent,
  72. Product: product,
  73. }, nil
  74. }
  75. func (s CtrContractService) DynamicsList(ctx context.Context, req *model.CtrContractDynamicsListReq) (int, interface{}, error) {
  76. dao := &s.DynamicsDao.CtrContractDynamicsDao
  77. if req.SearchText != "" {
  78. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  79. dao = dao.Where("(opn_people LIKE ? || opn_content LIKE ?)", likestr, likestr)
  80. }
  81. if req.ContractId != 0 {
  82. dao = dao.Where("contract_id = ?", req.ContractId)
  83. }
  84. if req.OpnPeopleId != 0 {
  85. dao = dao.Where("opn_people_id = ?", req.OpnPeopleId)
  86. }
  87. if req.OpnPeople != "" {
  88. likestr := fmt.Sprintf("%%%s%%", req.OpnPeople)
  89. dao = dao.Where("opn_people like ?", likestr)
  90. }
  91. if req.OpnType != "" {
  92. dao = dao.Where("opn_type = ?", req.OpnType)
  93. }
  94. // if req.OpnContent != "" {
  95. // likestr := fmt.Sprintf("%%%s%%", req.OpnContent)
  96. // dao = dao.Where("opn_content like ?", likestr)
  97. // }
  98. if req.BeginTime != "" {
  99. dao = dao.Where("created_time > ?", req.BeginTime)
  100. }
  101. if req.EndTime != "" {
  102. dao = dao.Where("created_time < ?", req.EndTime)
  103. }
  104. total, err := dao.Count()
  105. if err != nil {
  106. return 0, nil, err
  107. }
  108. if req.PageNum != 0 {
  109. dao = dao.Page(req.GetPage())
  110. }
  111. orderby := "created_time desc"
  112. if req.OrderBy != "" {
  113. orderby = req.OrderBy
  114. }
  115. dao = dao.Order(orderby)
  116. ents := []*model.CtrContractDynamics{}
  117. err = dao.Structs(&ents)
  118. if err != nil && err != sql.ErrNoRows {
  119. return 0, nil, err
  120. }
  121. ret := map[string][]*model.CtrContractDynamics{}
  122. for _, ent := range ents {
  123. date := ent.OpnDate.Format("Y-m-d")
  124. ret[date] = append(ret[date], ent)
  125. }
  126. return total, ret, err
  127. }
  128. func (s CtrContractService) List(ctx context.Context, req *model.CtrContractListReq) (int, []*model.CtrContract, error) {
  129. dao := &s.Dao.CtrContractDao
  130. if req.SearchText != "" {
  131. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  132. dao = dao.Where("(contract_code LIKE ? || contract_name LIKE ? || cust_name LIKE ? || nbo_name LIKE ?)", likestr, likestr, likestr, likestr)
  133. }
  134. if req.ContractCode != "" {
  135. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  136. dao = dao.Where("contract_code like ?", likestr)
  137. }
  138. if req.ContractName != "" {
  139. likestr := fmt.Sprintf("%%%s%%", req.ContractName)
  140. dao = dao.Where("contract_name like ?", likestr)
  141. }
  142. if req.CustId != 0 {
  143. dao = dao.Where("cust_id = ?", req.CustId)
  144. }
  145. if req.CustName != "" {
  146. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  147. dao = dao.Where("cust_name like ?", likestr)
  148. }
  149. if req.NboId != 0 {
  150. dao = dao.Where("nbo_id = ?", req.NboId)
  151. }
  152. if req.NboName != "" {
  153. likestr := fmt.Sprintf("%%%s%%", req.NboName)
  154. dao = dao.Where("nbo_name like ?", likestr)
  155. }
  156. if req.ApproStatus != "" {
  157. dao = dao.Where("appro_status = ?", req.ApproStatus)
  158. }
  159. if req.ContractType != "" {
  160. dao = dao.Where("contract_type = ?", req.ContractType)
  161. }
  162. // if req.ContractStartTime != nil {
  163. // dao = dao.Where("contract_start_time > ?", req.ContractStartTime)
  164. // }
  165. // if req.ContractEndTime != nil {
  166. // dao = dao.Where("contract_end_time < ?", req.ContractEndTime)
  167. // }
  168. if req.InchargeId != 0 {
  169. dao = dao.Where("incharge_id = ?", req.InchargeId)
  170. }
  171. if req.InchargeName != "" {
  172. likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
  173. dao = dao.Where("incharge_name like ?", likestr)
  174. }
  175. if req.SignatoryId != 0 {
  176. dao = dao.Where("signatory_id = ?", req.SignatoryId)
  177. }
  178. if req.SignatoryName != "" {
  179. likestr := fmt.Sprintf("%%%s%%", req.SignatoryName)
  180. dao = dao.Where("signatory_name like ?", likestr)
  181. }
  182. if req.DistributorId != 0 {
  183. dao = dao.Where("distributor_id = ?", req.DistributorId)
  184. }
  185. if req.DistributorName != "" {
  186. likestr := fmt.Sprintf("%%%s%%", req.DistributorName)
  187. dao = dao.Where("distributor_name like ?", likestr)
  188. }
  189. if req.BeginTime != "" {
  190. dao = dao.Where("created_time > ?", req.BeginTime)
  191. }
  192. if req.EndTime != "" {
  193. dao = dao.Where("created_time < ?", req.EndTime)
  194. }
  195. total, err := dao.Count()
  196. if err != nil {
  197. return 0, nil, err
  198. }
  199. if req.PageNum != 0 {
  200. dao = dao.Page(req.GetPage())
  201. }
  202. orderby := "created_time desc"
  203. if req.OrderBy != "" {
  204. orderby = req.OrderBy
  205. }
  206. dao = dao.Order(orderby)
  207. ents := []*model.CtrContract{}
  208. err = dao.Structs(&ents)
  209. if err != nil && err != sql.ErrNoRows {
  210. return 0, nil, err
  211. }
  212. return total, ents, err
  213. }
  214. func (s CtrContractService) BindProduct(tx *gdb.TX, id int, product []model.CtrAddProduct) error {
  215. var amount float64
  216. for _, p := range product {
  217. amount += p.TranPrice
  218. }
  219. _, err := tx.Delete("ctr_contract_product", "contract_id = ?", id)
  220. if err != nil {
  221. return err
  222. }
  223. tocreate := []model.CtrContractProduct{}
  224. for _, p := range product {
  225. product, err := s.ProductDao.Where("id = ?", p.ProdId).One()
  226. if err != nil {
  227. return err
  228. }
  229. if product == nil {
  230. return myerrors.NewMsgError(nil, fmt.Sprintf("产品: %d 不存在", p.ProdId))
  231. }
  232. tocreate = append(tocreate, model.CtrContractProduct{
  233. ContractId: id,
  234. ProdId: p.ProdId,
  235. ProdCode: product.ProdCode,
  236. ProdName: product.ProdName,
  237. ProdClass: product.ProdClass,
  238. ProdNum: p.ProdNum,
  239. MaintTerm: p.MaintTerm,
  240. SugSalesPrice: p.SugSalesPrice,
  241. TranPrice: p.TranPrice,
  242. ContractPrive: amount,
  243. Remark: p.Remark,
  244. CreatedBy: int(s.userInfo.Id),
  245. CreatedName: s.userInfo.NickName,
  246. CreatedTime: gtime.Now(),
  247. UpdatedBy: int(s.userInfo.Id),
  248. UpdatedName: s.userInfo.NickName,
  249. UpdatedTime: gtime.Now(),
  250. DeletedTime: gtime.Now(),
  251. })
  252. }
  253. if len(tocreate) != 0 {
  254. _, err = tx.Insert("ctr_contract_product", tocreate)
  255. if err != nil {
  256. return err
  257. }
  258. }
  259. return nil
  260. }
  261. func (s CtrContractService) AddDynamicsByCurrentUser(tx *gdb.TX, contractId int, opnType string, content map[string]interface{}) error {
  262. contentByte, err := json.Marshal(content)
  263. if err != nil {
  264. return err
  265. }
  266. _, err = tx.InsertAndGetId("ctr_contract_dynamics", model.CtrContractDynamics{
  267. ContractId: contractId,
  268. OpnPeopleId: s.userInfo.Id,
  269. OpnPeople: s.userInfo.NickName,
  270. OpnDate: gtime.Now(),
  271. OpnType: opnType,
  272. OpnContent: string(contentByte),
  273. Remark: "",
  274. CreatedBy: s.userInfo.Id,
  275. CreatedName: s.userInfo.NickName,
  276. CreatedTime: gtime.Now(),
  277. UpdatedBy: s.userInfo.Id,
  278. UpdatedName: s.userInfo.NickName,
  279. UpdatedTime: gtime.Now(),
  280. })
  281. return err
  282. }
  283. func (s CtrContractService) Add(ctx context.Context, req *model.CtrContractAddReq) (int, error) {
  284. validErr := gvalid.CheckStruct(ctx, req, nil)
  285. if validErr != nil {
  286. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  287. }
  288. if req.ContractCode == "" {
  289. req.ContractCode = "HT" + time.Now().Format("20060102150405")
  290. }
  291. c, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
  292. if err != nil {
  293. return 0, err
  294. }
  295. if c != nil {
  296. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
  297. }
  298. c, err = s.Dao.Where("contract_name = ?", req.ContractName).One()
  299. if err != nil {
  300. return 0, err
  301. }
  302. if c != nil {
  303. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
  304. }
  305. nbo, err := s.ProjBusinessDao.Where("id = ?", req.NboId).One()
  306. if err != nil {
  307. return 0, err
  308. }
  309. if nbo == nil {
  310. return 0, myerrors.NewMsgError(nil, "项目不存在")
  311. }
  312. var contractAmount float64
  313. for _, p := range req.Product {
  314. contractAmount += p.TranPrice
  315. }
  316. ctr := model.CtrContract{
  317. ContractCode: req.ContractCode,
  318. ContractName: req.ContractName,
  319. CustId: nbo.CustId,
  320. CustName: nbo.CustName,
  321. NboId: nbo.Id,
  322. NboName: nbo.NboName,
  323. ApproStatus: "",
  324. ContractType: req.ContractType,
  325. ContractAmount: contractAmount,
  326. InvoiceAmount: 0,
  327. CollectedAmount: 0,
  328. ContractStartTime: req.ContractStartTime,
  329. ContractEndTime: req.ContractEndTime,
  330. InchargeId: req.InchargeId,
  331. InchargeName: req.InchargeName,
  332. SignatoryId: req.SignatoryId,
  333. SignatoryName: req.SignatoryName,
  334. CustSignatoryId: req.CustSignatoryId,
  335. CustSignatoryName: req.CustSignatoryName,
  336. DistributorId: req.DistributorId,
  337. DistributorName: req.DistributorName,
  338. Remark: req.Remark,
  339. CreatedBy: int(s.userInfo.Id),
  340. CreatedName: s.userInfo.NickName,
  341. CreatedTime: gtime.Now(),
  342. UpdatedBy: int(s.userInfo.Id),
  343. UpdatedName: s.userInfo.NickName,
  344. UpdatedTime: gtime.Now(),
  345. DeletedTime: gtime.Now(),
  346. }
  347. var id int
  348. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  349. ctrid, err := tx.InsertAndGetId("ctr_contract", ctr)
  350. if err != nil {
  351. return err
  352. }
  353. err = s.BindProduct(tx, int(ctrid), req.Product)
  354. if err != nil {
  355. return err
  356. }
  357. err = s.AddDynamicsByCurrentUser(tx, int(ctrid), "创建合同", map[string]interface{}{})
  358. if err != nil {
  359. return err
  360. }
  361. id = int(ctrid)
  362. return nil
  363. })
  364. return id, txerr
  365. }
  366. func (s CtrContractService) Update(ctx context.Context, req *model.CtrContractUpdateReq) error {
  367. validErr := gvalid.CheckStruct(ctx, req, nil)
  368. if validErr != nil {
  369. return myerrors.NewMsgError(nil, validErr.Current().Error())
  370. }
  371. ent, err := s.Dao.Where("id = ?", req.Id).One()
  372. if err != nil {
  373. return err
  374. }
  375. if ent == nil {
  376. return myerrors.NewMsgError(nil, fmt.Sprintf("合同不存在: %d", req.Id))
  377. }
  378. // if req.ContractCode != "" {
  379. // exist, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
  380. // if err != nil {
  381. // return err
  382. // }
  383. // if exist != nil && exist.Id != req.Id {
  384. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
  385. // }
  386. // }
  387. if req.ContractName != "" {
  388. exist, err := s.Dao.Where("contract_name = ?", req.ContractName).One()
  389. if err != nil {
  390. return err
  391. }
  392. if exist != nil && exist.Id != req.Id {
  393. return myerrors.NewMsgError(nil, fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
  394. }
  395. }
  396. var nbo *proj.ProjBusiness
  397. if req.NboId != 0 {
  398. nbo, err = s.ProjBusinessDao.Where("id = ?", req.NboId).One()
  399. if err != nil {
  400. return err
  401. }
  402. if nbo == nil {
  403. return myerrors.NewMsgError(nil, "项目不存在")
  404. }
  405. }
  406. toupdate := map[string]interface{}{}
  407. // if req.ContractCode != "" {
  408. // toupdate["contract_code"] = req.ContractCode
  409. // }
  410. if req.ContractName != "" {
  411. toupdate["contract_name"] = req.ContractName
  412. }
  413. if req.NboId != 0 {
  414. toupdate["cust_id"] = nbo.CustId
  415. toupdate["cust_name"] = nbo.CustName
  416. toupdate["nbo_id"] = nbo.Id
  417. toupdate["nbo_name"] = nbo.NboName
  418. }
  419. // if req.ApproStatus != "" {
  420. // toupdate["appro_status"] = req.ApproStatus
  421. // }
  422. if req.ContractType != "" {
  423. toupdate["contract_type"] = req.ContractType
  424. }
  425. // if req.ContractAmount != 0 {
  426. // toupdate["contract_amount"] = req.ContractAmount
  427. // }
  428. // if req.InvoiceAmount != 0 {
  429. // toupdate["invoice_amount"] = req.InvoiceAmount
  430. // }
  431. // if req.CollectedAmount != 0 {
  432. // toupdate["collected_amount"] = req.CollectedAmount
  433. // }
  434. if req.ContractStartTime != nil {
  435. toupdate["contract_start_time"] = req.ContractStartTime
  436. }
  437. if req.ContractEndTime != nil {
  438. toupdate["contract_end_time"] = req.ContractEndTime
  439. }
  440. if req.InchargeId != 0 {
  441. toupdate["incharge_id"] = req.InchargeId
  442. }
  443. if req.InchargeName != "" {
  444. toupdate["incharge_name"] = req.InchargeName
  445. }
  446. if req.SignatoryId != 0 {
  447. toupdate["signatory_id"] = req.SignatoryId
  448. }
  449. if req.SignatoryName != "" {
  450. toupdate["signatory_name"] = req.SignatoryName
  451. }
  452. if req.CustSignatoryId != 0 {
  453. toupdate["cust_signatory_id"] = req.CustSignatoryId
  454. }
  455. if req.CustSignatoryName != "" {
  456. toupdate["cust_signatory_name"] = req.CustSignatoryName
  457. }
  458. if req.DistributorId != 0 {
  459. toupdate["distributor_id"] = req.DistributorId
  460. }
  461. if req.DistributorName != "" {
  462. toupdate["distributor_name"] = req.DistributorName
  463. }
  464. if req.Remark != nil {
  465. toupdate["remark"] = *req.Remark
  466. }
  467. if req.Product != nil {
  468. var contractAmount float64
  469. for _, p := range *req.Product {
  470. contractAmount += p.TranPrice
  471. }
  472. toupdate["contract_amount"] = contractAmount
  473. }
  474. if len(toupdate) != 0 {
  475. toupdate["updated_by"] = int(s.userInfo.Id)
  476. toupdate["updated_name"] = s.userInfo.NickName
  477. toupdate["updated_time"] = gtime.Now()
  478. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  479. _, err = tx.Update("ctr_contract", toupdate, "id = ?", req.Id)
  480. if err != nil {
  481. return err
  482. }
  483. if req.Product != nil {
  484. err = s.BindProduct(tx, req.Id, *req.Product)
  485. if err != nil {
  486. return err
  487. }
  488. }
  489. return nil
  490. })
  491. if txerr != nil {
  492. return txerr
  493. }
  494. }
  495. return nil
  496. }
  497. func (s CtrContractService) Transfer(ctx context.Context, req *model.CtrContractTransferReq) error {
  498. if len(req.Id) == 0 {
  499. return nil
  500. }
  501. ents := map[int]*model.CtrContract{}
  502. for _, i := range req.Id {
  503. ent, err := s.Dao.Where("id = ?", i).One()
  504. if err != nil {
  505. return err
  506. }
  507. if ent == nil {
  508. return myerrors.NewMsgError(nil, fmt.Sprintf("合同不存在: %d", req.Id))
  509. }
  510. ents[ent.Id] = ent
  511. }
  512. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  513. toupdate := map[string]interface{}{
  514. "incharge_id": req.InchargeId,
  515. "incharge_name": req.InchargeName,
  516. }
  517. _, err := tx.Update("ctr_contract", toupdate, "id in (?)", req.Id)
  518. if err != nil {
  519. return err
  520. }
  521. for _, ent := range ents {
  522. err = s.AddDynamicsByCurrentUser(tx, ent.Id, "转移合同", map[string]interface{}{
  523. "toInchargeId": req.InchargeId,
  524. "toInchargeName": req.InchargeName,
  525. "fromInchargeId": ent.InchargeId,
  526. "fromInchargeName": ent.InchargeName,
  527. "operatedId": s.userInfo.Id,
  528. "operatedName": s.userInfo.NickName,
  529. })
  530. if err != nil {
  531. return err
  532. }
  533. }
  534. return nil
  535. })
  536. if txerr != nil {
  537. return txerr
  538. }
  539. return nil
  540. }
  541. func (s CtrContractService) UpdateInvoiceAmount(tx *gdb.TX, id int) error {
  542. ctr := model.CtrContract{}
  543. err := tx.GetStruct(&ctr, "select * from ctr_contract where id = ?", id)
  544. if err == sql.ErrNoRows {
  545. return myerrors.NewMsgError(err, fmt.Sprintf("合同不存在 %d", id))
  546. }
  547. if err != nil {
  548. return err
  549. }
  550. v, err := tx.GetValue("select sum(invoice_amount) from ctr_contract_invoice where contract_id=? and appro_status='20' and deleted_time is null", id)
  551. if err != nil {
  552. return err
  553. }
  554. amount := v.Float64()
  555. _, err = tx.Update("ctr_contract",
  556. map[string]interface{}{
  557. "invoice_amount": amount,
  558. }, "id = ?", id)
  559. if err != nil {
  560. return err
  561. }
  562. return nil
  563. }
  564. func (s CtrContractService) UpdateCollectedAmount(tx *gdb.TX, id int) error {
  565. ctr := model.CtrContract{}
  566. err := tx.GetStruct(&ctr, "select * from ctr_contract where id = ?", id)
  567. if err == sql.ErrNoRows {
  568. return myerrors.NewMsgError(err, fmt.Sprintf("合同不存在 %d", id))
  569. }
  570. if err != nil {
  571. return err
  572. }
  573. v, err := tx.GetValue("select sum(collection_amount) from ctr_contract_collection where contract_id=? and appro_status='20' and deleted_time is null", id)
  574. if err != nil {
  575. return err
  576. }
  577. amount := v.Float64()
  578. _, err = tx.Update("ctr_contract",
  579. map[string]interface{}{
  580. "collected_amount": amount,
  581. }, "id = ?", id)
  582. if err != nil {
  583. return err
  584. }
  585. return nil
  586. }
  587. func (s CtrContractService) Delete(ctx context.Context, id []int) error {
  588. if len(id) == 0 {
  589. return nil
  590. }
  591. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  592. return err
  593. }