各位還想去外包公司的,來看看程式碼質量吧

bing8u發表於2020-07-16

嗯,我在外包公司了,工資還挺可以的,外企月薪資1W5,混了2個月了,但是感覺做不下去了。。。

為了吃飯的各位,能為了工作而到外包公司上班的老鐵,看看這些程式碼質量,這能看的下去的,說明你非常適合到外包公司上班。

這不是打擊人的,是在告訴各位外包公司是可以給得起工資,也並非全部是996。

至少為我這裡是早上9:30上班, 下午6:00下半,雙休,來了這麼久還未加過1次班。
加班不給加班費,第二天可以晚來加班的時長。

條件還挺好,但是我堅持不下去的原因,就是這些程式碼,每次修改一個功能就要重寫一大堆。。。

以前也去過另一個外包公司,到公司3天,走跑路了,2年前那時候薪資1W3,外包公司確實工資還可以的。
外包公司的缺點就是程式碼質量差,難改哦,各位看看這些質量吧。

外企有的福利都有,我捨不得現在不加班的舒適感,但是又感覺自己做不下來,先發發牢騷吧。

感覺都是laravel害了我,用了它4年了,如果寫的感覺還能簡潔的,就想繼續優化下去,它就是用這些優雅害得我都融入不了其他公司了,我哭笑啊!

檔案1

<?php
namespace Silksoftwarecorp\CmsTool\Model;

