<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\property;


use app\common\model\BaseModel;
use app\common\model\sale\Sale;
use app\common\model\works\ServiceWork;
use think\facade\Log;


/**
 * PropertyOrder模型
 * Class PropertyOrder
 * @package app\common\model
 */
class PropertyOrder extends BaseModel
{
    
    protected $name = 'property_order';

    public function propertyHead()
    {
        return $this->hasOne(PropertyHead::class, 'id', 'property_head_id')
            ->field('*');
    }
    public function propertyUser()
    {
        return $this->hasOne(PropertyUser::class, 'id', 'property_user_id')
            ->field('*');
    }
    public function propertyWork()
    {
        return $this->hasOne(ServiceWork::class, 'id', 'work_id')
            ->field('id,real_name,mobile,address,create_time');
    }
    public static function onBeforeInsert($model){
        $propertyHeadInfo = PropertyHead::where('id',$model->property_head_id)->findOrEmpty();
        if(!$propertyHeadInfo->isEmpty()){
            $propertyHeadInfo = $propertyHeadInfo->toArray();
            $model->sale_type = $propertyHeadInfo['sale_type']??0;
            $model->sale_id = $propertyHeadInfo['sale_id']??0;
            if($propertyHeadInfo['sale_id']){
                $saleGroupId = Sale::where('id',$propertyHeadInfo['sale_id'])->value('sale_group_id');
                $model->sale_group_id = $saleGroupId??0;
            }
        }
        Log::info('新增物业订单:'.json_encode([$model->property_head_id,$model->sale_type,$model->sale_id,$model->sale_group_id]));
    }

}