ctr_contract_collection.go 8.3 KB

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