审查视图

application/mobile/model/Secret.php 3.7 KB
何书鹏 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?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';
何书鹏 authored
15 16 17 18
    // 错误提示
    public $error = '';
    // 状态码
    public $code = 0;
何书鹏 authored
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

    // 追加属性
    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;
        }
何书鹏 authored
38
        $info = $this->get($data['secret_id']);
何书鹏 authored
39 40 41 42 43 44 45 46 47 48 49 50 51 52
        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)){
何书鹏 authored
53
            	$this->setError('规格信息不存在');
何书鹏 authored
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        		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();
70
            $pay_price = !empty($order) ? $spec['current_price']-$order['secret_price'] : $spec['current_price'];
何书鹏 authored
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        }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;
    }
何书鹏 authored
130
}