SettingForm.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\modules\admin\models\superSales;
  3. use app\models\CashExt;
  4. use app\models\Cat;
  5. use app\models\Goods;
  6. use app\models\GoodsCat;
  7. use app\models\Option;
  8. use yii\base\Model;
  9. class SettingForm extends Model
  10. {
  11. public $store_id;
  12. public $super_sales_direct_profit;
  13. public $super_sales_group_profit;
  14. public $pay_type;
  15. public $bank;
  16. public $lg;
  17. public $remaining_sum;
  18. public $cash_service_charge;
  19. public $min_money;
  20. public $cash_max_single_day;
  21. public $cash_max_day;
  22. public $goods_ids;
  23. public $cash_price_amount;
  24. public $cash_price_integral;
  25. public $cash_price_balance;
  26. public $cash_price_type;
  27. public $super_sales_bg;
  28. //立即升级
  29. public $upgrade_direct_profit;
  30. public function rules()
  31. {
  32. return [
  33. [['store_id', 'pay_type', 'bank', 'remaining_sum', 'upgrade_direct_profit'], 'integer'],
  34. [['super_sales_direct_profit', 'super_sales_group_profit', 'cash_service_charge',
  35. 'min_money', 'cash_max_single_day', 'cash_max_day', 'cash_price_amount', 'cash_price_integral', 'cash_price_balance'], 'number'],
  36. [['goods_ids', 'cash_price_type', 'super_sales_bg'], 'string'],
  37. [['bank', 'remaining_sum', 'lg'], 'in', 'range' => [0, 1]],
  38. [['pay_type'], 'in', 'range' => [0, 1, 2, 3]]
  39. ];
  40. }
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'store_id' => 'Store Id',
  45. 'super_sales_direct_profit' => '直推佣金比例',
  46. 'super_sales_group_profit' => '成团佣金比例',
  47. 'super_sales_bg' => '超级卖货系统背景',
  48. 'pay_type' => '提现方式【0微信 1支付宝 2微信&支付宝】',
  49. 'bank' => '是否开启银行卡提现',
  50. 'lg' => '是否开启灵工提现',
  51. 'remaining_sum' => '是否开启余额提现',
  52. 'cash_service_charge' => '提现手续费',
  53. 'min_money' => '最少提现金额',
  54. 'cash_max_single_day' => '每日提现上限',
  55. 'cash_max_day' => '平台提现上限',
  56. 'goods_ids' => '商品ID',
  57. 'upgrade_direct_profit' => '是否立即升级'
  58. ];
  59. }
  60. public function settingIndex() {
  61. try {
  62. $store_id = $this->store_id;
  63. $data = [
  64. 'super_sales_direct_profit' => 0,
  65. 'super_sales_group_profit' => 0,
  66. 'pay_type' => '',
  67. 'bank' => 0,
  68. 'remaining_sum' => 0,
  69. 'lg' => 0,
  70. 'cash_service_charge' => 0,
  71. 'min_money' => 0,
  72. 'cash_max_single_day' => 0,
  73. 'cash_max_day' => 0,
  74. 'goods_ids' => '',
  75. 'cash_price_type' => '1',
  76. 'cash_price_amount' => 100,
  77. 'cash_price_integral' => 0,
  78. 'cash_price_balance' => 0,
  79. 'upgrade_direct_profit' => '',
  80. 'super_sales_bg' => ''
  81. ];
  82. $super_sales_setting = Option::get('super_sales_setting', $store_id, 'super_sales')['value'];
  83. $super_sales_setting = json_decode($super_sales_setting ?? '', true);
  84. if (empty($super_sales_setting)) {
  85. $super_sales_setting = $data;
  86. }
  87. $super_sales_setting['goods_info'] = [];
  88. $goods_ids = explode(',', $super_sales_setting['goods_ids'] ?? '');
  89. if (!empty($goods_ids)) {
  90. $goods = Goods::find()->where(['id' => $goods_ids, 'is_delete' => 0, 'store_id' => $store_id])
  91. ->select('id, name, cover_pic, price, goods_num')->asArray()->all();
  92. foreach ($goods as &$item) {
  93. $goods_cat = GoodsCat::find()->alias('gc')
  94. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  95. ->where([ 'gc.goods_id' => $item['id'], 'c.is_delete' => 0, 'gc.is_delete' => 0, 'c.store_id' => $store_id])
  96. ->select(['c.name'])
  97. ->column();
  98. $item['cat'] = implode(' ', $goods_cat);
  99. }
  100. $super_sales_setting['goods_info'] = $goods;
  101. }
  102. return [
  103. 'code' => 0,
  104. 'msg' => '获取成功',
  105. 'data' => $super_sales_setting
  106. ];
  107. } catch (\Exception $e) {
  108. return [
  109. 'code' => 1,
  110. 'msg' => $e->getMessage()
  111. ];
  112. }
  113. }
  114. public function settingEdit() {
  115. try {
  116. $store_id = $this->store_id;
  117. if (!$this->validate()) {
  118. throw new \Exception($this->getErrorSummary(false)[0]);
  119. }
  120. $data = [
  121. 'super_sales_direct_profit' => floatval($this->super_sales_direct_profit),
  122. 'super_sales_group_profit' => floatval($this->super_sales_group_profit),
  123. 'pay_type' => intval($this->pay_type),
  124. 'bank' => intval($this->bank),
  125. 'lg' => intval($this->lg),
  126. 'remaining_sum' => intval($this->remaining_sum),
  127. 'cash_service_charge' => floatval($this->cash_service_charge),
  128. 'min_money' => floatval($this->min_money),
  129. 'cash_max_single_day' => floatval($this->cash_max_single_day),
  130. 'cash_max_day' => floatval($this->cash_max_day),
  131. 'goods_ids' => $this->goods_ids,
  132. 'cash_price_type' => $this->cash_price_type ?? '1',
  133. 'cash_price_amount' => $this->cash_price_amount ?? 100,
  134. 'cash_price_integral' => $this->cash_price_integral ?? 0,
  135. 'cash_price_balance' => $this->cash_price_balance ?? 0,
  136. 'upgrade_direct_profit' => $this->upgrade_direct_profit ?? 0,
  137. 'super_sales_bg' => $this->super_sales_bg ?? ''
  138. ];
  139. // $total_profit = 0;
  140. // $cash_price_type = explode(',', $this->cash_price_type);
  141. // if (in_array(CashExt::CASH_PRICE_TYPE_AMOUNT, $cash_price_type)) {
  142. // $total_profit = bcadd($total_profit, $this->cash_price_amount, 2);
  143. // }
  144. // if (in_array(CashExt::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type)) {
  145. // $total_profit = bcadd($total_profit, $this->cash_price_integral, 2);
  146. // }
  147. // if (in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
  148. // $total_profit = bcadd($total_profit, $this->cash_price_balance, 2);
  149. // }
  150. // if ($total_profit != 100) {
  151. // return [
  152. // 'code' => 1,
  153. // 'msg' => '佣金/积分/余额比例设置错误'
  154. // ];
  155. // }
  156. $goods_ids = explode(',', $data['goods_ids'] ?? '');
  157. if (empty($goods_ids)) {
  158. throw new \Exception('请选择商品');
  159. }
  160. foreach ($goods_ids as $goods_id) {
  161. $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'store_id' => $store_id]);
  162. if (!$goods) {
  163. throw new \Exception('商品信息错误' . $goods_id);
  164. }
  165. }
  166. Option::set('super_sales_setting', json_encode($data), $store_id, 'super_sales');
  167. return [
  168. 'code' => 0,
  169. 'msg' => '保存成功'
  170. ];
  171. } catch (\Exception $e) {
  172. return [
  173. 'code' => 1,
  174. 'msg' => $e->getMessage()
  175. ];
  176. }
  177. }
  178. }