<?php

namespace app\common\model;

use think\model;

class FlightPlan extends model
{
    // 表名
    protected $name = 'flight_plan';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;
    
    public function setCityAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }
    
    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }
    
    public function unique($field, $msg = '')
    {
        $map = [];
        foreach ($field as $key => $val) {
            if (is_numeric($key)) {
                $map[$val] = $this->data[$val];
            } else {
                $map[$key] = $val;
            }
        }
        
        $result = $this->where($map)->find();
        
        if ($result) {
            $this->error = $msg ?: '数据已存在';
            return false;
        }
        
        return true;
    }
    
    
}