Em.php 3.4 KB
<?php
namespace app\em\model;

class Em extends Base
{
    protected $table = 'em';

    public function getInitDateAttr($val){
        return date('Y-m-d H:i:s',$val);
    }
    public function setInitDateAttr($val){
        return strtotime($val);
    }


    //获取电表列表与基站相关信息
    public function getAll($pageConf=['list_rows'=>30]){
        $data = $this
            ->field('em.id,em.em_numb,em.em_type_id,em.create_time,s.station_name,s.original_sp_id,
                                 s.station_sp_code,s.proj_number,s.area_id,r.region_name,o.operator_name,er.pricing_type')
            ->leftJoin(['station_info'=>'s'],'em.station_id = s.station_id')
            ->leftJoin(['region'=>'r'],'s.area_id = r.id')
            ->leftJoin(['operator'=>'o'],'o.operator_id = s.original_sp_id')
            ->leftJoin(['em_rule'=>'er'],'er.em_id=em.id')
            ->with('account')
            ->order('id desc')
            ->paginate($pageConf);
        return $data;
    }

    public function account(){
        return $this->hasOne('em_account','em_id');
    }

    //通过id获取单个电表信息
    public function getEmInfoById($id,$mode=false){
        if($mode === false)
            $em = self::with('rule,dc,photo')->find($id);
        else
            $em = self::with($mode)->find($id);
        return $em;
    }

    /**
     * @param $id
     * @param bool $curr 获取本次抄表记录还是上次抄表记录
     * @return array|mixed|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getEmUseageInfoById($id,$curr=false){
        $em = $this->getEmInfoById($id,'type,rule');
        //最后一次抄表记录
        if($curr == false){
            $ue = Useage::order('create_time desc,id desc')->get(['em_id'=>$id]);
        } else {
            $ue = Useage::with('photo')->order('create_time desc,id desc')->get(['em_id'=>$id]);
        }
         //如果没有超过表 不论是峰谷表还是普通表 都是空的
        if(!$ue) {
            $ue = new Useage();
            $ue->last_date = $em->init_date;
            $ue->last_sum_numb = $em->init_numb ?: 0;
        } else {
            if($curr == false){
                $ue->last_sum_numb = $ue->current_sum_numb;
                $ue->last_date     = $ue->current_date;
            }
        }
        $ue->price = $em->rule->pricing_type==1 ? $em->rule->pay_price : '';
        $em->useage = $ue;
        return $em;
    }

    //获取电表信息与station信息
    public function getBaseInfo($em_id){
        $data = self::field('em.id,em.em_numb,em.em_type_id,em.rate')
                    ->field('s.station_sp_code,s.station_name')
                    ->field('et.name as typeName')
                    ->leftJoin(['station_info'=>'s'],'s.station_id=em.station_id')
                    ->leftJoin(['em_type'=>'et'],'et.id=em.em_type_id')
                    ->where('em.id','=',$em_id)
                    ->find();
        return $data;
    }

    //关联模型
    public function rule(){
        return $this->hasOne('EmRule','em_id','id');
    }

    public function dc(){
        return $this->belongsTo('Dc','dc_id');
    }

    public function photo(){
        return $this->belongsTo('Receipt','photo_id','id');
    }

    public function type(){
        return $this->belongsTo('EmType','em_type_id','id');
    }
}