You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

213 lines
6.4 KiB

  1. <?php
  2. namespace backend\modules\zzcs\logic;
  3. /**
  4. * Created by PhpStorm.
  5. * User: 501810442
  6. * Date: 2017/7/20
  7. * Time: 10:51
  8. */
  9. use backend\modules\zzcs\models\BaseUser;
  10. use backend\modules\zzcs\models\OperaTailorProduct;
  11. use backend\modules\zzcs\models\OperaTailorProductStation;
  12. use Yii;
  13. use yii\db\Exception;
  14. /**
  15. * Class OperaTailorBase处理共通数据
  16. */
  17. interface OperaTailorInterface
  18. {
  19. function mainProcess();
  20. function saveOperaTailorProduct(OperaTailorProduct $product);
  21. function saveOperaTailorProductStation(OperaTailorProductStation $station, $info);
  22. }
  23. abstract class OperaTailorBase implements OperaTailorInterface
  24. {
  25. protected $time;
  26. protected $user_id;
  27. protected $main_corp_id;
  28. protected $cancel_flag;
  29. function __construct()
  30. {
  31. //当前用户所属运营主体
  32. $base_user = new BaseUser();
  33. $user_main_corp = $base_user->getMainCorp();
  34. $this->time = date('Y-m-d H:i:s');
  35. // $this->user_id = Yii::$app->request->cookies->getValue('user_id');
  36. $this->user_id = 1;
  37. $this->main_corp_id = $user_main_corp;
  38. $this->cancel_flag = 0;
  39. }
  40. abstract function saveOperaTailorProduct(OperaTailorProduct $product);
  41. abstract function saveOperaTailorProductStation(OperaTailorProductStation $station, $info);
  42. }
  43. /**
  44. * Class OperaTailor
  45. * 处理 OperaTailorProduct 和OperaTailorProductStation
  46. */
  47. class OperaTailor extends OperaTailorBase implements OperaTailorInterface
  48. {
  49. /**
  50. * @var //opera_tailor_product
  51. */
  52. private $id;
  53. private $tailor_product_name;
  54. private $tailor_product_code;
  55. private $supplier_id;
  56. private $prod_type;
  57. private $is_onsale = 1;
  58. private $product_price;
  59. private $cost_price;
  60. private $memo;
  61. /**
  62. * @var //opera_tailor_product_station
  63. */
  64. private $info_arr;
  65. private $seq_id;
  66. private $res_id;
  67. private $area_id;
  68. private $leave_time;
  69. private $inout_type;
  70. private $product_id;
  71. function __construct($product, $product_station)
  72. {
  73. $fin_station_arr = [];
  74. if (isset($product_station['info'])) {
  75. $product_station_arr = explode('||', $product_station['info']);
  76. foreach ($product_station_arr as $k => $v) {
  77. $fin_station_arr[$k][] = explode('|', $v);
  78. }
  79. }
  80. $this->tailor_product_name = $product['tailor_product_name'];
  81. $this->tailor_product_code = $product['tailor_product_code'];
  82. $this->supplier_id = $product['supplier_id'];
  83. $this->prod_type = $product['prod_type'];
  84. $this->product_price = $product['product_price'];
  85. $this->cost_price = $product['cost_price'];
  86. $this->memo = $product['memo'];
  87. $this->id = isset($product['id']) ? $product['id'] : 0;
  88. $this->info_arr = $fin_station_arr;
  89. return parent::__construct();
  90. }
  91. /**
  92. * Function Description:保存动态产品主流程
  93. * Function Name: mainProcess
  94. * @param OperaTailorProduct $product
  95. *
  96. *
  97. * @author LUOCJ
  98. */
  99. function mainProcess()
  100. {
  101. $transaction = Yii::$app->db->beginTransaction();
  102. try {
  103. if ($this->id == 0) {
  104. $product = new OperaTailorProduct();
  105. } else {
  106. $product = OperaTailorProduct::findOne(['id' => $this->id]);
  107. }
  108. //操作OperaTailorProduct表
  109. $model_1 = $this->saveOperaTailorProduct($product);
  110. if (!$model_1) {
  111. throw new Exception('model_1 有误 请检查', 101);
  112. }
  113. //操作OperaTailorProductStation表
  114. $model_2 = true;
  115. //只要更新产品之前的数据全部置cancel_flag;
  116. if ($this->id != 0)
  117. OperaTailorProductStation::updateAll(['cancel_flag' => 1], "product_id = {$this->id}");
  118. foreach ($this->info_arr as $info) {
  119. if ($model_2) {
  120. $station = new OperaTailorProductStation();
  121. $model_2 = $this->saveOperaTailorProductStation($station, $info[0]);
  122. }
  123. }
  124. if (!$model_2) {
  125. throw new Exception('model_2 有误 请检查', 102);
  126. }
  127. //提交
  128. $transaction->commit();
  129. $json['code'] = 0;
  130. $json['info'] = '保存成功';
  131. } catch (Exception $exception) {
  132. # 回滚事务
  133. $transaction->rollback();
  134. $json['code'] = 1;
  135. $json['info'] = $exception->getMessage();
  136. }
  137. return $json;
  138. }
  139. /**
  140. * Function Description:处理OperaTailorProduct
  141. * Function Name: saveOperaTailorProduct
  142. *
  143. *
  144. * @author LUOCJ
  145. */
  146. function saveOperaTailorProduct(OperaTailorProduct $product)
  147. {
  148. $flag = true;
  149. $product->tailor_product_name = $this->tailor_product_name;
  150. $product->tailor_product_code = $this->tailor_product_code;
  151. $product->supplier_id = $this->supplier_id;
  152. $product->prod_type = $this->prod_type;
  153. $product->is_onsale = $this->is_onsale;
  154. $product->product_price = $this->product_price;
  155. $product->cost_price = $this->cost_price;
  156. $product->memo = $this->memo;
  157. $product->cancel_flag = $this->cancel_flag;
  158. $product->create_time = $product->update_time = $this->time;
  159. $product->create_user_id = $product->update_user_id = $this->user_id;
  160. $product->main_corp_id = $this->main_corp_id;
  161. if (!$product->save()) {
  162. $flag = false;
  163. }
  164. $this->product_id = $product->id;
  165. return $flag;
  166. }
  167. /**
  168. * Function Description:处理OperaTailorProductStation
  169. * Function Name: saveOperaTailorProductStation
  170. *
  171. *
  172. * @author LUOCJ
  173. */
  174. function saveOperaTailorProductStation(OperaTailorProductStation $station, $info)
  175. {
  176. $flag = true;
  177. $station->seq_id = (int)$info[0];
  178. $station->res_id = $info[1];
  179. $station->area_id = $info[2];
  180. $station->leave_time = $info[3];
  181. $station->inout_type = $info[4];
  182. $station->product_id = $this->product_id;
  183. $station->cancel_flag = $this->cancel_flag;
  184. $station->create_time = $station->update_time = $this->time;
  185. $station->create_user_id = $station->update_user_id = $this->user_id;
  186. $station->main_corp_id = $this->main_corp_id;
  187. if (!$station->save()) {
  188. $flag = false;
  189. }
  190. return $flag;
  191. }
  192. }