db()->insert($data); return $id; } # 获取某一个商户的订单总额 public function getTotal($mid, $status = 1) { $where['mid'] = $mid; if ($status > 0) { $where['status'] = $status; } return $this->db()->where($where)->sum('cash'); } # 查找订单 public function get($order_id, $merchant_order_id = false, $mid = false) { if ($merchant_order_id) { $where['merchant_order_id'] = $merchant_order_id; } else { if (is_numeric($order_id)) { $where['id'] = $order_id; } else { $where['order_id'] = $order_id; } } if ($mid) { $where['mid'] = $mid; } return $this->db()->where($where)->find(); } # 查找下单订单 public function getData() { $cur = time(); $time = 12 * 3600; $whereRaw = $cur . '-unix_timestamp(create_at) <=' . $time; return $this->db()->whereRaw('status = 1 and ' . $whereRaw)->order('id asc')->select(); } # 查找回调错误的订单 public function getErrorData() { return $this->db()->whereRaw('merchant_callback_error = 2 and merchant_callback_num < 5 and status in(2,3)')->order('id asc')->select(); } # 获取某一个商户的订单数量 public function getOrderTotal($mid, $status = 1) { $where['mid'] = $mid; if ($status > 0) { $where['status'] = $status; } return $this->db()->where($where)->count(); } public function cancelOrder($v){ $v['order']=$v['order_id']; MerchantService::instance()->qxdd($v); } # 获取成功的订单 public function getTotalData($day, $cid = false, $pid = false) { if (!$day) { $day = date('Y-m-d', strtotime('-1 day')); } $begin = $day . ' 00:00:00'; $after = $day . ' 23:59:59'; $time = [$begin, $after]; $db = $this->db()->whereRaw('status in(2,3) '); $where = array(); if ($cid) { $where['cid'] = $cid; } if ($pid) { $where['pid'] = $pid; } if ($where) { $db->where($where); } $data = $db->whereBetween('create_at', $time)->order('id asc')->select()->toArray(); $result = array(); if ($data) { $result['success'] = 0; $result['fail'] = 0; $result['total'] = 0; $result['product_cash'] = 0; $result['actual_cash'] = 0; $result['profit'] = 0; # 计算每个产品的总数 $result['product'] = array(); # 计算每个商户的总数 $result['merchant'] = array(); # 计算每个商户的总数 $result['merchant_total'] = array(); # 产品信息 $result['product_info'] = array(); $productService = ProductService::instance(); foreach ($data as $k => $v) { # 检查是否是固定数值 if (!isset($result['product_info'][$v['pid']])) { $result['product_info'][$v['pid']] = $productService->getInfo($v['pid']); } $product = $result['product_info'][$v['pid']]; if ($product) { if ($product['rule']) { $product['percent'] = MerchantService::instance()->getPercent($product['percent'], $v['account'], $v['product_key'], $v['cash'], $product['rule'], $v['isp']); } $product_percent_type = 1; if (isset($product['percent_type']) && $product['percent_type']) { $product_percent_type = $product['percent_type']; } if ($product_percent_type == 2) { $v['product_cash'] = $product['percent']; } else { $v['product_cash'] = $product['percent']*$v['cash']; } } if ($v['status'] == 3) { $result['fail'] += $v['cash']; } else { $result['success'] += $v['cash']; if (!isset($result['product'][$v['pid']])) { $result['product'][$v['pid']]['num'] = 0; $result['product'][$v['pid']]['actual_num'] = 0; $result['product'][$v['pid']]['percent'] = array(); } $result['product'][$v['pid']]['num'] += $v['cash']; $result['product'][$v['pid']]['actual_num'] += $v['product_cash']; $result['product'][$v['pid']]['actual_num'] = round($result['product'][$v['pid']]['actual_num'], 2); // var_dump($v);die; if (isset($result['product_info'][$v['pid']]['percent_type']) && $result['product_info'][$v['pid']]['percent_type'] == 2) { $percent = $v['product_cash']; } else { $percent = $v['product_cash']/$v['cash']; } $percent = 'k_' . $percent; if (!isset($result['product'][$v['pid']]['percent'][$percent][$v['cash']])) { $result['product'][$v['pid']]['percent'][$percent][$v['cash']]['num'] = 0; $result['product'][$v['pid']]['percent'][$percent][$v['cash']]['cash'] = $v['product_cash']; } $result['product'][$v['pid']]['percent'][$percent][$v['cash']]['num'] += 1; if (!isset($result['merchant'][$v['mid']][$v['pid']])) { $result['merchant'][$v['mid']][$v['pid']]['num'] = 0; $result['merchant'][$v['mid']][$v['pid']]['actual_num'] = 0; $result['merchant'][$v['mid']][$v['pid']]['product_num'] = 0; $result['merchant'][$v['mid']][$v['pid']]['percent'] = array(); $result['merchant'][$v['mid']][$v['pid']]['profit'] = 0; } $result['merchant'][$v['mid']][$v['pid']]['num'] += $v['cash']; $result['merchant'][$v['mid']][$v['pid']]['actual_num'] += $v['actual_cash']; $result['merchant'][$v['mid']][$v['pid']]['product_num'] += $v['product_cash']; $result['merchant'][$v['mid']][$v['pid']]['actual_num'] = round($result['merchant'][$v['mid']][$v['pid']]['actual_num'], 2); $result['merchant'][$v['mid']][$v['pid']]['product_num'] = round($result['merchant'][$v['mid']][$v['pid']]['product_num'], 2); $result['merchant'][$v['mid']][$v['pid']]['profit'] += $v['actual_cash'] - $v['product_cash']; $result['merchant'][$v['mid']][$v['pid']]['profit'] = round($result['merchant'][$v['mid']][$v['pid']]['profit'], 2); if (isset($result['product_info'][$v['pid']]['percent_type']) && $result['product_info'][$v['pid']]['percent_type'] == 2) { $percent = $v['actual_cash']; } else { $percent = $v['actual_cash']/$v['cash']; } $percent = 'k_' . $percent; if (!isset($result['merchant'][$v['mid']][$v['pid']]['percent'][$percent][$v['cash']])) { $result['merchant'][$v['mid']][$v['pid']]['percent'][$percent][$v['cash']]['num'] = 0; $result['merchant'][$v['mid']][$v['pid']]['percent'][$percent][$v['cash']]['cash'] = $v['actual_cash']; } $result['merchant'][$v['mid']][$v['pid']]['percent'][$percent][$v['cash']]['num'] += 1; if (!isset($result['merchant_total'][$v['mid']][$v['product_key']])) { $result['merchant_total'][$v['mid']][$v['product_key']]['num'] = 0; $result['merchant_total'][$v['mid']][$v['product_key']]['actual_num'] = 0; $result['merchant_total'][$v['mid']][$v['product_key']]['product_num'] = 0; $result['merchant_total'][$v['mid']][$v['product_key']]['cash'] = array(); $result['merchant_total'][$v['mid']][$v['product_key']]['profit'] = 0; } $result['merchant_total'][$v['mid']][$v['product_key']]['num'] += $v['cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['actual_num'] += $v['actual_cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['product_num'] += $v['product_cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['actual_num'] = round($result['merchant_total'][$v['mid']][$v['product_key']]['actual_num'], 2); $result['merchant_total'][$v['mid']][$v['product_key']]['product_num'] = round($result['merchant_total'][$v['mid']][$v['product_key']]['product_num'], 2); $result['merchant_total'][$v['mid']][$v['product_key']]['profit'] += $v['actual_cash'] - $v['product_cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['profit'] = round($result['merchant_total'][$v['mid']][$v['product_key']]['profit'], 2); if (!isset($result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']])) { $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['num'] = 0; $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['actual_num'] = 0; $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['product_num'] = 0; } $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['num'] += $v['cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['actual_num'] += $v['actual_cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['product_num'] += $v['product_cash']; $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['actual_num'] = round($result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['actual_num'], 2); $result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['product_num'] = round($result['merchant_total'][$v['mid']][$v['product_key']]['cash'][$v['cash']]['product_num'], 2); $result['product_cash'] += $v['product_cash']; $result['actual_cash'] += $v['actual_cash']; $result['profit'] = round($result['actual_cash'] - $result['product_cash'], 2); } /* if (isset($result['product_info'][$v['pid']]['percent_type']) && $result['product_info'][$v['pid']]['percent_type'] == 2 && $v['status'] != 3) { $result['success'] += $v['cash']; if (!isset($result['product'][$v['pid']])) { $result['product'][$v['pid']] = 0; } if (!isset($result['product_num'][$v['pid']])) { $result['product_num'][$v['pid']] = 0; } $result['product'][$v['pid']] += $v['cash']; $result['product_num'][$v['pid']] += 1; if (!isset($result['merchant'][$v['mid']][$v['pid']])) { $result['merchant'][$v['mid']][$v['pid']] = 0; } if (!isset($result['merchant_num'][$v['mid']][$v['pid']])) { $result['merchant_num'][$v['mid']][$v['pid']] = 0; } $result['merchant'][$v['mid']][$v['pid']] += $v['cash']; $result['merchant_num'][$v['mid']][$v['pid']] += 1; } else { if ($v['status'] == 2) { $result['success'] += $v['cash']; if (!isset($result['product'][$v['pid']])) { $result['product'][$v['pid']] = 0; } $result['product'][$v['pid']] += $v['cash']; if (!isset($result['merchant'][$v['mid']][$v['pid']])) { $result['merchant'][$v['mid']][$v['pid']] = 0; } $result['merchant'][$v['mid']][$v['pid']] += $v['cash']; } else { $result['fail'] += $v['cash']; } } */ } $result['total'] = $result['success'] + $result['fail']; } $test = input('test'); if ($test == 1) { print_r($result);die; } return $result; } # 修改商户回调信息 public function upMerchantMsg($order_id, $num, $msg, $error = 0) { $data['merchant_callback_msg'] = $msg; $data['merchant_callback_at'] = date('Y-m-d H:i:s'); $data['merchant_callback_num'] = $num + 1; if ($error > 0) { $data['merchant_callback_error'] = $error; } if (is_numeric($order_id)) { $where['id'] = $order_id; } else { $where['order_id'] = $order_id; } $this->db()->where($where)->update($data); } # 修正未扣款的订单 public function kou($day = false) { if (!$day) { $day = date('Y-m-d', strtotime('-1 day')); } $begin = $day . ' 00:00:00'; $after = $day . ' 23:59:59'; $time = [$begin, $after]; $db = $this->db()->whereRaw('status in(2,3) '); $data = $db->whereBetween('create_at', $time)->order('id asc')->select()->toArray(); if ($data) { $service = MerchantLogService::instance(); foreach ($data as $k => $v) { $where = array(); $where['mid'] = $v['mid']; if (strstr($v['order_id'], '_')) { $temp = explode('_', $v['order_id']); $v['order_id'] = $temp[0]; } $where['order_id'] = $v['order_id']; $log = $service->db()->where($where)->select()->toArray(); $num = count($log); if ($v['status'] == 3) { # 只有一条,要补充一下 if ($num == 1) { if ($log[0]['type'] == 3) { MerchantService::instance()->resFrozenAccount($v, $v['order_id'], $v['mid'], $v['pid'], $v['product_key'], $v['cash'], $v['project_id'], $v['account'], $v['isp']); } elseif ($log[0]['type'] == 4) { MerchantService::instance()->setFrozenAccount($v, $v['order_id'], $v['mid'], $v['pid'], $v['product_key'], $v['cash'], $v['project_id'], $v['account'], $v['isp'], $v['create_at']); } } elseif ($num == 2) { foreach ($log as $k1 => $v1) { $service->db()->where(array('id' => $v1['id']))->update(array('order_date' => $v['create_at'], 'create_at' => $v['create_at'])); } } } elseif ($v['status'] == 2) { if ($num <= 0) { MerchantService::instance()->setFrozenAccount($v, $v['order_id'], $v['mid'], $v['pid'], $v['product_key'], $v['cash'], $v['project_id'], $v['account'], $v['isp'], $v['create_at']); } } } } } # 删除订单 public function del($id) { $where['id'] = $id; return $this->db()->where($where)->delete(); } # 删除重复数据 public function delcf($day) { if (!$day) { $day = date('Y-m-d', strtotime('-1 day')); } $begin = $day . ' 00:00:00'; $after = $day . ' 23:59:59'; $time = [$begin, $after]; $db = $this->db(); $data = $db->whereBetween('create_at', $time)->order('id asc')->select()->toArray(); if ($data) { $result = array(); foreach ($data as $k => $v) { if (!isset($result[$v['order_id']])) { $result[$v['order_id']] = array(); } $result[$v['order_id']][] = $v['id']; } foreach ($result as $k => $v) { $num = count($v); if ($num > 1 && isset($v[0]) && isset($v[1])) { $this->db()->where(array('id' => $v[0]))->delete(); } } } } public function callSend($v, $type, $limit = true) { if ($v['status'] == 2 || $v['status'] == 3) { if (!$v['channel_callback_msg']) { $v['channel_callback_msg'] = '{}'; } $request = json_decode($v['channel_callback_msg'], true); $update['project_id'] = false; if (isset($v['project_id'])) { $update['project_id'] = $v['project_id']; } if (isset($v['card_id'])) { $update['card_id'] = $v['card_id']; } $update['account'] = $v['account']; $update['status'] = $v['status']; $update['data'] = $request; $update['cash'] = $v['cash']; $update['merchant_order_id'] = $v['merchant_order_id']; $update['order_id'] = $v['order_id']; $update['notify_num'] = $v['merchant_callback_num']; $number = ''; if (isset($request['s_nubmer']) && $request['s_nubmer']) { $number = $request['s_nubmer']; } elseif (isset($request['s_number']) && $request['s_number']) { $number = $request['s_number']; } $order_name = 's_nubmer'; if ($number && $order_name) { $update[$order_name] = $number; } elseif ($number) { $update['s_nubmer'] = $number; } if (isset($request['code']) && $request['code']) { $update['code'] = $request['code']; } if (isset($request['msg']) && $request['msg']) { $update['msg'] = $request['msg']; } if (isset($v['isp']) && $v['isp']) { $update['isp'] = $v['isp']; } if (isset($v['param']) && $v['param']) { $v['param'] = json_decode($v['param'], true); } else { $v['param'] = false; } if ($v['param'] && isset($v['param']['notify']) && $v['param']['notify']) { $update['notify'] = $v['param']['notify']; } # 商户后续操作 //if (!$v['merchant_callback_msg'] && !$v['merchant_callback_at']) { if (isset($v['error_account_oper']) && $v['error_account_oper'] == 1 && $type == 1) { # 需要进行扣费 MerchantService::instance()->up($v['id'], $v['mid'], $v['pid'], $v['product_key'], $update); } else { # 直接发送 MerchantService::instance()->notify($v['id'], $v['mid'], $v['pid'], $v['product_key'], $update, $type, $limit); } } } }