<?php // +---------------------------------------------------------------------- // | likeadmin快速开发前后端分离管理后台(PHP版) // +---------------------------------------------------------------------- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力 // | 开源版本可自由商用,可去除界面版权logo // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin // | github下载:https://github.com/likeshop-github/likeadmin // | 访问官网:https://www.likeadmin.cn // | likeadmin团队 版权所有 拥有最终解释权 // +---------------------------------------------------------------------- // | author: likeadminTeam // +---------------------------------------------------------------------- namespace app\common\model; use app\common\model\dict\DictData; use app\common\service\FileService; use think\Model; /** * 基础模型 * Class BaseModel * @package app\common\model */ class BaseModel extends Model { /** * @notes 公共处理图片,补全路径 * @param $value * @return string * @author 张无忌 * @date 2021/9/10 11:02 */ public function getImageAttr($value) { return trim($value) ? FileService::getFileUrl($value) : ''; } /** * @notes 公共图片处理,去除图片域名 * @param $value * @return mixed|string * @author 张无忌 * @date 2021/9/10 11:04 */ public function setImageAttr($value) { return trim($value) ? FileService::setFileUrl($value) : ''; } public function getAnnexAttr($value) { return !empty($value) ? json_decode($value, true) : ''; } public function getApproveCheckStatusTextAttr($value, $data) { $dict = DictData::where('type_value', 'check_status')->column('name', 'value'); return !empty($data['approve_check_status']) ? $dict[$data['approve_check_status']] : '待审核'; } /** * 统计sum */ public function statistics($where, $time, string $selectType, string $group = "", $sum = '') { switch ($selectType) { case "sum": $totalMoney = $this->where($where)->when(isset($time), function ($query) use ($time) { $query->whereBetweenTime('create_time', strtotime($time['start_time']), strtotime($time['end_time'])); })->sum($sum); break; case "group": $totalMoney = $this->getCurveData($where, $time, "sum($sum)", $group); break; default: throw new \Exception($this->name . ':selectType参数错误'); } if ($group) { $totalMoney = $this->trendYdata((array)$totalMoney, $time); } return $totalMoney; } /** * 统计count */ public function statistics_count($where, $time, string $selectType, string $group = "",) { switch ($selectType) { case "sum": $totalMoney = $this->where($where)->when(isset($time), function ($query) use ($time) { $query->whereBetweenTime('create_time', strtotime($time['start_time']), strtotime($time['end_time'])); })->count(); break; case "group": $totalMoney = $this->getCurveData($where, $time, "count(id)", $group); break; default: throw new \Exception($this->name . ':selectType参数错误'); } if ($group) { $totalMoney = $this->trendYdata((array)$totalMoney, $time); } return $totalMoney; } /** * 曲线统计 * @param $time * @param $type * @param $timeType * @return mixed */ public function getCurveData($where, $time, $str, string $group = 'create_time') { return $this->where($where) ->when(isset($time), function ($query) use ($time, $str, $group) { $query->whereBetweenTime($group, $time['start_time'], $time['end_time']); if ($time['days'] == 1) { $timeUinx = "%H"; } elseif ($time['days'] == 30) { $timeUinx = "%Y-%m-%d"; } elseif ($time['days'] == 365) { $timeUinx = "%Y-%m"; } elseif ($time['days'] > 1 && $time['days'] < 30) { $timeUinx = "%Y-%m-%d"; } elseif ($time['days'] > 30 && $time['days'] < 365) { $timeUinx = "%Y-%m"; } else { $timeUinx = "%Y-%m"; } $query->field("$str as number,FROM_UNIXTIME($group, '$timeUinx') as time"); $query->group("FROM_UNIXTIME($group, '$timeUinx')"); }) ->order("$group ASC")->select()->toArray(); } /** * 处理Y坐标数据 * @param array $data * @param array $timeKey * @return array * @throws \Exception */ public function trendYdata(array $data, array $timeKey) { $hourMoney = array(); $timeData = array(); //获取日期之间的天数 $getDayRange = function ($date, $timeKey) { $datearr = []; $stime = strtotime($timeKey['start_time']); $etime = strtotime($timeKey['end_time']); while ($stime <= $etime) { $datearr['x'][] = date($date, $stime); $datearr['y'][] = date($date, $stime); $stime = $stime + 86400; } return $datearr; }; //获取日期之间的月份 $getMonthRange = function ($date, $timeKey) { $datearr = []; $stime = date('Y-m-d', strtotime($timeKey['start_time'])); $etime = date('Y-m-d', strtotime($timeKey['end_time'])); $start = new \DateTime($stime); $end = new \DateTime($etime); $interval = \DateInterval::createFromDateString('1 month'); $period = new \DatePeriod($start, $interval, $end); foreach ($period as $dt) { $datearr['x'][] = $dt->format($date); $datearr['y'][] = $dt->format($date); } return $datearr; }; if ($timeKey['days'] == 1) { for ($i = 0; $i <= 24; $i++) { $timeData['x'][] = (string)($i < 10 ? ('0' . $i) : $i); $timeData['y'][] = $i < 10 ? ('0' . $i) : $i; //$timeData['y'][] = $i < 10 ? ('0' . $i . ":00") : $i . ":00"; //$timeData['x'][] = $i < 10 ? ('0' . $i . ":00") : $i . ":00"; } } elseif ($timeKey['days'] == 30) { $timeData = $getDayRange('Y-m-d', $timeKey); } elseif ($timeKey['days'] == 365) { $timeData = $getMonthRange('Y-m', $timeKey); } elseif ($timeKey['days'] > 1 && $timeKey['days'] < 30) { $timeData = $getDayRange('Y-m-d', $timeKey); } elseif ($timeKey['days'] > 30 && $timeKey['days'] < 365) { $timeData = $getMonthRange('Y-m', $timeKey); } if ($data) { $hourMoney = array_column($data, 'number', 'time'); } $y = array(); foreach ($timeData['y'] as $k => $v) { if (array_key_exists($v, $hourMoney)) { $y[$v] = $hourMoney[$v]; } else { $y[$v] = 0; } } return ['x' => $timeData['x'], 'y' => $y]; } }