REAPI/app/merchant/controller/MerchantRelation.php
2024-09-29 15:43:18 +08:00

225 lines
7.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\merchant\controller;
use think\admin\Controller;
use app\merchant\service\MerchantLogService;
use app\merchant\service\MerchantService;
use app\channel\service\ProductService;
use app\merchant\service\OrderService;
use app\merchant\service\PercentService;
/**
* 商户关联渠道管理
* Class MerchantRelation
* @package app\merchant\controller
*/
class MerchantRelation extends Controller
{
/**
* 绑定数据表
* @var string
*/
public $table = 'MerchantList';
/**
* 商户关联渠道列表
* @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->_query($this->table);
$query->equal('status')->dateBetween('create_at');
$query->like('name,phone,contacts');
// 加载对应数据列表
$this->type = input('type', 'all');
$query->where(['is_deleted' => 0]);
// 列表排序并显示
$query->order('id desc')->page();
}
/**
* 添加商户关联渠道
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function add()
{
$this->_applyFormToken();
$this->_form($this->table, 'form');
}
/**
* 编辑商户关联渠道
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function relation()
{
if ($this->request->isGet()) {
$id = input('id');
$this->title = '商户关联渠道列表';
$w = array
(
'mid' => $id,
'status' => 1,
);
$relation = $this->app->db->name('MerchantRelation')->where($w)->select()->toArray();
unset($w['mid']);
foreach ($relation as $k => $v) {
$this->relation[$v['pid']] = $v;
}
$w['is_deleted'] = 0;
$this->list = $this->app->db->name('ChannelList')->where($w)->select()->toArray();
foreach ($this->list as $k => $v) {
$this->list[$k]['product'] = $this->app->db->name('ChannelProduct')->where('cid',$v['id'])->where($w)->select()->toArray();
foreach ($this->list[$k]['product'] as $k1 => $v1) {
if (isset($this->relation[$v1['id']]) && $this->relation[$v1['id']]) {
$this->list[$k]['product'][$k1]['rstatus'] = 1;
} else {
$this->list[$k]['product'][$k1]['rstatus'] = 0;
}
}
}
$this->_form($this->table, 'relation','',[],$this->list);
} else {
$data = $this->_vali([
'cid.require' => '渠道ID不能为空',
'pid.require' => '商品ID不能为空',
'mid.require' => '商户ID不能为空',
'product_key.require' => '商品key不能为空',
'status.in:0,1' => '状态值范围异常!',
'status.require' => '状态值不能为空!',
]);
if ($data['status'] == 1) {
$info = $this->app->db->name('MerchantRelation')->where(['product_key'=>$data['product_key'], 'mid' => $data['mid'], 'status' => 1])->find();
if ($info) {
//$this->error('不能为一家商户设定两个相同的产品,请修改');
//撤销关联
$set = $data;
$set['id'] = $info['id'];
$set['status'] = 2;
data_save('MerchantRelation', $set, 'id');
}
}
$info = $this->app->db->name('MerchantRelation')->where(['cid'=>$data['cid'],'pid'=>$data['pid'],'mid'=>$data['mid'],'product_key'=>$data['product_key']])->find();
if ($info) {
$data['id'] = $info['id'];
}
if (data_save('MerchantRelation', $data, 'id')) {
# 同时也要更新分成
if ($info) {
PercentService::instance()->up($data['mid'], $data['cid'], $data['product_key'], $data['id']);
}
ProductService::instance()->get($data['mid'], $data['product_key'], false);
$this->success('操作成功', '');
} else {
$this->error('操作失败,请稍候再试!');
}
}
}
/**
* 列表数据处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _index_page_filter(array &$data)
{
$order = OrderService::instance();
foreach ($data as $k => $v) {
$data[$k]['order_yes'] = number_format($order->getTotal($v['id'], 2), 2);
$data[$k]['order_no'] = number_format($order->getTotal($v['id'], 3), 2);
}
}
/**
* 表单数据处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _form_filter(&$data)
{
// if ($this->request->isPost()) {
// if (isset($data['id']) && $data['id'] > 0) {
// unset($data['name']);
// } else {
// if (empty($data['name'])) $this->error('商户关联渠道名称不能为空!');
// $where = ['appid' => $data['appid'], 'is_deleted' => 0];
// if ($this->app->db->name($this->table)->where($where)->count() > 0) {
// $this->error("商户关联渠道{$data['name']}已经存在!");
// }
// }
// }
}
protected function _form_result($id, $data)
{
ProductService::instance()->get($data['mid'], $data['product_key'], false);
}
/**
* 修改商户关联渠道状态
* @auth true
* @throws \think\db\exception\DbException
*/
public function state()
{
$this->_applyFormToken();
$this->_save($this->table, $this->_vali([
'status.in:0,1' => '状态值范围异常!',
'status.require' => '状态值不能为空!',
]));
}
/**
* 删除商户关联渠道
* @auth true
* @throws \think\db\exception\DbException
*/
public function remove()
{
$this->_applyFormToken();
$this->_delete($this->table);
}
}