| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | <?php/** *cp管理 * @file:Cps.php * @Created by gnitif * @Date: 2023/3/20 * @Time: 13:57 */namespace Modules\CpManage\Models\Cp;use Catch\Exceptions\FailedException;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\DB;class Cps extends Model{    protected $table = "cps";    protected $primaryKey = "cp_id";    protected $fillable = [        'cp_id', 'cp_nick', 'cp_name', 'cp_company', 'manager', 'phone', 'email', 'share_per', 'settlement_type', 'address', 'is_deleted', 'created_at', 'updated_at', 'deleted_at',    ];    protected array $fields = ['cp_id', 'cp_nick', 'cp_name', 'cp_company', 'manager', 'phone', 'email', 'share_per','address', 'created_at', 'updated_at'];    protected array $form =  ['cp_nick',  'cp_name',  'cp_company',  'manager',  'phone',  'email',  'settlement_type','share_per',   'address'];    public function storeBy(array $data): mixed    {        DB::beginTransaction();        try {            if ($this->create($this->filterData($data))){                DB::commit();                return  $this->getKey();            }else{                throw new \Exception("添加失效");            }        }catch (\Exception $exception){            DB::rollBack();            throw new FailedException('添加失败!请刷新重试');        }        return false;    }    public function updateBy($id, array $data)    {        $updated = $this->where($this->getKeyName(), $id)->update($this->filterData($data));        return $updated;    }    public function deleteBy($id)    {        $this->where($this->getKeyName(), $id)->update(['is_deleted' => 1,'deleted_at' => date("Y-m-d H:i:s")]);    }    protected function filterData(array $data): array    {        // 表单保存的数据集合        $fillable = array_unique(array_merge($this->getFillable(), property_exists($this, 'form') ? $this->form : []));        foreach ($data as $k => $val) {            if (is_null($val) || (is_string($val) && ! $val)) {                unset($data[$k]);            }            if (! empty($fillable) && ! in_array($k, $fillable)) {                unset($data[$k]);            }            if (in_array($k, [$this->getUpdatedAtColumn(), $this->getCreatedAtColumn()])) {                unset($data[$k]);            }        }        return $data;    }}
 |