ctr_contract_collection_plan.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr)
  78. }
  79. if req.CustId != 0 {
  80. dao = dao.Where("cust_id = ?", req.CustId)
  81. }
  82. if req.CustName != "" {
  83. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  84. dao = dao.Where("cust_name like ?", likestr)
  85. }
  86. if req.ContractId != 0 {
  87. dao = dao.Where("contract_id = ?", req.ContractId)
  88. }
  89. if req.ContractCode != "" {
  90. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  91. dao = dao.Where("contract_code like ?", likestr)
  92. }
  93. if req.ContractStatus != "" {
  94. dao = dao.Where("contract_status = ?", req.ContractStatus)
  95. }
  96. if req.BeginTime != "" {
  97. dao = dao.Where("created_time > ?", req.BeginTime)
  98. }
  99. if req.EndTime != "" {
  100. dao = dao.Where("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 := "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. CashedAmount: 0,
  142. CashedDatetime: nil,
  143. Remark: req.Remark,
  144. CreatedBy: s.userInfo.Id,
  145. CreatedName: s.userInfo.NickName,
  146. CreatedTime: gtime.Now(),
  147. UpdatedBy: s.userInfo.Id,
  148. UpdatedName: s.userInfo.NickName,
  149. UpdatedTime: gtime.Now(),
  150. }
  151. var id int
  152. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  153. planid, err := tx.InsertAndGetId("ctr_contract_collection_plan", ent)
  154. if err != nil {
  155. return err
  156. }
  157. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款计划", map[string]interface{}{
  158. "id": planid,
  159. "planAmount": req.PlanAmount,
  160. "planDatetime": req.PlanDatetime,
  161. })
  162. if err != nil {
  163. return err
  164. }
  165. id = int(planid)
  166. return nil
  167. })
  168. return id, txerr
  169. }
  170. func (s CtrContractCollectionPlanService) Update(ctx context.Context, req *model.CtrContractCollectionPlanUpdateReq) error {
  171. validErr := gvalid.CheckStruct(ctx, req, nil)
  172. if validErr != nil {
  173. return myerrors.TipsError(validErr.Current().Error())
  174. }
  175. p, err := s.Dao.Where("id = ?", req.Id).One()
  176. if err != nil {
  177. //g.Log().Error(err)
  178. return err
  179. }
  180. if p == nil {
  181. return myerrors.TipsError(fmt.Sprintf("回款计划:%d 不存在", req.Id))
  182. }
  183. // var c *model.CtrContract
  184. // if req.ContractId != 0 {
  185. // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  186. // if err != nil {
  187. // return err
  188. // }
  189. // if c == nil {
  190. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  191. // }
  192. // }
  193. dao := &s.Dao.CtrContractCollectionPlanDao
  194. toupdate := map[string]interface{}{}
  195. // if req.ContractId != 0 {
  196. // toupdate["cust_id"] = c.CustId
  197. // toupdate["cust_name"] = c.CustName
  198. // toupdate["contract_id"] = req.ContractId
  199. // toupdate["contract_code"] = c.ContractCode
  200. // }
  201. if req.PlanAmount != nil {
  202. toupdate["plan_amount"] = *req.PlanAmount
  203. }
  204. if req.PlanDatetime != nil {
  205. toupdate["plan_datetime"] = req.PlanDatetime
  206. }
  207. // if req.CashedAmount != nil {
  208. // toupdate["cashed_amount"] = *req.CashedAmount
  209. // }
  210. // if req.CashedDatetime != nil {
  211. // toupdate["cashed_datetime"] = req.CashedDatetime
  212. // }
  213. if req.Remark != nil {
  214. toupdate["remark"] = *req.Remark
  215. }
  216. if len(toupdate) != 0 {
  217. toupdate["updated_by"] = s.userInfo.Id
  218. toupdate["updated_name"] = s.userInfo.NickName
  219. toupdate["updated_time"] = gtime.Now()
  220. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  221. if err != nil {
  222. return err
  223. }
  224. }
  225. return nil
  226. }
  227. func (s CtrContractCollectionPlanService) UpdateCashedAmount(tx *gdb.TX, id int) error {
  228. plan := model.CtrContractCollectionPlan{}
  229. err := tx.GetStruct(&plan, "select * from ctr_contract_collection_plan where id = ?", id)
  230. if err == sql.ErrNoRows {
  231. return myerrors.TipsError(fmt.Sprintf("回款计划不存在 %d", id))
  232. }
  233. if err != nil {
  234. return err
  235. }
  236. 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)
  237. if err != nil {
  238. return err
  239. }
  240. amount := v.Float64()
  241. var contractStatus string
  242. if amount < plan.PlanAmount {
  243. contractStatus = "20"
  244. } else {
  245. contractStatus = "30"
  246. }
  247. if amount == 0 {
  248. contractStatus = "10"
  249. }
  250. _, err = tx.Update("ctr_contract_collection_plan",
  251. map[string]interface{}{
  252. "contract_status": contractStatus,
  253. "cashed_amount": amount,
  254. "cashed_datetime": gtime.Now(),
  255. }, "id = ?", plan.Id)
  256. if err != nil {
  257. return err
  258. }
  259. return nil
  260. }
  261. func (s CtrContractCollectionPlanService) Delete(ctx context.Context, id []int) error {
  262. if len(id) == 0 {
  263. return nil
  264. }
  265. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  266. contractId := map[int]bool{}
  267. for _, i := range id {
  268. ent := model.CtrContractCollectionPlan{}
  269. err := tx.GetStruct(&ent, "select * from ctr_contract_collection_plan where id = ?", i)
  270. if err == sql.ErrNoRows {
  271. continue
  272. }
  273. if err != nil {
  274. return err
  275. }
  276. contractId[ent.ContractId] = true
  277. }
  278. _, err := tx.Delete("ctr_contract_collection_plan", "id in (?)", id)
  279. if err != nil {
  280. return err
  281. }
  282. _, err = tx.Delete("ctr_contract_collection", "plan_id in (?)", id)
  283. if err != nil {
  284. return err
  285. }
  286. for cid := range contractId {
  287. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  288. if err != nil {
  289. return err
  290. }
  291. }
  292. return nil
  293. })
  294. }