<?php

namespace App\Services\goods;

use App\Exception\custom\GoodsException;
use App\Models\goods\mysql\Ota;

class OtaService
{
    const COLUMNS = ['ota_id','ota_name','printer_sn','printer_key'];
    /**
     * 获取供应商列表（op 后台）
     * @param string $otaName
     * @param string $contacts
     * @param string $phone
     * @param int $offset
     * @param int $limit
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function list($otaName='',$printerSN='',$printerKey='',$offset=0,$limit=20)
    {
        $otaName && $where['ota_name'] = $otaName;
        $printerSN && $where["printer_sn"] = $printerSN;
        $printerKey && $where['printer_key'] = $printerKey;

        $where['status'] = Ota::STATUS_NORMAL;
        $where['ORDER'] = ['ota_id' => 'DESC'];
        $where['LIMIT'] = [$offset, $limit];

        $res['list'] = Ota::select(self::COLUMNS, $where);
        unset($where['ORDER'],$where['LIMIT']);
        $res['total'] = Ota::count('*', $where);

        return $res;
    }

    /**
     * 获取某个ota详情
     * @param $OtaId
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function detail($OtaId)
    {
        return Ota::get(self::COLUMNS,['ota_id'=>$OtaId]);
    }


    /**
     * 根据id批量获取供应商信息
     * @param $otaIds
     * @return \Api\PhpUtils\Mysql\MysqlBase|array
     */
    public static function getOtaListByIds($otaIds)
    {
        if(empty($otaIds)) {
            return [];
        }

        return Ota::select(self::COLUMNS,['ota_id'=>$otaIds]);
    }

    /**
     * 添加供应商
     * @param $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     * @throws GoodsException
     */
    public static function add($params)
    {
        //检查供应商是否已存在
        if (self::list($params['ota_name'])['total'] > 0) {
            throw new GoodsException(['cus' => GoodsException::OTA_ALREADY_EXIST]);
        }

        //检查打印机是否已存在 -- 打印机已被（商家名称）绑定
        if(isset($params['printer_sn']) && $params['printer_sn']) {
            $printer = self::list('',$params['printer_sn']);

            if ($printer['total'] > 0) {
                throw new GoodsException(
                    ['cus' => GoodsException::PRINTER_ALREADY_EXIST,'data'=>[$printer['list'][0]['ota_name']]]
                );
            }
        }

        $data['ota_name'] = $params['ota_name'];
        $data['printer_sn'] = $params['printer_sn'] ?? '';
        $data['printer_key'] = $params['printer_key'] ?? '';

        return Ota::insert($data);
    }

    /**
     * 删除供应商
     * @param $otaId
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function deleteOne($otaId)
    {
        return self::update(['ota_id'=>$otaId,'status'=>Ota::STATUS_DELETE]);
    }

    /**
     * 更新供应商信息
     * @param $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     * @throws GoodsException
     */
    public static function update($params)
    {
        if(empty($params['ota_id'])) {
            throw new GoodsException(['cus' => GoodsException::EMPTY_OTA_ID]);
        }
        //检查供应商是否存在
        if (empty(self::detail($params['ota_id']))) {
            throw new GoodsException(['cus' => GoodsException::OTA_NOT_EXIST]);
        }
        $data = [];
        isset($params['ota_name']) && $data['ota_name'] = $params['ota_name'];
        isset($params['printer_sn']) && $data['printer_sn'] = $params['printer_sn'];
        isset($params['printer_key']) && $data['printer_key'] = $params['printer_key'];
        isset($params['status']) && $data['status'] = $params['status'];

        return Ota::update(
            $data,
            ['ota_id'=>$params['ota_id']]
        );
    }

}