ReportForm2.php 7.9 KB
<?php

namespace common\models;

use common\helpers\Utils;
use yii\behaviors\TimestampBehavior;
use Yii;

/**
 * This is the model class for table "{{%report_form}}".
 *
 * @property int $id
 * @property int $report_uid 报单中心uid
 * @property int $uid 报单uid
 * @property int $recomm_id 推荐人UId
 * @property int $team_list 推荐人UId
 * @property string $price 金额
 * @property string $thumb 图片
 * @property string $card_front 图片
 * @property string $card_negative 图片
 * @property string $remark 备注
 * @property string $remarks 备注
 * @property string $name 备注
 * @property string $address 备注
 * @property string $mobile 备注
 * @property int $created_at 创建时间
 * @property int $status 状态
 * @property int $updated_at 更新时间
 */
class ReportForm extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%report_form}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['report_uid', 'uid', 'price', 'created_at',], 'required'],
            [['id', 'report_uid', 'uid', 'created_at', 'updated_at', 'status', 'goods_id', 'team_list'], 'integer'],
            [['price'], 'number'],
            [['thumb', 'remark', 'remarks', 'card_front', 'card_negative', 'name', 'address', 'mobile'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'report_uid' => '销售中心UID',
            'uid' => '销售UID',
            'price' => '金额',
            'thumb' => '凭证',
            'status' => '状态',
            'remark' => '驳回',
            'remarks' => '备注',
            'team_id' => '站队UID',
            'card_front' => '身份证正面',
            'card_negative' => '身份证反面',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            "goods_status"=>"订单状态",
            "express_type"=>"快递公司",
            "express_no"=>"快递单号",

        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',

            ],
        ];
    }

    /**
     * 查找自己及下级的报单记录
     * @param $uid
     * @return array
     */
    public static function findByUid($uid, $page)
    {
        $uids = User::find()->select('uid')->where(" superior REGEXP '^{$uid}_' ")->offset(($page - 1) * 5)->limit(5)->asArray()->all();
        array_push($uids, ['uid' => $uid]);
        $list = $lists = [];
        foreach ($uids as $item) {
            $list[] = self::find()->where(['uid' => $item['uid']])->orderBy("id desc")->asArray()->one();
        }
        foreach ($list as &$value) {
            if (!empty($value)) {
//                foreach ($item as &$value){
                $value['thumb'] = Utils::toMedia($value['thumb']);
                $user = User::findOne(['uid' => $value['uid']]);
                $value['name'] = $user['realname'];
                $value['price'] = $value['price'] . " 元";
                $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
                $value['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
                if ($value['status'] == 2) {
                    $value['status'] = '已审核';
                } elseif ($value['status'] == 1) {
                    $value['status'] = '待审核';
                } else {
                    $value['status'] = '已驳回';
                }
//                }
                $lists[] = $value;
            }
        }
        return $lists;
    }

    /**
     * 报单中心记录
     * @param $uid
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function findByreid($uid, $page)
    {
        $pages = self::find()->where(['report_uid' => $uid])->count(1);
        $list = self::find()->where(['report_uid' => $uid])->orderBy("id desc")->offset(($page - 1) * 5)->limit(5)->asArray()->all();
        $num = self::find()->where(['report_uid' => $uid,"status"=>2])->all();
        foreach ($list as $k => &$value) {
            $user = User::findOne(['uid' => $value['uid']]);
            $report_user = User::findOne(['uid' => $value['report_uid']]);
//            $value['report_uid'] = 'HC' . substr($report_user['mobile'], -6);
            $value['report_uid'] = $report_user['uid'];
            $value['thumb'] = Utils::toMedia($value['thumb']);
            $value['name'] = $user['realname'];
            $value['accountname'] = $user['accountname'];
            $value['created_at'] = date("Y-m-d H:i:s", $value['created_at']);
            $value['status'] = self::status($value['status']);
        }
        $ls['page'] = ceil($pages / 5);
        $ls['list'] = $list;
        $ls['count'] = count($num);
        return $ls;
    }

    /**
     * 查找单条
     * @param $uid
     * @return array|ReportForm|null|\yii\db\ActiveRecord
     */
    public static function findByID($uid)
    {
        $list = self::find()->where(['uid' => $uid])->orderBy("id desc")->asArray()->one();
        if (!empty($list)) {
            $list['thumb'] = empty($list['thumb']) ? '' : Utils::toMedia($list['thumb']);
            $list['created_at'] = date("Y-m-d H:i:s", $list['created_at']);
            $list['status'] = self::status($list['status']);
            $user = User::findOne(['uid' => $list['uid']]);
            $list['name'] = $user['realname'];
        }
        return $list;
    }

    //查询该组的位置上是否有人存在
    public static function findTeamPosi($team,$posi){
        $ishave = self::find()->where(['team_id' => $posi,"team_list"=>$team])->one();
        if(!empty($ishave)){
            return true;
        }else{
            return false;
        }
    }
    //查询该用户是否在该组报过单
    public static function findMembOnTeam($team=0,$uid=0){
//        $ishaves = self::find()->where(['uid' => $uid,"team_list"=>$team,'status'=>2])->one();
        $ishaves = self::find()->where(['uid' => $uid,'status'=>2])->all();
        if(!empty($ishaves)){
            return count($ishaves);
        }else{
            return count($ishaves);
        }
    }

    public static function status($data)
    {
        switch ($data) {
            case 1 :
                return '待审核';
                break;
            case 2 :
                return '通过审核';
                break;
            case 3:
                return '驳回';
                break;
            default :
                return '未知类型';
        }
    }
    //手动添加数据
    public static function addreport($data){
        $model=new ReportForm();
            $model->report_uid=$data['report_uid'];
            $model->uid=$data['uid'];
            $model->name=$data['name'];
            $model->address=$data['address'];
            $model->mobile=$data['mobile'];
            $model->team_list=$data['team_list'];
            $model->posi=$data['posi'];
            $model->is_auto=$data['is_auto'];
            $model->recomm_id=$data['recomm_id'];
            $model->is_auto=$data['is_auto'];
            $model->goods_id=$data['goods_id'];
            $model->price=$data['price'];
            $model->status=$data['status'];
            $model->goods_status=$data['goods_status']; //已收货
            $model->operateid=$data['operateid'];
            $model->created_at=$data['created_at'];
            $model->updated_at=$data['updated_at'];
//        if($model->load($data)){
            if($model->save(false)){
                $arr=['code'=>1,'data'=>Yii::$app->db->getLastInsertID(),'msg'=>"成功报单"];

            }else{
                $arr=['code'=>0,'data'=>"",'msg'=>"报单失败"];
            }
//        }

        return $arr;
    }


}