ctr_contract.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. DeletedTime: gtime.Now(),
  252. })
  253. }
  254. if len(tocreate) != 0 {
  255. _, err = tx.Insert("ctr_contract_product", tocreate)
  256. if err != nil {
  257. return err
  258. }
  259. }
  260. return nil
  261. }
  262. func (s CtrContractService) AddDynamicsByCurrentUser(tx *gdb.TX, contractId int, opnType string, content map[string]interface{}) error {
  263. contentByte, err := json.Marshal(content)
  264. if err != nil {
  265. return err
  266. }
  267. _, err = tx.InsertAndGetId("ctr_contract_dynamics", model.CtrContractDynamics{
  268. ContractId: contractId,
  269. OpnPeopleId: s.userInfo.Id,
  270. OpnPeople: s.userInfo.NickName,
  271. OpnDate: gtime.Now(),
  272. OpnType: opnType,
  273. OpnContent: string(contentByte),
  274. Remark: "",
  275. CreatedBy: s.userInfo.Id,
  276. CreatedName: s.userInfo.NickName,
  277. CreatedTime: gtime.Now(),
  278. UpdatedBy: s.userInfo.Id,
  279. UpdatedName: s.userInfo.NickName,
  280. UpdatedTime: gtime.Now(),
  281. })
  282. return err
  283. }
  284. func (s CtrContractService) Add(ctx context.Context, req *model.CtrContractAddReq) (int, error) {
  285. validErr := gvalid.CheckStruct(ctx, req, nil)
  286. if validErr != nil {
  287. return 0, myerrors.TipsError(validErr.Current().Error())
  288. }
  289. if req.ContractCode == "" {
  290. req.ContractCode = "HT" + time.Now().Format("20060102150405")
  291. }
  292. c, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
  293. if err != nil {
  294. return 0, err
  295. }
  296. if c != nil {
  297. return 0, myerrors.TipsError(fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
  298. }
  299. c, err = s.Dao.Where("contract_name = ?", req.ContractName).One()
  300. if err != nil {
  301. return 0, err
  302. }
  303. if c != nil {
  304. return 0, myerrors.TipsError(fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
  305. }
  306. nbo, err := s.ProjBusinessDao.Where("id = ?", req.NboId).One()
  307. if err != nil {
  308. return 0, err
  309. }
  310. if nbo == nil {
  311. return 0, myerrors.TipsError("项目不存在")
  312. }
  313. var contractAmount float64
  314. for _, p := range req.Product {
  315. contractAmount += p.TranPrice
  316. }
  317. ctr := model.CtrContract{
  318. ContractCode: req.ContractCode,
  319. ContractName: req.ContractName,
  320. CustId: nbo.CustId,
  321. CustName: nbo.CustName,
  322. NboId: nbo.Id,
  323. NboName: nbo.NboName,
  324. ApproStatus: "",
  325. ContractType: req.ContractType,
  326. ContractAmount: contractAmount,
  327. InvoiceAmount: 0,
  328. CollectedAmount: 0,
  329. ContractStartTime: req.ContractStartTime,
  330. ContractEndTime: req.ContractEndTime,
  331. InchargeId: req.InchargeId,
  332. InchargeName: req.InchargeName,
  333. SignatoryId: req.SignatoryId,
  334. SignatoryName: req.SignatoryName,
  335. CustSignatoryId: req.CustSignatoryId,
  336. CustSignatoryName: req.CustSignatoryName,
  337. DistributorId: req.DistributorId,
  338. DistributorName: req.DistributorName,
  339. Remark: req.Remark,
  340. CreatedBy: int(s.userInfo.Id),
  341. CreatedName: s.userInfo.NickName,
  342. CreatedTime: gtime.Now(),
  343. UpdatedBy: int(s.userInfo.Id),
  344. UpdatedName: s.userInfo.NickName,
  345. UpdatedTime: gtime.Now(),
  346. DeletedTime: gtime.Now(),
  347. }
  348. var id int
  349. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  350. ctrid, err := tx.InsertAndGetId("ctr_contract", ctr)
  351. if err != nil {
  352. return err
  353. }
  354. err = s.BindProduct(tx, int(ctrid), req.Product)
  355. if err != nil {
  356. return err
  357. }
  358. err = s.AddDynamicsByCurrentUser(tx, int(ctrid), "创建合同", map[string]interface{}{})
  359. if err != nil {
  360. return err
  361. }
  362. id = int(ctrid)
  363. return nil
  364. })
  365. return id, txerr
  366. }
  367. func (s CtrContractService) Update(ctx context.Context, req *model.CtrContractUpdateReq) error {
  368. validErr := gvalid.CheckStruct(ctx, req, nil)
  369. if validErr != nil {
  370. return myerrors.TipsError(validErr.Current().Error())
  371. }
  372. ent, err := s.Dao.Where("id = ?", req.Id).One()
  373. if err != nil {
  374. return err
  375. }
  376. if ent == nil {
  377. return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.Id))
  378. }
  379. // if req.ContractCode != "" {
  380. // exist, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
  381. // if err != nil {
  382. // return err
  383. // }
  384. // if exist != nil && exist.Id != req.Id {
  385. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
  386. // }
  387. // }
  388. if req.ContractName != "" {
  389. exist, err := s.Dao.Where("contract_name = ?", req.ContractName).One()
  390. if err != nil {
  391. return err
  392. }
  393. if exist != nil && exist.Id != req.Id {
  394. return myerrors.TipsError(fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
  395. }
  396. }
  397. var nbo *proj.ProjBusiness
  398. if req.NboId != 0 {
  399. nbo, err = s.ProjBusinessDao.Where("id = ?", req.NboId).One()
  400. if err != nil {
  401. return err
  402. }
  403. if nbo == nil {
  404. return myerrors.TipsError("项目不存在")
  405. }
  406. }
  407. toupdate := map[string]interface{}{}
  408. // if req.ContractCode != "" {
  409. // toupdate["contract_code"] = req.ContractCode
  410. // }
  411. if req.ContractName != "" {
  412. toupdate["contract_name"] = req.ContractName
  413. }
  414. if req.NboId != 0 {
  415. toupdate["cust_id"] = nbo.CustId
  416. toupdate["cust_name"] = nbo.CustName
  417. toupdate["nbo_id"] = nbo.Id
  418. toupdate["nbo_name"] = nbo.NboName
  419. }
  420. // if req.ApproStatus != "" {
  421. // toupdate["appro_status"] = req.ApproStatus
  422. // }
  423. if req.ContractType != "" {
  424. toupdate["contract_type"] = req.ContractType
  425. }
  426. // if req.ContractAmount != 0 {
  427. // toupdate["contract_amount"] = req.ContractAmount
  428. // }
  429. // if req.InvoiceAmount != 0 {
  430. // toupdate["invoice_amount"] = req.InvoiceAmount
  431. // }
  432. // if req.CollectedAmount != 0 {
  433. // toupdate["collected_amount"] = req.CollectedAmount
  434. // }
  435. if req.ContractStartTime != nil {
  436. toupdate["contract_start_time"] = req.ContractStartTime
  437. }
  438. if req.ContractEndTime != nil {
  439. toupdate["contract_end_time"] = req.ContractEndTime
  440. }
  441. if req.InchargeId != 0 {
  442. toupdate["incharge_id"] = req.InchargeId
  443. }
  444. if req.InchargeName != "" {
  445. toupdate["incharge_name"] = req.InchargeName
  446. }
  447. if req.SignatoryId != 0 {
  448. toupdate["signatory_id"] = req.SignatoryId
  449. }
  450. if req.SignatoryName != "" {
  451. toupdate["signatory_name"] = req.SignatoryName
  452. }
  453. if req.CustSignatoryId != 0 {
  454. toupdate["cust_signatory_id"] = req.CustSignatoryId
  455. }
  456. if req.CustSignatoryName != "" {
  457. toupdate["cust_signatory_name"] = req.CustSignatoryName
  458. }
  459. if req.DistributorId != 0 {
  460. toupdate["distributor_id"] = req.DistributorId
  461. }
  462. if req.DistributorName != "" {
  463. toupdate["distributor_name"] = req.DistributorName
  464. }
  465. if req.Remark != nil {
  466. toupdate["remark"] = *req.Remark
  467. }
  468. if req.Product != nil {
  469. var contractAmount float64
  470. for _, p := range *req.Product {
  471. contractAmount += p.TranPrice
  472. }
  473. toupdate["contract_amount"] = contractAmount
  474. }
  475. if len(toupdate) != 0 {
  476. toupdate["updated_by"] = int(s.userInfo.Id)
  477. toupdate["updated_name"] = s.userInfo.NickName
  478. toupdate["updated_time"] = gtime.Now()
  479. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  480. _, err = tx.Update("ctr_contract", toupdate, "id = ?", req.Id)
  481. if err != nil {
  482. return err
  483. }
  484. if req.Product != nil {
  485. err = s.BindProduct(tx, req.Id, *req.Product)
  486. if err != nil {
  487. return err
  488. }
  489. }
  490. return nil
  491. })
  492. if txerr != nil {
  493. return txerr
  494. }
  495. }
  496. return nil
  497. }
  498. func (s CtrContractService) Transfer(ctx context.Context, req *model.CtrContractTransferReq) error {
  499. if len(req.Id) == 0 {
  500. return nil
  501. }
  502. ents := map[int]*model.CtrContract{}
  503. for _, i := range req.Id {
  504. ent, err := s.Dao.Where("id = ?", i).One()
  505. if err != nil {
  506. return err
  507. }
  508. if ent == nil {
  509. return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.Id))
  510. }
  511. ents[ent.Id] = ent
  512. }
  513. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  514. toupdate := map[string]interface{}{
  515. "incharge_id": req.InchargeId,
  516. "incharge_name": req.InchargeName,
  517. }
  518. _, err := tx.Update("ctr_contract", toupdate, "id in (?)", req.Id)
  519. if err != nil {
  520. return err
  521. }
  522. for _, ent := range ents {
  523. err = s.AddDynamicsByCurrentUser(tx, ent.Id, "转移合同", map[string]interface{}{
  524. "toInchargeId": req.InchargeId,
  525. "toInchargeName": req.InchargeName,
  526. "fromInchargeId": ent.InchargeId,
  527. "fromInchargeName": ent.InchargeName,
  528. "operatedId": s.userInfo.Id,
  529. "operatedName": s.userInfo.NickName,
  530. })
  531. if err != nil {
  532. return err
  533. }
  534. }
  535. return nil
  536. })
  537. if txerr != nil {
  538. return txerr
  539. }
  540. return nil
  541. }
  542. func (s CtrContractService) UpdateInvoiceAmount(tx *gdb.TX, id int) error {
  543. ctr := model.CtrContract{}
  544. err := tx.GetStruct(&ctr, "select * from ctr_contract where id = ?", id)
  545. if err == sql.ErrNoRows {
  546. return myerrors.TipsError(fmt.Sprintf("合同不存在 %d", id))
  547. }
  548. if err != nil {
  549. return err
  550. }
  551. 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)
  552. if err != nil {
  553. return err
  554. }
  555. amount := v.Float64()
  556. _, err = tx.Update("ctr_contract",
  557. map[string]interface{}{
  558. "invoice_amount": amount,
  559. }, "id = ?", id)
  560. if err != nil {
  561. return err
  562. }
  563. return nil
  564. }
  565. func (s CtrContractService) UpdateCollectedAmount(tx *gdb.TX, id int) error {
  566. ctr := model.CtrContract{}
  567. err := tx.GetStruct(&ctr, "select * from ctr_contract where id = ?", id)
  568. if err == sql.ErrNoRows {
  569. return myerrors.TipsError(fmt.Sprintf("合同不存在 %d", id))
  570. }
  571. if err != nil {
  572. return err
  573. }
  574. 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)
  575. if err != nil {
  576. return err
  577. }
  578. amount := v.Float64()
  579. _, err = tx.Update("ctr_contract",
  580. map[string]interface{}{
  581. "collected_amount": amount,
  582. }, "id = ?", id)
  583. if err != nil {
  584. return err
  585. }
  586. return nil
  587. }
  588. func (s CtrContractService) Delete(ctx context.Context, id []int) error {
  589. if len(id) == 0 {
  590. return nil
  591. }
  592. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  593. return err
  594. }