<?php


namespace JinDouYun\Model\System;


use JinDouYun\Dao\System\DAdminSetting;
use JinDouYun\Dao\System\DProcessSetting;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

/**
 * @copyright   Copyright (c) https://www.qianniaovip.com All rights reserved
 * Description:
 * Class MProcessSetting
 * @package JinDouYun\Model\System
 */
class MProcessSetting
{
    /**
     * @var DProcessSetting
     */
    private $objDProcessSetting;

    /**
     * @var int
     */
    private $enterpriseId;

    /**
     * @var DAdminSetting
     */
    private $objDAdminSetting;

    /**
     * MProcessSetting constructor.
     * @param $enterpriseId
     */
    public function __construct($enterpriseId)
    {
        $this->enterpriseId = $enterpriseId;
        $this->objDProcessSetting = new DProcessSetting();
        $this->objDAdminSetting = new DAdminSetting();
        self::init();
    }

    /**
     * Doc: (des="初始化")
     * User: XMing
     * Date: 2020/11/6
     * Time: 2:19 下午\
     * @return ResultWrapper
     */
    private function init(): ResultWrapper
    {
        $adminLists = $this->objDAdminSetting->select(['type' => StatusCode::$adminSettingType['process']]);
        if ($adminLists === false) {
            return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
        }
        if (empty($adminLists)) {
            return ResultWrapper::success(true);
        }
        $lists = $this->objDProcessSetting->select(['enterpriseId' => $this->enterpriseId]);
        if ($lists === false) {
            return ResultWrapper::fail($this->objDProcessSetting->error(), ErrorCode::$dberror);
        }
        $processTypeIds = [];
        foreach ($lists as $val) {
            $processTypeIds[] = $val['processType'];
        }
        $inserts = [];
        foreach ($adminLists as $val) {
            if (in_array($val['signId'],$processTypeIds)){
                continue;
            }
            $inserts[] = [
                'processType' => $val['signId'],
                'enterpriseId' => $this->enterpriseId,
                'content' => $val['content'],
                'createTime' => time(),
            ];
        }

        if (!empty($inserts)){
            $result = $this->objDProcessSetting->insert($inserts,true);
            if ($result === false){
                return ResultWrapper::fail($this->objDProcessSetting->error(),ErrorCode::$dberror);
            }
        }
        return ResultWrapper::success(true);
    }

    /**
     * Doc: (des="设置")
     * User: XMing
     * Date: 2020/11/6
     * Time: 11:58 上午
     * @param array $params
     * @return ResultWrapper
     */
    public function set(array $params): ResultWrapper
    {
        $result = $this->objDProcessSetting->replace($params,true);
        if ($result === false) {
            return ResultWrapper::fail($this->objDProcessSetting->error(), ErrorCode::$paramError);
        }
        return ResultWrapper::success(true);
    }

    /**
     * Doc: (des="获取配置")
     * User: XMing
     * Date: 2020/11/6
     * Time: 12:11 下午
     */
    public function getAll(): ResultWrapper
    {
        $lists = $this->objDProcessSetting->select(['enterpriseId' => $this->enterpriseId]);
        if ($lists === false) {
            return ResultWrapper::fail($this->objDProcessSetting->error(), ErrorCode::$dberror);
        }
        foreach ($lists as &$list) {
            $list['content'] = empty($list['content']) ? [] : json_decode($list['content'], true);
        }
        return ResultWrapper::success($lists);

    }

    /**
     * Doc: (des="获取流程配置")
     * User: XMing
     * Date: 2020/11/6
     * Time: 4:10 下午
     * @param int $type
     * @param string $field
     * @return ResultWrapper
     */
    public function getProcessSettingByType(int $type,string $field = ''): ResultWrapper
    {
        $result = $this->objDProcessSetting->get(['enterpriseId'=>$this->enterpriseId,'processType'=>$type],'content');
        if ($result === false){
            return ResultWrapper::fail($this->objDProcessSetting->error(),ErrorCode::$dberror);
        }
        $set = isset($result['content']) ? json_decode($result['content'],true) : [];
        if (!empty($field) && !empty($set)){
            isset($set[$field]) && $set = $set[$field];
        }
        return ResultWrapper::success($set);
    }


    /**
     * Doc: (des="构建设置数据")
     * User: XMing
     * Date: 2020/11/6
     * Time: 12:06 下午
     * @param array $params
     * @return array
     */
    private static function buildProcessSetting(array $params): array
    {
        return [
            "processType"  => isset($params['processType']) ? $params['processType'] : 0,
            //int(10) NOT NULL DEFAULT '0' COMMENT '流程类型',
            "content"      => isset($params['content']) ? json_encode($params['content']) : null,
            //json DEFAULT NULL COMMENT '流程配置',
            "enterpriseId" => isset($params['enterpriseId']) ? $params['enterpriseId'] : 0,
            //int(11) NOT NULL COMMENT '企业id',
            "createTime"   => time(),
            //int(10) NOT NULL DEFAULT '0' COMMENT '创建时间',
            "updateTime"   => time(),
            //int(10) NOT NULL DEFAULT '0' COMMENT '更新时间'
        ];
    }


}