Business.php 9.2 KB
<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 * 商家端接口
 */
class Business extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $postParam;
    protected $store_id;
    protected $staff;
    protected $staff_id;
    protected $store;

    public function _initialize()
    {
        $postParam = $this->request->param();
        $this->postParam = $postParam;

        parent::_initialize();
        $this->auth->id = 1;//测试用
        $postParam = $this->request->param();
        $this->postParam = $postParam;
        if (empty($postParam['code'])) {
            $this->error("门店code异常");
        }
        $this->store_id = $postParam['code'];
        $store = Db::name("verification_store")->where("id", $this->store_id)->find();
        if (empty($store)) {
            $this->error("门店不存在,请退出登录后重试!");
        }
        $this->store = $store;
        $staff = Db::name("verification_staff")->where("verification_store_id", $this->store_id)->find();
        if (empty($staff)) {
            $this->error("暂无权限!");
        }
        $this->staff = $staff;
        $this->staff_id = $staff['id'];
    }


    /**
     * 1、商家首页 获取商家人员信息
     */
    public function getstaff(){
        $this->success('活动获取成功', $this->staff);
    }
    /**
     * 2、核销
     */
    public function checkconsumption(){
        $receive_no=$this->postParam['receive_no'];
        //先查询到领卷记录
        $where["receive_no"] = ["=", $receive_no];
        $where["verification_store_id"] = ["=", $this->store_id];
        $receive=Db::name('verification_receive')->where($where)->find();
        if(empty($receive)){
            $this->error('无效卡卷');
        }
        if($receive['type']==1){
            $this->error('该卡卷已使用');
        }

        if($receive['type']==2){
            $this->error('该卡卷已过期');
        }
        $time=time();
        if($receive['closetime']<$time){
            $this->error('该卡卷已过期');
        }
        Db::startTrans();
        try {
            $iswhere["id"] = ["=", $receive['id']];
            $isdata=[
                'usetime'=>$time,
                'type'=>1
            ];
            //先把领卷记录修改了
            $isreceive=Db::name('verification_receive')->where($iswhere)->update($isdata);
            //添加核销记录
            $canceldata=[
                'verification_store_id'=>$this->store_id,
                'verification_staff_id'=>$this->staff_id,
                'verification_receive_id'=>$receive['id'],
                'createtime'=>$time,
            ];
            $cancel=Db::name('verification_cancel')->insertGetId($canceldata);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        if($cancel){
            $this->success('核销成功');
        }else{
            $this->error('核销失败');
        }
        //核销data

    }

    /**
     *3、核销页面的核销记录 单条
     */
    public function getverification_cancel(){
        $where["verification_store_id"] = ["=", $this->store_id];
        $where["verification_staff_id"] = ["=", $this->staff_id];
        $cancel=Db::name('verification_cancel')->where($where)->find();
        $this->success('查询成功',$cancel);

    }

    /**
     *4、核销记录
     */
    public function selectverification_cancel(){
        $keyfield=$this->postParam['keyfield'];
        $where=  " url like %{$keyfield}% or content like %{$keyfield}%";
        $cancel=Db::name('admin_log')->where($where)->fetchSql()->count();
        $this->success('查询成功',$cancel);
        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);
        $where["a.verification_store_id"] = ["=", $this->store_id];
        if($keyfield){
            $where["c.receive_no"] = ["LIKE", "%{$keyfield}%"];

        }
        $field = "a.id,a.createtime,b.name,d.name as coupon_name,c.type";
        $cancel=Db::name('verification_cancel')
            ->alias('a')
            ->join('verification_staff b','a.verification_staff_id=b.id')
            ->join('verification_receive c','a.verification_receive_id=c.id')
            ->join('verification_coupon d','c.verification_coupon_id=d.id')
            ->where($where)
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        $this->success('查询成功',$cancel);

    }

    /**
     * 5、个人业绩
     */
    public function performance(){
        $where["verification_store_id"] = ["=", $this->store_id];
        $activity=Db::name('verification_activity')->where($where)->order('id DESC')->find();
        //先查询活动
        $this->success('查询成功',$activity);

    }

    /**
     * 6、订单查询
     */
    public function selectorder(){
        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);
        $where["a.verification_store_id"] = ["=", $this->store_id];
        $field = "a.id,a.order_no,a.name,a.phone,a.type,b.title";
        $order=Db::name('verification_order')
            ->alias('a')
            ->join('verification_activity b','a.verification_activity_id=b.id')
            ->where($where)
            ->field($field)
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        $this->success('查询成功',$order);
    }

    /**
     * 7、订单详情
     */
    public function orderinfo(){
        $id = $this->request->post('id' );
        $where["a.id"] = ["=", $id];
        $field = "a.id,a.order_no,a.name,a.phone,a.type,b.title";
        $order=Db::name('verification_order')
            ->alias('a')
            ->join('verification_activity b','a.verification_activity_id=b.id')
            ->where($where)
            ->field($field)
            ->find();
        //查询具体卡卷
        $receivewhere['verification_order_id']=['=',$order['id']];
        $receive=Db::name("cv_verification_receive")->where($receivewhere)->select();
        $order['receive']=$receive;
        $this->success('查询成功',$order);
    }

    /**
     * 8、手动核销订单中的某个卡卷
     */
    public function ordercheckconsumption(){

        $id=$this->postParam['id'];
        //先查询到领卷记录
        $where["id"] = ["=", $id];
        $where["verification_store_id"] = ["=", $this->store_id];
        $receive=Db::name('verification_receive')->where($where)->find();
        if(empty($receive)){
            $this->error('无效卡卷');
        }
        if($receive['type']==1){
            $this->error('该卡卷已使用');
        }

        if($receive['type']==2){
            $this->error('该卡卷已过期');
        }
        $time=time();
        if($receive['closetime']<$time){
            $this->error('该卡卷已过期');
        }
        Db::startTrans();
        try {
            $iswhere["id"] = ["=", $receive['id']];
            $isdata=[
                'usetime'=>$time,
                'type'=>1
            ];
            //先把领卷记录修改了
            $isreceive=Db::name('verification_receive')->where($iswhere)->update($isdata);
            //添加核销记录
            $canceldata=[
                'verification_store_id'=>$this->store_id,
                'verification_staff_id'=>$this->staff_id,
                'verification_receive_id'=>$receive['id'],
                'createtime'=>$time,
            ];
            $cancel=Db::name('verification_cancel')->insertGetId($canceldata);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        if($cancel){
            $this->success('核销成功');
        }else{
            $this->error('核销失败');
        }
        //核销data

    }

    /**
     * 9、用户员工管理
     */
    public function userstaff(){
        $store_id=$this->store_id;
        //查询员工
        $page = $this->request->post('page', 1);
        $total = $this->request->post('total', 10);
        $where["a.verification_store_id"] = ["=", $this->store_id];
        $staff=Db::name('verification_staff')
            ->where($where)
            ->paginate($total, false, ['page' => $page])
            ->toArray();
        $this->success("查询成功",$staff);
    }

    /**
     *10、修改员工职务
     */
    public function updatestaff(){
        $id=$this->postParam['id'];
        $type=$this->postParam['type'];
        $res=Db::name('verification_staff')
            ->where('id',$id)
            ->update([
                'type'=>$type
            ]);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改异常");
        }
    }

    /**
     * 11、邀请代理二维码
     */
    public function inviteagent(){
        $image=$this->store['agent_image'];
        $this->success("查询成功",$image);
    }

    /**
     * 12、业绩查询
     */
    public function selectPerformance(){
        $store_id=$this->store_id;
        $row='';
        $this->success("查询成功",$row);
    }

}