<?php

namespace app\models\company;

use think\facade\Db;
use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

class CompanyModel extends BaseModel
{
	/**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'company';

    use ModelTrait;

    public static function systemPage($where)
    {
        $model = new self;
        if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
        if ($where['sid'] !== '') $model = $model->where('sid', $where['sid']);
        $model = $model->where('is_del', 0);
        $model = $model->order('sort desc,id desc');
        $count = $model->count();
        $list = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
            $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
        })->toArray();
        return compact('count', 'list');
    }

    /**
     * 详情
     */
    public static function getOne($id)
    {
        $info = self::where('is_del', 0)->find($id);
        if ($info) {
            if ($info['start_time'])
                $start_time = date('Y-m-d H:i:s', $info['start_time']);

            if ($info['end_time'])
                $end_time = date('Y-m-d H:i:s', $info['end_time']);
            if (isset($start_time) && isset($end_time))
                $info['section_time'] = [$start_time, $end_time];
            else
                $info['section_time'] = [];
            unset($info['start_time'], $info['end_time']);
        }
        if ($info['poster'])
            $info['poster'] = json_decode($info['poster'], true);
        else
            $info['poster'] = [];
        $info['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $id])->order('role asc, level asc')->select();
        return $info;
    }

}