ctr_contract_collection_plan.go 8.4 KB

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