REAPI/app/order/service/KamiOrderHistoryService.php

340 lines
14 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\order\service;
use app\core\Service;
use app\merchant\service\MerchantLogService;
use think\admin\extend\CodeExtend;
use app\merchant\service\MerchantService;
use app\channel\service\ChannelService;
use app\channel\service\ProductService;
use app\merchant\service\PercentService;
/**
* 商户卡密已完成订单服务
* Class MerchantService
* @package app\merchant\service
*/
class KamiOrderHistoryService extends Service
{
/**
* 设置默认操作表
* @var string
*/
public $table = 'merchant_order_history_kami91';
# 提交订单
public function up($data)
{
$id = $this->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);
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 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();
}
}
}
}
}