<?php

namespace App\Models;

use App\Events\CompletedInfo;
use App\Events\CreatedPartner;
use App\Events\PartnerFeedPoolChange;
use App\Events\UpdatedPartner;
use App\Models\Partner\PartnerBeautyUser;
use App\Models\User\UserModel;

/**
 * App\Models\PartnerModel
 *
 * @property int $id 主键
 * @property int $uid 用户id
 * @property string $media_id 公众号id
 * @property string $home 故乡
 * @property string $address 常居城市
 * @property int $sex 性别[0未知；1男；2女]
 * @property string|null $photo_src 照片地址
 * @property int $check_photo 照片审核
 * @property string|null $photo_1 相册1
 * @property int|null $photo_1_check 相册1是否通过：-1未通过；0审核态；1通过
 * @property string|null $photo_2 相册2
 * @property int|null $photo_2_check 相册2是否通过：-1未通过；0审核态；1通过
 * @property string|null $photo_3 相册3
 * @property int|null $photo_3_check 相册3是否通过：-1未通过；0审核态；1通过
 * @property string|null $photo_4 相册4
 * @property int|null $photo_4_check 相册4是否通过：-1未通过；0审核态；1通过
 * @property string|null $voice 语音地址
 * @property string|null $school 学校
 * @property string $subject 专业
 * @property string $grade 年级
 * @property int $height 身高
 * @property string|null $age 生日
 * @property string $star 星座
 * @property int $check_contact 联系方式审核（弃用）
 * @property string|null $introduce 自我描述
 * @property string|null $expect 对另一半的期望
 * @property string|null $secret 暗号
 * @property int $times 被获取次数
 * @property int $praises 点赞数
 * @property int $feed_cnt 曝光次数
 * @property int $last_three_day_feed 近三天滑动次数
 * @property float $charm 魅力值
 * @property int $dechecks 审核失败次数
 * @property int $checks 审核成功次数
 * @property float $total_flower 已收获小fa
 * @property int $upload_at 最近上架时间
 * @property int $update_at 上次敏感信息修改时间
 * @property int $commited_at 提交审核时间
 * @property int $is_self 是否个人交友卡片(弃用)
 * @property int $feed_push_type feed流push类型
 * @property int $is_sell 1上架
 * 0下架
 * -1投诉下架
 * 2审核中
 * -2审核不通过
 * -3删除(或注销删除)
 * 3审核通过未上架
 * @property int $is_push_feed 是否推送到信息流
 * @property int $allow 是否推文
 * @property int $auto_sell 自动上架
 * @property int $is_hide 是否隐藏
 * @property string $qq QQ
 * @property string $weixin 微信
 * @property string $education 学历
 * @property int $base_score 基础分
 * @property int $score 热度
 * @property int $feedcheck_cnt 滑动审核次数
 * @property int $intro_check 描述是否通过：-1未通过；0审核态；1通过
 * @property int $voice_check 语音是否通过：-1未通过；0审核态；1通过
 * @property int $is_fpdx 是否是分配对象：0否；1是(弃用)
 * @property int $is_commit_check 是否提交审核:0否；1是
 * @property int $is_recommend 是否推荐卡片:0否；1是
 * @property int $is_voice_recommend 是否推荐语音:0否；1是
 * @property int $login_at 登录时间
 * @property int $long_no_login_tag 长时间未登录标记
 * @property int $black_at 拉黑时间
 * @property float $lat 纬度
 * @property float $lng 经度
 * @property int $feed_tag_cnt 更新后被滑动次数
 * @property int $feed_tag_like 更新后被喜欢次数
 * @property string $feed_pool_level 卡片池
 * @property int $wait_low_pool 待救活状态=0不待救活1待救活2操作过
 * @property int $wait_select 待精选状态=0不待精选1待精选2操作过
 * @property int $is_select 是否精选0否1是
 * @property int $select_at 精选时间
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 * @property-read \App\Models\User\UserModel $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereAge($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereAllow($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereAutoSell($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereBaseScore($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereBlackAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereCharm($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereCheckContact($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereCheckPhoto($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereChecks($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereCommitedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereDechecks($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereEducation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereExpect($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereFeedCnt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereFeedPoolLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereFeedPushType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereFeedTagCnt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereFeedTagLike($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereFeedcheckCnt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereGrade($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereHeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereHome($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIntroCheck($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIntroduce($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsCommitCheck($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsFpdx($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsHide($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsPushFeed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsSelect($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsSelf($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsSell($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereIsVoiceRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereLastThreeDayFeed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereLoginAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereLongNoLoginTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereMediaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto1($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto1Check($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto2Check($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto3($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto3Check($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto4($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhoto4Check($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePhotoSrc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel wherePraises($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereQq($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereSchool($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereScore($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereSecret($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereSelectAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereStar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereSubject($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereTimes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereTotalFlower($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereUpdateAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereUploadAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereVoice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereVoiceCheck($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereWaitSelect($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PartnerModel whereWeixin($value)
 * @mixin \Eloquent
 */
