FinanceService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  15. * 平台财务管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class FinanceService
  19. * @package App\Services\Common
  20. */
  21. class FinanceService extends BaseService
  22. {
  23. protected static $instance=null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * FinanceService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new FinanceModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['a.mark' => 1];
  53. $status = isset($params['status'])? $params['status'] : 0;
  54. $type = isset($params['type'])? $params['type'] : 0;
  55. if($status>0){
  56. $where['a.status'] = $status;
  57. }
  58. if($type>0){
  59. $where['a.type'] = $type;
  60. }
  61. $list = $this->model->from('finance as a')
  62. ->where($where)
  63. ->where(function ($query) use($params){
  64. // 日期
  65. $date = isset($params['date']) ? $params['date'] : [];
  66. $start = isset($date[0])? $date[0] : '';
  67. $end = isset($date[1])? $date[1] : '';
  68. $end = $start>=$end? '' : $end;
  69. if ($start) {
  70. $query->where('a.create_time','>=', strtotime($start));
  71. }
  72. if($end){
  73. $query->where('a.create_time','<', strtotime($end));
  74. }
  75. })
  76. ->select(['a.*'])
  77. ->orderBy('a.create_time','desc')
  78. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  79. $list = $list? $list->toArray() :[];
  80. if($list){
  81. foreach($list['data'] as &$item){
  82. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  83. }
  84. }
  85. return [
  86. 'pageSize'=> $pageSize,
  87. 'total'=>isset($list['total'])? $list['total'] : 0,
  88. 'list'=> isset($list['data'])? $list['data'] : []
  89. ];
  90. }
  91. /**
  92. * 添加或编辑
  93. * @return array
  94. * @since 2020/11/11
  95. * @author laravel开发员
  96. */
  97. public function edit()
  98. {
  99. $data = request()->all();
  100. return parent::edit($data); // TODO: Change the autogenerated stub
  101. }
  102. /**
  103. * 平台结算
  104. * @param $money
  105. * @param int $changeType
  106. * @param int $type 类型:1-增加,2-扣除
  107. * @return mixed
  108. */
  109. public function settleBonus($money, $changeType = 1, $type = 1)
  110. {
  111. $date = date('Y-m-d');
  112. $info = $this->model->where(['date'=> $date,'mark'=>1])->first();
  113. if(!$info){
  114. $data = ['date'=> $date,'type'=>1,'create_time'=>time(),'update_time'=> time(),'status'=>1];
  115. if($changeType==1){
  116. $data['income'] = $money;
  117. }else{
  118. $data['expend'] = $money;
  119. }
  120. return $this->model->insertGetId($data);
  121. }else{
  122. if($changeType == 1){
  123. $info->income += $money;
  124. }else{
  125. $info->expend += $money;
  126. }
  127. return $info->save();
  128. }
  129. }
  130. }