ctr_contract_collection_plan.go 8.3 KB

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