ctr_contract.go 17 KB

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