|
- <?php
-
- namespace backend\modules\zzcs\logic;
-
- /**
- * Created by PhpStorm.
- * User: 501810442
- * Date: 2017/7/20
- * Time: 10:51
- */
-
- use backend\modules\zzcs\models\BaseUser;
- use backend\modules\zzcs\models\OperaTailorProduct;
- use backend\modules\zzcs\models\OperaTailorProductStation;
- use Yii;
- use yii\db\Exception;
-
- /**
- * Class OperaTailorBase处理共通数据
- */
- interface OperaTailorInterface
- {
- function mainProcess();
-
- function saveOperaTailorProduct(OperaTailorProduct $product);
-
- function saveOperaTailorProductStation(OperaTailorProductStation $station, $info);
- }
-
- abstract class OperaTailorBase implements OperaTailorInterface
- {
- protected $time;
- protected $user_id;
- protected $main_corp_id;
- protected $cancel_flag;
-
- function __construct()
- {
- //当前用户所属运营主体
- $base_user = new BaseUser();
- $user_main_corp = $base_user->getMainCorp();
-
- $this->time = date('Y-m-d H:i:s');
- // $this->user_id = Yii::$app->request->cookies->getValue('user_id');
- $this->user_id = 1;
- $this->main_corp_id = $user_main_corp;
- $this->cancel_flag = 0;
- }
-
- abstract function saveOperaTailorProduct(OperaTailorProduct $product);
-
- abstract function saveOperaTailorProductStation(OperaTailorProductStation $station, $info);
- }
-
- /**
- * Class OperaTailor
- * 处理 OperaTailorProduct 和OperaTailorProductStation
- */
- class OperaTailor extends OperaTailorBase implements OperaTailorInterface
- {
- /**
- * @var //opera_tailor_product
- */
- private $id;
- private $tailor_product_name;
- private $tailor_product_code;
- private $supplier_id;
- private $prod_type;
- private $is_onsale = 1;
- private $product_price;
- private $cost_price;
- private $memo;
- /**
- * @var //opera_tailor_product_station
- */
- private $info_arr;
- private $seq_id;
- private $res_id;
- private $area_id;
- private $leave_time;
- private $inout_type;
- private $product_id;
-
- function __construct($product, $product_station)
- {
- $fin_station_arr = [];
- if (isset($product_station['info'])) {
- $product_station_arr = explode('||', $product_station['info']);
- foreach ($product_station_arr as $k => $v) {
- $fin_station_arr[$k][] = explode('|', $v);
- }
- }
- $this->tailor_product_name = $product['tailor_product_name'];
- $this->tailor_product_code = $product['tailor_product_code'];
- $this->supplier_id = $product['supplier_id'];
- $this->prod_type = $product['prod_type'];
- $this->product_price = $product['product_price'];
- $this->cost_price = $product['cost_price'];
- $this->memo = $product['memo'];
- $this->id = isset($product['id']) ? $product['id'] : 0;
-
- $this->info_arr = $fin_station_arr;
- return parent::__construct();
- }
-
- /**
- * Function Description:保存动态产品主流程
- * Function Name: mainProcess
- * @param OperaTailorProduct $product
- *
- *
- * @author LUOCJ
- */
- function mainProcess()
- {
- $transaction = Yii::$app->db->beginTransaction();
- try {
-
- if ($this->id == 0) {
- $product = new OperaTailorProduct();
- } else {
- $product = OperaTailorProduct::findOne(['id' => $this->id]);
- }
- //操作OperaTailorProduct表
- $model_1 = $this->saveOperaTailorProduct($product);
- if (!$model_1) {
- throw new Exception('model_1 有误 请检查', 101);
- }
- //操作OperaTailorProductStation表
- $model_2 = true;
- //只要更新产品之前的数据全部置cancel_flag;
- if ($this->id != 0)
- OperaTailorProductStation::updateAll(['cancel_flag' => 1], "product_id = {$this->id}");
- foreach ($this->info_arr as $info) {
- if ($model_2) {
- $station = new OperaTailorProductStation();
- $model_2 = $this->saveOperaTailorProductStation($station, $info[0]);
- }
- }
- if (!$model_2) {
- throw new Exception('model_2 有误 请检查', 102);
- }
- //提交
- $transaction->commit();
- $json['code'] = 0;
- $json['info'] = '保存成功';
- } catch (Exception $exception) {
- # 回滚事务
- $transaction->rollback();
- $json['code'] = 1;
- $json['info'] = $exception->getMessage();
- }
- return $json;
- }
-
- /**
- * Function Description:处理OperaTailorProduct
- * Function Name: saveOperaTailorProduct
- *
- *
- * @author LUOCJ
- */
- function saveOperaTailorProduct(OperaTailorProduct $product)
- {
- $flag = true;
- $product->tailor_product_name = $this->tailor_product_name;
- $product->tailor_product_code = $this->tailor_product_code;
- $product->supplier_id = $this->supplier_id;
- $product->prod_type = $this->prod_type;
- $product->is_onsale = $this->is_onsale;
- $product->product_price = $this->product_price;
- $product->cost_price = $this->cost_price;
- $product->memo = $this->memo;
-
- $product->cancel_flag = $this->cancel_flag;
- $product->create_time = $product->update_time = $this->time;
- $product->create_user_id = $product->update_user_id = $this->user_id;
- $product->main_corp_id = $this->main_corp_id;
- if (!$product->save()) {
- $flag = false;
- }
- $this->product_id = $product->id;
- return $flag;
- }
-
- /**
- * Function Description:处理OperaTailorProductStation
- * Function Name: saveOperaTailorProductStation
- *
- *
- * @author LUOCJ
- */
- function saveOperaTailorProductStation(OperaTailorProductStation $station, $info)
- {
- $flag = true;
- $station->seq_id = (int)$info[0];
- $station->res_id = $info[1];
- $station->area_id = $info[2];
- $station->leave_time = $info[3];
- $station->inout_type = $info[4];
- $station->product_id = $this->product_id;
-
- $station->cancel_flag = $this->cancel_flag;
- $station->create_time = $station->update_time = $this->time;
- $station->create_user_id = $station->update_user_id = $this->user_id;
- $station->main_corp_id = $this->main_corp_id;
- if (!$station->save()) {
- $flag = false;
- }
- return $flag;
- }
-
- }
|