DiscountCouponController.php 4.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/8/30
 * Time: 14:29
 */
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class DiscountCouponController extends AdminBaseController
{

    public function add()
    {
        $request = request();
        if ($request->isPost()) {
            $post = $request->param();
            if (empty($post['discount_coupon_name'])) {
                $this->error('请输入优惠券名称');
            }
            if (empty($post['overflow']) || $post['overflow'] < 0) {
                $this->error('请输入正确满减金额');
            }
            if (empty($post['reduce']) || $post['reduce'] < 0) {
                $this->error('请输入正确满减金额');
            }
            if (empty($post['deadline'])) {
                $this->error('请选择截止日期');
            }
            if (empty($post['user_id'])) {
                $this->error('请匹配用户');
            }
            $n = count($post['user_id']);
            $data = [];
            for ($i = 0; $i < $n; $i++) {
                $data[$i] = array(
                    'user_id' => $post['user_id'][$i],
                    'discount_coupon_name' => $post['discount_coupon_name'],
                    'overflow' => $post['overflow'],
                    'reduce' => $post['reduce'],
                    'deadline' => strtotime($post['deadline']),
                    'status' => 1
                );
            }
            if (Db::name('discount_coupon')->insertAll($data)) {
                $this->success('创建优惠券成功');
            } else {
                $this->error('创建优惠券失败');
            }
        } else {
            return $this->fetch('', [
                'user' => $this->getUserList()
            ]);
        }
    }

    public function index()
    {
        $posts = Db::name('discount_coupon')->alias('d')->field('sami_discount_coupon.id,discount_coupon_name,deadline,user_nickname,status')
            ->join('sami_user u', 'u.id=d.user_id')
            ->order("id DESC")
            ->paginate(20);
        return $this->fetch('', [
            'posts' => $posts,
            'page' => $posts->render()
        ]);
    }

    public function getUserList()
    {
        $keywords = request()->param('keywords');
        $map = array();
        $map['user_type'] = ['eq', 2];
        if (!empty($keywords)) {
            $map['user_nickname'] = array('like', "%$keywords%");
        }
        $user = Db::name('user')->field('id,user_nickname')->where($map)->select();
        $html = '<tr>
                    <th><input type="checkbox" id="All" onclick="checkAll()"></th>
                    <th width="300"><input style="width: 200px; display: unset" class="form-control" type="text" placeholder="搜索" id="Search" value="' . $keywords . '" onchange="ajax()">&nbsp;&nbsp;&nbsp;搜索</th>
                </tr>';
        foreach ($user as $item) {
            $html .= '<tr>
                        <td><input type="checkbox" name="user_id[]" value="' . $item['id'] . '"></td>
                        <td>' . $item['user_nickname'] . '</td>
                    </tr>';
        }
        if (request()->isAjax()) {
            echo json_encode(['data' => $html]);
            exit();
        } else {
            return $user;
        }
    }

    public function edit()
    {
        $request = request();
        $id = $request->param('id');
        if ($request->isPost()) {
            $post = $request->param();
            if (empty($post['discount_coupon_name'])) {
                $this->error('请输入优惠券名称');
            }
            if (empty($post['overflow']) || $post['overflow'] < 0) {
                $this->error('请输入正确满减金额');
            }
            if (empty($post['reduce']) || $post['reduce'] < 0) {
                $this->error('请输入正确满减金额');
            }
            if (empty($post['deadline'])) {
                $this->error('请选择截止日期');
            }
            $post['id'] = $id;
            $post['deadline'] = strtotime($post['deadline']);
            if (Db::name('discount_coupon')->update($post)) {
                $this->success('编辑优惠券成功');
            } else {
                $this->error('您未作出任何修改');
            }
        } else {
            return $this->fetch('', [
                'id' => $id,
                'info' => Db::name('discount_coupon')->where(array('id' => $id))->find()
            ]);
        }
    }

    public function del()
    {
        $id = request()->param('id');
        if (Db::name('discount_coupon')->delete($id)) {
            $this->success('销毁成功');
        } else {
            $this->error('销毁失败');
        }
    }

}