Production.php 2.39 KB
<?php

namespace domain\production;

use Yii;
use domain\production\models\Production     as ProductionModel;
use Exception;

/**
 * 生产日期
 * Class Production
 * @package domain\production
 */
class Production
{
    /**
     * 创建生产日期
     * @param $item
     */
    static function create($item)
    {
        try {
            $findProductionModel = ProductionModel::findOne(['name' => $item["name"]]);
            if (!empty($findProductionModel)) {
                return -1;
            }
            $productionModel = Yii::createObject(ProductionModel::className());
            $productionModel->production_no = self::getProductionNo();
            $productionModel->name = $item["name"]; // 生产日期
            $saveResult = $productionModel->save();
            return $saveResult;
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * 更新生产日期
     * @param $id
     * @param $item
     * @return null|static
     */
    static function update($id, $item)
    {
        $productionModel = ProductionModel::findOne($id);
        if (empty($productionModel)) {
            return false;
        }
        if (isset($item['name']) && $productionModel->name != $item['name']) {
            $findProductionModel = ProductionModel::findOne(['name' => $item["name"]]);
            if (!empty($findProductionModel)) {
                return -1;
            }
        }
        if (isset($item['name']) && !empty($item['name'])) {
            $productionModel->name = $item['name'];
        }

        $resultSave = $productionModel->save();
        return $resultSave;
    }

    /**
     * 删除生产日期
     * @param $id
     * @param $item
     * @return null|static
     */
    public static function delete($id)
    {
        $productionModel = ProductionModel::findOne($id);
        if (empty($productionModel)) {
            return false;
        }

        return ProductionModel::updateAll(["is_delete" => 1], ["id" => $id]);
    }

    /**
     * 获取十六进制生产日期编号
     */
    private static function getProductionNo()
    {
        $findProduction = ProductionModel::find()->orderBy("id desc")->asArray()->one();
        if (empty($findProduction)) {
            return "0001";
        }
        $dataNo = hexdec($findProduction['production_no']) + 1;
        $dataNo = sprintf('%04X', $dataNo);

        return strtoupper($dataNo);
    }
}