57171b2f57 feat(merchant): 添加码速达自动发卡功能
- 新增码速达自动发卡设置界面
- 实现码速达账号UID和密钥的保存和使用
- 添加消息订阅功能
- 优化自动发卡逻辑,增加错误处理和状态更新
2025-03-17 03:38:44 +08:00

261 lines
8.2 KiB
PHP

<?php
namespace app\merchant\controller;
use app\gateway\service\RedisService;
use think\admin\Controller;
use app\merchant\service\PercentService;
/**
* 商户分成管理
* Class ChannelProduct
* @package app\channel\controller
*/
class Percent extends Controller
{
/**
* 绑定数据表
* @var string
*/
private $table = 'merchant_percent';
/**
* 商户分成管理
* @auth true
* @menu true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->title = '商户折扣管理';
$this->mid = input('mid');
$query = $this->_query('merchant_product');
$query->equal('mid')->dateBetween('create_at');
$query->group('product_key')->order('id desc')->page();
}
/**
* 数据列表处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _page_filter(&$data)
{
$this->key = PercentService::instance()->getIsp();
foreach ($data as &$vo) {
$merchant = $this->app->db->name('merchant_list')->where(['id' => $vo['mid']])->find();
$vo['mname'] = $merchant['name'];
$vo['product_name'] = $this->app->db->name('channel_product')->where(['key' => $vo['product_key']])->value('name');
$percent = $this->app->db->name('merchant_percent')->where(['mid' => $vo['mid'], 'product_key' => $vo['product_key']])->find();
$vo['rule'] = '';
if ($percent) {
$vo['percent_type'] = $percent['percent_type'];
$vo['percent'] = $percent['percent'];
$vo['rule'] = $this->rule($vo['product_key'], $percent['rule'], $percent['percent_type']);
} else {
$vo['percent'] = $merchant['percent'];
$vo['percent_type'] = 1;
}
if ($vo['percent_type'] == 2) {
$vo['percent_type_name'] = '固定数值';
} else {
$vo['percent_type_name'] = '百分比';
}
}
}
protected function rule($product_key, $rule, $type)
{
if ($rule) {
$rule = json_decode($rule, true);
if ($type == 1) {
$name = '百分比';
} else {
$name = '固定数值';
}
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">'.$name.'</td></tr>';
foreach ($rule as $k => $v) {
if ($product_key == 'dhcz' && isset($this->key[$k]) && $this->key[$k]) {
$k = $this->key[$k];
}
$table .= '<tr><td class="text-left nowrap">'.$k.'</td>
<td class="text-left nowrap">'.$v.'</td></tr>';
}
$table .= '</table>';
return $table;
} else {
return '';
}
}
# 获取所有选择的面值
private function getCash($mid, $product_key)
{
$product = $this->app->db->name('merchant_product')->where(['mid' => $mid, 'product_key' => $product_key])->select();
$this->key = false;
if ($product_key == 'dhcz') {
$this->key = PercentService::instance()->getIsp(2);
}
$cash = array();
if ($product) {
foreach ($product as $k => $v) {
$v['product'] = $this->app->db->name('channel_product')->where(['id' => $v['pid']])->find();
if (!$v['cash']) {
$v['cash'] = $v['product']['value'];
}
if ($v['cash']) {
$temp = explode(',', $v['cash']);
foreach ($temp as $k1 => $v1) {
if ($this->key && isset($this->key[$v1]) && $this->key[$v1]) {
$cash[$this->key[$v1]] = $v1;
} else {
$cash[$v1] = $v1;
}
}
}
}
}
return $cash;
}
/**
* 表单数据处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _form_filter(&$data)
{
$this->mid = input('mid');
$this->pid = input('pid');
$this->product_key = input('product_key');
$this->cash = $this->getCash($this->mid, $this->product_key);
$this->rule = array();
$info = $this->app->db->name('merchant_percent')->where(['mid' => $this->mid, 'product_key' => $this->product_key])->find();
if (!$info) {
$merchant = $this->app->db->name('merchant_list')->where(['id' => $this->mid])->find();
$data['percent'] = $merchant['percent'];
$data['percent_type'] = 1;
} else {
$data['percent'] = $info['percent'];
$data['percent_type'] = $info['percent_type'];
if ($info['rule']) {
$this->rule = json_decode($info['rule'], true);
}
if ($info['rule']) {
$rule = json_decode($info['rule'], true);
if ($this->product_key == 'dhcz') {
$this->rule = array();
$key = PercentService::instance()->getIsp();
foreach ($rule as $k => $v) {
if (isset($key[$k]) && $key[$k]) {
$this->rule[$key[$k]] = $v;
}
}
} else {
$this->rule = $rule;
}
}
}
}
protected function _form_save(&$data)
{
return false;
}
protected function _form_result($id, $data)
{
$this->mid = input('mid');
$this->pid = input('pid');
$this->product_key = input('product_key');
$this->cash = $this->getCash($this->mid, $this->product_key);
$info = $this->app->db->name('merchant_percent')->where(['mid' => $this->mid, 'product_key' => $this->product_key])->find();
$percent = input('percent');
$percent_type = input('percent_type');
if ($this->cash) {
$rule = array();
foreach ($this->cash as $k => $v) {
$input = input('cash_percent_' . $k);
if ($input) {
if ($this->key && isset($this->key[$v]) && $this->key[$v]) {
$v = $this->key[$v];
}
$rule[$v] = $input;
}
}
if ($rule) {
$rule = json_encode($rule);
}
} else {
$rule = '';
}
if (!$info) {
$mid = $insert['mid'] = $this->mid;
$product_key = $insert['product_key'] = $this->product_key;
$insert['percent'] = $percent;
$insert['percent_type'] = $percent_type;
if ($rule) {
$insert['rule'] = $rule;
}
$this->app->db->name('merchant_percent')->insert($insert);
} else {
$mid = $update['mid'] = $this->mid;
$product_key = $update['product_key'] = $this->product_key;
$update['percent'] = $percent;
$update['percent_type'] = $percent_type;
if ($rule) {
$update['rule'] = $rule;
}
$this->app->db->name('merchant_percent')->where(['id' => $info['id']])->update($update);
}
//PercentService::instance()->get($mid, $product_key, false);
$this->success('操作成功', '');
}
/**
* 编辑
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit()
{
$this->_form($this->table, 'form');
}
/**
* 删除渠道商品
* @auth true
* @throws \think\db\exception\DbException
*/
public function remove()
{
$this->_delete($this->table);
}
}