Secret.php 3.7 KB
<?php
namespace app\mobile\model;

use think\Model;

class Secret extends Model
{
	// 表名
    protected $name = 'mobile_secret';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 错误提示
    public $error = '';
    // 状态码
    public $code = 0;

    // 追加属性
    protected $append = [
        'do_num'
    ];

    public function getDoNumAttr($value,$data){
    	return $data['do_num_virtual'] + $data['do_num_real'];
    }

    /**
     * 下单预览
     */
    public function payView($user,$data)
    {
        if(empty($data['secret_id'])){
        	$this->setError('缺少必要参数');
        	return false;
        }
        $info = $this->get($data['secret_id']);
        if(empty($info)){
        	$this->setError('密卷信息不存在');
        	return false;
        }
        if($user['group_id'] == 1){
            $company = Company::get(['user_id'=>$user['id']]);
            if(empty($data['spec_id'])){
            	$this->setError('请选择规格',2);
            	return false;
            }
            $spec = SecretSpec::where('id',$data['spec_id'])
                ->where('secret_id',$data['secret_id'])
                ->find();
            if(empty($spec)){
            	$this->setError('规格信息不存在');
        		return false;
            }
            // 企业下员工数量(包含管理员)
            $people_num = CompanyUser::where('company_id',$company['id'])
                ->where('status','1')
                ->count();
            if($spec['is_top'] == '0' && $spec['people_num'] < $people_num){
            	$this->setError('该套餐满足不了您公司');
        		return false;
            }
            // 企业已购该套餐
            $order = SecretOrder::where('company_id',$company['id'])
                ->where('secret_id',$data['secret_id'])
                ->where('pay_status','1')
                ->order('people_num desc')
                ->find();
            $pay_price = !empty($order) ? $spec['current_price']-$order['secret_price'] : $spec['current_price'];
        }else{
            // 积分
            $score_info = ['score'=>$user['score'],'score_price'=>0,'use_score'=>0];
            if(!empty($data['score_switch'])){
                if($info['current_price'] >= $user['score']){
                    $score_info['score_price'] = $user['score'];
                    $score_info['use_score'] = $user['score'];
                }else{
                    $score_info['score_price'] = $info['current_price'];
                    $score_info['use_score'] = ceil($info['current_price']);
                }
            }
            $pay_price = bcsub($info['current_price'], $score_info['score_price'], 2);
        }
        return [
            'secret_info' => $info,
            'spec_info' => $user['group_id'] == 1 ? $spec : [],
            'secret_price' => $info['current_price'],
            'score_info' => $user['group_id'] == 1 ? [] : $score_info,
            'pay_price' => $pay_price,
        ];
    }

    /**
     * 设置错误信息
     * @param $error
     */
    private function setError($error,$code=0)
    {
        empty($this->error) && $this->error = $error;
        $this->code = $code;
    }

    /**
     * 是否存在错误
     * @return bool
     */
    private function hasError()
    {
        return !empty($this->error);
    }

    /**
     * 获取错误信息
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * 获取状态码
     * @return string
     */
    public function getCode()
    {
        return $this->code;
    }
}