FinanceService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\FinanceModel;
  13. use App\Services\BaseService;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 平台财务管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class FinanceService
  20. * @package App\Services\Common
  21. */
  22. class FinanceService extends BaseService
  23. {
  24. protected static $instance=null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * FinanceService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new FinanceModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15)
  52. {
  53. $where = ['a.mark' => 1];
  54. $status = isset($params['status'])? $params['status'] : 0;
  55. $type = isset($params['type'])? $params['type'] : 0;
  56. if($status>0){
  57. $where['a.status'] = $status;
  58. }
  59. if($type>0){
  60. $where['a.type'] = $type;
  61. }
  62. $list = $this->model->from('finance as a')
  63. ->where($where)
  64. ->where(function ($query) use($params){
  65. // 日期
  66. $date = isset($params['date']) ? $params['date'] : [];
  67. $start = isset($date[0])? $date[0] : '';
  68. $end = isset($date[1])? $date[1] : '';
  69. $end = $start>=$end? '' : $end;
  70. if ($start) {
  71. $query->where('a.create_time','>=', strtotime($start));
  72. }
  73. if($end){
  74. $query->where('a.create_time','<', strtotime($end));
  75. }
  76. })
  77. ->select(['a.*'])
  78. ->orderBy('a.create_time','desc')
  79. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  80. $list = $list? $list->toArray() :[];
  81. if($list){
  82. foreach($list['data'] as &$item){
  83. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  84. $item['date'] = $item['create_time']? datetime($item['create_time'],'Y-m-d') : $item['date'];
  85. }
  86. }
  87. return [
  88. 'pageSize'=> $pageSize,
  89. 'total'=>isset($list['total'])? $list['total'] : 0,
  90. 'list'=> isset($list['data'])? $list['data'] : []
  91. ];
  92. }
  93. /**
  94. * 统计
  95. * @param $params
  96. * @return array
  97. */
  98. public function getCount($params)
  99. {
  100. $where = ['a.mark' => 1];
  101. $status = isset($params['status'])? $params['status'] : 0;
  102. $type = isset($params['type'])? $params['type'] : 0;
  103. if($status>0){
  104. $where['a.status'] = $status;
  105. }
  106. if($type>0){
  107. $where['a.type'] = $type;
  108. }
  109. $model = $this->model->from('finance as a')
  110. ->where($where)
  111. ->where(function ($query) use($params){
  112. // 日期
  113. $date = isset($params['date']) ? $params['date'] : [];
  114. $start = isset($date[0])? $date[0] : '';
  115. $end = isset($date[1])? $date[1] : '';
  116. $end = $start>=$end? '' : $end;
  117. if ($start) {
  118. $query->where('a.create_time','>=', strtotime($start));
  119. }
  120. if($end){
  121. $query->where('a.create_time','<', strtotime($end));
  122. }
  123. });
  124. $counts = [
  125. 'income'=> $model->sum('a.income'),
  126. 'expend'=> $model->sum('a.expend'),
  127. ];
  128. return $counts;
  129. }
  130. /**
  131. * 按日期统计流水金额
  132. * @param string $beginAt 开始时间
  133. * @param string $endAt 结束时间
  134. * @param int $field 类型:income-收入,expand-支出
  135. * @return mixed
  136. */
  137. public function getTotalByTime($beginAt='', $endAt='', $field='income')
  138. {
  139. $where = ['mark' => 1,'user_id'=>0,'coin_type'=>1];
  140. $field = !in_array($field,['income','expand'])? 'income' : $field;
  141. return $this->model->where($where)->where(function($query) use($beginAt,$endAt){
  142. if($beginAt && $endAt){
  143. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  144. }else if($beginAt){
  145. $query->where('create_time','>=', strtotime($beginAt));
  146. }
  147. })->sum($field);
  148. }
  149. /**
  150. * 添加或编辑
  151. * @return array
  152. * @since 2020/11/11
  153. * @author laravel开发员
  154. */
  155. public function edit()
  156. {
  157. $data = request()->all();
  158. return parent::edit($data); // TODO: Change the autogenerated stub
  159. }
  160. /**
  161. * 平台结算
  162. * @param $money
  163. * @param int $changeType
  164. * @param int $type 类型:1-增加,2-扣除
  165. * @return mixed
  166. */
  167. public function settleBonus($money, $changeType = 1, $type = 1)
  168. {
  169. $date = date('Y-m-d');
  170. $info = $this->model->where(['date'=> $date,'mark'=>1])->first();
  171. if(!$info){
  172. $data = ['date'=> $date,'type'=>1,'create_time'=>time(),'update_time'=> time(),'status'=>1];
  173. if($changeType==1){
  174. $data['income'] = $money;
  175. }else{
  176. $data['expend'] = $money;
  177. }
  178. return $this->model->insertGetId($data);
  179. }else{
  180. if($changeType == 1){
  181. $info->income += $money;
  182. }else{
  183. $info->expend += $money;
  184. }
  185. return $info->save();
  186. }
  187. }
  188. }