<?php
/**
 * 基本设置
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/12/5
 * Time: 15:24
 */

namespace JinDouYun\Model\System;

use Jindouyun\Cache\EnterpriseCache;
use JinDouYun\Model\SysAreaChina\MSysAreaChina;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\System\DBasicSetup;

class MBasicSetup
{
    private $objDBasicSetup;
    private $onlineEnterpriseId;
    private $objEnterpriseCache;

    public function __construct($enterpriseId)
    {
        $this->onlineEnterpriseId = $enterpriseId;
        $this->objDBasicSetup = new DBasicSetup('default');
        $this->objEnterpriseCache = new EnterpriseCache();

    }

    /**
     * @param $params
     * @return ResultWrapper
     */
    public function setting($params)
    {
        $dbResult = $this->objDBasicSetup->replace($params);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
        }

        //缓存redis企业生产日期设置
        $cacheData = json_decode($params['basicData'], true);
        $this->objEnterpriseCache->EnterpriseDataCache($this->onlineEnterpriseId, 'shelfLifeSetUp', $cacheData['shelfLifeSetUp']);

        //删除企业缓存
        $this->objEnterpriseCache->delEnterpriseSetting($this->onlineEnterpriseId);
        //缓存企业设置
        $cacheResult = $this->objEnterpriseCache->setEnterpriseSetting($this->onlineEnterpriseId, $cacheData);
        if(!$cacheResult){
            return ResultWrapper::fail('缓存添加失败', ErrorCode::$paramError);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * @param bool $enterpriseId
     * @return ResultWrapper
     */
    public function getBasicSetup($enterpriseId = false)
    {
        !$enterpriseId && $enterpriseId = $this->onlineEnterpriseId;
        //获取缓存企业设置
        $cacheResult = $this->objEnterpriseCache->getEnterpriseSetting($enterpriseId);
        if($cacheResult){
            $format['basicData'] = $cacheResult;
            $dbResult = self::formatArea($format);
            return ResultWrapper::success($dbResult);
        }
        unset($cacheResult);

        $dbResult = $this->objDBasicSetup->get(['enterpriseId' => $enterpriseId]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
        }
        if (!empty($dbResult)) {
            $dbResult['basicData'] = empty($dbResult['basicData']) ? [] : json_decode($dbResult['basicData'], true);
            //缓存企业设置
            $cacheResult = $this->objEnterpriseCache->setEnterpriseSetting($enterpriseId, $dbResult['basicData']);
            if(!$cacheResult){
                return ResultWrapper::fail('缓存添加失败', ErrorCode::$paramError);
            }

            if (!empty($dbResult['basicData']) && isset($dbResult['basicData'])) {
                $dbResult = self::formatArea($dbResult);
            }
        }

        return ResultWrapper::success($dbResult);
    }

    /**
     * 格式化省市区
     * @param $params
     * @return mixed
     */
    private function formatArea($params)
    {
        $objMSysAreaChina = new MSysAreaChina();
        $areaName = $objMSysAreaChina->getNameByCode([
            isset($params['basicData']['region'][0]) ? $params['basicData']['region'][0] : 0,
            isset($params['basicData']['region'][1]) ? $params['basicData']['region'][1] : 0,
            isset($params['basicData']['region'][2]) ? $params['basicData']['region'][2] : 0
        ]);
        if (!empty($areaName)){
            if (isset($params['basicData']['region'][0])){
                $params['area']['provinceName'] = isset($areaName[$params['basicData']['region'][0]]) ? $areaName[$params['basicData']['region'][0]] : '';
            }
            if (isset($params['basicData']['region'][1])){
                $params['area']['cityName'] = isset($areaName[$params['basicData']['region'][1]]) ? $areaName[$params['basicData']['region'][1]] : '';
            }
            if (isset($params['basicData']['region'][2])){
                $params['area']['districtName'] = isset($areaName[$params['basicData']['region'][2]]) ? $areaName[$params['basicData']['region'][2]] : '';
            }
        }
        return $params;
    }

    /**
     * 获取保质期设置
     */
    public function getShelfLifeSetUp()
    {
        $dbResult = $this->objDBasicSetup->get(['enterpriseId' => $this->onlineEnterpriseId]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
        }
        if (!empty($dbResult)) {
            $dbResult['basicData'] = empty($dbResult['basicData']) ? [] : json_decode($dbResult['basicData'], true);
        }
        $returnData = $dbResult['basicData']['shelfLifeSetUp'];//保质期设置 4:禁用 5:启用
        return ResultWrapper::success($returnData);
    }

    /**
     * @param $field
     * @return ResultWrapper
     */
    public function getBasicField($field)
    {
        $dbResult = $this->objDBasicSetup->get_field('basicData', ['enterpriseId' => $this->onlineEnterpriseId]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
        }
        $basicData = [];
        if (!empty($dbResult)) {
            $basicData = empty($dbResult) ? [] : json_decode($dbResult, true);
        }

        $return = [];
        if (is_string($field)) {
            $return = [
                $field => isset($basicData[$field]) ? $basicData[$field] : '',
            ];
        } elseif (is_array($field)) {
            foreach ($field as $key => $value) {
                $return[$value] = isset($basicData[$value]) ? $basicData[$value] : '';
            }
        }
        return ResultWrapper::success($return);
    }
}