ctr_contract.go 18 KB

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