ctr_contract_collection.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.CtrContractCollectionListRsp, 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("(collection.cust_name LIKE ? || collection.contract_code LIKE ?)", likestr, likestr)
  62. }
  63. if req.PlanId != 0 {
  64. dao = dao.Where("collection.plan_id = ?", req.PlanId)
  65. }
  66. if req.CustId != 0 {
  67. dao = dao.Where("collection.cust_id = ?", req.CustId)
  68. }
  69. if req.CustName != "" {
  70. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  71. dao = dao.Where("collection.cust_name like ?", likestr)
  72. }
  73. if req.ContractId != 0 {
  74. dao = dao.Where("collection.contract_id = ?", req.ContractId)
  75. }
  76. if req.ContractCode != "" {
  77. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  78. dao = dao.Where("collection.contract_code like ?", likestr)
  79. }
  80. if req.CollectionType != "" {
  81. dao = dao.Where("collection.collection_type = ?", req.CollectionType)
  82. }
  83. if req.ApproStatus != "" {
  84. dao = dao.Where("collection.appro_status = ?", req.ApproStatus)
  85. }
  86. if req.BeginTime != "" {
  87. dao = dao.Where("collection.created_time > ?", req.BeginTime)
  88. }
  89. if req.EndTime != "" {
  90. dao = dao.Where("collection.created_time < ?", req.EndTime)
  91. }
  92. if req.CollectionDatetimeStart != nil {
  93. dao = dao.Where("collection.collection_datetime > ?", req.CollectionDatetimeStart)
  94. }
  95. if req.CollectionDatetimeEnd != nil {
  96. dao = dao.Where("collection.collection_datetime < ?", req.CollectionDatetimeEnd)
  97. }
  98. if req.InchargeId != 0 {
  99. dao = dao.Where("contract.incharge_id = ?", req.InchargeId)
  100. }
  101. if req.InchargeName != "" {
  102. likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
  103. dao = dao.Where("contract.incharge_name like ?", likestr)
  104. }
  105. if req.CustProvinceId != 0 {
  106. dao = dao.Where("contract.cust_province_id = ?", req.CustProvinceId)
  107. }
  108. if req.CustCityId != 0 {
  109. dao = dao.Where("contract.cust_city_id = ?", req.CustCityId)
  110. }
  111. total, err := dao.Count()
  112. if err != nil {
  113. return 0, nil, err
  114. }
  115. if req.PageNum != 0 {
  116. dao = dao.Page(req.GetPage())
  117. }
  118. orderby := "collection.collection_datetime desc"
  119. if req.OrderBy != "" {
  120. orderby = req.OrderBy
  121. }
  122. dao = dao.Order(orderby)
  123. ents := []*model.CtrContractCollectionListRsp{}
  124. err = dao.Fields("collection.*, contract.incharge_id, contract.incharge_name, contract.cust_province_id, contract.cust_city_id, contract.cust_province, contract.cust_city").Structs(&ents)
  125. if err != nil && err != sql.ErrNoRows {
  126. return 0, nil, err
  127. }
  128. return total, ents, err
  129. }
  130. func (s CtrContractCollectionService) Add(ctx context.Context, req *model.CtrContractCollectionAddReq) (int, error) {
  131. validErr := gvalid.CheckStruct(ctx, req, nil)
  132. if validErr != nil {
  133. return 0, myerrors.TipsError(validErr.Current().Error())
  134. }
  135. c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  136. if err != nil {
  137. return 0, err
  138. }
  139. if c == nil {
  140. return 0, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", req.ContractId))
  141. }
  142. ent := model.CtrContractCollection{
  143. PlanId: req.PlanId,
  144. CustId: c.CustId,
  145. CustName: c.CustName,
  146. ContractId: req.ContractId,
  147. ContractCode: c.ContractCode,
  148. CollectionDatetime: req.CollectionDatetime,
  149. CollectionAmount: req.CollectionAmount,
  150. CollectionType: req.CollectionType,
  151. ApproStatus: "20", // 回款没有审核逻辑,这里修改为创建即为已回款
  152. ContractAmount: c.ContractAmount,
  153. Remark: req.Remark,
  154. CreatedBy: s.userInfo.Id,
  155. CreatedName: s.userInfo.NickName,
  156. CreatedTime: gtime.Now(),
  157. UpdatedBy: s.userInfo.Id,
  158. UpdatedName: s.userInfo.NickName,
  159. UpdatedTime: gtime.Now(),
  160. }
  161. var id int
  162. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  163. collectionId, err := tx.InsertAndGetId("ctr_contract_collection", ent)
  164. if err != nil {
  165. return err
  166. }
  167. err = s.UpdateAmount(tx, int(collectionId))
  168. if err != nil {
  169. return err
  170. }
  171. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款", map[string]interface{}{
  172. "id": collectionId,
  173. "collectionDatetime": req.CollectionDatetime,
  174. "collectionAmount": req.CollectionAmount,
  175. "collectionType": req.CollectionType,
  176. })
  177. if err != nil {
  178. return err
  179. }
  180. id = int(collectionId)
  181. return nil
  182. })
  183. return id, txerr
  184. }
  185. func (s CtrContractCollectionService) UpdateAmount(tx *gdb.TX, id int) error {
  186. ent := model.CtrContractCollection{}
  187. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", id)
  188. if err == sql.ErrNoRows {
  189. return myerrors.TipsError(fmt.Sprintf("回款不存在 %d", id))
  190. }
  191. if err != nil {
  192. return err
  193. }
  194. if ent.PlanId != 0 {
  195. err = s.planSrv.UpdateCashedAmount(tx, ent.PlanId)
  196. if err != nil {
  197. return err
  198. }
  199. }
  200. return s.ctrSrv.UpdateCollectedAmount(tx, ent.ContractId)
  201. }
  202. func (s CtrContractCollectionService) Update(ctx context.Context, req *model.CtrContractCollectionUpdateReq) error {
  203. validErr := gvalid.CheckStruct(ctx, req, nil)
  204. if validErr != nil {
  205. return myerrors.TipsError(validErr.Current().Error())
  206. }
  207. ent, err := s.Dao.Where("id = ?", req.Id).One()
  208. if err != nil {
  209. return err
  210. }
  211. if ent == nil {
  212. return myerrors.TipsError(fmt.Sprintf("回款:%d 不存在", req.Id))
  213. }
  214. // var c *model.CtrContract
  215. // if req.ContractId != 0 {
  216. // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  217. // if err != nil {
  218. // return err
  219. // }
  220. // if c == nil {
  221. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  222. // }
  223. // }
  224. toupdate := map[string]interface{}{}
  225. // if req.PlanId != 0 {
  226. // toupdate["plan_id"] = req.PlanId
  227. // }
  228. // if req.ContractId != 0 {
  229. // toupdate["cust_id"] = c.CustId
  230. // toupdate["cust_name"] = c.CustName
  231. // toupdate["contract_id"] = req.ContractId
  232. // toupdate["contract_code"] = c.ContractCode
  233. // toupdate["contract_amount"] = c.ContractAmount
  234. // }
  235. if req.CollectionDatetime != nil {
  236. toupdate["collection_datetime"] = req.CollectionDatetime
  237. }
  238. if req.CollectionAmount != nil {
  239. toupdate["collection_amount"] = *req.CollectionAmount
  240. }
  241. if req.CollectionType != "" {
  242. toupdate["collection_type"] = req.CollectionType
  243. }
  244. if req.ApproStatus != "" {
  245. toupdate["appro_status"] = req.ApproStatus
  246. }
  247. if req.Remark != nil {
  248. toupdate["remark"] = *req.Remark
  249. }
  250. if len(toupdate) != 0 {
  251. toupdate["updated_by"] = s.userInfo.Id
  252. toupdate["updated_name"] = s.userInfo.NickName
  253. toupdate["updated_time"] = gtime.Now()
  254. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  255. _, err = tx.Update("ctr_contract_collection", toupdate, "id= ?", req.Id)
  256. if err != nil {
  257. return err
  258. }
  259. if req.CollectionAmount != nil || req.ApproStatus != "" {
  260. err = s.UpdateAmount(tx, req.Id)
  261. if err != nil {
  262. return err
  263. }
  264. }
  265. return nil
  266. })
  267. if txerr != nil {
  268. return txerr
  269. }
  270. }
  271. return nil
  272. }
  273. func (s CtrContractCollectionService) Delete(ctx context.Context, id []int) error {
  274. if len(id) == 0 {
  275. return nil
  276. }
  277. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  278. planId := map[int]bool{}
  279. contractId := map[int]bool{}
  280. for _, i := range id {
  281. ent := model.CtrContractCollection{}
  282. err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", i)
  283. if err == sql.ErrNoRows {
  284. continue
  285. }
  286. if err != nil {
  287. return err
  288. }
  289. if ent.ApproStatus != "20" {
  290. continue
  291. }
  292. contractId[ent.ContractId] = true
  293. if ent.PlanId != 0 {
  294. planId[ent.PlanId] = true
  295. }
  296. }
  297. _, err := tx.Delete("ctr_contract_collection", "id in (?)", id)
  298. if err != nil {
  299. return err
  300. }
  301. for pid := range planId {
  302. err = s.planSrv.UpdateCashedAmount(tx, pid)
  303. if err != nil {
  304. return err
  305. }
  306. }
  307. for cid := range contractId {
  308. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  309. if err != nil {
  310. return err
  311. }
  312. }
  313. return nil
  314. })
  315. }