use Silksoftwarecorp\CmsTool\Api\CampaignInterface;
use Magento\Framework\Serialize\SerializerInterface;
class CampaignRepository implements CampaignInterface
{
    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    private $categoryRepository;
    protected $_productRepository;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    protected $typeModel;
    protected $cache;
    /**
     * @var SerializerInterface
     */
    private $serializer;
    protected $product;
    public function __construct(
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Silksoftwarecorp\CmsTool\Model\TypeFactory $typeModel,
        SerializerInterface $serializer,
        \Magento\Catalog\Model\Product $product,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\App\CacheInterface $cache
    ){
        $this->categoryRepository = $categoryRepository;
        $this->_productRepository = $productRepository;
        $this->typeModel = $typeModel;
        $this->storeManager = $storeManager;
        $this->cache = $cache;
        $this->_product = $product;
        $this->serializer   = $serializer;
    }
    /**
     * Get Tools Campaign
     *
     * @param string $channel
     * @param string $category_id
     * @param string $is_main_category
     * @return mixed
     */
    public function getToolsCampaign($channel, $category_id)
    {
        $result = array();
        $data = [];
        $storeId = (int)$this->storeManager->getStore()->getId();
        $cacheKey = 'cmstool_api_campaign_'.$channel.'_'. $storeId.'_'.($category_id?'_'.$category_id:null);

        $mata = $this->cache->load($cacheKey);
        /*var_dump($mata);
        exit;*/
        if(!$mata)
        {
            $helper = $this->getObject('Silksoftwarecorp\CmsTool\Helper\Datas');
            $templates = $helper->getToolsCampaignData($channel,$category_id);
    //        $mediaUrl = $this->getObject('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl().'pub/media/';
            $mediaUrl = $this->getObject('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);        
            if($templates)
            {
                if (isset($templates[0]['template'])) {
                    $campaigns = json_decode($templates[0]['template'],true);
                    if ($campaigns){
                        foreach ($campaigns as $campaign) {
                            $campaignData = [];
                            $fields = [];
                            $typesModel = $this->typeModel->create()->load($campaign['type_id']);
                            $params = 'campaign_id,campaign_name,category_id,sort,channel,start_date,end_date,type_id,template_name';
                            if($typesModel->getFields())
                            {
                                $params = $params.','.$typesModel->getFields();
                            }
                            $campaignData['type_text'] = $typesModel->getTypeText();
                            $campaignData['type_name'] = $typesModel->getTypeName();
                            $fields = explode(',',$params);
                            $mata = [];
                            $cata = [];
                            foreach ($campaign as $key => $value) {
                                if(in_array($key, $fields))
                                {
                                    if ($value) {       
                                        $campaignData[$key] = $value;
                                        if ($key == 'img') {
                                            $campaignData[$key] = $mediaUrl.$value; // ing url completion
                                        }
                                        if ($key == 'img_pc') {
                                            $campaignData[$key] = $mediaUrl.$value; // ing url completion
                                        }
                                        if ($key == 'img_mb') {
                                            $campaignData[$key] = $mediaUrl.$value; // ing url completion
                                        }
                                        if ($key == 'start_date') {
                                            $campaignData[$key] = strtotime($value); // ing url completion
                                        }
                                        if ($key == 'end_date') {
                                            $campaignData[$key] = strtotime($value); // ing url completion
                                        }
                                        for($i=1;$i<11;$i++)
                                        {
                                            if($key == 'link_category_id_'.$i)
                                            {
                                                $mata[$i-1]['link_category_id'] = $value;
                                                $category = $this->categoryRepository->get($value,$storeId);
                                                if(!empty($category))
                                                {
                                                    $url = $category->getUrl();
                                                    $mata[$i-1]['web_url'] = $url;  
                                                }
                                                unset($campaignData[$key]);
                                            }
                                            if($key == 'link_category_name_'.$i)
                                            {
                                                $mata[$i-1]['link_category_name'] = $value;   
                                                unset($campaignData[$key]);
                                            }
                                            if($key=='link_category_name_'.$i.'_zh')
                                            {
                                                $mata[$i-1]['link_category_name_zh'] = $value;   
                                                unset($campaignData[$key]);   
                                            }
                                        }
                                        if($key == 'link_category_id')
                                        {
                                            $category = $this->categoryRepository->get($value,$storeId);
                                            if(!empty($category))
                                            {
                                                $url = $category->getUrl();  
                                                $campaignData['web_url'] = $url;  
                                            }
                                        }

                                        if ($key == 'link_sku'&&!empty($value)) {
                                            try {
                                                $product = $this->_productRepository->get($value,false,$storeId);
                                            } catch (\Magento\Framework\Exception\NoSuchEntityException $e){
                                                $product = false;
                                            }
                                            if($product)
                                            {
                                                $product_zh = $this->_productRepository->get($value,false,1);
                                                $product_en = $this->_productRepository->get($value,false,2);  
                                                $campaignData['product_url'] = $product->getProductUrl();
                                                $campaignData['price'] = number_format($product->getFinalPrice(), 2, '.', ''); 
                                                if(!empty($product_zh))
                                                {
                                                    $campaignData['name_zh'] = $product_zh->getName();     
                                                }
                                                if(!empty($product_en))
                                                {
                                                    $campaignData['name'] = $product_en->getName();

                                                }
                                            }    
                                        }

                                    }
                                } 

                            }
                            if(!empty($mata))
                            {
                                foreach($mata as $k=>$v)
                                {
                                    $cata[] = $v;
                                }
                            }
                            $campaignData['category_list'] = $cata;


                            $data[] = $campaignData;
                        }

                    }
                }
            }else{
                $templatet = $helper->getToolsTemplateData($channel,$category_id);
                if ($templatet) {
                    foreach ($templatet as $template) {
                        $templateData = [];
                        if(isset($template['channel'])&&in_array($channel, explode(',',$template['channel'])))
                        {
                            $fields = [];
                            $params = 'template_id,template_name,category_id,sort,channel,start_date,end_date,show_homepage,sort_homepage,is_main_category,type_text,type_id';


                            if(isset($template['fields']))
                            {
                                $params = $params.','.$template['fields'];
                            }
                            $fields = explode(',',$params);
                            $mata = [];
                            $cata = [];
                            foreach ($template as $key => $value) {
                                if(in_array($key, $fields))
                                {
                                    if ($value) {       
                                        $templateData[$key] = $value;
                                        if ($key == 'img') {
                                            $templateData[$key] = $mediaUrl.$value; // ing url completion
                                        }
                                        if ($key == 'img_pc') {
                                            $templateData[$key] = $mediaUrl.$value; // ing url completion
                                        }
                                        if ($key == 'img_mb') {
                                            $templateData[$key] = $mediaUrl.$value; // ing url completion
                                        }
                                        if ($key == 'start_date') {
                                            $templateData[$key] = strtotime($value); // ing url completion
                                        }
                                        if ($key == 'end_date') {
                                            $templateData[$key] = strtotime($value); // ing url completion
                                        }
                                        for($i=1;$i<11;$i++)
                                        {
                                            if($key == 'link_category_id_'.$i)
                                            {
                                                $mata[$i-1]['link_category_id'] = $value;
                                                $category = $this->categoryRepository->get($value,$storeId);
                                                if(!empty($category))
                                                {
                                                    $url = $category->getUrl();
                                                    $mata[$i-1]['web_url'] = $url;  
                                                }
                                                unset($templateData[$key]);
                                            }
                                            if($key == 'link_category_name_'.$i)
                                            {  
                                                $mata[$i-1]['link_category_name'] = $value;   
                                                unset($templateData[$key]);
                                            }
                                            if($key=='link_category_name_'.$i.'_zh')
                                            {
                                                $mata[$i-1]['link_category_name_zh'] = $value;   
                                                unset($templateData[$key]);   
                                            }
                                        }
                                        if($key == 'link_category_id')
                                        {
                                            $category = $this->categoryRepository->get($value,$storeId);
                                            if(!empty($category))
                                            {
                                                $url = $category->getUrl();  
                                                $templateData['web_url'] = $url;  
                                            }
                                        }

                                        if ($key == 'link_sku'&&!empty($value)) {
                                            try {
                                                $product = $this->_productRepository->get($value,false,$storeId);
                                            } catch (\Magento\Framework\Exception\NoSuchEntityException $e){
                                                $product = false;
                                            }
                                            if($product)
                                            {
                                                $product_zh = $this->_productRepository->get($value,false,1);
                                                $product_en = $this->_productRepository->get($value,false,2);  
                                                $templateData['product_url'] = $product->getProductUrl();
                                                $templateData['price'] = number_format($product->getFinalPrice(), 2, '.', '');  
                                                if(!empty($product_zh))
                                                {
                                                    $templateData['name_zh'] = $product_zh->getName();     
                                                }
                                                if(!empty($product_en))
                                                {
                                                    $templateData['name'] = $product_en->getName();   
                                                }
                                            }    
                                        }

                                    }
                                } 

                            }
                            if(!empty($mata))
                            {
                                foreach($mata as $k=>$v)
                                {
                                    $cata[] = $v;
                                }
                            }
                            $templateData['category_list'] = $cata;

                        }
                        $data[] = $templateData;
                    }       
                }
            }
            $mata = $this->serializer->serialize($data);
            $this->cache->save($mata, $cacheKey, [], 60);
        } 
        $result['ret_code'] = 200;
        $result['data'] = $this->serializer->unserialize($mata);

        return $this->renderResult($result);
    }


    /**
     * @param $result
     * @return mixed
     */
    public function renderResult($result)
    {
        return $this->getObject('Magento\Framework\Json\EncoderInterface')->encode($result);
    }

    /**
     * @param $className
     * @return mixed
     */
    public function getObject($className)
    {
        return \Magento\Framework\App\ObjectManager::getInstance()->get($className);
    }

    /**
     * @param $className
     * @return mixed
     */
    public function createObject($className)
    {
        return \Magento\Framework\App\ObjectManager::getInstance()->create($className);
    }
}

檔案2

<?php
namespace Silksoftwarecorp\CmsTool\Model;

use Silksoftwarecorp\CmsTool\Api\TemplateInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class TemplateRepository implements TemplateInterface
{
    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    private $categoryRepository;
    protected $_productRepository;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    protected $cache;
    /**
     * @var SerializerInterface
     */
    private $serializer;
    public function __construct(
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        SerializerInterface $serializer,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\App\CacheInterface $cache
    ){
        $this->serializer   = $serializer;
        $this->categoryRepository = $categoryRepository;
        $this->_productRepository = $productRepository;
        $this->storeManager = $storeManager;
        $this->cache = $cache;
    }
    /**
     * Get Tools Template
     *
     * @param string $channel
     * @param string $category_id
     * @param string $is_main_category
     * @return mixed
     */
    public function getToolsTemplate($channel, $category_id = null ,$is_main_category = null)
    {
        $result = array();
        $data = [];
        $storeId = (int)$this->storeManager->getStore()->getId();
        $cacheKey = 'cmstool_api_template_'.$channel.'_'.$storeId.'_'.($category_id?'_'.$category_id:null);

        $mata = $this->cache->load($cacheKey);
        if(!$mata)
        {
            $helper = $this->getObject('Silksoftwarecorp\CmsTool\Helper\Data');
            $templates = $helper->getToolsTemplateData($channel,$category_id,$is_main_category);
    //        $mediaUrl = $this->getObject('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl().'pub/media/';
            $mediaUrl = $this->getObject('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

            // Only get exist field


            if ($templates) {
                foreach ($templates as $template) {
                    $templateData = [];
                    if(isset($template['channel'])&&in_array($channel, explode(',',$template['channel'])))
                    {
                        $fields = [];
                        $params = 'template_id,template_name,category_id,sort,channel,start_date,end_date,show_homepage,sort_homepage,is_main_category,type_text,type_id';


                        if(isset($template['fields']))
                        {
                            $params = $params.','.$template['fields'];
                        }
                        $fields = explode(',',$params);
                        $mata = [];
                        $cata = [];
                        foreach ($template as $key => $value) {
                            if(in_array($key, $fields))
                            {
                                if ($value) {       
                                    $templateData[$key] = $value;
                                    if ($key == 'img') {
                                        $templateData[$key] = $mediaUrl.$value; // ing url completion
                                    }
                                    if ($key == 'img_pc') {
                                        $templateData[$key] = $mediaUrl.$value; // ing url completion
                                    }
                                    if ($key == 'img_mb') {
                                        $templateData[$key] = $mediaUrl.$value; // ing url completion
                                    }
                                    if ($key == 'start_date') {
                                        $templateData[$key] = strtotime($value); // ing url completion
                                    }
                                    if ($key == 'end_date') {
                                        $templateData[$key] = strtotime($value); // ing url completion
                                    }
                                    for($i=1;$i<11;$i++)
                                    {
                                        if($key == 'link_category_id_'.$i)
                                        {
                                            $mata[$i-1]['link_category_id'] = $value;
                                            $category = $this->categoryRepository->get($value,$storeId);
                                            if(!empty($category))
                                            {
                                                $url = $category->getUrl();
                                                $mata[$i-1]['web_url'] = $url;  
                                            }
                                            unset($templateData[$key]);
                                        }
                                        if($key == 'link_category_name_'.$i)
                                        {  
                                            $mata[$i-1]['link_category_name'] = $value;   
                                            unset($templateData[$key]);
                                        }
                                        if($key=='link_category_name_'.$i.'_zh')
                                        {
                                            $mata[$i-1]['link_category_name_zh'] = $value;   
                                            unset($templateData[$key]);   
                                        }
                                    }
                                    if($key == 'link_category_id')
                                    {
                                        $category = $this->categoryRepository->get($value,$storeId);
                                        if(!empty($category))
                                        {
                                            $url = $category->getUrl();  
                                            $templateData['web_url'] = $url;  
                                        }
                                    }

                                    if ($key == 'link_sku'&&!empty($value)) {
                                        try {
                                            $product = $this->_productRepository->get($value,false,$storeId);
                                        } catch (\Magento\Framework\Exception\NoSuchEntityException $e){
                                            $product = false;
                                        }
                                        if($product)
                                        {
                                            $product_zh = $this->_productRepository->get($value,false,1);
                                            $product_en = $this->_productRepository->get($value,false,2);  
                                            $templateData['product_url'] = $product->getProductUrl();
                                            $templateData['price'] = number_format($product->getFinalPrice(), 2, '.', '');  
                                            if(!empty($product_zh))
                                            {
                                                $templateData['name_zh'] = $product_zh->getName();     
                                            }
                                            if(!empty($product_en))
                                            {
                                                $templateData['name'] = $product_en->getName();   
                                            }
                                        }    
                                    }

                                }
                            } 

                        }
                        if(!empty($mata))
                        {
                            foreach($mata as $k=>$v)
                            {
                                $cata[] = $v;
                            }
                        }
                        $templateData['category_list'] = $cata;

                    }
                    $data[] = $templateData;
                }
            }
            $mata = $this->serializer->serialize($data);
            $this->cache->save($mata, $cacheKey, [], 60);
        }


        $result['ret_code'] = 200;
        $result['data'] = $this->serializer->unserialize($mata);

        return $this->renderResult($result);
    }


    /**
     * @param $result
     * @return mixed
     */
    public function renderResult($result)
    {
        return $this->getObject('Magento\Framework\Json\EncoderInterface')->encode($result);
    }

    /**
     * @param $className
     * @return mixed
     */
    public function getObject($className)
    {
        return \Magento\Framework\App\ObjectManager::getInstance()->get($className);
    }

    /**
     * @param $className
     * @return mixed
     */
    public function createObject($className)
    {
        return \Magento\Framework\App\ObjectManager::getInstance()->create($className);
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章