使用Redis的哈希生成统计数据

需求:看如下表格的统计需求

首先使用yii2生成各条入库单的数据,再将入库单的数据算成品种等级的数据,再生成时使用hash类型,因为键是由基地,品种,等级的复合键,值为退货库存,其他库存,定向采购销售出库,非定向采购销售出库

代码如下:

public function actionExport()
    {
        $data = [];
        $wms_check_begin_at = 0;
        $wms_check_end_at = strtotime('2019-01-30 23:59:59');
        $common_producer_info_list = \core\models\CommonProducerInfo::getCommonProducerInfoList();
        foreach ($common_producer_info_list as $common_producer_info){
            $common_producer_info_id = $common_producer_info['id'];
            $dataProvider = \core\models\WmsInfo::getWmsProductInfoListProvider($common_producer_info_id, $wms_check_begin_at, $wms_check_end_at);
            foreach ($dataProvider->getModels() as $model){
                $data[] = $model;
            }
        }

        foreach ($data as &$sheet_row){
            $in_sheet_number = $sheet_row['in_sheet_number'];
            $in_sheet = \core\models\WmsProductInSheet::findOne(['wms_product_in_sheet_number'=>$in_sheet_number]);
            if (strpos($in_sheet->stock_origin_type, '退货') !==false){
                $sheet_row['refund_weight'] = \common\models\Base::weightBcdiv($sheet_row['weight']);
                $sheet_row['not_refund_weight'] = 0;
            }else{
                $sheet_row['refund_weight'] = 0;
                $sheet_row['not_refund_weight'] = \common\models\Base::weightBcdiv($sheet_row['weight']);
            }
            $command = (new \yii\db\Query())->select([
                'SUM(
                IFNULL(
                    podet.wms_product_out_detail_info_out_weight,
                    0
                )
            ) AS sum_out_weight',
            ])->from('{{%wms_product_out_detail_info}} podet')
                ->leftJoin('{{%wms_product_out_sheet}} po', 'po.wms_product_out_sheet_number = podet.wms_product_out_sheet_number')
                ->where('podet.is_del = 0
                AND po.is_del = 0
                AND po.wms_product_out_sheet_status = 1
                AND po.wms_product_out_sheet_type = :type
                AND podet.wms_product_in_sheet_number = :in_sheet_number
        AND po.wms_product_out_sheet_product_out_date >= :wms_check_begin_at
        AND po.wms_product_out_sheet_product_out_date <= :wms_check_end_at',
                    [
                        ':in_sheet_number'=>$in_sheet_number,
                        ':type'=>'销售',
                        ':wms_check_begin_at'=>$wms_check_begin_at,
                        ':wms_check_end_at'=>$wms_check_end_at
                    ])->createCommand();
            $sales_out_weight = $command->queryScalar();
            if (strpos($in_sheet->stock_origin_type, '采购') !==false){
                $sheet_row['ding_weight'] = \common\models\Base::weightBcdiv($sales_out_weight);
                $sheet_row['not_ding_weight'] = 0;
            }else{
                $sheet_row['ding_weight'] = 0;
                $sheet_row['not_ding_weight'] = \common\models\Base::weightBcdiv($sales_out_weight);
            }
            unset($sheet_row);
        }


        /*
        $excel_name = '销售库存统计';
        $headers = [
            'producer_name' => '基地',
            'info_name' => '品种',
            'grade_name' => '等级',
            'in_sheet_number' => '单号',
            'refund_weight' => '库存退货',
            'not_refund_weight' => '其他库存',
            'ding_weight' => '销售出库定向采购',
            'not_ding_weight' => '销售出库非定向采购',
        ];

        $options = [
            'creator'=>'中国汉广集团IT信息中心',
            'last_modified_by'=>'中国汉广集团IT信息中心',
            'title'=>$excel_name,
            'subject'=>$excel_name,
            'description'=>$excel_name,
            'keywords'=>$excel_name,
            'category'=>$excel_name,
            'summary'=>[
                'producer_name' => false,
                'info_name' => false,
                'grade_name' => false,
                'in_sheet_number' => false,
                'refund_weight' => false,
                'not_refund_weight' =>false,
                'ding_weight' => false,
                'not_ding_weight' =>false,
            ]
        ];

        $style_options = [
            'h_align'=>[
                'producer_name' => 'left',
                'info_name' => 'left',
                'grade_name' => 'left',
                'in_sheet_number' => 'left',
                'refund_weight' => 'right',
                'not_refund_weight' =>'right',
                'ding_weight' => 'right',
                'not_ding_weight' =>'right',
            ]
        ];
        \core\components\MyExcelHelper::array2excel($data, $excel_name, $headers, $options, $style_options);
        die;*/
        
        $ret_data = [];

        foreach ($data as &$ret_item){
            foreach ($ret_item as $item_key=>$ret_val){
                if (!in_array($item_key, ['producer_name', 'info_name', 'grade_name', 'refund_weight', 'not_refund_weight', 'ding_weight', 'not_ding_weight'])){
                    unset($ret_item[$item_key]);
                }
            }
            unset($ret_item);
        }
        foreach ($data as $item){
            $rk = $item['producer_name']."-".$item['info_name']."-".$item['grade_name'];
            if (\Yii::$app->redis->exists($rk)){
                $refund_weight = \Yii::$app->redis->hget($rk, 'refund_weight');
                $not_refund_weight = \Yii::$app->redis->hget($rk, 'not_refund_weight');
                $ding_weight = \Yii::$app->redis->hget($rk, 'ding_weight');
                $not_ding_weight = \Yii::$app->redis->hget($rk, 'not_ding_weight');
                \Yii::$app->redis->del($rk);
                \Yii::$app->redis->hmset($rk,
                    'producer_name', $item['producer_name'],
                    'info_name', $item['info_name'],
                    'grade_name', $item['grade_name'],
                    'refund_weight', bcadd($refund_weight, $item['refund_weight'], 2),
                    'not_refund_weight', bcadd($not_refund_weight, $item['not_refund_weight'], 2),
                    'ding_weight', bcadd($ding_weight, $item['ding_weight'], 2),
                    'not_ding_weight', bcadd($not_ding_weight, $item['not_ding_weight'], 2));
            }else{
                \Yii::$app->redis->hmset($rk,
                    'producer_name', $item['producer_name'],
                    'info_name', $item['info_name'],
                    'grade_name', $item['grade_name'],
                    'refund_weight', $item['refund_weight'],
                    'not_refund_weight', $item['not_refund_weight'],
                    'ding_weight', $item['ding_weight'],
                    'not_ding_weight', $item['not_ding_weight']);
            }
        }

        $rks = \Yii::$app->redis->keys('*-*-*');
        sort($rks, SORT_STRING);
        foreach ($rks as $rk){
            $rk_st = explode('-', $rk);
            if (count($rk_st) == 3){
                $ret_data[] = [
                    'producer_name'=>$rk_st[0],
                    'info_name'=>$rk_st[1],
                    'grade_name'=>$rk_st[2],
                    'refund_weight'=>\Yii::$app->redis->hget($rk, 'refund_weight'),
                    'not_refund_weight'=>\Yii::$app->redis->hget($rk, 'not_refund_weight'),
                    'ding_weight'=>\Yii::$app->redis->hget($rk, 'ding_weight'),
                    'not_ding_weight'=>\Yii::$app->redis->hget($rk, 'not_ding_weight'),
                ];
                \Yii::$app->redis->del($rk);
            }

        }

        $excel_name = '销售库存统计';
        $headers = [
            'producer_name' => '基地',
            'info_name' => '品种',
            'grade_name' => '等级',
            'refund_weight' => '库存退货',
            'not_refund_weight' => '其他库存',
            'ding_weight' => '销售出库定向采购',
            'not_ding_weight' => '销售出库非定向采购',
        ];

        $options = [
            'creator'=>'中国汉广集团IT信息中心',
            'last_modified_by'=>'中国汉广集团IT信息中心',
            'title'=>$excel_name,
            'subject'=>$excel_name,
            'description'=>$excel_name,
            'keywords'=>$excel_name,
            'category'=>$excel_name,
            'summary'=>[
                'producer_name' => false,
                'info_name' => false,
                'grade_name' => false,
                'refund_weight' => false,
                'not_refund_weight' =>false,
                'ding_weight' => false,
                'not_ding_weight' =>false,
            ]
        ];

        $style_options = [
            'h_align'=>[
                'producer_name' => 'left',
                'info_name' => 'left',
                'grade_name' => 'left',
                'refund_weight' => 'right',
                'not_refund_weight' =>'right',
                'ding_weight' => 'right',
                'not_ding_weight' =>'right',
            ]
        ];
        \core\components\MyExcelHelper::array2excel($ret_data, $excel_name, $headers, $options, $style_options);

    }

 将生成的数据导出EXCEL;

再将EXCEL数据进行数据透视形成汇总数据

猜你喜欢

转载自www.cnblogs.com/liuzhiqaingxyz/p/10465070.html
今日推荐