ctr_contract_collection.go 8.6 KB

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