文件
mashangyoujiu/Application/Retail/Controller/VipLabelController.class.php

349 行
9.7 KiB
PHP
可执行文件

<?php
/**
* 后台-零售小程序商品评价
* @author shao nianyi
* @create_time 2020年4月22日
*/
namespace Retail\Controller;
use Common\Model\RetailVipLabelModel;
use Common\Model\RetailVipWithLabelModel;
use Think\Controller;
//[1.8.8]
class VipLabelController extends BaseController
{
// 获取当前会员的标签
public function getUserLabel()
{
$vid = I('post.vid');
if (!$vid) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失!';
$this->ajaxReturn($res);
}
$map = [
'rvwl.is_del' => 1,
'rvl.is_del' => 1,
'vid' => $vid,
'rvl.store_id' => $this->store_id,
];
$field = [
'rvwl.vip_label_id',
'rvl.name'
];
$RetailVipLabelModel = new RetailVipLabelModel();
$vipLabel = $RetailVipLabelModel->getUserLabel($map, $field, 1, 20);
if ($vipLabel['list']) {
$res['status'] = 1;
$res['data'] = $vipLabel;
$res['msg'] = '数据获取成功!';
} else {
$res['status'] = 0;
$res['msg'] = '暂无数据,请稍后重试!';
}
$this->ajaxReturn($res);
}
// 给用户添加标签
public function addUserLabel()
{
$vid = I('post.vid');
$vip_label_id = I('post.vip_label_id');
if (!$vid || !$vip_label_id) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失,!';
$this->ajaxReturn($res);
}
$field = [
'vid' => $vid,
'vip_label_id' => $vip_label_id,
'created_at' => date('Y-m-d H:i:s', time())
];
if ($this->checkUserLabel($vid, $vip_label_id)) {
$res['status'] = 0;
$res['msg'] = '用户标签已拥有!';
$this->ajaxReturn($res);
}
$RetailVipWithLabelModel = new RetailVipWithLabelModel();
$anchor_info = $RetailVipWithLabelModel->addOrder($field);
if ($anchor_info) {
$res['status'] = 1;
$res['msg'] = '添加成功';
} else {
$res['status'] = 0;
$res['msg'] = '数据异常,请稍后重试!';
}
$this->ajaxReturn($res);
}
// 批量添加用户标签
public function addUserLabelArray()
{
$vid = I('post.vid');
$vip_label_id = I('post.vip_label_id');
if (!$vid || !$vip_label_id) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失,!';
$this->ajaxReturn($res);
}
// 转数组
$vip_label_arr = explode(',', $vip_label_id);
$RetailVipWithLabelModel = new RetailVipWithLabelModel();
foreach ($vip_label_arr as &$value) {
if ($this->checkUserLabel($vid, $value)) {
$res['status'] = 0;
$res['msg'] = '用户标签已拥有!';
$this->ajaxReturn($res);
}
$field = [
'vid' => $vid,
'vip_label_id' => $value,
'created_at' => date('Y-m-d H:i:s', time())
];
$label_arr = $RetailVipWithLabelModel->addOrder($field);
}
if ($label_arr) {
$res['status'] = 1;
$res['msg'] = '添加成功';
} else {
$res['status'] = 0;
$res['msg'] = '数据异常,请稍后重试!';
}
$this->ajaxReturn($res);
}
// 删除会员的标签
public function delUserLabel()
{
$vid = I('post.vid');
$vip_label_id = I('post.vip_label_id');
if (!$vid || !$vip_label_id) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失,!';
$this->ajaxReturn($res);
}
$where = [
'vip_label_id' => $vip_label_id,
'vid' => $vid,
'is_del' => 1,
];
$save = [
'is_del' => 2,
'deleted_at' => date('Y-m-d H:i:s', time())
];
$RetailVipWithLabelModel = new RetailVipWithLabelModel();
$label_info = $RetailVipWithLabelModel->updateOrder($where, $save);
if ($label_info) {
$res['status'] = 1;
$res['msg'] = '操作成功!';
} else {
$res['status'] = 0;
$res['msg'] = '操作失败,请稍后重试!';
}
$this->ajaxReturn($res);
}
/* ************************************** 会员标签Start ************************************** */
// 添加会员标签
public function addVipLabel()
{
$name = I('post.name');
if (!$name) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失!';
$this->ajaxReturn($res);
}
if ($this->checkNameRepeat($name)) {
$res['status'] = 0;
$res['msg'] = '标签重复!';
$this->ajaxReturn($res);
}
$field = [
'store_id' => $this->store_id,
'merchant_id' => $this->merchant_id,
'name' => $name,
'created_at' => date('Y-m-d H:i:s', time())
];
$RetailVipLabelModel = new RetailVipLabelModel();
$anchor_info = $RetailVipLabelModel->addOrder($field);
if ($anchor_info) {
$res['status'] = 1;
$res['msg'] = '添加成功';
} else {
$res['status'] = 0;
$res['msg'] = '数据异常,请稍后重试!';
}
$this->ajaxReturn($res);
}
// 获取会员标签
public function getVipLabel()
{
$name = I('post.name');
$page = I('post.page');
$add_time = I('post.add_time');
$pageSize = I('post.pageSize') ? I('post.pageSize') : 10;
$map['is_del'] = 1;
$map['merchant_id'] = $this->merchant_id;
$map['store_id'] = $this->store_id;
if ($name) {
$map['name'] = ['like', "%{$name}%"];
}
if (!empty($add_time)) {
$map['created_at'] = array('between', array($add_time[0], $add_time[1]));
}
$field = [
'id',
'name',
'created_at'
];
$RetailVipLabelModel = new RetailVipLabelModel();
$RetailVipWithLabelModel = new RetailVipWithLabelModel();
$report['list'] = $RetailVipLabelModel->getSelect($map, $field, 'id desc', $page . ',' . $pageSize);
$report['total'] = $RetailVipLabelModel->getCount($map);
if ($report['list']) {
foreach ($report['list'] as $key => &$value) {
// 获取该标签会员数
$value['vip_num'] = $RetailVipWithLabelModel->getCount(['vip_label_id' => $value['id'], 'is_del' => 1]);
}
}
$res['status'] = 1;
$res['data'] = $report;
$res['msg'] = !empty($report) ? '数据获取成功' : '暂无数据';
$this->ajaxReturn($res);
}
// 编辑会员标签
public function editVipLabel()
{
$id = I('post.label_id');
$name = I('post.name');
if (!$id) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失!';
$this->ajaxReturn($res);
}
if ($this->checkNameRepeat($name)) {
$res['status'] = 0;
$res['msg'] = '标签重复!';
$this->ajaxReturn($res);
}
$map = [
'id' => $id
];
$save = [
'name' => $name
];
$RetailVipLabelModel = new RetailVipLabelModel();
$result = $RetailVipLabelModel->updateOrder($map, $save);
if ($result) {
$res['status'] = 1;
$res['msg'] = '操作成功!';
} else {
$res['status'] = 0;
$res['msg'] = '操作失败,请稍后重试!';
}
$this->ajaxReturn($res);
}
// 删除会员标签--软删除
public function delVipLabel()
{
$id = I('post.label_id');
if (!$id) {
$res['status'] = 0;
$res['msg'] = '必要参数缺失!';
$this->ajaxReturn($res);
}
$map = [
'id' => $id,
'merchant_id' => $this->merchant_id,
'store_id' => $this->store_id,
];
$save = [
'is_del' => 2,
'deleted_at' => date('Y-m-d H:i:s', time())
];
$RetailVipLabelModel = new RetailVipLabelModel();
$result = $RetailVipLabelModel->updateOrder($map, $save);
if ($result) {
// 同步处理会员-标签
$RetailVipWithLabelModel = new RetailVipWithLabelModel();
$RetailVipWithLabelModel->updateOrder(['vip_label_id' => $id], $save);
$res['status'] = 1;
$res['msg'] = '操作成功!';
} else {
$res['status'] = 0;
$res['msg'] = '操作失败,请稍后重试!';
}
$this->ajaxReturn($res);
}
// 标签是否重复
public function checkNameRepeat($name)
{
$RetailVipLabelModel = new RetailVipLabelModel();
$nameInfo = $RetailVipLabelModel->getFind(['name' => $name, 'is_del' => 1, 'merchant_id' => $this->merchant_id, 'store_id' => $this->store_id]);
if ($nameInfo) {
return true;
} else {
return false;
}
}
// 用户标签是否重复
public function checkUserLabel($vid, $label_id)
{
$RetailVipWithLabelModel = new RetailVipWithLabelModel();
$nameInfo = $RetailVipWithLabelModel->getFind(['vid' => $vid, 'vip_label_id' => $label_id, 'is_del' => 1, 'merchant_id' => $this->merchant_id]);
if ($nameInfo) {
return true;
} else {
return false;
}
}
}