ctr_contract_collection.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. }
  137. var id int
  138. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  139. collectionId, err := tx.InsertAndGetId("ctr_contract_collection", ent)
  140. if err != nil {
  141. return err
  142. }
  143. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款", map[string]interface{}{
  144. "id": collectionId,
  145. "collectionDatetime": req.CollectionDatetime,
  146. "collectionAmount": req.CollectionAmount,
  147. "collectionType": req.CollectionType,
  148. })
  149. if err != nil {
  150. return err
  151. }
  152. id = int(collectionId)
  153. return nil
  154. })
  155. return id, txerr
  156. }
  157. func (s CtrContractCollectionService) UpdateAmount(tx *gdb.TX, id int) error {
  158. ent := model.CtrContractCollection{}
  159. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", id)
  160. if err == sql.ErrNoRows {
  161. return myerrors.TipsError(fmt.Sprintf("回款不存在 %d", id))
  162. }
  163. if err != nil {
  164. return err
  165. }
  166. if ent.PlanId != 0 {
  167. err = s.planSrv.UpdateCashedAmount(tx, ent.PlanId)
  168. if err != nil {
  169. return err
  170. }
  171. }
  172. return s.ctrSrv.UpdateCollectedAmount(tx, ent.ContractId)
  173. }
  174. func (s CtrContractCollectionService) Update(ctx context.Context, req *model.CtrContractCollectionUpdateReq) error {
  175. validErr := gvalid.CheckStruct(ctx, req, nil)
  176. if validErr != nil {
  177. return myerrors.TipsError(validErr.Current().Error())
  178. }
  179. ent, err := s.Dao.Where("id = ?", req.Id).One()
  180. if err != nil {
  181. return err
  182. }
  183. if ent == nil {
  184. return myerrors.TipsError(fmt.Sprintf("回款:%d 不存在", req.Id))
  185. }
  186. // var c *model.CtrContract
  187. // if req.ContractId != 0 {
  188. // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  189. // if err != nil {
  190. // return err
  191. // }
  192. // if c == nil {
  193. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  194. // }
  195. // }
  196. toupdate := map[string]interface{}{}
  197. // if req.PlanId != 0 {
  198. // toupdate["plan_id"] = req.PlanId
  199. // }
  200. // if req.ContractId != 0 {
  201. // toupdate["cust_id"] = c.CustId
  202. // toupdate["cust_name"] = c.CustName
  203. // toupdate["contract_id"] = req.ContractId
  204. // toupdate["contract_code"] = c.ContractCode
  205. // toupdate["contract_amount"] = c.ContractAmount
  206. // }
  207. if req.CollectionDatetime != nil {
  208. toupdate["collection_datetime"] = req.CollectionDatetime
  209. }
  210. if req.CollectionAmount != nil {
  211. toupdate["collection_amount"] = *req.CollectionAmount
  212. }
  213. if req.CollectionType != "" {
  214. toupdate["collection_type"] = req.CollectionType
  215. }
  216. if req.ApproStatus != "" {
  217. toupdate["appro_status"] = req.ApproStatus
  218. }
  219. if req.Remark != nil {
  220. toupdate["remark"] = *req.Remark
  221. }
  222. if len(toupdate) != 0 {
  223. toupdate["updated_by"] = s.userInfo.Id
  224. toupdate["updated_name"] = s.userInfo.NickName
  225. toupdate["updated_time"] = gtime.Now()
  226. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  227. _, err = tx.Update("ctr_contract_collection", toupdate, "id= ?", req.Id)
  228. if err != nil {
  229. return err
  230. }
  231. if req.CollectionAmount != nil || req.ApproStatus != "" {
  232. err = s.UpdateAmount(tx, req.Id)
  233. if err != nil {
  234. return err
  235. }
  236. }
  237. return nil
  238. })
  239. if txerr != nil {
  240. return txerr
  241. }
  242. }
  243. return nil
  244. }
  245. func (s CtrContractCollectionService) Delete(ctx context.Context, id []int) error {
  246. if len(id) == 0 {
  247. return nil
  248. }
  249. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  250. planId := map[int]bool{}
  251. contractId := map[int]bool{}
  252. for _, i := range id {
  253. ent := model.CtrContractCollection{}
  254. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", i)
  255. if err == sql.ErrNoRows {
  256. continue
  257. }
  258. if err != nil {
  259. return err
  260. }
  261. if ent.ApproStatus != "20" {
  262. continue
  263. }
  264. contractId[ent.ContractId] = true
  265. if ent.PlanId != 0 {
  266. planId[ent.PlanId] = true
  267. }
  268. }
  269. _, err := tx.Delete("ctr_contract_collection", "id in (?)", id)
  270. if err != nil {
  271. return err
  272. }
  273. for pid := range planId {
  274. err = s.planSrv.UpdateCashedAmount(tx, pid)
  275. if err != nil {
  276. return err
  277. }
  278. }
  279. for cid := range contractId {
  280. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  281. if err != nil {
  282. return err
  283. }
  284. }
  285. return nil
  286. })
  287. }