class PartnerModel extends Model
{

    protected $table = 'kdgx_partner_charge_partner';

    protected $dateFormat = 'U';

    protected $fillable = [
        'uid',
        'media_id',
        'home',
        'address',
        'sex',
        'photo_src',
        'check_photo',
        'voice',
        'school',
        'subject',
        'grade',
        'height',
        'age',
        'star',
        'check_contact',
        'introduce',
        'expect',
        'intro_check',
        'secret',
        'login_at',
        'upload_at',
        'update_at',
        'created_at',
        'updated_at',
        'black_at',
        'is_self',
        'is_sell',
        'feed_push_type',
        'allow',
        'auto_sell',
        'is_hide',
        'qq',
        'weixin',
        'education',
        'is_recommend',
        'is_voice_recommend',
        'feedcheck_cnt',
        'photo_1',
        'photo_1_check',
        'photo_2',
        'photo_2_check',
        'photo_3',
        'photo_3_check',
        'photo_4',
        'photo_4_check',
        'base_score',
        'is_push_feed',
        'voice_check',
        'is_commit_check',
        'is_fpdx',
        'is_sell',
        'auto_sell',
        'day_limit',
        'long_no_login_tag',
        'feed_tag_cnt',
        'feed_tag_like',
        'feed_pool_level',
        'wait_select',
        'is_select',
        'wait_low_pool',
    ];

    protected $casts = [
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'login_at' => 'integer',
        'upload_at' => 'integer',
        'update_at' => 'integer',
        'black_at' => 'integer',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne(UserModel::class, 'uid', 'uid');
    }

    public static function boot()
    {
        parent::boot();

        self::created(function ($model) {
            event(new CreatedPartner($model));
        });

        self::saving(function ($model) {
            /** @var PartnerModel $model */
            $dirty = $model->getDirty();
            $updatekeys = array(
                'check_photo',
                'photo_1_check',
                'photo_2_check',
                'photo_3_check',
                'photo_4_check',
                'voice_check',
                'weixin',
                'qq'
            );
            $inter = array_intersect(array_keys($dirty), $updatekeys);
            if (!empty($inter)) {
                // 修正卡片类型
                if (
                    5 == $model->feed_push_type
                    && (1 == $model->check_photo
                        || 1 == $model->photo_1_check
                        || 1 == $model->photo_2_check
                        || 1 == $model->photo_3_check
                        || 1 == $model->photo_4_check
                        || 1 == $model->voice_check)
                ) {
                    $model->feed_push_type = 3;
                    // 提醒后补全信息回应事件
                    event(new CompletedInfo($model->uid));
                } elseif (
                    3 == $model->feed_push_type
                    && (1 != $model->photo_src
                        && 1 != $model->photo_1_check
                        && 1 != $model->photo_2_check
                        && 1 != $model->photo_3_check
                        && 1 != $model->photo_4_check
                        && 1 != $model->voice_check)
                ) {
                    $model->feed_push_type = 5;
                }

                foreach ($inter as $v) {
                    if (1 != $model->getOriginal($v) && 1 == $model->$v) {
                        $model->feed_tag_cnt = 0;
                        $model->feed_tag_like = 0;
                        $model->wait_select = 0;
                        $model->is_select = 0;
                        $model->feed_pool_level = "new";
                        if (5 == $model->feed_push_type) {
                            $model->feed_pool_level = "low";
                        }
                        break;
                    } elseif (1 == $model->getOriginal($v) && 1 != $model->$v) {
                        $model->feed_tag_cnt = 0;
                        $model->feed_tag_like = 0;
                        $model->wait_select = 0;
                        $model->is_select = 0;
                        $model->feed_pool_level = "new";
                        if (5 == $model->feed_push_type) {
                            $model->feed_pool_level = "low";
                        }
                        break;
                    }
                }
                if (5 == $model->feed_push_type) {
                    $model->feed_pool_level = "low";
                }
            }

            if ("low" == $model->feed_pool_level) {
                if (2 != $model->wait_low_pool) {
                    $model->wait_low_pool = 1;
                }
            } else {
                $model->wait_low_pool = 0;
            }
        });


        self::saved(function ($model) {
            $fields = ['photo_src', 'photo_1', 'photo_2', 'photo_3', 'photo_4'];
            foreach ($fields as $field) {
                if ($model->isDirty($field)) {
                    PartnerBeautyUser::updateOrCreate(
                        ['partner_id' => $model->id, 'field' => $field],
                        ['uid' => $model->uid, 'replaced_at' => 0, 'original_photo' => ($model->{$field} ?: '')]
                    );
                }
            }
        });


        self::updated(function (/** @var PartnerModel $model */ $model) {
            event(new UpdatedPartner($model));
            if ($model->isDirty(['feed_pool_level'])) {
                // 卡片信息流层级池改变
                event(new PartnerFeedPoolChange($model));
            }
        });
    }
}
