<?php
/**
 * 工具类
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/03/20
 * Time: 22:38
 */
namespace Jindouyun\Controller\Common;

use Exception;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Factory;

use JinDouYun\Cache\GoodsBasicRelevant;

use JinDouYun\Controller\BaseController;

use JinDouYun\Dao\GoodsManage\DSku;

class Tools extends BaseController
{
    public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
    }

    /**
     * 缓存重置skuId和skuName的绑定
     */
    public function resetCacheSkuName()
    {
        $params = $this->request->getRawJson();
        $data = [
            'enterpriseId' => isset($params['enterpriseId']) ? $params['enterpriseId'] : '',
        ];
        foreach($data as $key => $value){
            if(empty($value)) parent::sendOutput($key.'参数为空', ErrorCode::$paramError);
        }
        $enterpriseId = $data['enterpriseId'];
        $objDSku = new DSku('default');
        $objDSku->setTable($objDSku->get_Table().'_'.$enterpriseId);
        $dbResult = $objDSku->select(['deleteStatus' => StatusCode::$standard]);
        if($dbResult === false){
            parent::sendOutput($objDSku->error(), ErrorCode::$dberror);
        }

        if(empty($dbResult)) parent::sendOutput('查询数据为空', ErrorCode::$paramError);

        $objGoodsBasicRelevant = new GoodsBasicRelevant($enterpriseId);

        foreach ($dbResult as $value) {
            $cacheResult = $objGoodsBasicRelevant->cacheSkuIdRelationName($value['unitName'], $value['id']);
            if(!$cacheResult){
                parent::sendOutput('缓存添加失败', ErrorCode::$paramError);
            }
        }
        parent::sendOutput('添加成功');
    }
}