| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- namespace addons\CloudStock\backend\controllers;
- use addons\CloudStock\common\models\CloudStockAgent;
- use addons\CloudStock\common\models\CloudStockAgentGuota;
- use addons\CloudStock\common\models\CloudStockGoods;
- use addons\CloudStock\common\models\CloudStockLevel;
- use addons\CloudStock\common\models\CouponSetting;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\CloudStock\backend\controllers
- */
- class GoodsController extends BaseController
- {
- public $enableCsrfValidation = false;
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $params = [
- 'where' => $where, 'order' => 'sort asc,id desc'
- ];
- $page_data = CloudStockGoods::listPage($params,false);
- $level_list = CloudStockLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'order' => 'level asc', 'index' => 'level', 'select' => 'level,name']);
- if (!empty($page_data['list'])) {
- $goods_id_arr = array_column($page_data['list'], 'goods_id');
- $goods_arr = Goods::lists(['where' => [['id' => $goods_id_arr]], 'index' => 'id']);
- foreach ($page_data['list'] as &$item) {
- $item['goods_name'] = $item['price'] = $item['cover_pic'] = '';
- if ($goods_arr[$item['goods_id']]) {
- $item['goods_name'] = $goods_arr[$item['goods_id']]['goods_name'];
- $item['price'] = $goods_arr[$item['goods_id']]['price'];
- $item['cover_pic'] = $goods_arr[$item['goods_id']]['cover_pic'];
- }
- $equal_level_list_arr = json_decode($item['equal_level_list'], true);
- $fill_level_list_arr = json_decode($item['fill_level_list'], true);
- $agent_price_arr = json_decode($item['agent_price'], true);
- $over_level_list_arr = json_decode($item['over_level_list'], true);
- if (!empty($over_level_list_arr)) $over_level_list_arr = array_column($over_level_list_arr, null, 'level');
- $over_level_list_arr_2 = [];
- if(!empty($item['over_level_list_2'])){
- $over_level_list_arr_2 = json_decode($item['over_level_list_2'], true);
- if (!empty($over_level_list_arr_2)) $over_level_list_arr_2 = array_column($over_level_list_arr_2, null, 'level');
- }
- if (!empty($fill_level_list_arr)) $fill_level_list_arr = array_column($fill_level_list_arr, null, 'level');
- $equal_level_list_rows = [];
- if (!empty($equal_level_list_arr)) {
- foreach ($equal_level_list_arr as $val) {
- $equal_level_list_rows[$val['level'] . $val['config_key']] = $val;
- }
- }
- if (!empty($agent_price_arr)) $agent_price_arr = array_column($agent_price_arr, null, 'level');
- list($agent_price,$over_level_list,$over_level_list_2,$fill_level_list,$equal_level_list) = $this->formatDataIndex($level_list,$agent_price_arr,$over_level_list_arr,$over_level_list_arr_2,$fill_level_list_arr,$equal_level_list_rows);
- if(!empty($over_level_list_2)){
- $over_level_list_2_arr = array_column($over_level_list_2, null, 'level');
- }
- foreach ($over_level_list as &$value){
- $value['price2'] = $over_level_list_2_arr[$value['level']]['price']??0;
- }
- $item['agent_price'] = $agent_price;
- $item['over_level_list'] = $over_level_list;
- $item['over_level_list_2'] = $over_level_list_2;
- $item['fill_level_list'] = $fill_level_list;
- $item['equal_level_list'] = $equal_level_list;
- }
- }
- return $this->success('操作成功', $page_data);
- }
- return $this->render($this->action->id, []);
- }
- public function actionEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $id = Yii::$app->request->get('id', '');
- $data = CloudStockGoods::getOne([['id' => $id], ['mall_id' => $this->mall_id], ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]], true);
- if (!empty($data)) {
- $level_list = CloudStockLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'order' => 'level asc']);
- $data['agent_price'] = json_decode($data['agent_price'], true);
- $data['price_difference'] = json_decode($data['price_difference'], true);
- if (empty($data['price_difference'])) {
- $data['price_difference'] = [];
- }
- $agent_price_arr = array_column($data['agent_price'], null, 'level');
- $price_difference_arr = array_column($data['price_difference'], null, 'level');
- $data['equal_level_list'] = json_decode($data['equal_level_list'], true);
- $equal_level_list_rows = [];
- foreach ($data['equal_level_list'] as $val) {
- $equal_level_list_rows[$val['level'] . $val['config_key']] = $val;
- }
- $data['fill_level_list'] = json_decode($data['fill_level_list'], true);
- $fill_level_list_arr = array_column($data['fill_level_list'], null, 'level');
- $data['over_level_list'] = json_decode($data['over_level_list'], true);
- $over_level_list_arr = array_column($data['over_level_list'], null, 'level');
- $over_level_list_arr_2 = [];
- if(!empty($data['over_level_list_2'])){
- $data['over_level_list_2'] = json_decode($data['over_level_list_2'], true);
- $over_level_list_arr_2 = array_column($data['over_level_list_2'], null, 'level');
- }
- $monthly_purchase_arr= [];
- if(!empty($data['monthly_purchase'])){
- $data['monthly_purchase'] = json_decode($data['monthly_purchase'], true);
- $monthly_purchase_arr = array_column($data['monthly_purchase'], null, 'level');
- }
- $additional_purchase_restrictions_arr= [];
- if(!empty($data['additional_purchase_restrictions'])){
- $data['additional_purchase_restrictions'] = json_decode($data['additional_purchase_restrictions'], true);
- $additional_purchase_restrictions_arr = array_column($data['additional_purchase_restrictions'], null, 'level');
- }
- list($agent_price,$over_level_list,$over_level_list_2,$fill_level_list,$equal_level_list,$monthly_purchase,
- $additional_purchase_restrictions, $price_difference) = $this->formatData($level_list, $agent_price_arr, $over_level_list_arr, $over_level_list_arr_2, $fill_level_list_arr,
- $equal_level_list_rows, $monthly_purchase_arr, $additional_purchase_restrictions_arr, $price_difference_arr);
- $data['agent_price'] = $agent_price;
- $data['price_difference'] = $price_difference;
- $data['fill_level_list'] = $fill_level_list;
- $data['over_level_list'] = $over_level_list;
- $data['over_level_list_2'] = $over_level_list_2;
- $data['equal_level_list'] = $equal_level_list;
- $data['monthly_purchase'] = $monthly_purchase;
- $data['additional_purchase_restrictions'] = $additional_purchase_restrictions;
- $restrict_permissions = json_decode($data['restrict_permissions'],true);
- $data['restrict_permissions'] = !empty($restrict_permissions)?$restrict_permissions:[];
- }
- $goods = Goods::findOne(['id' => $data['goods_id']]);
- $data['goods_name'] = $goods['goods_name'];
- $data['cover_pic'] = $goods['cover_pic'];
- $data['price'] = $goods['price'];
- return $this->success('操作成功', $data);
- }
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- $res = CloudStockGoods::setData($params);
- if ($res === false) return $this->error(CloudStockAgent::getStaticError());
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render('edit');
- }
- protected function formatDataIndex($level_list,$agent_price_arr,$over_level_list_arr,$over_level_list_arr_2,$fill_level_list_arr,$equal_level_list_rows){
- $agent_price = $over_level_list = $over_level_list_2 = $fill_level_list = $equal_level_list = [];
- foreach ($level_list as $val) {
- $row = [
- 'name' => $val['name'],
- 'level' => $val['level'],
- 'is_use' => 1,
- ];
- $row['price'] = $agent_price_arr[$val['level']]['price'] ?? 0;
- $row['price_list'] = $agent_price_arr[$val['level']]['price_list'] ?? [];
- $agent_price[] = $row;
- unset($row['price_list']);
- $row['price'] = $over_level_list_arr[$val['level']]['price'] ?? 0;
- $over_level_list[] = $row;
- $row['price'] = $over_level_list_arr_2[$val['level']]['price'] ?? 0;
- $over_level_list_2[] = $row;
- $row['price'] = $fill_level_list_arr[$val['level']]['price'] ?? 0;
- $fill_level_list[] = $row;
- $equal_key = 'a_equal_level_one';
- $row = [
- 'level_name' => $val['name'] . '平一级',
- 'level' => $val['level'],
- 'config_key' => $equal_key,
- 'config_value' => $equal_level_list_rows[$val['level'] . $equal_key]['config_value'] ?? 0,
- 'mall_id' => $val['mall_id'],
- 'is_use' => 1,
- ];
- $equal_level_list[] = $row;
- $equal_key = 'a_equal_level_two';
- $row = [
- 'level_name' => $val['name'] . '平二级',
- 'level' => $val['level'],
- 'config_key' => $equal_key,
- 'config_value' => $equal_level_list_rows[$val['level'] . $equal_key]['config_value'] ?? 0,
- 'mall_id' => $val['mall_id'],
- 'is_use' => 1,
- ];
- $equal_level_list[] = $row;
- }
- return [$agent_price,$over_level_list,$over_level_list_2,$fill_level_list,$equal_level_list];
- }
- protected function formatData($level_list, $agent_price_arr, $over_level_list_arr, $over_level_list_arr_2, $fill_level_list_arr
- , $equal_level_list_rows, $monthly_purchase_arr, $additional_purchase_restrictions_arr, $price_difference_arr)
- {
- $agent_price = $over_level_list = $over_level_list_2 = $fill_level_list = $equal_level_list = $monthly_purchase = $additional_purchase_restrictions= [];
- $price_difference = [];
- foreach ($level_list as $val) {
- $row = [
- 'name' => $val['name'],
- 'level' => $val['level'],
- 'is_use' => 1,
- ];
- $row['price'] = $agent_price_arr[$val['level']]['price'] ?? 0;
- $row['price_list'] = $agent_price_arr[$val['level']]['price_list'] ?? [];
- $agent_price[] = $row;
- unset($row['price_list']);
- $row['price'] = $over_level_list_arr[$val['level']]['price'] ?? 0;
- $over_level_list[] = $row;
- $row['price'] = $over_level_list_arr_2[$val['level']]['price'] ?? 0;
- $over_level_list_2[] = $row;
- $row['price'] = $fill_level_list_arr[$val['level']]['price'] ?? 0;
- $fill_level_list[] = $row;
- $equal_key = 'a_equal_level_one';
- $row = [
- 'level_name' => $val['name'] . '平一级',
- 'level' => $val['level'],
- 'config_key' => $equal_key,
- 'config_value' => $equal_level_list_rows[$val['level'] . $equal_key]['config_value'] ?? 0,
- 'mall_id' => $val['mall_id'],
- 'is_use' => 1,
- ];
- $equal_level_list[] = $row;
- $equal_key = 'a_equal_level_two';
- $row = [
- 'level_name' => $val['name'] . '平二级',
- 'level' => $val['level'],
- 'config_key' => $equal_key,
- 'config_value' => $equal_level_list_rows[$val['level'] . $equal_key]['config_value'] ?? 0,
- 'mall_id' => $val['mall_id'],
- 'is_use' => 1,
- ];
- $equal_level_list[] = $row;
- $row = [
- 'name' => $val['name'],
- 'level' => $val['level'],
- 'is_use' => 1,
- ];
- $row['nums'] = $monthly_purchase_arr[$val['level']]['nums'] ?? 0;
- $monthly_purchase[] = $row;
- $row = [
- 'name' => $val['name'],
- 'level' => $val['level'],
- 'is_use' => 1,
- ];
- $row['nums'] = $additional_purchase_restrictions_arr[$val['level']]['nums'] ?? 0;
- $additional_purchase_restrictions[] = $row;
- $price_difference[] = [
- 'name' => $val['name'],
- 'level' => $val['level'],
- 'is_use' => 1,
- 'price' => $price_difference_arr[$val['level']]['price'] ?? 0
- ];
- }
- return [$agent_price, $over_level_list, $over_level_list_2, $fill_level_list, $equal_level_list, $monthly_purchase, $additional_purchase_restrictions, $price_difference];
- }
- //不用验证权限
- public function actionGetSingleGoodsList()
- {
- $params = Yii::$app->request->get();
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'is_attr', 0];
- $where[] = ['=', 'is_on_sale', 1];
- $where[] = ['>', 'stock', 0];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- if(isset($params['keyword']) && $params['keyword'] !== '') {
- $keyword = trim($params['keyword']);
- $goodsNameWhere = ['like','goods_name',$keyword,false];
- if (is_numeric($keyword)) {
- $where[] = ['or', $goodsNameWhere, ['=', 'id', $keyword]];
- } else {
- $where[] = $goodsNameWhere;
- }
- }
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $params['select'] = 'id,goods_name,cover_pic,price';
- $page_data = Goods::listPage($params, false);
- return $this->success('操作成功', $page_data);
- }
- public function actionPatch()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = $request->post();
- CloudStockGoods::patch($params);
- return $this->success('操作成功');
- }
- }
- }
- public function actionDelete(){
- $post = Yii::$app->request->post();
- CloudStockGoods::updateAll(['status'=>StatusEnum::DELETE],['id'=>$post['id'],'mall_id'=>$this->mall_id]);
- $goods = CloudStockGoods::findOne(['id'=>$post['id']]);
- if(!empty($goods)){
- $data = ['goods_id'=>$goods['goods_id'],'mall_id'=>$this->mall_id];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, CloudStockAgentGuota::class, 'computeByGoodsDel');
- }
- return $this->success('操作成功');
- }
- public function actionGetCloudStockGoodsList(){
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $params = [
- 'where' => $where, 'order' => 'id desc','select'=>'id,goods_id'
- ];
- $page_data = CloudStockGoods::listPage($params, false,true);
- if(!empty($page_data['list'])){
- $goods_id_arr = array_column($page_data['list'],'goods_id');
- $goods_arr = Goods::lists(['where'=>[['id'=>$goods_id_arr]],'index'=>'id']);
- foreach ($page_data['list'] as &$item){
- $item['goods_name'] = $item['cover_pic'] = '';
- if(!empty($goods_arr[$item['goods_id']])){
- $item['goods_name'] = $goods_arr[$item['goods_id']]['goods_name'];
- $item['cover_pic'] = $goods_arr[$item['goods_id']]['cover_pic'];
- $item['cost_price'] = $goods_arr[$item['goods_id']]['cost_price'];
- $item['stock'] = $goods_arr[$item['goods_id']]['stock'];
- $item['id'] = $item['goods_id'];
- }
- }
- }
- return $this->success('操作成功', $page_data);
- }
- /**
- * 获取优惠券的设置
- * @return mixed|void
- */
- public function actionGetCouponSetting()
- {
- $params = Yii::$app->request->post();
- $data = [];
- $goods = CloudStockGoods::findOne(['goods_id'=>$params['goods_id'],'status' => StatusEnum::ENABLED]);
- // 获取配置
- $result = CouponSetting::find()->where([ 'mall_id' => $this->mall_id,'goods_id' => $params['goods_id']])->indexBy('level')->asArray()->all();
- $coupon_id_arr = [-1];
- if(empty($result) == false){
- foreach ($result as $key=>$item) {
- $coupon_setting = $item['coupon_setting']?json_decode($item['coupon_setting'],true):[];
- if(empty($coupon_setting) == false){
- $coupon_ids = array_column($coupon_setting,'coupon_id');
- $coupon_id_arr = array_merge($coupon_id_arr,$coupon_ids);
- }
- $result[$key]['coupon_setting'] = $coupon_setting;
- }
- }
- $params = [];
- $params['where'][] = ['mall_id' => $this->mall_id];
- $params['where'][] = ['status' => [StatusEnum::ENABLED]];
- $params['where'][] = ['in','id' ,$coupon_id_arr];
- $params['order'] = 'sort asc,id desc';
- $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
- $list = AddonsCoupon::lists($params);
- $list = empty($list)?[]:array_column($list,null,'id');
- $level_list = CloudStockLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]],'order'=>'level asc']);
- // 重新组合数组
- $data['is_buhuo'] = $goods['is_buhuo'];
- $data['is_peihuo'] = $goods['is_peihuo'];
- $data['coupon_data'] = [];
- foreach ($level_list as $level){
- $item = [];
- $item['level'] = $level['level'];
- $item['level_name'] = $level['name'];
- if(isset($result[$level['level']])){
- $item['num'] = $result[$level['level']]['num'];
- $coupon_setting = $result[$level['level']]['coupon_setting'];
- if(empty($coupon_setting) == false && empty($list) == false){
- foreach ($coupon_setting as $key=>$coupon){
- if(isset($list[$coupon['coupon_id']])){
- $list_item = $list[$coupon['coupon_id']];
- $coupon_setting[$key] = [
- 'discount_method' => $list_item['type'] == 1 ? $list_item['discount'] . '折' : $list_item['sub_price'] . '元',
- 'name' => $list_item['name'],
- 'num' => $coupon['num'],
- 'relevant_id' => $coupon['coupon_id'],
- ];
- }else{
- unset($coupon_setting[$key]);
- }
- }
- }
- $item['multiple_selection'] = $coupon_setting;
- }else{
- $item['num'] = 0;
- $item['multiple_selection'] = [];
- }
- $data['coupon_data'][] = $item;
- }
- return $this->success('操作成功', $data);
- }
- /**
- * 设置优惠券设配置
- * @return mixed|void
- */
- public function actionSetSetting()
- {
- $params = Yii::$app->request->post();
- if($params['goods_id'] <= 0){
- return $this->error('缺少商品参数.');
- }
- $data = [];
- $goods = CloudStockGoods::findOne(['goods_id'=>$params['goods_id'],'status' => StatusEnum::ENABLED]);
- $t = Yii::$app->db->beginTransaction();
- $time = time();
- $goods->is_buhuo = $params['is_buhuo'];
- $goods->is_peihuo = $params['is_peihuo'];
- $goods->updated_at = $time;
- $res = $goods->save();
- if(!$res) {
- $t->rollBack();
- return $this->error('数据保存失败,请稍后再试.');
- }
- $res = CouponSetting::setData(['goods_id'=>$params['goods_id'],'mall_id'=>$this->mall_id],$params['coupon_data']);
- if(!$res){
- $t->rollBack();
- return $this->error(CouponSetting::getStaticError());
- }
- $t->commit();
- return $this->success('操作成功', $data);
- }
- }
|