| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace app\v1\logic\tool\dataReport;
- use support\think\Db;
- class BaseDataReportLogic
- {
- protected function replaceData($where, $db='db_data_report'): bool
- {
- // 设置归属人
- $this->siteAuth();
- // 先删除,再写入
- Db::connect($db)->table($this->table)->where($where)->delete();
- // 保持每个子数组键一致,无值则填充0
- $insertData = $this->normalizeArrayKeys($this->data);
- return Db::connect($db)->table($this->table)->insertAll($insertData) !== false;
- }
- protected function pushData($rows): void
- {
- if ($rows) {
- foreach ($rows as $row) {
- $groupArr = explode(',', str_replace(' ', '', $this->group));
- $uniqueKey = [];
- foreach ($groupArr as $groupKey){
- $groupKey = trim($groupKey);
- $kk = $row[$groupKey] ?? "";
- $uniqueKey[] = $kk;
- }
- $uniqueKey = implode("_", $uniqueKey);
- foreach ($row as $k => $v) {
- if(in_array($k, $groupArr)){
- $this->data[$uniqueKey][$k] = $v;
- }else{
- $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
- }
- }
- }
- }
- }
- protected function siteAuth(): void
- {
- $agentList = Db::connect('db_advert')->table("agent_list")->column("id, media_id, auth_id");
- $agentListMap = array_column($agentList, null, "id");
- if($this->data){
- foreach ($this->data as &$item){
- $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0; // 负责人
- $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0; // 媒体
- }
- }
- }
- protected function normalizeArrayKeys(array $data): array {
- // 收集所有可能的键
- $allKeys = [];
- foreach ($data as $item) {
- $allKeys = array_merge($allKeys, array_keys($item));
- }
- $allKeys = array_unique($allKeys);
- // 标准化每个子数组
- $result = [];
- foreach ($data as $item) {
- $normalizedItem = [];
- foreach ($allKeys as $key) {
- $normalizedItem[$key] = $item[$key] ?? 0;
- }
- $result[] = $normalizedItem;
- }
- return $result;
- }
- }
|