MotorRecord.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * @param $value
  18. * @return false|string
  19. */
  20. public function getCreatedAtAttr($value)
  21. {
  22. return is_int($value)? ['val'=>$value,'text'=>date('m.d H:i', $value)] : $value;
  23. }
  24. /**
  25. * @desc 资金变动日志
  26. * @param Model $user 用户
  27. * @param string $field 字段
  28. * @param float $money 金额
  29. * @param int $type 类型
  30. * @param string $memo 备注
  31. * @param int $tag 收支
  32. * @return array|false
  33. * @date 2020/8/14/014
  34. */
  35. public function setMoneyLog(Model $user, string $field, float $money, int $type = 10, string $memo = '', int $tag = 1)
  36. {
  37. if (floatval($money) <= 0) {
  38. return false;
  39. }
  40. $before = 0;
  41. $after = 0;
  42. if ($tag == 2 && $field && isset($user->$field)) {
  43. $after = $user->$field;
  44. $before = $after + $money;
  45. }
  46. elseif ($tag == 1 && $field && isset($user->$field)) {
  47. $after = $user->$field;
  48. $before = $after - $money > 0 ? $after - $money : 0;
  49. }
  50. $this->money_log[] = [
  51. 'user_id' => $user['id'] ?? 0,
  52. 'money' => $money,
  53. 'before' => $before ?: 0,
  54. 'after' => $after ?: 0,
  55. 'tag' => $tag,
  56. 'type' => $type,
  57. 'memo' => $memo,
  58. ];
  59. return $this->money_log;
  60. }
  61. public function saveMoneyLog()
  62. {
  63. return $this->allowField(true)->saveAll($this->money_log);
  64. }
  65. public function getMonthTime($month)
  66. {
  67. //
  68. $firstday = date('Y-m-01', strtotime(date('Y') . '-' . $month . '-1'));
  69. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  70. return array($firstday,$lastday);
  71. }
  72. }