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

210 lines
6.6 KiB
PHP

<?php
namespace app\merchant\controller;
use think\admin\Controller;
use app\merchant\service\ProjectPercentService;
/**
* 商户通道分成管理
* Class ChannelProduct
* @package app\channel\controller
*/
class ProjectPercent extends Controller
{
/**
* 绑定数据表
* @var string
*/
private $table = 'merchant_project_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');
$this->project_id = input('project_id');
$query = $this->_query('merchant_project_product');
$query->equal('mid,project_id')->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)
{
foreach ($data as &$vo) {
$merchant = $this->app->db->name('merchant_list')->where(['id' => $vo['mid']])->find();
$vo['mname'] = $merchant['name'];
$project = $this->app->db->name('merchant_project')->where(['id' => $vo['project_id']])->find();
$vo['pname'] = $project['name'];
$vo['product_name'] = $this->app->db->name('channel_product')->where(['key' => $vo['product_key']])->value('name');
$percent = $this->app->db->name($this->table)->where(['mid' => $vo['mid'], 'project_id' => $vo['project_id'], 'product_key' => $vo['product_key']])->find();
$vo['rule'] = '';
if ($percent) {
$vo['percent'] = $percent['percent'];
$vo['rule'] = $this->rule($percent['rule']);
} else {
$vo['percent'] = $merchant['percent'];
}
}
}
protected function rule($rule)
{
if ($rule) {
$rule = json_decode($rule, true);
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">折扣</td></tr>';
foreach ($rule as $k => $v) {
$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, $project_id)
{
$product = $this->app->db->name('merchant_product')->where(['mid' => $mid, 'product_key' => $product_key, 'project_id' => $project_id])->select();
$cash = array();
if ($product) {
foreach ($product as $k => $v) {
if ($v['cash']) {
$temp = explode(',', $v['cash']);
foreach ($temp as $k1 => $v1) {
if (isset($cash[$v1]) && $cash[$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->project_id = input('project_id');
$this->pid = input('pid');
$this->product_key = input('product_key');
$this->cash = $this->getCash($this->mid, $this->product_key, $this->project_id);
$this->rule = array();
$info = $this->app->db->name($this->table)->where(['mid' => $this->mid, 'project_id' => $this->project_id, 'product_key' => $this->product_key])->find();
if (!$info) {
$merchant = $this->app->db->name('merchant_list')->where(['id' => $this->mid])->find();
$data['percent'] = $merchant['percent'];
} else {
$data['percent'] = $info['percent'];
if ($info['rule']) {
$this->rule = json_decode($info['rule'], true);
}
}
}
protected function _form_result($id, $data)
{
$this->mid = input('mid');
$this->project_id = input('project_id');
$this->pid = input('pid');
$this->product_key = input('product_key');
$this->cash = $this->getCash($this->mid, $this->product_key, $this->project_id);
$info = $this->app->db->name($this->table)->where(['mid' => $this->mid, 'project_id' => $this->project_id, 'product_key' => $this->product_key])->find();
$percent = input('percent');
if ($this->cash) {
$rule = array();
foreach ($this->cash as $k => $v) {
$input = input('cash_percent_' . $v);
if ($input) {
$rule[$v] = $input;
}
}
if ($rule) {
$rule = json_encode($rule);
}
} else {
$rule = '';
}
if (!$info) {
$mid = $insert['mid'] = $this->mid;
$project_id = $insert['project_id'] = $this->project_id;
$product_key = $insert['product_key'] = $this->product_key;
$insert['percent'] = $percent;
if ($rule) {
$insert['rule'] = $rule;
}
$this->app->db->name($this->table)->insert($insert);
} else {
$mid = $update['mid'] = $this->mid;
$project_id = $update['project_id'] = $this->project_id;
$product_key = $update['product_key'] = $this->product_key;
$update['percent'] = $percent;
if ($rule) {
$update['rule'] = $rule;
}
$this->app->db->name($this->table)->where(['id' => $info['id']])->update($update);
}
}
/**
* 编辑
* @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);
}
}