REAPI/app/order/controller/Kami91Order.php

445 lines
13 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------
namespace app\order\controller;
use think\admin\Controller;
use app\order\service\Kami91OrderService;
use app\merchant\service\MerchantService;
use app\merchant\service\MerchantLogService;
use app\merchant\service\PercentService;
use app\merchant\service\KamiOrderService as OrderBase;
use app\merchant\service\OrderHistoryService;
use app\channel\service\CardService;
use app\merchant\service\CardService as CardLogService;
use \PhpOffice\PhpSpreadsheet\IOFactory;
use dever\Log;
/**
* 卡密订单列表
* Class Kami91Order
* @package app\order\controller
*/
class Kami91Order extends Controller
{
/**
* 绑定数据表
* @var string
*/
public $table = 'merchant_order_kami91';
/**
* cell
*
* @var array
*/
private $cell = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
/**
* 卡密订单列表
* @auth true
* @menu true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->title = '自动发货卡密订单映射列表';
$query = $this->getDb();
$query->equal('mid,pid,cardno,cash,product_key')->like('order_id,merchant_order_id')->dateBetween('create_at,callback_at');
// try{
// $query->order('create_at desc')->page();
// }catch(\Exception $e){
// var_dump($e);exit;
// }
if (input('output') === 'json') {
$result = $query->order('create_at desc')->page(true, false);
$this->success('获取数据列表成功', $result);
} else {
$res= $query->order('create_at desc')->page();
}
}
private function getDb()
{
$data_type = input('data_type', 1);
if ($data_type == 2) {
$this->table = 'merchant_order_history_kami91';
}
$this->data_type = $data_type;
// $this->isp = PercentService::instance()->getIsp();
$query = $this->_query($this->table);
$time = input('time');
if ($time > 0) {
$start = $end = 0;
switch ($time) {
case 1:
$start = 0;
$end = 60;
break;
case 2:
$start = 60;
$end = 600;
break;
case 3:
$start = 600;
$end = 1200;
break;
case 4:
$start = 1200;
$end = 1800;
break;
case 5:
$start = 1800;
$end = 2400;
break;
case 6:
$start = 2400;
$end = 3000;
break;
case 7:
$start = 3000;
$end = 3600;
break;
case 8:
$start = 3600;
$end = 8640000;
break;
default:
# code...
break;
}
$cur = time();
$sql = '(status in(2,3) and unix_timestamp(callback_at) - unix_timestamp(create_at) > ' . $start . ' and unix_timestamp(callback_at) - unix_timestamp(create_at) <= ' . $end .')';
$sql .= ' or ';
$sql .= '(status not in(2,3) and '.$cur.' - unix_timestamp(create_at) > ' . $start . ' and '.$cur.' - unix_timestamp(create_at) <= ' . $end .')';
//$query->whereRaw(' channel_callback_at > 0 and timestampdiff(second,create_at, channel_callback_at) > ' . $start . ' and timestampdiff(second,create_at, channel_callback_at) <= ' . $end);
$query->whereRaw($sql);
}
$status = input('status');
if ($status) {
if ($status == 4) {
$query->whereRaw('status in(4,5)');
} else {
$query->whereRaw('status = ' . $status);
}
}
// $is_number = input('is_number');
//
// if ($is_number == 1) {
// # 有流水号的
// $query->whereRaw(' channel_callback_msg like("%s_nubmer%")');
// } elseif ($is_number == 2) {
// # 无流水号的
// $query->whereRaw(' channel_callback_msg not like("%s_nubmer%")');
// }
return $query;
}
/**
* 实时统计
* @login true
* @auth true
* @param integer $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function stat()
{
$query = $this->getDb();
$config = $this->getConfig();
$this->total = Kami91OrderService::instance()->total();
$this->_form($this->table, 'stat');
}
/**
* 查看详细数据
* @login true
* @auth true
* @param integer $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function info($id = 0)
{
$data_type = input('data_type', 1);
if ($data_type == 2) {
$this->table = 'merchant_order_history_kami91';
}
$this->_applyFormToken();
$this->verify = false;
$this->_form($this->table, 'info');
}
public function set()
{
$order_id = input('order_id');
if (!$order_id) {
$this->error('请选择订单');
}
$status = input('status');
$msg = input('msg');
$data_type = input('data_type', 1);
$order = Kami91OrderService::instance();
// $order->setCallback($data_type, $order_id, $status, $msg);
$where = array();
$where['order_id'] = $order_id;
$data = [];
$data['callback_msg'] = '{}';
$data['callback_at'] = date('Y-m-d H:i:s');
$data['status'] = $status;
$order->db()->where($where)->update($data);
$this->success('设置成功,商户回调结果要等系统统一推送才会同步,请关注同步结果!也可以手动发起回调!', '');
}
/**
* 数据列表处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _page_filter(&$data)
{
$this->mlist = Kami91OrderService::instance()->getMerchantList(true);
$this->plist = Kami91OrderService::instance()->getProductList(true);
$this->pkeylist = Kami91OrderService::instance()->getProductKeyList(true);
$this->search = $this->getConfig();
$page = input('page', 1);
$limit = input('limit', 20);
foreach ($data as $k => &$vo) {
$k = $k + 1;
$vo['index'] = $k;
if ($page > 1) {
$vo['index'] = $k + ($limit * ($page - 1));
}
$vo['minfo'] = $this->mlist[$vo['mid']] ?? false;
$vo['pinfo'] = $this->plist[$vo['pid']] ?? false;
$vo['time'] = '';
// if ($vo['callback_msg']) {
// $vo['callback_msg'] = json_decode($vo['callback_msg'], true);
// }
$vo['buy_num'] = 1;
if ($vo['param']) {
$vo['param'] = json_decode($vo['param'], true);
if (isset($vo['param']['num']) && $vo['param']['num']) {
$vo['buy_num'] = $vo['param']['num'];
}
}
// if($this->clist[$vo['cid']]){
//
// }
$vo['show_set'] = 2;
if ($vo['callback_at']) {
$start = $this->maketime($vo['create_at']);
$end = $this->maketime($vo['callback_at']);
$time = ($end - $start);
$vo['time'] = $this->secToTime($time);
} elseif ($vo['status'] == 3) {
$vo['time'] = '0秒';
} else {
$start = $this->maketime($vo['create_at']);
$end = time();
$time = ($end - $start);
$vo['time'] = $this->secToTime($time);
}
$set_callback = input('set_callback', 2);
if ($set_callback == 1) {
$vo['show_set'] = 1;
}
}
$this->total = Kami91OrderService::instance()->total();
}
private function getConfig(): array
{
$config['cash'] = [5,10,20,30,50,100,200,300,500,1000,2000];
$config['status'] = [''=>'-- 全部 --','1'=>'下单','2'=>'提卡成功', '3'=> '提卡失败', '4' => '处理中', '5' => '异常', '6' => '已处理未发送消息' , '7' => '已退款待锁卡'];
$config['time'] = ['1'=>'小于1分钟','2'=>'1分钟-10分钟', '3' => '10分钟-20分钟','4' => '20分钟-30分钟','5' => '30分钟-40分钟','6' => '40分钟-50分钟','7' => '50分钟-60分钟','8' => '大于60分钟'];
$config['data_type'] = ['1'=>'今日','2'=>'历史'];
// $config['is_number'] = [''=>'-- 全部 --','1'=>'有流水号','2'=>'无流水号'];
return $config;
}
public function maketime($v)
{
if (!$v) {
return '';
}
if (is_numeric($v)) {
return $v;
}
if (is_array($v)) {
$v = $v[1];
}
if (strstr($v, ' ')) {
$t = explode(' ', $v);
$v = $t[0];
$s = explode(':', $t[1]);
} else {
$s = array(0, 0, 0);
}
if (!isset($s[1])) {
$s[1] = 0;
}
if (!isset($s[2])) {
$s[2] = 0;
}
if (strstr($v, '-')) {
$t = explode('-', $v);
} elseif (strstr($v, '/')) {
$u = explode('/', $v);
$t[0] = $u[2];
$t[1] = $u[0];
$t[2] = $u[1];
}
if (!isset($t)) {
$t = array(0, 0, 0);
}
if (!isset($t[1])) {
$t[1] = 0;
}
if (!isset($t[2])) {
$t[2] = 0;
}
$v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
return $v;
}
public function secToTime($times)
{
$result = '0秒';
if ($times>0) {
$hour = floor($times/3600);
$minute = floor(($times-3600 * $hour)/60);
$second = floor((($times-3600 * $hour) - 60 * $minute) % 60);
$result = '';
if ($hour > 0) {
$result = $hour . '时';
}
if ($minute > 0) {
$result .= $minute . '分';
}
if ($second > 0) {
$result .= $second . '秒';
}
}
return $result;
}
protected function _info_form_filter(&$data)
{
$data['info'] = Kami91OrderService::instance()->getOrderInfo($data['mid'],$data['pid']);
$data['card'] = array();
// if ($data['card_id'] && $data['card_id'] > 0) {
// $data['card'] = CardService::instance()->db()->where(array('id' => $data['card_id']))->find();
//
// $data['card']['log'] = CardLogService::instance()->getData($data['card_id']);
// }
#虚拟商品卡密数据channel_callback_msg
// $data['kami'] = array();
// if((str_contains($data['product_key'], '_cardbuy') || $data['product_key'] =='cardbuy') && isset($data['channel_callback_msg'])){
// $tempMsg=json_decode($data['channel_callback_msg'] ,true);
// if(isset($tempMsg['kami']) && $tempMsg['kami']){
// $data['kami'] ['cardno'] = $tempMsg['kami']['cardno'];
// $data['kami'] ['cardpwd'] = $tempMsg['kami']['cardpwd'];
// $data['kami'] ['expired'] = $tempMsg['kami']['expired'];
//
// }
// }
}
# 设置订单状态
private function up($card, $cash = 0)
{
if (is_numeric($card) && $card) {
return $card;
} else {
return -1;
}
}
}