base_distributor.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. package base
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "time"
  8. "dashoo.cn/opms_libary/myerrors"
  9. "github.com/gogf/gf/container/garray"
  10. "github.com/gogf/gf/database/gdb"
  11. "github.com/gogf/gf/frame/g"
  12. "github.com/gogf/gf/os/gtime"
  13. "github.com/gogf/gf/util/gconv"
  14. "github.com/gogf/gf/util/gvalid"
  15. "dashoo.cn/micro/app/dao/base"
  16. contractdao "dashoo.cn/micro/app/dao/contract"
  17. projdao "dashoo.cn/micro/app/dao/proj"
  18. model "dashoo.cn/micro/app/model/base"
  19. contractmodel "dashoo.cn/micro/app/model/contract"
  20. projmodel "dashoo.cn/micro/app/model/proj"
  21. "dashoo.cn/micro/app/service"
  22. )
  23. type distributorService struct {
  24. *service.ContextService
  25. Dao *base.BaseDistributorDao
  26. DynamicsDao *base.BaseDistributorDynamicsDao
  27. TargetDao *base.BaseDistributorTargetDao
  28. RecordDao *base.BaseDistributorRecordDao
  29. ProjDao *projdao.ProjBusinessDao
  30. ContractDao *contractdao.CtrContractDao
  31. }
  32. func NewDistributorService(ctx context.Context) (svc *distributorService, err error) {
  33. svc = new(distributorService)
  34. if svc.ContextService, err = svc.Init(ctx); err != nil {
  35. return nil, err
  36. }
  37. svc.Dao = base.NewBaseDistributorDao(svc.Tenant)
  38. svc.DynamicsDao = base.NewBaseDistributorDynamicsDao(svc.Tenant)
  39. svc.ProjDao = projdao.NewProjBusinessDao(svc.Tenant)
  40. svc.ContractDao = contractdao.NewCtrContractDao(svc.Tenant)
  41. svc.TargetDao = base.NewBaseDistributorTargetDao(svc.Tenant)
  42. svc.RecordDao = base.NewBaseDistributorRecordDao(svc.Tenant)
  43. return svc, nil
  44. }
  45. // GetList 经销商信息列表
  46. func (s *distributorService) GetList(req *model.BaseDistributorSearchReq) (total int, distributorList []*model.BaseDistributorListRsp, err error) {
  47. distributorModel := s.Dao.FieldsEx(s.Dao.C.DeletedTime)
  48. // 用户仅有销售工程师角色展示自己的数据,其他人可以看到所有数据
  49. if garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") {
  50. distributorModel = distributorModel.WhereIn("belong_sale_id", s.DataScope["userIds"])
  51. }
  52. if req.DistCode != "" {
  53. distributorModel = distributorModel.WhereLike(s.Dao.C.DistCode, "%"+req.DistCode+"%")
  54. }
  55. if req.DistName != "" {
  56. distributorModel = distributorModel.WhereLike(s.Dao.C.DistName, "%"+req.DistName+"%")
  57. }
  58. if req.BelongSale != "" {
  59. distributorModel = distributorModel.WhereLike(s.Dao.C.BelongSale, "%"+req.BelongSale+"%")
  60. }
  61. if len(req.ProvinceId) > 0 {
  62. distributorModel = distributorModel.WhereIn(s.Dao.C.ProvinceId, req.ProvinceId)
  63. }
  64. if req.DistType != "" {
  65. distributorModel = distributorModel.WhereIn(s.Dao.C.DistType, req.DistType)
  66. }
  67. total, err = distributorModel.Count()
  68. if err != nil {
  69. err = myerrors.DbError("获取总行数失败。")
  70. return
  71. }
  72. err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList)
  73. if req.WithStatistic {
  74. for i, dist := range distributorList {
  75. statistic, err := s.statistic(dist.Id)
  76. if err != nil {
  77. return 0, nil, err
  78. }
  79. distributorList[i].BaseDistributorStatistic = statistic
  80. }
  81. }
  82. return
  83. }
  84. func (s *distributorService) statistic(id int) (stat model.BaseDistributorStatistic, err error) {
  85. v, err := s.Dao.DB.GetValue("select count(*) from proj_business where distributor_id=? and appro_status ='30' and nbo_type in (10,20,30) and deleted_time is null", id)
  86. if err != nil {
  87. return stat, err
  88. }
  89. stat.ProjectNum = v.Int()
  90. v, err = s.Dao.DB.GetValue("select sum(est_trans_price) from proj_business where distributor_id=? and appro_status ='30' and nbo_type in (10,20,30) and deleted_time is null", id)
  91. if err != nil {
  92. return stat, err
  93. }
  94. stat.AllProductAmount = v.Float64()
  95. v, err = s.Dao.DB.GetValue("select count(distinct(b.id)) from ctr_contract a left join proj_business b on a.nbo_id=b.id where a.distributor_id=? and a.appro_status ='30' and b.appro_status ='30' and a.deleted_time is null and b.deleted_time is null", id)
  96. if err != nil {
  97. return stat, err
  98. }
  99. stat.SaledProjectNum = v.Int()
  100. v, err = s.Dao.DB.GetValue("select sum(contract_amount) from ctr_contract where distributor_id=? and appro_status='30' and deleted_time is null", id)
  101. if err != nil {
  102. return stat, err
  103. }
  104. stat.SaledAmount = v.Float64()
  105. v, err = s.Dao.DB.GetValue("select sum(contract_amount - collected_amount) from ctr_contract where distributor_id=? and appro_status='30' and deleted_time is null", id)
  106. if err != nil {
  107. return stat, err
  108. }
  109. stat.UnpaidAmount = v.Float64()
  110. v, err = s.Dao.DB.GetValue("select sum(invoice_amount) from ctr_contract where distributor_id=? and appro_status='30' and deleted_time is null", id)
  111. if err != nil {
  112. return stat, err
  113. }
  114. stat.InvoicedAmount = v.Float64()
  115. return
  116. }
  117. // 获取经销商编号
  118. func (s *distributorService) getDistributorCode(distCode string) (string, error) {
  119. sequence, err := service.Sequence(s.Dao.DB, "distributor_code")
  120. if err != nil {
  121. return "", err
  122. }
  123. return distCode + sequence, nil
  124. }
  125. // Create 经销商创建
  126. func (s *distributorService) Create(req *model.AddDistributor) (lastId int64, err error) {
  127. DistributorData := new(model.BaseDistributor)
  128. if err = gconv.Struct(req, DistributorData); err != nil {
  129. return
  130. }
  131. DistributorData.DistCode, err = s.getDistributorCode("JXS")
  132. if err != nil {
  133. return 0, err
  134. }
  135. service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName())
  136. lastId, err = s.Dao.InsertAndGetId(DistributorData)
  137. if err != nil {
  138. return 0, err
  139. }
  140. err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  141. err = s.AddDynamicsByCurrentUser(tx, int(lastId), "创建经销商/代理商", map[string]interface{}{})
  142. return err
  143. })
  144. return
  145. }
  146. // GetEntityById 详情
  147. func (s *distributorService) GetEntityById(id int64) (distributorInfo *model.BaseDistributorListRsp, err error) {
  148. err = s.Dao.Where(base.BaseProduct.C.Id, id).Scan(&distributorInfo)
  149. if err != nil {
  150. return nil, err
  151. }
  152. statistic, err := s.statistic(int(id))
  153. if err != nil {
  154. return nil, err
  155. }
  156. distributorInfo.BaseDistributorStatistic = statistic
  157. target, err := s.TargetDao.Where("dist_id = ?", id).Where("year = ?", time.Now().Year()).One()
  158. if err != nil {
  159. return nil, err
  160. }
  161. if target != nil {
  162. distributorInfo.YearTarget = target.Total
  163. }
  164. return
  165. }
  166. // UpdateById 修改数据
  167. func (s *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) {
  168. count, err := s.Dao.Where("id = ", req.Id).Count()
  169. if err != nil {
  170. g.Log().Error(err)
  171. return
  172. }
  173. if count == 0 {
  174. err = myerrors.TipsError("无修改数据")
  175. return
  176. }
  177. distData := new(model.BaseDistributor)
  178. if err = gconv.Struct(req, distData); err != nil {
  179. return
  180. }
  181. service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName())
  182. _, err = s.Dao.FieldsEx(s.Dao.C.DistCode, s.Dao.C.Id,
  183. s.Dao.C.CreatedName, s.Dao.C.CreatedBy, s.Dao.C.CreatedTime).WherePri(s.Dao.C.Id, req.Id).Update(distData)
  184. if err != nil {
  185. g.Log().Error(err)
  186. return
  187. }
  188. err = s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  189. err = s.AddDynamicsByCurrentUser(tx, req.Id, "更新经销商/代理商", map[string]interface{}{})
  190. return err
  191. })
  192. return
  193. }
  194. func (s *distributorService) ToProxy(ctx context.Context, req *model.DistributorToProxyReq) (err error) {
  195. validErr := gvalid.CheckStruct(ctx, req, nil)
  196. if validErr != nil {
  197. return myerrors.TipsError(validErr.Current().Error())
  198. }
  199. ent, err := s.Dao.Where("id = ?", req.Id).One()
  200. if err != nil {
  201. return err
  202. }
  203. if ent == nil {
  204. return myerrors.TipsError(fmt.Sprintf("代理商/经销商不存在: %d", req.Id))
  205. }
  206. txerr := s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  207. _, err = tx.Update("base_distributor", map[string]interface{}{
  208. "dist_type": "20",
  209. "customer_type": req.CustomerType,
  210. "proxy_start_time": req.ProxyStartTime,
  211. "proxy_end_time": req.ProxyEndTime,
  212. "proxy_district": req.ProxyDistrict,
  213. "contract_url": req.ContractUrl,
  214. }, "id = ?", req.Id)
  215. if err != nil {
  216. return err
  217. }
  218. _, err = tx.Insert("base_distributor_record", model.BaseDistributorRecord{
  219. DistId: req.Id,
  220. DistType: "20",
  221. BusinessScope: ent.BusinessScope,
  222. CustomerType: req.CustomerType,
  223. ProxyDistrict: req.ProxyDistrict,
  224. ProxyStartTime: req.ProxyStartTime,
  225. ProxyEndTime: req.ProxyEndTime,
  226. ContractUrl: req.ContractUrl,
  227. ExistedProduct: ent.ExistedProduct,
  228. HistoryCustomer: ent.HistoryCustomer,
  229. ToDistReason: "",
  230. Remark: "",
  231. CreatedBy: s.GetCxtUserId(),
  232. CreatedName: s.GetCxtUserName(),
  233. CreatedTime: gtime.Now(),
  234. UpdatedBy: s.GetCxtUserId(),
  235. UpdatedName: s.GetCxtUserName(),
  236. UpdatedTime: gtime.Now(),
  237. })
  238. if err != nil {
  239. return err
  240. }
  241. err = s.AddDynamicsByCurrentUser(tx, req.Id, "转为代理商", map[string]interface{}{})
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. })
  247. return txerr
  248. }
  249. func (s *distributorService) Renew(ctx context.Context, req *model.DistributorRenewReq) (err error) {
  250. validErr := gvalid.CheckStruct(ctx, req, nil)
  251. if validErr != nil {
  252. return myerrors.TipsError(validErr.Current().Error())
  253. }
  254. ent, err := s.Dao.Where("id = ?", req.Id).One()
  255. if err != nil {
  256. return err
  257. }
  258. if ent == nil {
  259. return myerrors.TipsError(fmt.Sprintf("代理商/经销商不存在: %d", req.Id))
  260. }
  261. txerr := s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  262. _, err = tx.Update("base_distributor", map[string]interface{}{
  263. "customer_type": req.CustomerType,
  264. "proxy_start_time": req.ProxyStartTime,
  265. "proxy_end_time": req.ProxyEndTime,
  266. "proxy_district": req.ProxyDistrict,
  267. "contract_url": req.ContractUrl,
  268. }, "id = ?", req.Id)
  269. if err != nil {
  270. return err
  271. }
  272. _, err = tx.Insert("base_distributor_record", model.BaseDistributorRecord{
  273. DistId: req.Id,
  274. DistType: "20",
  275. BusinessScope: ent.BusinessScope,
  276. CustomerType: req.CustomerType,
  277. ProxyDistrict: req.ProxyDistrict,
  278. ProxyStartTime: req.ProxyStartTime,
  279. ProxyEndTime: req.ProxyEndTime,
  280. ContractUrl: req.ContractUrl,
  281. ExistedProduct: ent.ExistedProduct,
  282. HistoryCustomer: ent.HistoryCustomer,
  283. ToDistReason: "",
  284. Remark: "",
  285. CreatedBy: s.GetCxtUserId(),
  286. CreatedName: s.GetCxtUserName(),
  287. CreatedTime: gtime.Now(),
  288. UpdatedBy: s.GetCxtUserId(),
  289. UpdatedName: s.GetCxtUserName(),
  290. UpdatedTime: gtime.Now(),
  291. })
  292. if err != nil {
  293. return err
  294. }
  295. err = s.AddDynamicsByCurrentUser(tx, req.Id, "续签代理商", map[string]interface{}{})
  296. if err != nil {
  297. return err
  298. }
  299. return nil
  300. })
  301. return txerr
  302. }
  303. func (s *distributorService) ToDist(ctx context.Context, req *model.DistributorToDistReq) (err error) {
  304. validErr := gvalid.CheckStruct(ctx, req, nil)
  305. if validErr != nil {
  306. return myerrors.TipsError(validErr.Current().Error())
  307. }
  308. ent, err := s.Dao.Where("id = ?", req.Id).One()
  309. if err != nil {
  310. return err
  311. }
  312. if ent == nil {
  313. return myerrors.TipsError(fmt.Sprintf("代理商/经销商不存在: %d", req.Id))
  314. }
  315. txerr := s.Dao.DB.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  316. _, err = tx.Update("base_distributor", map[string]interface{}{
  317. "dist_type": "10",
  318. }, "id = ?", req.Id)
  319. if err != nil {
  320. return err
  321. }
  322. _, err = tx.Insert("base_distributor_record", model.BaseDistributorRecord{
  323. DistId: req.Id,
  324. DistType: "10",
  325. BusinessScope: ent.BusinessScope,
  326. CustomerType: ent.CustomerType,
  327. ProxyDistrict: "",
  328. ProxyStartTime: nil,
  329. ProxyEndTime: nil,
  330. ContractUrl: "",
  331. ExistedProduct: ent.ExistedProduct,
  332. HistoryCustomer: ent.HistoryCustomer,
  333. ToDistReason: req.ToDistReason,
  334. Remark: "",
  335. CreatedBy: s.GetCxtUserId(),
  336. CreatedName: s.GetCxtUserName(),
  337. CreatedTime: gtime.Now(),
  338. UpdatedBy: s.GetCxtUserId(),
  339. UpdatedName: s.GetCxtUserName(),
  340. UpdatedTime: gtime.Now(),
  341. })
  342. if err != nil {
  343. return err
  344. }
  345. err = s.AddDynamicsByCurrentUser(tx, req.Id, "转为经销商", map[string]interface{}{})
  346. if err != nil {
  347. return err
  348. }
  349. return nil
  350. })
  351. return txerr
  352. }
  353. func (s *distributorService) TransRecord(ctx context.Context, req *model.DistributorTransRecordReq) (int, []*model.BaseDistributorRecord, error) {
  354. dao := s.RecordDao.As("a")
  355. if req.DistId != 0 {
  356. dao = dao.Where("a.dist_id = ?", req.DistId)
  357. }
  358. total, err := dao.Count()
  359. if err != nil {
  360. return 0, nil, err
  361. }
  362. if req.PageNum != 0 {
  363. dao = dao.Page(req.GetPage())
  364. }
  365. orderby := "a.created_time desc"
  366. if req.OrderBy != "" {
  367. orderby = req.OrderBy
  368. }
  369. dao = dao.Order(orderby)
  370. ents := []*model.BaseDistributorRecord{}
  371. err = dao.Structs(&ents)
  372. if err != nil && err != sql.ErrNoRows {
  373. return 0, nil, err
  374. }
  375. return total, ents, nil
  376. }
  377. // DeleteByIds 删除
  378. func (s *distributorService) DeleteByIds(ids []int64) (err error) {
  379. _, err = s.Dao.WhereIn(s.Dao.C.Id, ids).Delete()
  380. if err != nil {
  381. return err
  382. }
  383. return
  384. }
  385. func (s distributorService) AddDynamicsByCurrentUser(tx *gdb.TX, distId int, opnType string, content map[string]interface{}) error {
  386. contentByte, err := json.Marshal(content)
  387. if err != nil {
  388. return err
  389. }
  390. _, err = tx.InsertAndGetId("base_distributor_dynamics", model.BaseDistributorDynamics{
  391. DistId: distId,
  392. OpnPeopleId: s.GetCxtUserId(),
  393. OpnPeople: s.GetCxtUserName(),
  394. OpnDate: gtime.Now(),
  395. OpnType: opnType,
  396. OpnContent: string(contentByte),
  397. Remark: "",
  398. CreatedBy: s.GetCxtUserId(),
  399. CreatedName: s.GetCxtUserName(),
  400. CreatedTime: gtime.Now(),
  401. UpdatedBy: s.GetCxtUserId(),
  402. UpdatedName: s.GetCxtUserName(),
  403. UpdatedTime: gtime.Now(),
  404. })
  405. return err
  406. }
  407. func (s distributorService) ContractList(ctx context.Context, req *model.DistributorContractListReq) (int, []*contractmodel.CtrContractListRsp, error) {
  408. dao := s.ContractDao.As("a")
  409. if req.DistId != 0 {
  410. dao = dao.Where("a.distributor_id = ?", req.DistId)
  411. }
  412. if req.SearchText != "" {
  413. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  414. dao = dao.Where("(a.contract_code LIKE ? || a.contract_name LIKE ? || a.cust_name LIKE ? || a.nbo_name LIKE ?)", likestr, likestr, likestr, likestr)
  415. }
  416. if req.ContractCode != "" {
  417. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  418. dao = dao.Where("a.contract_code like ?", likestr)
  419. }
  420. if req.ContractName != "" {
  421. likestr := fmt.Sprintf("%%%s%%", req.ContractName)
  422. dao = dao.Where("a.contract_name like ?", likestr)
  423. }
  424. if req.CustId != 0 {
  425. dao = dao.Where("a.cust_id = ?", req.CustId)
  426. }
  427. if req.CustName != "" {
  428. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  429. dao = dao.Where("a.cust_name like ?", likestr)
  430. }
  431. if req.NboId != 0 {
  432. dao = dao.Where("a.nbo_id = ?", req.NboId)
  433. }
  434. if req.NboName != "" {
  435. likestr := fmt.Sprintf("%%%s%%", req.NboName)
  436. dao = dao.Where("a.nbo_name like ?", likestr)
  437. }
  438. if req.ApproStatus != "" {
  439. dao = dao.Where("a.appro_status = ?", req.ApproStatus)
  440. }
  441. if req.ContractType != "" {
  442. dao = dao.Where("a.contract_type = ?", req.ContractType)
  443. }
  444. if req.InchargeId != 0 {
  445. dao = dao.Where("a.incharge_id = ?", req.InchargeId)
  446. }
  447. if req.InchargeName != "" {
  448. likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
  449. dao = dao.Where("a.incharge_name like ?", likestr)
  450. }
  451. if req.SignatoryId != 0 {
  452. dao = dao.Where("a.signatory_id = ?", req.SignatoryId)
  453. }
  454. if req.SignatoryName != "" {
  455. likestr := fmt.Sprintf("%%%s%%", req.SignatoryName)
  456. dao = dao.Where("a.signatory_name like ?", likestr)
  457. }
  458. if req.DistributorName != "" {
  459. likestr := fmt.Sprintf("%%%s%%", req.DistributorName)
  460. dao = dao.Where("a.distributor_name like ?", likestr)
  461. }
  462. if req.BeginTime != "" {
  463. dao = dao.Where("a.created_time > ?", req.BeginTime)
  464. }
  465. if req.EndTime != "" {
  466. dao = dao.Where("a.created_time < ?", req.EndTime)
  467. }
  468. total, err := dao.Count()
  469. if err != nil {
  470. return 0, nil, err
  471. }
  472. if req.PageNum != 0 {
  473. dao = dao.Page(req.GetPage())
  474. }
  475. orderby := "a.created_time desc"
  476. if req.OrderBy != "" {
  477. orderby = req.OrderBy
  478. }
  479. dao = dao.Order(orderby)
  480. ents := []*contractmodel.CtrContractListRsp{}
  481. err = dao.Structs(&ents)
  482. if err != nil && err != sql.ErrNoRows {
  483. return 0, nil, err
  484. }
  485. return total, ents, nil
  486. }
  487. func (s distributorService) ProjectList(ctx context.Context, req *model.DistributorProjectListReq) (int, []*projmodel.ProjBusiness, error) {
  488. dao := &s.ProjDao.ProjBusinessDao
  489. if req.DistId != 0 {
  490. dao = dao.Where("distributor_id = ?", req.DistId)
  491. }
  492. if req.SearchText != "" {
  493. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  494. dao = dao.Where("(cust_name LIKE ? || nbo_name LIKE ?)", likestr, likestr)
  495. }
  496. if req.NboName != "" {
  497. likestr := fmt.Sprintf("%%%s%%", req.NboName)
  498. dao = dao.Where("nbo_name like ?", likestr)
  499. }
  500. if req.CustName != "" {
  501. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  502. dao = dao.Where("cust_name like ?", likestr)
  503. }
  504. if req.CreatedTimeStart != nil {
  505. dao = dao.Where("created_time > ?", req.CreatedTimeStart)
  506. }
  507. if req.CreatedTimeEnd != nil {
  508. dao = dao.Where("created_time < ?", req.CreatedTimeEnd)
  509. }
  510. total, err := dao.Count()
  511. if err != nil {
  512. return 0, nil, err
  513. }
  514. if req.PageNum != 0 {
  515. dao = dao.Page(req.GetPage())
  516. }
  517. orderby := "created_time desc"
  518. if req.OrderBy != "" {
  519. orderby = req.OrderBy
  520. }
  521. dao = dao.Order(orderby)
  522. ents := []*projmodel.ProjBusiness{}
  523. err = dao.Structs(&ents)
  524. if err != nil && err != sql.ErrNoRows {
  525. return 0, nil, err
  526. }
  527. return total, ents, nil
  528. }
  529. func (s distributorService) DynamicsList(ctx context.Context, req *model.DistributorDynamicsListReq) (int, interface{}, error) {
  530. dao := &s.DynamicsDao.BaseDistributorDynamicsDao
  531. if req.SearchText != "" {
  532. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  533. dao = dao.Where("(opn_people LIKE ? || opn_content LIKE ?)", likestr, likestr)
  534. }
  535. if req.DistId != 0 {
  536. dao = dao.Where("dist_id = ?", req.DistId)
  537. }
  538. if req.OpnPeopleId != 0 {
  539. dao = dao.Where("opn_people_id = ?", req.OpnPeopleId)
  540. }
  541. if req.OpnPeople != "" {
  542. likestr := fmt.Sprintf("%%%s%%", req.OpnPeople)
  543. dao = dao.Where("opn_people like ?", likestr)
  544. }
  545. if req.OpnType != "" {
  546. dao = dao.Where("opn_type = ?", req.OpnType)
  547. }
  548. if req.BeginTime != "" {
  549. dao = dao.Where("created_time > ?", req.BeginTime)
  550. }
  551. if req.EndTime != "" {
  552. dao = dao.Where("created_time < ?", req.EndTime)
  553. }
  554. total, err := dao.Count()
  555. if err != nil {
  556. return 0, nil, err
  557. }
  558. if req.PageNum != 0 {
  559. dao = dao.Page(req.GetPage())
  560. }
  561. orderby := "created_time desc"
  562. if req.OrderBy != "" {
  563. orderby = req.OrderBy
  564. }
  565. dao = dao.Order(orderby)
  566. ents := []*model.BaseDistributorDynamics{}
  567. err = dao.Structs(&ents)
  568. if err != nil && err != sql.ErrNoRows {
  569. return 0, nil, err
  570. }
  571. ret := map[string][]*model.BaseDistributorDynamics{}
  572. for _, ent := range ents {
  573. date := ent.OpnDate.Format("Y-m-d")
  574. ret[date] = append(ret[date], ent)
  575. }
  576. return total, ret, err
  577. }