FinancialRecordDao.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\system\merchant;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\system\merchant\FinancialRecord;
  14. class FinancialRecordDao extends BaseDao
  15. {
  16. protected function getModel(): string
  17. {
  18. return FinancialRecord::class;
  19. }
  20. /**
  21. * @return string
  22. * @author xaboy
  23. * @day 2020/6/9
  24. */
  25. public function getSn()
  26. {
  27. list($msec, $sec) = explode(' ', microtime());
  28. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  29. $orderId = 'jy' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  30. return $orderId;
  31. }
  32. public function inc(array $data, $merId)
  33. {
  34. $data['mer_id'] = $merId;
  35. $data['financial_pm'] = 1;
  36. $data['financial_record_sn'] = $this->getSn();
  37. return $this->create($data);
  38. }
  39. public function dec(array $data, $merId)
  40. {
  41. $data['mer_id'] = $merId;
  42. $data['financial_pm'] = 0;
  43. $data['financial_record_sn'] = $this->getSn();
  44. return $this->create($data);
  45. }
  46. public function search(array $where)
  47. {
  48. $query = $this->getModel()::getDB()
  49. ->when(isset($where['financial_type']) && $where['financial_type'] !== '', function ($query) use ($where) {
  50. $query->whereIn('financial_type', $where['financial_type']);
  51. })
  52. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  53. $query->where('mer_id', $where['mer_id']);
  54. })
  55. ->when(isset($where['user_info']) && $where['user_info'] !== '', function ($query) use ($where) {
  56. $query->where('user_info', $where['user_info']);
  57. })
  58. ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
  59. $query->where('user_id', $where['user_id']);
  60. })
  61. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  62. $query->whereLike('order_sn|user_info|financial_record_sn', "%{$where['keyword']}%");
  63. })
  64. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  65. getModelTime($query, $where['date'], 'create_time');
  66. })
  67. ->when(isset($where['is_mer']) && $where['is_mer'] !== '', function ($query) use ($where) {
  68. if($where['is_mer']){
  69. $query->where('mer_id',$where['is_mer'])->where('type','in',[0,1]);
  70. }else{
  71. $query->where('type','in',[1,2]);
  72. }
  73. });
  74. return $query;
  75. }
  76. /**
  77. * TODO 根据条件和时间查询出相对类型的数量个金额
  78. * @param int $type
  79. * @param array $where
  80. * @param string $date
  81. * @param array $financialType
  82. * @return array
  83. * @author Qinii
  84. * @day 4/14/22
  85. */
  86. public function getDataByType(int $type, array $where, string $date, array $financialType)
  87. {
  88. if (empty($financialType)) return [0,0];
  89. $query = $this->search($where)->where('financial_type','in',$financialType);
  90. if($type == 1) {
  91. $query->whereDay('create_time',$date);
  92. } else {
  93. $query->whereMonth('create_time',$date);
  94. }
  95. $count = $query->group('order_id')->count();
  96. $number = $query->sum('number');
  97. return [$count,$number];
  98. }
  99. }