StoreInform.php 10.6 KB
<?php

namespace app\admin\controller\store;

use app\admin\model\Admin;
use app\admin\model\AuthGroupAccess;
use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 商家发布信息管理
 *
 * @icon fa fa-circle-o
 */
class StoreInform extends Backend
{
    
    /**
     * StoreInform模型对象
     * @var \app\admin\model\store\StoreInform
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\store\StoreInform;
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index($ids = null)
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            $where_s = [
                'store_id' => $ids
            ];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['store'])
                    ->where($where_s)
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['store'])
                    ->where($where_s)
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('store')->visible(['store_name']);
            }
            $list = collection($list)->toArray();
            foreach ($list as &$v) {
                $v['house_name'] = Db::name('house')->whereIn('id',$v['house_ids'])->column('name');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this->assignconfig('ids',$ids);
        return $this->view->fetch();
    }

    /**
     * 审核
     */
    public function examine($ids = null)
    {
        if($ids) {
            $row = $this->model->lock(true)->find($ids);
            if (!$row) {
                $this->error(__('No Results were found'));
            }
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                if (!in_array($row[$this->dataLimitField], $adminIds)) {
                    $this->error(__('You have no permission'));
                }
            }
            $params = $this->request->param();
            if($params['status'] == 2) {
                if($row->status == 2) {
                    $this->error('该商圈信息已通过审核');
                }
            }
            if($params['status'] == 4) {
                if($row->status == 4) {
                    $this->error('该商圈信息已驳回审核');
                }
            }
            $row->status = $params['status'];
            $fail_name = '审核失败';
            Db::startTrans();
            try {
                $result = $row->save();
                $openid = Db::name('third')->where('user_id',$row->user_id)->value('openid');
                $url = config('option.vue_url') . '/releasedetal?id='.$row->id;
                if($params['status'] == 2) {
                    // 发送模板消息
                    $send_data = array(
                        "first"  => '您的广告投放已出结果。',
                        "keyword1" => $row->content,
                        "keyword2"  => '审核通过',
                        "remark" => ['点击查看详情','#FF0000'],
                    );
                    $this->wxsendmessage($openid,$send_data,config('option.template')['ad_pass'],$url);
                    // 给用户发送,领取红包提示
                    if($row->type == 1){
                        $send_data1 = array(
                            "first"  => '您的广告投放已出结果。',
                            "keyword1" => $row->content,
                            "keyword2"  => '审核通过',
                            "remark" => ['点击查看详情','#FF0000'],
                        );
                        // 推广社区
                        $house_ids = trim($row->house_ids,',');
                        // 该店铺投放广告的小区业主ID
                        $user_id_arr = \app\api\model\UserHouse::where('status',2)
                            ->where('house_id','in',$house_ids)
                            ->column('user_id');
                        if(!empty($user_id_arr)){
                            $store = $row->store;
                            $send_data1 = array(
                                "first"  => '你家小区附近有商家发福利啦,快来领取!',
                                "keyword1" => $store->store_name,
                                "keyword2"  => date('Y-m-d H:i'),
                                "remark" => ['点击领取红包','#FF0000'],
                            );
                            $url1 = config('option.vue_url') . '/shopdetails?id='.$store->id;
                            // 用户去重
                            $user_id_arr = array_unique($user_id_arr);
                            // 随机红包数量用户
                            $new_user_id_arr = [];
                            if(count($user_id_arr) > $row->number){
                                $random_keys = array_rand($user_id_arr,$row->number);
                                if(is_array($random_keys)){
                                    foreach ($random_keys as $rand) {
                                        $new_user_id_arr[] = $user_id_arr[$rand];
                                    }
                                }else{
                                    $new_user_id_arr[] = $user_id_arr[$random_keys];
                                }
                            }else{
                                $new_user_id_arr = $user_id_arr;
                            }
                            foreach ($new_user_id_arr as $user_id) {
                                $openid1 = Db::name('third')->where('user_id',$user_id)->value('openid');
                                $this->wxsendmessage($openid1,$send_data1,config('option.template')['store_inform'],$url1);
                            }
                        }
                    }
                }
                if($params['status'] == 4) {
                    $user_model = new \app\admin\model\User();
                    $user = $user_model->where('id', $row->user_id)->find();
                    // 积分变动记录
                    $log = [
                        'user_id' => $row->user_id,
                        'score' => $row->score,
                        'before' => $user['score'],
                        'after' => $user['score'] + $row->score,
                        'createtime' => time(),
                        'status' => 3,
                        'memo' => '商圈信息审核失败'
                    ];
                    $result_log = Db::name('user_score_log')->insertGetId($log);
                    // 返还用户余额
                    $result_user = $user_model->where('id', $user['id'])->setInc('score', $row->score);
                    // 发送模板消息
                    $nickname = Db::name('user')->where('id',$row->user_id)->value('nickname');
                    $send_data = array(
                        "first"  => '抱歉!您的广告 审核失败。',
                        "keyword1" => $row->content,
                        "keyword2"  => $nickname,
                        "keyword3" => '您的广告未通过',
                        "remark" => ['您的广告未通过审核, 请您重新申请。','#FF0000'],
                    );
                    $this->wxsendmessage($openid,$send_data,config('option.template')['ad_fail'],$url);
                }
                Db::commit();
            } catch (ValidateException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (\think\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(!$result) {
                $this->error($fail_name,'',$result);
            }
            $this->success('审核成功');
        }

    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    // 删除商家发布信息点赞
                    Db::name('store_inform_good')->where('object_id',$v['id'])->delete();
                    // 删除红包领取记录
                    Db::name('store_inform_log')->where('inform_id',$v['id'])->delete();
                    // 删除商圈浏览量记录
                    Db::name('store_inform_view')->where('object_id',$v['id'])->delete();
                    $count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}