NewDiyForm.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\NewDiyTemplate;
  9. use yii\base\Model;
  10. use yii\helpers\Json;
  11. use yii\data\Pagination;
  12. use app\models\Store;
  13. use app\models\Goods;
  14. use app\models\GoodsBook;
  15. use app\models\GoodsCat;
  16. use app\models\Cat;
  17. use app\models\WechatConfig;
  18. use app\models\Option;
  19. class NewDiyForm extends Model
  20. {
  21. private $alliance_store_id = 0; //联盟店铺id=0
  22. /**
  23. * @return array
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [[]]
  29. ];
  30. }
  31. public function attributeLabels()
  32. {
  33. return [
  34. ];
  35. }
  36. /**
  37. * 获取商品列表
  38. */
  39. public static function getGoods($reqeruy)
  40. {
  41. $goods_id = $reqeruy['goods_id'];
  42. if(is_string($goods_id) && $goods_id != ''){
  43. $goods_id = explode(',', $goods_id);
  44. }
  45. $goods_id = (array)$goods_id;
  46. $query = Goods::find()->alias('g')->leftJoin(['s' => Store::tableName()], 'g.store_id = s.id')
  47. ->where([
  48. 'g.is_delete' => 0,
  49. 'g.status' => 1
  50. ])->andWhere(['not like', 'g.name', '当面付']);
  51. if ($reqeruy['store_id'] > -1) {
  52. $query->andWhere(['g.store_id' => $reqeruy['store_id'], 's.is_delete' => 0]);
  53. }
  54. if (isset($reqeruy['product_type']) && $reqeruy['product_type']) {
  55. $query->andWhere(['g.product_type' => $reqeruy['product_type']]);
  56. }
  57. if (!empty($goods_id)) {
  58. $query->andWhere([
  59. 'in', 'g.id', $goods_id
  60. ]);
  61. $query->orderBy([new \yii\db\Expression('FIELD(g.id, :ids)')]);
  62. $query->addParams([':ids' => implode(',', array_map(function($id) {
  63. return (int)$id;
  64. }, $goods_id))]);
  65. } else {
  66. if ($reqeruy['cat_id']) {
  67. if ($reqeruy['product_type'] == 2) {
  68. $query->andWhere(['g.id' => \app\models\BookingGoodsExt::find()->where(['cat_id' => $reqeruy['cat_id']])->select('goods_id')]);
  69. }else{
  70. $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id')
  71. ->andWhere(['gc.cat_id' => Cat::getCatId($reqeruy['cat_id'])])->groupBy('gc.goods_id');
  72. }
  73. }
  74. $count = $query->count();
  75. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $reqeruy['limit'], 'page' => $reqeruy['page'] - 1]);
  76. $query->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
  77. }
  78. //$query->leftJoin(['s' => Store::tableName()], 's.id=g.store_id')->andWhere(['s.is_delete'=>0]);
  79. //$select = ['g.goods_num', 'g.status', 'g.product_type', 'g.virtual_sales', 'g.name', 'g.id', 'g.service', 'g.attr', 'g.cover_pic', 'g.attr', 'g.use_attr', 'g.price', 'g.original_price','g.store_id','s.name store_name','s.business_model'];
  80. $select = ['g.goods_num', 'g.status', 'g.product_type', 'g.virtual_sales', 'g.name', 'g.id', 'g.service', 'g.attr', 'g.cover_pic', 'g.attr', 'g.use_attr', 'g.price', 'g.original_price','g.store_id', 'g.integral_price', 'g.goods_take_price', 'g.goods_send_profit'];
  81. $goods_list = $query->groupBy('g.id')->select($select)->asArray()->all();
  82. foreach ($goods_list as $key => $value) {
  83. $goods = Goods::findOne($value['id']);
  84. $goods_list[$key]['virtual_sales'] = $goods->getSalesVolume() + $value['virtual_sales'];
  85. if ($value['product_type'] == 1) {
  86. $goods_list[$key]['date_book'] = Json::decode(GoodsBook::findOne(['goods_id' => $value['id']])->date_book);
  87. }
  88. if ($value['product_type'] == 2) {
  89. $goods_list[$key]['service_book'] = Json::decode(GoodsBook::findOne(['goods_id' => $value['id']])->service_book);
  90. }
  91. $goods_list[$key]['storeInfo'] = [
  92. 'id' => '0',
  93. 'name' => Option::get('name', 0, 'saas', '平台运营')['value'],
  94. 'logo' => Option::get('logo', 0, 'saas', '')['value'],
  95. 'coordinate' => '',
  96. 'created_at' => '',
  97. 'category_id' => '',
  98. 'tags' => '',
  99. 'sales' => '',
  100. 'rank' => '',
  101. 'per_spend' => '',
  102. 'business_model' => '',
  103. 'address' => '',
  104. 's_time' => '00:00',
  105. 'e_time' => '23:59'
  106. ];
  107. $goods_list[$key]['take_price'] = 0;
  108. $goods_list[$key]['send_price'] = 0;
  109. if ($value['goods_take_price'] > 0) {
  110. $goods_list[$key]['take_price'] = \round($value['price'] * $value['goods_take_price'] / 100, 2);
  111. }
  112. if ($value['goods_send_profit'] > 0) {
  113. $goods_list[$key]['send_price'] = \round($value['price'] * $value['goods_send_profit'] / 100, 2);
  114. }
  115. if($value['store_id'] > 0){
  116. $goods_list[$key]['storeInfo'] = Store::find()->where(['id'=>$value['store_id'],'is_delete'=>0])->select(['id', 'name', 'logo', 'coordinate', 'created_at', 'category_id', 'tags', 'sales', 'rank', 'per_spend','business_model','address', 's_time', 'e_time'])->asArray()->one();
  117. $wechatInfo = WechatConfig::find()->where(['store_id'=>$value['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
  118. if(isset($goods_list[$key]['storeInfo']['id']) && $goods_list[$key]['storeInfo']['id']>0){
  119. if($wechatInfo){
  120. $goods_list[$key]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
  121. $goods_list[$key]['storeInfo']['gh_wechat_app_id'] = $wechatInfo['gh_wechat_app_id'];
  122. }
  123. $alipayInfo = Option::get('alipay_config',$value['store_id'],'alipay');
  124. if($alipayInfo && $alipayInfo['value']){
  125. $alipayInfo = json_decode($alipayInfo['value'],true);
  126. $goods_list[$key]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
  127. }
  128. }
  129. }
  130. }
  131. return [
  132. 'code' => 0,
  133. 'data' => [
  134. 'row_count' => isset($count) ? $count: 0,
  135. 'page_count' => isset($pagination) ? $pagination->pageCount : 0,
  136. 'list' => $goods_list
  137. ]
  138. ];
  139. }
  140. /**
  141. * 新diy
  142. * @return array
  143. */
  144. public function saveNewTemplate()
  145. {
  146. $template = post_params('template');
  147. if ($template == '{}') {
  148. return [
  149. 'code' => 1,
  150. 'msg' => '保存模板数据为空'
  151. ];
  152. }
  153. $controls = post_params('controls');
  154. $id = post_params('id', 0);
  155. $name = post_params('name', "DIY");
  156. $type = post_params('type', 0);
  157. $form = NewDiyTemplate::findOne(['store_id' => $this->alliance_store_id, 'is_delete' => 0, 'id' => $id]) ?: new NewDiyTemplate();
  158. $form->store_id = $this->alliance_store_id;
  159. $form->name = $name;
  160. $form->template = $template;
  161. $form->is_delete = 0;
  162. $form->addtime = time();
  163. $form->type = $type;
  164. if ($form->save()) {
  165. // 保存控件
  166. if ($controls) {
  167. self::saveControls($controls);
  168. }
  169. return [
  170. 'code' => 0,
  171. 'msg' => '保存成功',
  172. 'data' => [
  173. 'template' => $form,
  174. 'controls' => NewDiyTemplate::find()
  175. ->where([
  176. 'store_id' => $this->alliance_store_id,
  177. 'is_delete' => 0,
  178. 'type' => 100
  179. ])->asArray()->one()['template']
  180. ]
  181. ];
  182. } else {
  183. return [
  184. 'code' => 1,
  185. 'msg' => $form->errors
  186. ];
  187. }
  188. }
  189. public function saveControls($controls)
  190. {
  191. // controls type=100
  192. $controlsDiy = NewDiyTemplate::findOne(['store_id' => $this->alliance_store_id, 'is_delete' => 0, 'type' => 100]) ?: new NewDiyTemplate();
  193. $controlsDiy->store_id = $this->alliance_store_id;
  194. $controlsDiy->name = 'controls';
  195. $controlsDiy->template = $controls;
  196. $controlsDiy->is_delete = 0;
  197. $controlsDiy->addtime = time();
  198. $controlsDiy->type = 100;
  199. $controlsDiy->save();
  200. }
  201. public function getNewTemplateList()
  202. {
  203. $query = NewDiyTemplate::find()
  204. ->where([
  205. 'store_id' => $this->alliance_store_id,
  206. 'is_delete' => 0,
  207. ]);
  208. $list = $query->andWhere(['!=', 'type', 100])->orderBy(['id' => SORT_ASC])->asArray()->all();
  209. return [
  210. 'code' => 0,
  211. 'msg' => '加载成功',
  212. 'data' => [
  213. 'list' => $list,
  214. 'controls' => NewDiyTemplate::find()
  215. ->where([
  216. 'store_id' => $this->alliance_store_id,
  217. 'is_delete' => 0,
  218. 'type' => 100
  219. ])->asArray()->one()['template']
  220. ]
  221. ];
  222. }
  223. }