ctr_contract_collection_plan.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.CtrContractCollectionPlanListRsp, 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. if req.PlanBeginTime != "" {
  103. dao = dao.Where("plan.plan_datetime > ?", req.PlanBeginTime)
  104. }
  105. if req.PlanEndTime != "" {
  106. dao = dao.Where("plan.plan_datetime < ?", req.PlanEndTime)
  107. }
  108. if req.InchargeId != 0 {
  109. dao = dao.Where("contract.incharge_id = ?", req.InchargeId)
  110. }
  111. if req.InchargeName != "" {
  112. likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
  113. dao = dao.Where("contract.incharge_name like ?", likestr)
  114. }
  115. if req.CustProvinceId != 0 {
  116. dao = dao.Where("contract.cust_province_id = ?", req.CustProvinceId)
  117. }
  118. if req.CustCityId != 0 {
  119. dao = dao.Where("contract.cust_city_id = ?", req.CustCityId)
  120. }
  121. total, err := dao.Count()
  122. if err != nil {
  123. return 0, nil, err
  124. }
  125. if req.PageNum != 0 {
  126. dao = dao.Page(req.GetPage())
  127. }
  128. orderby := "plan.plan_datetime desc"
  129. if req.OrderBy != "" {
  130. orderby = req.OrderBy
  131. }
  132. dao = dao.Order(orderby)
  133. ents := []*model.CtrContractCollectionPlanListRsp{}
  134. err = dao.Fields("plan.*, contract.incharge_id, contract.incharge_name, contract.cust_province_id, contract.cust_city_id, contract.cust_province, contract.cust_city").Structs(&ents)
  135. if err != nil && err != sql.ErrNoRows {
  136. return 0, nil, err
  137. }
  138. return total, ents, err
  139. }
  140. func (s CtrContractCollectionPlanService) Add(ctx context.Context, req *model.CtrContractCollectionPlanAddReq) (int, error) {
  141. validErr := gvalid.CheckStruct(ctx, req, nil)
  142. if validErr != nil {
  143. return 0, myerrors.TipsError(validErr.Current().Error())
  144. }
  145. c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  146. if err != nil {
  147. return 0, err
  148. }
  149. if c == nil {
  150. return 0, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", req.ContractId))
  151. }
  152. ent := model.CtrContractCollectionPlan{
  153. CustId: c.CustId,
  154. CustName: c.CustName,
  155. ContractId: req.ContractId,
  156. ContractCode: c.ContractCode,
  157. ContractStatus: "10",
  158. PlanAmount: req.PlanAmount,
  159. PlanDatetime: req.PlanDatetime,
  160. PlanScale: req.PlanScale,
  161. PlanCondition: req.PlanCondition,
  162. CashedAmount: 0,
  163. CashedDatetime: nil,
  164. Remark: req.Remark,
  165. CreatedBy: s.userInfo.Id,
  166. CreatedName: s.userInfo.NickName,
  167. CreatedTime: gtime.Now(),
  168. UpdatedBy: s.userInfo.Id,
  169. UpdatedName: s.userInfo.NickName,
  170. UpdatedTime: gtime.Now(),
  171. }
  172. var id int
  173. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  174. planid, err := tx.InsertAndGetId("ctr_contract_collection_plan", ent)
  175. if err != nil {
  176. return err
  177. }
  178. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款计划", map[string]interface{}{
  179. "id": planid,
  180. "planAmount": req.PlanAmount,
  181. "planDatetime": req.PlanDatetime,
  182. })
  183. if err != nil {
  184. return err
  185. }
  186. id = int(planid)
  187. return nil
  188. })
  189. return id, txerr
  190. }
  191. func (s CtrContractCollectionPlanService) Update(ctx context.Context, req *model.CtrContractCollectionPlanUpdateReq) error {
  192. validErr := gvalid.CheckStruct(ctx, req, nil)
  193. if validErr != nil {
  194. return myerrors.TipsError(validErr.Current().Error())
  195. }
  196. p, err := s.Dao.Where("id = ?", req.Id).One()
  197. if err != nil {
  198. //g.Log().Error(err)
  199. return err
  200. }
  201. if p == nil {
  202. return myerrors.TipsError(fmt.Sprintf("回款计划:%d 不存在", req.Id))
  203. }
  204. // var c *model.CtrContract
  205. // if req.ContractId != 0 {
  206. // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
  207. // if err != nil {
  208. // return err
  209. // }
  210. // if c == nil {
  211. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
  212. // }
  213. // }
  214. dao := &s.Dao.CtrContractCollectionPlanDao
  215. toupdate := map[string]interface{}{}
  216. // if req.ContractId != 0 {
  217. // toupdate["cust_id"] = c.CustId
  218. // toupdate["cust_name"] = c.CustName
  219. // toupdate["contract_id"] = req.ContractId
  220. // toupdate["contract_code"] = c.ContractCode
  221. // }
  222. if req.PlanAmount != nil {
  223. toupdate["plan_amount"] = *req.PlanAmount
  224. }
  225. if req.PlanDatetime != nil {
  226. toupdate["plan_datetime"] = req.PlanDatetime
  227. }
  228. if req.PlanScale != nil {
  229. toupdate["plan_scale"] = req.PlanScale
  230. }
  231. if req.PlanCondition != nil {
  232. toupdate["plan_condition"] = req.PlanCondition
  233. }
  234. // if req.CashedAmount != nil {
  235. // toupdate["cashed_amount"] = *req.CashedAmount
  236. // }
  237. // if req.CashedDatetime != nil {
  238. // toupdate["cashed_datetime"] = req.CashedDatetime
  239. // }
  240. if req.Remark != nil {
  241. toupdate["remark"] = *req.Remark
  242. }
  243. if len(toupdate) != 0 {
  244. toupdate["updated_by"] = s.userInfo.Id
  245. toupdate["updated_name"] = s.userInfo.NickName
  246. toupdate["updated_time"] = gtime.Now()
  247. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  248. if err != nil {
  249. return err
  250. }
  251. }
  252. return nil
  253. }
  254. func (s CtrContractCollectionPlanService) UpdateCashedAmount(tx *gdb.TX, id int) error {
  255. plan := model.CtrContractCollectionPlan{}
  256. err := tx.GetStruct(&plan, "select * from ctr_contract_collection_plan where id = ?", id)
  257. if err == sql.ErrNoRows {
  258. return myerrors.TipsError(fmt.Sprintf("回款计划不存在 %d", id))
  259. }
  260. if err != nil {
  261. return err
  262. }
  263. 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)
  264. if err != nil {
  265. return err
  266. }
  267. amount := v.Float64()
  268. var contractStatus string
  269. if amount < plan.PlanAmount {
  270. contractStatus = "20"
  271. } else {
  272. contractStatus = "30"
  273. }
  274. if amount == 0 {
  275. contractStatus = "10"
  276. }
  277. _, err = tx.Update("ctr_contract_collection_plan",
  278. map[string]interface{}{
  279. "contract_status": contractStatus,
  280. "cashed_amount": amount,
  281. "cashed_datetime": gtime.Now(),
  282. }, "id = ?", plan.Id)
  283. if err != nil {
  284. return err
  285. }
  286. return nil
  287. }
  288. func (s CtrContractCollectionPlanService) Delete(ctx context.Context, id []int) error {
  289. if len(id) == 0 {
  290. return nil
  291. }
  292. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  293. contractId := map[int]bool{}
  294. for _, i := range id {
  295. ent := model.CtrContractCollectionPlan{}
  296. err := tx.GetStruct(&ent, "select * from ctr_contract_collection_plan where id = ?", i)
  297. if err == sql.ErrNoRows {
  298. continue
  299. }
  300. if err != nil {
  301. return err
  302. }
  303. contractId[ent.ContractId] = true
  304. }
  305. _, err := tx.Delete("ctr_contract_collection_plan", "id in (?)", id)
  306. if err != nil {
  307. return err
  308. }
  309. _, err = tx.Delete("ctr_contract_collection", "plan_id in (?)", id)
  310. if err != nil {
  311. return err
  312. }
  313. for cid := range contractId {
  314. err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
  315. if err != nil {
  316. return err
  317. }
  318. }
  319. return nil
  320. })
  321. }