Serial.php 7.6 KB
<?php

namespace app\school\controller\project_approval;

use app\common\controller\DoubleBase;
use app\school\controller\project_common\ProjectData;
use app\school\model\ManystoreShop;
use app\school\model\project_common\ProjectRegister;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Serial extends DoubleBase
{
    protected $containerId = null;
    protected $model = null;
    // 评审阶段(0:立项评审;1:中期检查;2:项目结题)
    protected $reviewStage = 0;
    protected $shopIdAutoCondition = false;
    // 项目级别列表
    protected $levels = [
        '1' => '院级',
        '2' => '校级',
        '3' => '省级',
        '4' => '国家级'
    ];
    // 引入获取项目数据的traits
    use ProjectData;

    public function _initialize()
    {
        parent::_initialize();
        $this->containerId = $_COOKIE['container_id'];
        $this->model = new ProjectRegister();
    }

    public function index()
    {
        if ($this->request->isAjax())
        {
            $map = [];
            $map['a.review_stage'] = ['IN', '1,2'];
            if ($this->auth->rule_type == 1){
                //所属学院或部门
                $map['a.project_college'] =  $this->auth->college_id;
            }
            $result = $this->approvalProjectData($map);
            return json($result);
        }

        $this->assignconfig('project_level', $this->levels);
        $this->assign('levels', $this->levels);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->where(array('id'=>$ids,'school_id'=>SCHOOL_ID))->find();


        $domain = strtoupper(ManystoreShop::get($this->auth->school_id)['subdomain_name']);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if($domain == 'BTMC') {
                    $params['project_no'] = $domain.'DC-2021'.$params['project_no'];
                }
                if($this->model->where(['school_id' => SCHOOL_ID,'project_no' => $params['project_no']])->where('id','neq',$params['id'])->find()) {
                    $this->error(__('项目编号已存在'));
                }
                $result = false;
                Db::startTrans();
                try {
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        if($domain == 'BTMC') {
            $newProjectNo = substr($row['project_no'],11);
            $row['project_no'] = $newProjectNo;
        }
        $this->view->assign("row", $row);
        $this->assignconfig("domian", $domain);
        $this->assign('project_levels', $this->levels);
        return $this->view->fetch();
    }

    /**
     * 项目编号
     */
    public function serial()
    {
        if ($this->request->isAjax()) {
            $ids = explode(',', $this->request->param('ids'));
            $projectNos = [];
            // 已经使用过的项目编号
            $savedProjectNos = $this->model->column('project_no');
            // 与id数量对应的唯一编号数组
            $uniqueProjectNos = $this->getNoRepeatProjectNos(count($ids), $savedProjectNos);

            foreach ($ids as $k => $id) {
                $projectNos[] = [
                    'id' => $id,
                    'project_no' => $uniqueProjectNos[$k]
                ];
            }
            Db::startTrans();
            try {
                $this->model->allowField(true)->saveAll($projectNos);
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $errMsg = $e->getMessage();
                // 数据重复
                if (strpos($errMsg, 'Duplicate entry')) {
                    if (strpos($errMsg, 'project_no')) {
                        $errMsg = '项目编号重复';
                    }
                }
                $this->error($errMsg);
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
    }
//    public function serial()
//    {
//        if ($this->request->isAjax()) {
//            $ids = explode(',', $this->request->param('ids'));
//            $projectNos = [];
//            // 已经使用过的项目编号
//            $savedProjectNos = $this->model->column('project_no');
//            // 与id数量对应的唯一编号数组
//            $uniqueProjectNos = $this->getNoRepeatProjectNos(count($ids), $savedProjectNos);
//
//            foreach ($ids as $k => $id) {
//                $projectNos[] = [
//                    'id' => $id,
//                    'project_no' => $uniqueProjectNos[$k]
//                ];
//            }
//            Db::startTrans();
//            try {
//                $this->model->allowField(true)->saveAll($projectNos);
//                Db::commit();
//            } catch (PDOException $e) {
//                Db::rollback();
//                $errMsg = $e->getMessage();
//                // 数据重复
//                if (strpos($errMsg, 'Duplicate entry')) {
//                    if (strpos($errMsg, 'project_no')) {
//                        $errMsg = '项目编号重复';
//                    }
//                }
//                $this->error($errMsg);
//            } catch (Exception $e) {
//                Db::rollback();
//                $this->error($e->getMessage());
//            }
//            $this->success();
//        }
//    }


    /**
     * 获取指定个数的编号(与数据库中项目编号不重复)
     * @param $len int 需要的编号个数
     * @param $savedProjectNos array 已经存在的编号
     * @return array
     */
    private function getNoRepeatProjectNos($len, $savedProjectNos) {
        $result = [];
        while ($len--) {
            $result[] = $this->getProjectNo();
        }
        $repeats = array_intersect($result, $savedProjectNos);
        // 存在重复编号
        if (0 < count($repeats)) {
            foreach ($repeats as $k => $v) {
                $result[$k] = $this->getNoRepeatNo($savedProjectNos);
            }
        }
        return $result;
    }

    /**
     * 递归获取唯一的项目编号
     * @param $savedProjectNos array 已经存在的编号
     * @return string
     */
    private function getNoRepeatNo($savedProjectNos)
    {
        $res = $this->getProjectNo();
        if (!in_array($res, $savedProjectNos)) {
            return $res;
        }
        $this->getNoRepeatNo($savedProjectNos);
    }

    /**
     * 生成项目编号,格式(WL + 年月日时分秒 + 微秒(6位))
     * @return string
     */
    private  function getProjectNo()
    {
        return 'WL' . date('YmdHis') . $this->getMicrotime();
    }

    /**
     * 获取当前时间的微秒数(6位)
     * @return mixed|string
     */
    private function getMicrotime() {
        $tempTime = microtime(true);
        return explode('.', number_format($tempTime - intval($tempTime), 6))[1];
    }
}