ctr_contract_collection_plan.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 CtrContractCollectionPlanService struct {
  16. Dao *dao.CtrContractCollectionPlanDao
  17. ContractDao *dao.CtrContractDao
  18. ctrSrv *CtrContractService
  19. Tenant string
  20. userInfo request.UserInfo
  21. }
  22. func NewCtrContractCollectionPlanService(ctx context.Context) (*CtrContractCollectionPlanService, error) {
  23. tenant, err := micro_srv.GetTenant(ctx)
  24. if err != nil {
  25. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  26. }
  27. // 获取用户信息
  28. userInfo, err := micro_srv.GetUserInfo(ctx)
  29. if err != nil {
  30. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  31. }
  32. ctrSrv, err := NewCtrContractService(ctx)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &CtrContractCollectionPlanService{
  37. Dao: dao.NewCtrContractCollectionPlanDao(tenant),
  38. ContractDao: dao.NewCtrContractDao(tenant),
  39. ctrSrv: ctrSrv,
  40. Tenant: tenant,
  41. userInfo: userInfo,
  42. }, nil
  43. }
  44. func (s CtrContractCollectionPlanService) List(ctx context.Context, req *model.CtrContractCollectionPlanListReq) (int, []*model.CtrContractCollectionPlan, error) {
  45. dao := &s.Dao.CtrContractCollectionPlanDao
  46. if req.SearchText != "" {
  47. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  48. dao = dao.Where("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr)
  49. }
  50. if req.CustId != 0 {
  51. dao = dao.Where("cust_id = ?", req.CustId)
  52. }
  53. if req.CustName != "" {
  54. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  55. dao = dao.Where("cust_name like ?", likestr)
  56. }
  57. if req.ContractId != 0 {
  58. dao = dao.Where("contract_id = ?", req.ContractId)
  59. }
  60. if req.ContractCode != "" {
  61. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  62. dao = dao.Where("contract_code like ?", likestr)
  63. }
  64. if req.ContractStatus != "" {
  65. dao = dao.Where("contract_status = ?", req.ContractStatus)
  66. }
  67. if req.BeginTime != "" {
  68. dao = dao.Where("created_time > ?", req.BeginTime)
  69. }
  70. if req.EndTime != "" {
  71. dao = dao.Where("created_time < ?", req.EndTime)
  72. }
  73. total, err := dao.Count()
  74. if err != nil {
  75. return 0, nil, err
  76. }
  77. if req.PageNum != 0 {
  78. dao = dao.Page(req.GetPage())
  79. }
  80. orderby := "created_time desc"
  81. if req.OrderBy != "" {
  82. orderby = req.OrderBy
  83. }
  84. dao = dao.Order(orderby)
  85. ents := []*model.CtrContractCollectionPlan{}
  86. err = dao.Structs(&ents)
  87. if err != nil && err != sql.ErrNoRows {
  88. return 0, nil, err
  89. }
  90. return total, ents, err
  91. }
  92. func (s CtrContractCollectionPlanService) Add(ctx context.Context, req *model.CtrContractCollectionPlanAddReq) (int, error) {
  93. validErr := gvalid.CheckStruct(ctx, req, nil)
  94. if validErr != nil {
  95. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  96. }
  97. c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  98. if err != nil {
  99. return 0, err
  100. }
  101. if c == nil {
  102. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  103. }
  104. id, err := s.Dao.InsertAndGetId(model.CtrContractCollectionPlan{
  105. CustId: c.CustId,
  106. CustName: c.CustName,
  107. ContractId: req.ContractId,
  108. ContractCode: c.ContractCode,
  109. ContractStatus: "10",
  110. PlanAmount: req.PlanAmount,
  111. PlanDatetime: req.PlanDatetime,
  112. CashedAmount: req.CashedAmount,
  113. CashedDatetime: req.CashedDatetime,
  114. Remark: req.Remark,
  115. CreatedBy: s.userInfo.Id,
  116. CreatedName: s.userInfo.NickName,
  117. CreatedTime: gtime.Now(),
  118. UpdatedBy: s.userInfo.Id,
  119. UpdatedName: s.userInfo.NickName,
  120. UpdatedTime: gtime.Now(),
  121. DeletedTime: gtime.Now(),
  122. })
  123. if err != nil {
  124. return 0, err
  125. }
  126. return int(id), err
  127. }
  128. func (s CtrContractCollectionPlanService) Update(ctx context.Context, req *model.CtrContractCollectionPlanUpdateReq) error {
  129. validErr := gvalid.CheckStruct(ctx, req, nil)
  130. if validErr != nil {
  131. return myerrors.NewMsgError(nil, validErr.Current().Error())
  132. }
  133. p, err := s.Dao.Where("id = ?", req.Id).One()
  134. if err != nil {
  135. return err
  136. }
  137. if p == nil {
  138. return myerrors.NewMsgError(nil, fmt.Sprintf("回款计划:%d 不存在", req.Id))
  139. }
  140. // var c *model.CtrContract
  141. // if req.ContractId != 0 {
  142. // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  143. // if err != nil {
  144. // return err
  145. // }
  146. // if c == nil {
  147. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  148. // }
  149. // }
  150. dao := &s.Dao.CtrContractCollectionPlanDao
  151. toupdate := map[string]interface{}{}
  152. // if req.ContractId != 0 {
  153. // toupdate["cust_id"] = c.CustId
  154. // toupdate["cust_name"] = c.CustName
  155. // toupdate["contract_id"] = req.ContractId
  156. // toupdate["contract_code"] = c.ContractCode
  157. // }
  158. if req.PlanAmount != nil {
  159. toupdate["plan_amount"] = *req.PlanAmount
  160. }
  161. if req.PlanDatetime != nil {
  162. toupdate["plan_datetime"] = req.PlanDatetime
  163. }
  164. // if req.CashedAmount != nil {
  165. // toupdate["cashed_amount"] = *req.CashedAmount
  166. // }
  167. // if req.CashedDatetime != nil {
  168. // toupdate["cashed_datetime"] = req.CashedDatetime
  169. // }
  170. if req.Remark != nil {
  171. toupdate["remark"] = *req.Remark
  172. }
  173. if len(toupdate) != 0 {
  174. toupdate["updated_by"] = s.userInfo.Id
  175. toupdate["updated_name"] = s.userInfo.NickName
  176. toupdate["updated_time"] = gtime.Now()
  177. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  178. if err != nil {
  179. return err
  180. }
  181. }
  182. return nil
  183. }
  184. func (s CtrContractCollectionPlanService) UpdateCashedAmount(tx *gdb.TX, id int) error {
  185. plan := model.CtrContractCollectionPlan{}
  186. err := tx.GetStruct(&plan, "select * from ctr_contract_collection_plan where id = ?", id)
  187. if err == sql.ErrNoRows {
  188. return myerrors.NewMsgError(err, fmt.Sprintf("回款计划不存在 %d", id))
  189. }
  190. if err != nil {
  191. return err
  192. }
  193. v, err := tx.GetValue("select sum(collection_amount) from ctr_contract_collection where plan_id=? and appro_status='20' and deleted_time is null", id)
  194. if err != nil {
  195. return err
  196. }
  197. amount := v.Float64()
  198. var contractStatus string
  199. if amount < plan.PlanAmount {
  200. contractStatus = "20"
  201. } else {
  202. contractStatus = "30"
  203. }
  204. if amount == 0 {
  205. contractStatus = "10"
  206. }
  207. _, err = tx.Update("ctr_contract_collection_plan",
  208. map[string]interface{}{
  209. "contract_status": contractStatus,
  210. "cashed_amount": amount,
  211. "cashed_datetime": gtime.Now(),
  212. }, "id = ?", plan.Id)
  213. if err != nil {
  214. return err
  215. }
  216. return nil
  217. }
  218. func (s CtrContractCollectionPlanService) Delete(ctx context.Context, id []int) error {
  219. if len(id) == 0 {
  220. return nil
  221. }
  222. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  223. contractId := map[int]bool{}
  224. for _, i := range id {
  225. ent := model.CtrContractCollectionPlan{}
  226. err := tx.GetStruct(&ent, "select * from ctr_contract_collection_plan where id = ?", i)
  227. if err == sql.ErrNoRows {
  228. continue
  229. }
  230. if err != nil {
  231. return err
  232. }
  233. contractId[ent.ContractId] = true
  234. }
  235. _, err := tx.Delete("ctr_contract_collection_plan", "id in (?)", id)
  236. if err != nil {
  237. return err
  238. }
  239. _, err = tx.Delete("ctr_contract_collection", "plan_id in (?)", id)
  240. if err != nil {
  241. return err
  242. }
  243. for cid := range contractId {
  244. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  245. if err != nil {
  246. return err
  247. }
  248. }
  249. return nil
  250. })
  251. }