ctr_contract_collection.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.BeginTime != "" {
  77. dao = dao.Where("created_time > ?", req.BeginTime)
  78. }
  79. if req.EndTime != "" {
  80. dao = dao.Where("created_time < ?", req.EndTime)
  81. }
  82. total, err := dao.Count()
  83. if err != nil {
  84. return 0, nil, err
  85. }
  86. if req.PageNum != 0 {
  87. dao = dao.Page(req.GetPage())
  88. }
  89. orderby := "created_time desc"
  90. if req.OrderBy != "" {
  91. orderby = req.OrderBy
  92. }
  93. dao = dao.Order(orderby)
  94. ents := []*model.CtrContractCollection{}
  95. err = dao.Structs(&ents)
  96. if err != nil && err != sql.ErrNoRows {
  97. return 0, nil, err
  98. }
  99. return total, ents, err
  100. }
  101. func (s CtrContractCollectionService) Add(ctx context.Context, req *model.CtrContractCollectionAddReq) (int, error) {
  102. validErr := gvalid.CheckStruct(ctx, req, nil)
  103. if validErr != nil {
  104. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  105. }
  106. c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  107. if err != nil {
  108. return 0, err
  109. }
  110. if c == nil {
  111. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  112. }
  113. id, err := s.Dao.InsertAndGetId(model.CtrContractCollection{
  114. PlanId: req.PlanId,
  115. CustId: c.CustId,
  116. CustName: c.CustName,
  117. ContractId: req.ContractId,
  118. ContractCode: c.ContractCode,
  119. CollectionDatetime: req.CollectionDatetime,
  120. CollectionAmount: req.CollectionAmount,
  121. CollectionType: req.CollectionType,
  122. ApproStatus: req.ApproStatus,
  123. ContractAmount: c.ContractAmount,
  124. Remark: req.Remark,
  125. CreatedBy: s.userInfo.Id,
  126. CreatedName: s.userInfo.NickName,
  127. CreatedTime: gtime.Now(),
  128. UpdatedBy: s.userInfo.Id,
  129. UpdatedName: s.userInfo.NickName,
  130. UpdatedTime: gtime.Now(),
  131. DeletedTime: gtime.Now(),
  132. })
  133. if err != nil {
  134. return 0, err
  135. }
  136. return int(id), err
  137. }
  138. func (s CtrContractCollectionService) UpdateAmount(tx *gdb.TX, id int) error {
  139. ent := model.CtrContractCollection{}
  140. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", id)
  141. if err == sql.ErrNoRows {
  142. return myerrors.NewMsgError(err, fmt.Sprintf("回款不存在 %d", id))
  143. }
  144. if err != nil {
  145. return err
  146. }
  147. if ent.PlanId != 0 {
  148. err = s.planSrv.UpdateCashedAmount(tx, ent.PlanId)
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. return s.ctrSrv.UpdateCollectedAmount(tx, ent.ContractId)
  154. }
  155. func (s CtrContractCollectionService) Update(ctx context.Context, req *model.CtrContractCollectionUpdateReq) error {
  156. validErr := gvalid.CheckStruct(ctx, req, nil)
  157. if validErr != nil {
  158. return myerrors.NewMsgError(nil, validErr.Current().Error())
  159. }
  160. ent, err := s.Dao.Where("id = ?", req.Id).One()
  161. if err != nil {
  162. return err
  163. }
  164. if ent == nil {
  165. return myerrors.NewMsgError(nil, fmt.Sprintf("回款:%d 不存在", req.Id))
  166. }
  167. var c *model.CtrContract
  168. if req.ContractId != 0 {
  169. c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  170. if err != nil {
  171. return err
  172. }
  173. if c == nil {
  174. return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  175. }
  176. }
  177. toupdate := map[string]interface{}{}
  178. if req.PlanId != 0 {
  179. toupdate["plan_id"] = req.PlanId
  180. }
  181. if req.ContractId != 0 {
  182. toupdate["cust_id"] = c.CustId
  183. toupdate["cust_name"] = c.CustName
  184. toupdate["contract_id"] = req.ContractId
  185. toupdate["contract_code"] = c.ContractCode
  186. toupdate["contract_amount"] = c.ContractAmount
  187. }
  188. if req.CollectionDatetime != nil {
  189. toupdate["collection_datetime"] = req.CollectionDatetime
  190. }
  191. if req.CollectionAmount != nil {
  192. toupdate["collection_amount"] = *req.CollectionAmount
  193. }
  194. if req.CollectionType != "" {
  195. toupdate["collection_type"] = req.CollectionType
  196. }
  197. if req.ApproStatus != "" {
  198. toupdate["appro_status"] = req.ApproStatus
  199. }
  200. if req.Remark != nil {
  201. toupdate["remark"] = *req.Remark
  202. }
  203. if len(toupdate) != 0 {
  204. toupdate["updated_by"] = s.userInfo.Id
  205. toupdate["updated_name"] = s.userInfo.NickName
  206. toupdate["updated_time"] = gtime.Now()
  207. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  208. _, err = tx.Update("ctr_contract_collection", toupdate, "id= ?", req.Id)
  209. if err != nil {
  210. return err
  211. }
  212. if req.CollectionAmount != nil || req.ApproStatus != "" {
  213. err = s.UpdateAmount(tx, req.Id)
  214. if err != nil {
  215. return err
  216. }
  217. }
  218. return nil
  219. })
  220. if txerr != nil {
  221. return txerr
  222. }
  223. }
  224. return nil
  225. }
  226. func (s CtrContractCollectionService) Delete(ctx context.Context, id []int) error {
  227. if len(id) == 0 {
  228. return nil
  229. }
  230. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  231. planId := map[int]bool{}
  232. contractId := map[int]bool{}
  233. for _, i := range id {
  234. ent := model.CtrContractCollection{}
  235. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", i)
  236. if err == sql.ErrNoRows {
  237. continue
  238. }
  239. if err != nil {
  240. return err
  241. }
  242. if ent.ApproStatus != "20" {
  243. continue
  244. }
  245. contractId[ent.ContractId] = true
  246. if ent.PlanId != 0 {
  247. planId[ent.PlanId] = true
  248. }
  249. }
  250. _, err := tx.Delete("ctr_contract_collection", "id in (?)", id)
  251. if err != nil {
  252. return err
  253. }
  254. for pid := range planId {
  255. err = s.planSrv.UpdateCashedAmount(tx, pid)
  256. if err != nil {
  257. return err
  258. }
  259. }
  260. for cid := range contractId {
  261. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. return nil
  267. })
  268. }