334 lines
10 KiB
PHP
334 lines
10 KiB
PHP
<?php
|
|
|
|
namespace app\channel\service;
|
|
|
|
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 CardService extends Service
|
|
{
|
|
/**
|
|
* 设置默认操作表
|
|
* @var string
|
|
*/
|
|
public $table = 'channel_card';
|
|
|
|
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 up($k, $cnum, $cpwd, $value, $mid = -1, $use = 1, $sid = 1, $group_id = 1)
|
|
{
|
|
$data = array();
|
|
$data['cnum'] = $cnum;
|
|
$data['cpwd'] = $cpwd;
|
|
$data['value'] = $value;
|
|
$data['sid'] = $sid;
|
|
$info = $this->db()->where($data)->find();
|
|
|
|
$state = false;
|
|
if (!$info) {
|
|
$data['use'] = $use;
|
|
$data['status'] = 1;
|
|
$data['use_mid'] = $mid;
|
|
$data['group_id'] = $group_id;
|
|
CardGroupService::instance()->db()->where(array('id' => $group_id))->update(array('status' => '1'));
|
|
$state = $this->db()->insertGetId($data);
|
|
|
|
if ($state && $mid > 0) {
|
|
$data['id'] = $state;
|
|
$this->setMerchant($k, $mid, $data);
|
|
}
|
|
}
|
|
|
|
return $state;
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |