ctr_contract_collection.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. dao "dashoo.cn/micro/app/dao/contract"
  7. model "dashoo.cn/micro/app/model/contract"
  8. "dashoo.cn/opms_libary/micro_srv"
  9. "dashoo.cn/opms_libary/myerrors"
  10. "dashoo.cn/opms_libary/request"
  11. "github.com/gogf/gf/database/gdb"
  12. "github.com/gogf/gf/os/gtime"
  13. "github.com/gogf/gf/util/gvalid"
  14. )
  15. type CtrContractCollectionService struct {
  16. Dao *dao.CtrContractCollectionDao
  17. ContractDao *dao.CtrContractDao
  18. planSrv *CtrContractCollectionPlanService
  19. ctrSrv *CtrContractService
  20. Tenant string
  21. userInfo request.UserInfo
  22. }
  23. func NewCtrContractCollectionService(ctx context.Context) (*CtrContractCollectionService, error) {
  24. tenant, err := micro_srv.GetTenant(ctx)
  25. if err != nil {
  26. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  27. }
  28. // 获取用户信息
  29. userInfo, err := micro_srv.GetUserInfo(ctx)
  30. if err != nil {
  31. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  32. }
  33. planSrv, err := NewCtrContractCollectionPlanService(ctx)
  34. if err != nil {
  35. return nil, err
  36. }
  37. ctrSrv, err := NewCtrContractService(ctx)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &CtrContractCollectionService{
  42. Dao: dao.NewCtrContractCollectionDao(tenant),
  43. ContractDao: dao.NewCtrContractDao(tenant),
  44. planSrv: planSrv,
  45. ctrSrv: ctrSrv,
  46. Tenant: tenant,
  47. userInfo: userInfo,
  48. }, nil
  49. }
  50. func (s CtrContractCollectionService) List(ctx context.Context, req *model.CtrContractCollectionListReq) (int, []*model.CtrContractCollection, error) {
  51. dao := &s.Dao.CtrContractCollectionDao
  52. if req.SearchText != "" {
  53. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  54. dao = dao.Where("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr)
  55. }
  56. if req.PlanId != 0 {
  57. dao = dao.Where("plan_id = ?", req.PlanId)
  58. }
  59. if req.CustId != 0 {
  60. dao = dao.Where("cust_id = ?", req.CustId)
  61. }
  62. if req.CustName != "" {
  63. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  64. dao = dao.Where("cust_name like ?", likestr)
  65. }
  66. if req.ContractId != 0 {
  67. dao = dao.Where("contract_id = ?", req.ContractId)
  68. }
  69. if req.ContractCode != "" {
  70. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  71. dao = dao.Where("contract_code like ?", likestr)
  72. }
  73. if req.CollectionType != "" {
  74. dao = dao.Where("collection_type = ?", req.CollectionType)
  75. }
  76. if req.ApproStatus != "" {
  77. dao = dao.Where("appro_status = ?", req.ApproStatus)
  78. }
  79. if req.BeginTime != "" {
  80. dao = dao.Where("created_time > ?", req.BeginTime)
  81. }
  82. if req.EndTime != "" {
  83. dao = dao.Where("created_time < ?", req.EndTime)
  84. }
  85. total, err := dao.Count()
  86. if err != nil {
  87. return 0, nil, err
  88. }
  89. if req.PageNum != 0 {
  90. dao = dao.Page(req.GetPage())
  91. }
  92. orderby := "created_time desc"
  93. if req.OrderBy != "" {
  94. orderby = req.OrderBy
  95. }
  96. dao = dao.Order(orderby)
  97. ents := []*model.CtrContractCollection{}
  98. err = dao.Structs(&ents)
  99. if err != nil && err != sql.ErrNoRows {
  100. return 0, nil, err
  101. }
  102. return total, ents, err
  103. }
  104. func (s CtrContractCollectionService) Add(ctx context.Context, req *model.CtrContractCollectionAddReq) (int, error) {
  105. validErr := gvalid.CheckStruct(ctx, req, nil)
  106. if validErr != nil {
  107. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  108. }
  109. c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  110. if err != nil {
  111. return 0, err
  112. }
  113. if c == nil {
  114. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  115. }
  116. ent := model.CtrContractCollection{
  117. PlanId: req.PlanId,
  118. CustId: c.CustId,
  119. CustName: c.CustName,
  120. ContractId: req.ContractId,
  121. ContractCode: c.ContractCode,
  122. CollectionDatetime: req.CollectionDatetime,
  123. CollectionAmount: req.CollectionAmount,
  124. CollectionType: req.CollectionType,
  125. ApproStatus: "10",
  126. ContractAmount: c.ContractAmount,
  127. Remark: req.Remark,
  128. CreatedBy: s.userInfo.Id,
  129. CreatedName: s.userInfo.NickName,
  130. CreatedTime: gtime.Now(),
  131. UpdatedBy: s.userInfo.Id,
  132. UpdatedName: s.userInfo.NickName,
  133. UpdatedTime: gtime.Now(),
  134. DeletedTime: gtime.Now(),
  135. }
  136. var id int
  137. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  138. collectionId, err := tx.InsertAndGetId("ctr_contract_collection", ent)
  139. if err != nil {
  140. return err
  141. }
  142. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款", map[string]interface{}{
  143. "id": collectionId,
  144. "collectionDatetime": req.CollectionDatetime,
  145. "collectionAmount": req.CollectionAmount,
  146. "collectionType": req.CollectionType,
  147. })
  148. if err != nil {
  149. return err
  150. }
  151. id = int(collectionId)
  152. return nil
  153. })
  154. return id, txerr
  155. }
  156. func (s CtrContractCollectionService) UpdateAmount(tx *gdb.TX, id int) error {
  157. ent := model.CtrContractCollection{}
  158. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", id)
  159. if err == sql.ErrNoRows {
  160. return myerrors.NewMsgError(err, fmt.Sprintf("回款不存在 %d", id))
  161. }
  162. if err != nil {
  163. return err
  164. }
  165. if ent.PlanId != 0 {
  166. err = s.planSrv.UpdateCashedAmount(tx, ent.PlanId)
  167. if err != nil {
  168. return err
  169. }
  170. }
  171. return s.ctrSrv.UpdateCollectedAmount(tx, ent.ContractId)
  172. }
  173. func (s CtrContractCollectionService) Update(ctx context.Context, req *model.CtrContractCollectionUpdateReq) error {
  174. validErr := gvalid.CheckStruct(ctx, req, nil)
  175. if validErr != nil {
  176. return myerrors.NewMsgError(nil, validErr.Current().Error())
  177. }
  178. ent, err := s.Dao.Where("id = ?", req.Id).One()
  179. if err != nil {
  180. return err
  181. }
  182. if ent == nil {
  183. return myerrors.NewMsgError(nil, fmt.Sprintf("回款:%d 不存在", req.Id))
  184. }
  185. // var c *model.CtrContract
  186. // if req.ContractId != 0 {
  187. // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  188. // if err != nil {
  189. // return err
  190. // }
  191. // if c == nil {
  192. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  193. // }
  194. // }
  195. toupdate := map[string]interface{}{}
  196. // if req.PlanId != 0 {
  197. // toupdate["plan_id"] = req.PlanId
  198. // }
  199. // if req.ContractId != 0 {
  200. // toupdate["cust_id"] = c.CustId
  201. // toupdate["cust_name"] = c.CustName
  202. // toupdate["contract_id"] = req.ContractId
  203. // toupdate["contract_code"] = c.ContractCode
  204. // toupdate["contract_amount"] = c.ContractAmount
  205. // }
  206. if req.CollectionDatetime != nil {
  207. toupdate["collection_datetime"] = req.CollectionDatetime
  208. }
  209. if req.CollectionAmount != nil {
  210. toupdate["collection_amount"] = *req.CollectionAmount
  211. }
  212. if req.CollectionType != "" {
  213. toupdate["collection_type"] = req.CollectionType
  214. }
  215. if req.ApproStatus != "" {
  216. toupdate["appro_status"] = req.ApproStatus
  217. }
  218. if req.Remark != nil {
  219. toupdate["remark"] = *req.Remark
  220. }
  221. if len(toupdate) != 0 {
  222. toupdate["updated_by"] = s.userInfo.Id
  223. toupdate["updated_name"] = s.userInfo.NickName
  224. toupdate["updated_time"] = gtime.Now()
  225. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  226. _, err = tx.Update("ctr_contract_collection", toupdate, "id= ?", req.Id)
  227. if err != nil {
  228. return err
  229. }
  230. if req.CollectionAmount != nil || req.ApproStatus != "" {
  231. err = s.UpdateAmount(tx, req.Id)
  232. if err != nil {
  233. return err
  234. }
  235. }
  236. return nil
  237. })
  238. if txerr != nil {
  239. return txerr
  240. }
  241. }
  242. return nil
  243. }
  244. func (s CtrContractCollectionService) Delete(ctx context.Context, id []int) error {
  245. if len(id) == 0 {
  246. return nil
  247. }
  248. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  249. planId := map[int]bool{}
  250. contractId := map[int]bool{}
  251. for _, i := range id {
  252. ent := model.CtrContractCollection{}
  253. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", i)
  254. if err == sql.ErrNoRows {
  255. continue
  256. }
  257. if err != nil {
  258. return err
  259. }
  260. if ent.ApproStatus != "20" {
  261. continue
  262. }
  263. contractId[ent.ContractId] = true
  264. if ent.PlanId != 0 {
  265. planId[ent.PlanId] = true
  266. }
  267. }
  268. _, err := tx.Delete("ctr_contract_collection", "id in (?)", id)
  269. if err != nil {
  270. return err
  271. }
  272. for pid := range planId {
  273. err = s.planSrv.UpdateCashedAmount(tx, pid)
  274. if err != nil {
  275. return err
  276. }
  277. }
  278. for cid := range contractId {
  279. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  280. if err != nil {
  281. return err
  282. }
  283. }
  284. return nil
  285. })
  286. }