DayAgentModel.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * DayPayModel.php UTF-8
  4. *
  5. *
  6. * @date : 2017/12/18 11:41
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : liguanglong <lgl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\data;
  13. use huo\model\user\UserModel;
  14. class DayAgentModel extends DayBaseModel {
  15. protected $table = 'dw_day_agent';
  16. public $map = [];
  17. public function agent() {
  18. return $this->belongsTo('huo\model\user\UserModel', 'agent_id', 'id')
  19. ->field('id,user_login,user_nicename,role_id,parent_id');
  20. }
  21. public function mpagent() {
  22. return $this->belongsTo(UserModel::className(), 'agent_id', 'id')->setEagerlyType(0);
  23. }
  24. /**
  25. * 根据日期获取
  26. *
  27. * @param string $field 字段,包括别名
  28. * @param string $start_date 开始日期
  29. * @param string $end_date 结束日期
  30. *
  31. * @param string $alias 别名
  32. *
  33. * @return array
  34. */
  35. public function getByDate($field, $start_date, $end_date, $alias = null) {
  36. $alias or $alias = $field;
  37. $_map = $this->map;
  38. $_rows = $this->where($_map)->whereBetween('date', [$start_date, $end_date])
  39. ->field("date, $field")
  40. ->select()->toArray();
  41. $_data = [];
  42. foreach ($_rows as $_row) {
  43. $_data[$_row['date']] = $_row[$alias];
  44. }
  45. $result = [];
  46. for ($_i = strtotime($start_date); $_i <= strtotime($end_date); $_i += 24 * 3600) {
  47. $_key = date('Y-m-d', $_i);
  48. $result[date('m-d', $_i)] = isset($_data[$_key]) ? doubleval($_data[$_key]) : 0;
  49. }
  50. return $result;
  51. }
  52. }