MotorRecord.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\api\model\taxi;
  3. use app\common\model\BaseModel;
  4. use think\Model;
  5. class MotorRecord extends BaseModel
  6. {
  7. protected $name = 'users_motor_record';
  8. /**
  9. * @var array 资金变动日志
  10. */
  11. protected $money_log = [];
  12. public function getMoneyLog()
  13. {
  14. return $this->money_log;
  15. }
  16. /**
  17. * @desc 资金变动日志
  18. * @param Model $user 用户
  19. * @param string $field 字段
  20. * @param float $money 金额
  21. * @param int $type 类型
  22. * @param string $memo 备注
  23. * @param int $tag 收支
  24. * @return array|false
  25. * @date 2020/8/14/014
  26. */
  27. public function setMoneyLog(Model $user, string $field, float $money, int $type = 10, string $memo = '', int $tag = 1)
  28. {
  29. if (floatval($money) <= 0) {
  30. return false;
  31. }
  32. $before = 0;
  33. $after = 0;
  34. if ($tag == 2 && $field && isset($user->$field)) {
  35. $after = $user->$field;
  36. $before = $after + $money;
  37. }
  38. elseif ($tag == 1 && $field && isset($user->$field)) {
  39. $after = $user->$field;
  40. $before = $after - $money > 0 ? $after - $money : 0;
  41. }
  42. $this->money_log[] = [
  43. 'user_id' => $user['id'] ?? 0,
  44. 'money' => $money,
  45. 'before' => $before ?: 0,
  46. 'after' => $after ?: 0,
  47. 'tag' => $tag,
  48. 'type' => $type,
  49. 'memo' => $memo,
  50. ];
  51. return $this->money_log;
  52. }
  53. public function saveMoneyLog()
  54. {
  55. return $this->allowField(true)->saveAll($this->money_log);
  56. }
  57. public function getMonthTime($month)
  58. {
  59. //
  60. $firstday = date('Y-m-01', strtotime(date('Y') . '-' . $month . '-1'));
  61. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  62. return array($firstday,$lastday);
  63. }
  64. }