ctr_contract_collection_plan.go 9.0 KB

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