<?php
/**
 * Author        : Tengda
 * Date          : 2021/7/29
 * Time          : 11:12 上午
 */

use App\Base\Base;
use App\Services\goods\OtaService;
use Validate\OtaValidate;

class OtaController extends Base
{

    /**
     * 供应商列表
     */
    public function listAction()
    {
        (new OtaValidate())->scene('list')->validate();

        $name = $this->params['ota_name'] ?? '';
        $printerSn = $this->params['printer_sn'] ?? '';
        $printerKey = $this->params['printer_key'] ?? '';
        $labelPrinterSn = $this->params['label_printer_sn'] ?? '';
        $labelPrinterKey = $this->params['label_printer_key'] ?? '';
        $isNormal = $this->params['is_normal'] ?? false;
        $offset = $this->params['offset'] ?? 0;
        $limit = $this->params['limit'] ?? 20;
        $res = OtaService::list($name, $printerSn, $printerKey,$labelPrinterSn, $labelPrinterKey, $isNormal, $offset, $limit);

        $this->success(['result' => $res]);
    }

    /**
     * 根据id批量获取供应商信息
     * @throws \App\Exception\custom\ParamException
     */
    public function ota_list_by_idsAction()
    {
        (new OtaValidate())->scene('listByIds')->validate();
        $otaIds = $this->params['ota_id'] ?? '';
        $otaIds = explode(',',$otaIds);
        $res = OtaService::getOtaListByIds($otaIds);

        $this->success(['result' => $res]);
    }

    /**
     * 添加供应商
     * @throws \App\Exception\custom\ParamException
     */
    public function addAction()
    {
        (new OtaValidate())->scene('add')->validate();
        $res = OtaService::add($this->params);

        $this->success(['status' => $res ? 'success' : 'failed']);
    }

    /**
     * 删除一个供应商
     * @throws \App\Exception\custom\ParamException
     */
    public function deleteAction()
    {
        (new OtaValidate())->scene('delete')->validate();
        $res = OtaService::deleteOne($this->params['ota_id']);

        $this->success(['status' => $res ? 'success' : 'failed']);
    }

    /**
     * 删除一个供应商
     * @throws \App\Exception\custom\ParamException
     */
    public function undeleteAction()
    {
        (new OtaValidate())->scene('undelete')->validate();
        $res = OtaService::undeleteOne($this->params['ota_id']);

        $this->success(['status' => $res ? 'success' : 'failed']);
    }


    /**
     * 更新供应商
     * @throws Exception
     */
    public function updateAction()
    {
        (new OtaValidate())->scene('update')->validate();
        $res = OtaService::update($this->params, true);

        $this->success(['status' => $res ? 'success' : 'failed']);
    }
}