REAPI/app/kami/service/LockCardService.php
2024-12-04 12:33:47 +08:00

427 lines
13 KiB
PHP

<?php
namespace app\kami\service;
use app\channel\service\ChannelService;
use app\core\Service;
//use app\merchant\service\MerchantRelationService;
use app\merchant\service\ProductService as MerchantRelationService;
use app\merchant\service\CardService as MerchantCardService;
use app\channel\service\CardGroupService;
use app\gateway\service\AuthService;
use think\facade\Db;
/**
* 卡密服务
* Class ChannelService
* @package app\channel\service
*/
class LockCardService extends Service
{
/**
* 设置默认操作表
* @var string
*/
public $table = 'kami_lockcard';
public $class;
# 获取服务
public function call($method, $cid,$cardno,$cardpwd,$pid,$cash,)
{
$ChannelService = ChannelService::instance();
$channel = $ChannelService->get($cid);
if (!$channel) {
return '锁卡渠道错误';
}
if(str_contains($channel['other_data'],'channel_key')){
$json = json_decode($channel['other_data'],true);
$name = ucfirst($json['channel_key']);
}else{
$name = ucfirst($channel['key']);
}
$class = '\\app\\kami\\controller\\suoka\\' . $name;
$this->class = new $class($this->app);
if (method_exists($this->class, $method)) {
return call_user_func(array($this->class, $method), $cid,$cardno,$cardpwd,$pid,$cash);
} else{
return '锁卡渠道未定义此方法';
}
}
#锁卡记录
public function lockcard_log($type,$order,$kami,$infos = false){
$data = array();
$data['order_id'] = $order['order_id'];
$data['mid'] = $order['mid'];
$info = $this->db()->where($data)->find();
if($info){
if($info['lock_type'] == $type){
return $info['id'];
}
$this->db()->where($data)->update(['lock_type' => $type]);
return $info['id'];
}else{
$up = $this->up($kami['cardno'], $kami['cardpwd'], $order['cash'], $order['cid'], $order['pid'],$type, $order['mid'], $infos, $order['order_id']);
if($up){
return $up;
}else{
return false;
}
}
}
#提交记录
public function up( $cardno, $cardpwd, $value,$cid, $pid,$type=1,$mid = -1, $infos = false, $order_id = false)
{
$data = array();
$data['cardno'] = $cardno;
$data['cardpwd'] = $cardpwd;
$data['value'] = $value;
$data['mid'] = $mid;
// $data['type'] = $type;
$info = $this->db()->where($data)->find();
$state = false;
if (!$info) {
$data['lock_type'] = $type;
$data['status'] = 1;
$data['cid'] = $cid;
$data['pid'] = $pid;
if($infos){
$data['info'] = $infos;
}
if($order_id){
$data['order_id'] = $order_id;
}
$state = $this->db()->insertGetId($data);
}
return $state;
}
public function getInfo($order_id,$cardno = false,$mid = false)
{
$where = [];
if($order_id){
$where['order_id'] = $order_id;
}
if($cardno){
$where['cardno'] = $cardno;
}
if($mid){
$where['mid'] = $mid;
}
return $this->db()->where($where)->find();
}
#================================================================================
public function getMerchant()
{
return '2';
}
protected function checkOpenTime($opentime)
{
if ($opentime && strstr($opentime, ':')) {
$opentime = str_replace(':', '', $opentime);
if (str_contains($opentime, '-')) {
$value = explode('-', $opentime);
$cur = intval(date('Hi'));
$value[0] = intval($value[0]);
$value[1] = intval($value[1]);
if ($value[1] < $value[0]) {
if ($cur >= $value[0] || $cur < $value[1]) {
return true;
}
} else {
if ($cur >= $value[0] && $cur < $value[1]) {
return true;
}
}
}
}
return false;
}
# 自动变成已使用
public function upStatus()
{
$time = '22:30-01:00';
$state = $this->checkOpenTime($time);
if ($state) {
echo 1;die;
}
$num = mt_rand(1,5);
$data = $this->db()->where(array('chongzhi_use' => 1))->limit($num)->order('id asc')->select()->toArray();
if ($data) {
$where = array();
foreach ($data as $k => $v) {
$update['chongzhi_use'] = 2;
$where['id'] = $v['id'];
$this->db()->where($where)->update($update);
}
}
}
# 根据卡密获取卡信息
public function getOne($cnum, $cpwd, $state = false)
{
$where = array();
$where['cnum'] = $cnum;
$where['cpwd'] = $cpwd;
$info = $this->db()->where($where)->find();
if ($info && $state == 1) {
$this->db()->where(array('id' => $info['id']))->update(array('use' => 2, 'use_info' => '下单中'));
}
return $info;
}
# 随机抽取一个未使用的卡密
public function getData($value, $mid = false, $num = 1, $state = true, $page = false, $group_id = false)
{
if (!$group_id) {
$group_id = CardGroupService::instance()->getOne();
}
if ($group_id) {
$where['group_id'] = $group_id;
}
$where['use'] = 1;
$where['status'] = 1;
if ($value > 0) {
$where['value'] = $value;
}
//$data = $this->db()->where($where)->orderRaw('rand()')->find();
$db = $this->db()->where($where);
if ($mid > 0) {
$db->whereRaw('use_mid in(-1,'.$mid.')');
} elseif ($mid == -1) {
$db->whereRaw('use_mid = -1');
}
if ($num <= 1 && !$page) {
$data = $db->order('create_at asc')->find();
if ($data && $state) {
# 变为不可用
if ($state === 100) {
$this->db()->where(array('id' => $data['id']))->delete();
} else {
$update = $this->db()->where(array('id' => $data['id'], 'use' => 1))->update(array('use' => 2, 'use_info' => '下单中'));
if (!$update) {
return $this->getData($value, $mid, $num, $state, $page);
}
}
}
} else {
$db->order('create_at asc');
if ($page) {
$data = $db->paginate($num)->toArray();
} else {
if ($num > 0) {
$db->limit($num);
}
$data = $db->select()->toArray();
}
if (isset($data['data']) && $state) {
# 变为不可用
foreach ($data['data'] as $k => $v) {
if ($state === 100) {
$this->db()->where(array('id' => $v['id']))->delete();
} else {
$this->db()->where(array('id' => $v['id']))->update(array('use' => 2, 'use_info' => '下单中'));
}
}
} elseif ($data && $state) {
# 变为不可用
foreach ($data as $k => $v) {
if ($state === 100) {
$this->db()->where(array('id' => $v['id']))->delete();
} else {
$this->db()->where(array('id' => $v['id']))->update(array('use' => 2, 'use_info' => '下单中'));
}
}
}
}
return $data;
}
# 设置mid
public function setMid($k, $data, $mid)
{
$state = $this->db()->where(array('id' => $data['id']))->update(array('use_mid' => $mid));
# 如果这个商户有自动上卡接口,则走接口
$config = $this->getMerchant();
$config = explode(',', $config);
if (in_array($mid, $config)) {
$this->setMerchant($k, $mid, $data);
}
return $state;
}
public function setMerchant($k, $mid, $data)
{
$param['card'] = $data['cnum'];
$param['pwd'] = $data['cpwd'];
$param['cash'] = $data['value'];
$param['card_id'] = $data['id'];
$param['order'] = $this->createOrderId($k);
$msg = ChannelService::instance()->use($mid, 'zshsk', $param, true);
}
protected function createOrderId($i = 0)
{
return 'KA' . date('Ymd').substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(100000, 999999)) . '_' . $i;
}
# 记录日志
public function log($order, $status, $info = '', $msg = '')
{
$id = $order['card_id'];
$mid = $order['mid'];
$order_id = $order['order_id'];
$card = $order['account'];
$cash = $order['cash'];
$state = $this->db()->where(array('id' => $id))->update(array('use' => $status, 'use_info' => $info, 'use_num' => 1));
if ($state) {
MerchantCardService::instance()->up($id, $mid, $order_id, $card, $cash, $status, $info, $msg);
if ($status == 4 || $status == 6) {
$this->db()->where(array('id' => $id))->update(array('use' => 1));
} elseif ($status == 7) {
# 重新回去,变为可用
$num = MerchantCardService::instance()->getNum($id, 7);
if ($num < 5) {
//$this->db()->where(array('id' => $id))->update(array('create_at' => date('Y-m-d H:i:s'), 'use' => 1));
$this->db()->where(array('id' => $id))->update(array('use' => 1));
}
}
$info = $this->db()->where(array('id' => $id))->find();
$state = 1;
if ($info && $state == 2) {
$this->cb($info, $status, 1);
}
}
}
private function cd($info, $status, $num = 1)
{
if ($num > 5) {
return false;
}
$key = '0ee936aaec7152fcac73ef96a2ec23cd';
$input['cnum'] = $info['cnum'];
$input['cpwd'] = $info['cpwd'];
if ($status == 5) {
$input['status'] = 1;
} elseif ($status == 8 || $status == 9 || $status == 10) {
$input['status'] = 2;
} else {
return;
}
$input = AuthService::get($input, $key, 2);
$url = 'http://huishou.5dev.cn/Home/card/shoucard?';
$result = $this->curl('post', $url, $input);
if ($result != 'success') {
$num = $num + 1;
$this->cb($info, $status, $num);
}
}
# 添加记录
public function total($state = true)
{
$where = array();
$use = input('use');
if ($use) {
$where['use'] = $use;
}
$create_at = input('create_at');
$col = 'create_at';
if ($create_at) {
$temp = explode(' - ', $create_at);
$begin = $temp[0] . ' 00:00:00';
$after = $temp[1] . ' 23:59:59';
$time = [$begin, $after];
} else {
$time = false;
}
$result['num'] = $this->getDb($where, $time, $state, '*', false, false);
$result['yes_num'] = $this->getDb($where, $time, $state, '*', 5, false);
$result['use_num'] = $this->getDb($where, $time, $state, '*', 1, false);
$result['used_num'] = $this->getDb($where, $time, $state, '*', '2,3', false);
$result['no_num'] = $this->getDb($where, $time, $state, '*', '4,6,7,8', false);
return $result;
}
private function getDb($where, $time, $state, $col = 'cash', $use = false, $status = false)
{
$db = $this->db();
if ($where) {
$db->where($where);
}
if ($use) {
if ($state) {
$c = 'use';
} else {
$c = 'chongzhi_use';
}
$db->whereRaw(' `'.$c.'` in('.$use.')');
}
if ($status) {
$db->whereRaw(' status in('.$status.')');
}
if ($time) {
$time_col = 'create_at';
$db = $db->whereBetween($time_col, $time);
}
if ($col == '*') {
return round($db->count($col), 2);
}
return round($db->sum($col), 2);
}
}