ProductionController.php 5.85 KB
<?php

namespace app\ht\modules\production\controllers;

use Yii;
use yii\data\Pagination;
use domain\production\Production;
use domain\production\ProductionRepository;
use app\ht\controllers\BaseController;

/**
 * 生产日期管理
 * Class ProductionController
 * @package app\ht\modules\production\controllers
 */
class ProductionController extends BaseController
{
    /**
     * 生产日期管理
     */
    public function actionIndex()
    {
        $params = $this->dataList(1);
        /**
         * 渲染模板
         */
        return $this->render('index', $params);
    }

    /**
     * 查询数据列表
     */
    protected function dataList($type = '')
    {
        $request     = Yii::$app->request;
        $creatTime   = $request->get('creatTime');
        $endTime     = $request->get('endTime');
        $productionCreatTime   = $request->get('productionCreatTime');
        $productionEndTime     = $request->get('productionEndTime');

        $gets = [
            'creatTime'  => $creatTime,
            'endTime'    => $endTime,
            'productionCreatTime'  => $productionCreatTime,
            'productionEndTime'    => $productionEndTime,
        ];

        $where = ['and'];
        if ($creatTime) {
            $creatTime = strtotime($creatTime);
            $where[] = ['>=', 'p.created_at', $creatTime];
        }
        if ($endTime) {
            $endTime = strtotime($endTime) + 86400;
            $where[] = ['<=', 'p.created_at', $endTime];
        }
        if ($productionCreatTime) {
            $productionCreatTime = strtotime($productionCreatTime);
            $where[] = ['>=', 'p.name', $productionCreatTime];
        }
        if ($productionEndTime) {
            $productionEndTime = strtotime($productionEndTime) + 86400;
            $where[] = ['<=', 'p.name', $productionEndTime];
        }
        if ($type == 0) {
            $pageList = ProductionRepository::getPageList($where, 0 , 0);
            $pages = null;
        } else {
            $pageSize = 20;
            $pages = new Pagination(['totalCount' => ProductionRepository::getPageCount($where), 'pageSize' => $pageSize]);
            $pageList = ProductionRepository::getPageList($where, $pages->offset, $pages->limit);
        }

        /**
         * 数据整理
         */
        return [
            'listdata'   => $pageList,
            'pages'      => $pages,
            'gets'       => $gets
        ];
    }

    /**
     * 创建生产日期
     * @return string
     */
    public function actionCreate()
    {
        return $this->render('create');
    }

    /**
     * 创建生产日期
     * @return string
     */
    public function actionDoAdd()
    {
        $request = Yii::$app->request;
        $name = $request->post("name"); // 生产日期
        if (empty($name)) {
            Yii::$app->session->setFlash('error', '生产日期不能为空');
            return $this->render('create');
        }
        $result = Production::create($request->post());
        if ($result === -1) {
            Yii::$app->session->setFlash('error', '添加失败,生产日期' . $name . '已存在');
            return $this->render('create');
        }
        if ($result) {
            Yii::$app->session->setFlash('success', '添加成功');
        } else {
            Yii::$app->session->setFlash('error', '添加失败');
        }

        return $this->redirect('index');
    }

    /**
     * 编辑生产日期
     * @return string
     */
    public function actionEdit()
    {
        $productionId = $this->request->get("pid");
        $info = ProductionRepository::selectOne($productionId, true);
        return $this->render('edit', ["info" => $info]);
    }

    /**
     * 编辑生产日期
     * @return string
     */
    public function actionDoEdit()
    {
        $request = Yii::$app->request;
        $name = $request->post("name");
        $pid = $request->post("pid");
        if (empty($pid)) {
            Yii::$app->session->setFlash('error', '生产日期编号不能为空');
            $params = $this->dataList(1);
            return $this->render('index', $params);
        }
        $production = ProductionRepository::selectOne($pid,true);
        if (empty($production)) {
            Yii::$app->session->setFlash('error', '生产日期记录不存在');
            $params = $this->dataList(1);
            return $this->render('index', $params);
        }
        if (empty(trim($name))) {
            Yii::$app->session->setFlash('error', '生产日期不能为空');
            return $this->render('edit', ["info" => $production]);
        }

        $result = Production::update($pid, $request->post());

        if ($result === -1) {
            Yii::$app->session->setFlash('error', '修改的生产日期' . $name . '已存在');
            return $this->render('edit', ["info" => $production]);
        }
        if ($result) {
            Yii::$app->session->setFlash('success', '编辑成功');
        } else {
            Yii::$app->session->setFlash('error', '编辑失败');
        }
        $production = ProductionRepository::selectOne($pid,true);
        return $this->render('edit', ["info" => $production]);
    }

    /**
     * 删除生产日期
     * @return string
     * @throws \Exception
     */
    public function actionDoDel()
    {
        $request = Yii::$app->request;
        $itemId = $request->post("data_id");
        $msg = array();

        // 删除对应的生产日期
        if (Production::delete($itemId)) {
            $msg['status'] = 1;
            $msg['msg'] = "操作成功";
        } else {
            $msg['status'] = 0;
            $msg['msg'] = "操作失败";
        }

        return $this->renderJson($msg);
    }

    /**
     * 导出厂商数据
     * @return string
     */
    public function actionExport()
    {
        $params = $this->dataList(0);
        return $this->renderPartial('export', $params);
    }
}