<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use think\Db;

/**
 * 优惠券
 */
class Coupon extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';



    //优惠券管理
    public function lists(){
        $status = input('status',1);

        $where = [
            'company_id' => $this->auth->company_id,
            'status'     => $status,
        ];

        $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select();
        $this->success(1,$list);
    }

    //新增
    public function add(){
        $field = ['name','info','days'];
        $data = request_post_hub($field);

        $data['company_id'] = $this->auth->company_id;
        $data['createtime'] = time();
        $data['status'] = 1;

        Db::name('coupons')->insertGetId($data);
        $this->success('添加成功');
    }

    //上下架
    public function changestatus(){
        $id = input('id',0);
        $info = Db::name('coupons')->where('id',$id)->update(['status'=>0]);
        $this->success();
    }

    //详情
    public function info(){
        $id = input('id',0);
        $info = Db::name('coupons')->where('id',$id)->find();
        $this->success(1,$info);
    }

    //编辑
    public function edit(){
        $id = input('id','');

        $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
        if(empty($check)){
            $this->error('不存在的卡券');
        }

        //
        $field = ['name','info','days'];
        $data = request_post_hub($field);

        Db::name('coupons')->where('id',$id)->update($data);
        $this->success('编辑成功');
    }

    //删除
    public function delete(){
        $id = input('id','');

        $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
        if(empty($check)){
            $this->error('不存在的优惠券');
        }

        Db::name('coupons')->where('id',$id)->delete();
        $this->success('删除成功');
    }

    //统一核销,获取信息
    public function gethexiao(){
        $code = input('code','','trim');
        $code = htmlspecialchars_decode($code);
        $code = explode('_',$code);
        if(!is_array($code) || !isset($code[0]) || !isset($code[1])){
            $this->error('错误的核销码');
        }

        $action = $code[0];
        $id = intval($code[1]);

        if($action != 'hexiaocoupon' && $action != 'hexiaoorder'){
            $this->error('错误的核销码');
        }

        if($action == 'hexiaocoupon'){
            $map = [
                'company_id' => $this->auth->company_id,
                'id'         => $id,
                'remain'     => ['gt',0],
            ];
            $check = Db::name('user_coupons')->field('id,coupon_name as name,endtime,getfrom')->where($map)->find();
            if(!empty($check)){
                $check['action'] = $action;
            }
        }else{
            $map = [
                'company_id' => $this->auth->company_id,
                'id'         => $id,
                'ordertype'  => 3,
                'status'     => 2, //2=已支付,待处理
            ];
            $check = Db::name('order')->field('id,package_title as name,package_endtime as endtime,package_price as price')->where($map)->find();
            if(!empty($check)){
                $check['getfrom'] = '购买';
                $check['action'] = $action;
            }
        }
        if(empty($check))
        {
            $this->error('错误的核销码');
        }

        $this->success(1,$check);
    }

    //统一核销,提交
    public function hexiao(){
        $code = input('code','');
        $code = explode('_',$code);
        $action = $code[0];
        $id = $code[1];

        if($action != 'hexiaocoupon' && $action != 'hexiaoorder'){
            $this->error('错误的核销码');
        }

        $this->$action($id);
    }

    //核销用户一张卡券
    public function hexiaocoupon($user_coupon_id){

        $map = [
            'company_id' => $this->auth->company_id,
            'id'         => $user_coupon_id,
        ];

        Db::startTrans();
        $check = Db::name('user_coupons')->where($map)->lock(true)->find();
        if(empty($check)){
            Db::rollback();
            $this->error('不存在的卡券');
        }
        if($check['remain'] <= 0){
            Db::rollback();
            $this->error('卡券数量不足');
        }
        if($check['endtime'] <= time()){
            Db::rollback();
            $this->error('卡券已过期');
        }

        //核销日志
        $log = [
            'user_id' => $check['user_id'],
            'company_id' => $check['company_id'],
            'coupons_id' => $check['coupons_id'],
            'coupon_name' => $check['coupon_name'],
            'user_coupon_id' => $check['id'],
            'staff_id' => $this->auth->id,
            'createtime' => time(),
        ];
        $log_id = Db::name('user_coupons_log')->insertGetId($log);
        if(!$log_id){
            Db::rollback();
            $this->error('核销失败');
        }

        //数量减一
        $rs = Db::name('user_coupons')->where($map)->update(['remain'=>$check['remain']-1]);
        if($rs === false){
            Db::rollback();
            $this->error('核销失败');
        }

        Db::commit();
        $this->success('卡券核销完成');
    }


    //核销用户的套餐订单
    public function hexiaoorder($order_id){
        $map = [
            'company_id' => $this->auth->company_id,
            'id'         => $order_id,
            'ordertype'  => 3,
            'status'     => 2, //2=已支付,待处理
        ];

        Db::startTrans();
        $check = Db::name('order')->where($map)->lock(true)->find();
        if(empty($check)){
            Db::rollback();
            $this->error('不存在的订单');
        }

        if($check['package_endtime'] < time()){
            Db::rollback();
            $this->error('该套餐已到期');
        }


        $rs = Db::name('order')->where($map)->update(['hexiao_time'=>time()]);
        if($rs === false){
            Db::rollback();
            $this->error('核销失败');
        }

        Db::commit();
        $this->success('订单核销完成');
    }

}