IndexController.class.php 2.29 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 18/6/21
 * Time: 17:08
 */
namespace Frontend\Controller\UpdateDB;

use Think\Db;

class IndexController extends AbstractController
{
    /**
     * 数据库升级脚本
     *      1、新增人员属性:英文名、上级,为旧数据写入默认数据;
     * @author liyifei
     * @return string
     */
    public function Index()
    {
        $db = &Db::getInstance();

        // 所有企业
        $all_domain = $db->query("SELECT DISTINCT `domain` FROM `oa_contact_attr` WHERE `status` < 3");
        $all_domain = array_column($all_domain, 'domain');

        // 删除已写入的默认数据
        try {
            $db->query("DELETE FROM `oa_contact_attr` WHERE `field_name` IN ('memEnglishName', 'memIsleader') AND `status` < 3");

        } catch (\Exception $e) {
            // exit("数据库升级失败:" . $e->getMessage());
        }

        // 为所有企业写入默认数据
        $content = '';
        $times = MILLI_TIME;
        $sql = "INSERT INTO `oa_contact_attr` (`field_name`, `attr_name`, `postion`, `type`, `option`, `order`, `is_system`, `is_open`, `is_open_edit`, `is_required`, `is_required_edit`, `is_open_cp`, `is_open_cp_edit`, `is_required_cp`, `is_required_cp_edit`, `is_show`, `is_show_edit`, `is_allow_user_modify`, `is_allow_user_modify_edit`, `domain`, `created`) VALUES ";

        $option = serialize(
            [
                [
                    'name' => '是',
                    'value' => 1
                ],
                [
                    'name' => '否',
                    'value' => 0
                ]
            ]
        );
        foreach ($all_domain as $domain) {
            $content .= "('memEnglishName', '英文名', 1, 1, '', 10, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, '{$domain}', {$times}), ('memIsleader', '上级', 1, 7, '{$option}', 10, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, '{$domain}', {$times}),";
        }

        // 执行sql,写入"培训阶段"数据
        if (!empty($content)) {
            $sql .= substr($content, 0, -1) . ';';

            try {
                $db->query($sql);

            } catch (\Exception $e) {

                // exit("数据库升级失败:" . $e->getMessage());
            }
        }

        exit('SUCCESS');
    }
}