Bill.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\api\model\massager;
  3. use app\api\model\BaseModel;
  4. class Bill extends BaseModel
  5. {
  6. // 表名
  7. protected $name = 'massager_bill';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'type_text'
  17. ];
  18. public function getTypeList()
  19. {
  20. return ['money' => __('Money'), 'score' => __('Score')];
  21. }
  22. public function getTypeTextAttr($value, $data)
  23. {
  24. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  25. $list = $this->getTypeList();
  26. return isset($list[$value]) ? $list[$value] : '';
  27. }
  28. public function sumByMassagerAndThisMonth($m_id)
  29. {
  30. $sum = $this->query("
  31. SELECT
  32. SUM( `CHANGE` ) AS tp_sum
  33. FROM
  34. `ma_massager_bill`
  35. WHERE
  36. ( DATE_FORMAT( FROM_UNIXTIME( createtime ), '%Y-%m' ) = date_format( now(), '%Y-%m' ) )
  37. AND `massager_id` = {$m_id}
  38. AND `currency_type` = 'score'
  39. LIMIT 1
  40. ");
  41. return fixed2Float($sum[0]["tp_sum"]);
  42. }
  43. function fetchBill($m_id, $currency_type, $change_types, $page = 1, $size = 10)
  44. {
  45. $query = $this->where([
  46. "massager_id" => $m_id,
  47. "currency_type" => $currency_type
  48. ]);
  49. if (!in_array("*", $change_types)) {
  50. $query->where("change_type", "in", $change_types);
  51. }
  52. return $query->order("createtime", "desc")
  53. ->page($page)
  54. ->paginate($size);
  55. }
  56. }