YonyouBipBaseData.php 2.91 KB
<?php

namespace app\admin\model;

use think\Model;

/**
 * 用友BIP基础数据模型
 * 用于缓存从用友BIP获取的基础数据
 */
class YonyouBipBaseData extends Model
{
    // 表名
    protected $name = 'yonyou_bip_base_data';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    /**
     * 数据类型常量
     */
    const TYPE_ORGANIZATION = 'organization';      // 销售组织
    const TYPE_TRANSACTION_TYPE = 'transaction_type';  // 交易类型
    const TYPE_DEPARTMENT = 'department';        // 销售部门
    const TYPE_PERSON = 'person';                // 销售业务员
    const TYPE_CUSTOMER = 'customer';           // 客户

    /**
     * 根据类型和编码获取数据
     * @param string $type 数据类型
     * @param string $code 编码
     * @return array|null
     */
    public static function getByTypeAndCode($type, $code)
    {
        return self::where(['data_type' => $type, 'bip_code' => $code])->find();
    }

    /**
     * 根据类型和本地ID获取数据
     * @param string $type 数据类型
     * @param int $localId 本地ID
     * @return array|null
     */
    public static function getByTypeAndLocalId($type, $localId)
    {
        return self::where(['data_type' => $type, 'local_id' => $localId])->find();
    }

    /**
     * 获取指定类型的所有数据
     * @param string $type 数据类型
     * @return array
     */
    public static function getListByType($type)
    {
        return self::where(['data_type' => $type])->select();
    }

    /**
     * 保存或更新基础数据
     * @param string $type 数据类型
     * @param string $bipCode 用友BIP编码
     * @param string $bipName 用友BIP名称
     * @param int|null $localId 本地ID(可选)
     * @param array $extraData 额外数据
     * @return bool
     */
    public static function saveOrUpdate($type, $bipCode, $bipName, $localId = null, $extraData = [])
    {
        $data = self::where(['data_type' => $type, 'bip_code' => $bipCode])->find();
        
        $saveData = [
            'data_type' => $type,
            'bip_code' => $bipCode,
            'bip_name' => $bipName,
            'local_id' => $localId,
            'extra_data' => json_encode($extraData, JSON_UNESCAPED_UNICODE),
            'updatetime' => time(),
        ];

        if ($data) {
            // 更新
            return $data->save($saveData);
        } else {
            // 新增
            $saveData['createtime'] = time();
            return self::create($saveData);
        }
    }

    /**
     * 清除指定类型的数据
     * @param string $type 数据类型
     * @return bool
     */
    public static function clearByType($type)
    {
        return self::where(['data_type' => $type])->delete();
    }
}