FormDataForm.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\SaasUser;
  9. use app\models\FormData;
  10. use app\models\Goods;
  11. use yii\base\Model;
  12. class FormDataForm extends Model
  13. {
  14. public $export;
  15. public $store_id;
  16. public $verify_code;
  17. public $id;
  18. public $goods_id;
  19. public $name; //名称
  20. public $gender;
  21. public $age;
  22. public $form;
  23. public $type;
  24. public $cat_id;
  25. public $user;
  26. public $user_id;
  27. public $worker_id;
  28. public $level;
  29. public $desc;
  30. public $tel;
  31. public $logo;
  32. public $status;
  33. public $open_status;
  34. public $reason;
  35. public $area;
  36. public $lat;
  37. public $lng;
  38. public $province_id;
  39. public $city_id;
  40. public $district_id;
  41. public $address;
  42. public $nickname;
  43. public $order_id;
  44. public $goods_name;
  45. public $score;
  46. public $begin;
  47. public $end;
  48. public $key_word;
  49. public $is_hide;
  50. public $setting;
  51. public $pic_url;
  52. public function rules()
  53. {
  54. return [
  55. [['id', 'user_id', 'level', 'type', 'status', 'province_id', 'city_id', 'district_id', 'open_status', 'gender', 'age'], 'integer'],
  56. [['desc', 'name', 'tel', 'logo', 'reason', 'area', 'lat', 'lng', 'address', 'nickname', 'verify_code', 'cat_id'], 'string'],
  57. [['user', 'form', 'order_id', 'worker_id', 'goods_name', 'score', 'begin', 'end', 'key_word', 'gender', 'is_hide', 'setting', 'pic_url', 'export'], 'safe'],
  58. ];
  59. }
  60. public function list() {
  61. try {
  62. $store_id = $this->store_id;
  63. $status = $this->status;
  64. $goods_name = $this->goods_name;
  65. $goods_id = $this->goods_id;
  66. $nickname = $this->nickname;
  67. $tel = $this->tel;
  68. $query = FormData::find()->alias('f')->where(['f.store_id' => $store_id, 'f.is_delete' => 0])
  69. ->leftJoin(['g' => Goods::tableName()], 'f.goods_id = g.id')
  70. ->leftJoin(['su' => SaasUser::tableName()], 'f.saas_user_id = su.id');
  71. $query->andWhere(['>', 'f.goods_id', 0]);
  72. if (!is_null($status) && $status >= 0) {
  73. $query->andWhere(['f.status' => $status]);
  74. }
  75. if (is_null($status)) {
  76. $query->andWhere(['f.status' => FormData::STATUS_VALID]);
  77. }
  78. if ($goods_id) {
  79. if(is_array($goods_id)){
  80. $goods_id = explode(',', $goods_id);
  81. }
  82. $query->andWhere(['f.goods_id' => $goods_id]);
  83. }
  84. if ($goods_name) {
  85. $query->andWhere(['LIKE', 'g.name', $goods_name]);
  86. }
  87. if ($nickname) {
  88. $query->andWhere(['LIKE', 'su.name', $nickname]);
  89. }
  90. if ($tel) {
  91. $query->andWhere(['LIKE', 'su.mobile', $tel]);
  92. }
  93. if($this->begin){
  94. $query->andWhere(['>=', 'f.created_at', strtotime($this->begin)]);
  95. }
  96. if($this->end){
  97. $query->andWhere(['<=', 'f.created_at', strtotime($this->end)]);
  98. }
  99. $query->select('f.*, su.name nickname, su.avatar, su.mobile, g.name goods_name, g.cover_pic goods_cover_pic')
  100. ->orderBy('f.id desc');
  101. $list = pagination_make($query);
  102. foreach ($list['list'] as &$item) {
  103. $item['form'] = json_decode($item['form'], true);
  104. $item['status'] = (int)$item['status'];
  105. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  106. }
  107. if($this->export){
  108. return $this->export($list['list']);
  109. }
  110. return [
  111. 'code' => 0,
  112. 'msg' => 'success',
  113. 'data' => $list,
  114. 's' => $query->createCommand()->getRawSql(),
  115. ];
  116. } catch (\Exception $e) {
  117. return [
  118. 'code' => 1,
  119. 'msg' => $e->getMessage()
  120. ];
  121. }
  122. }
  123. private function export($list) {
  124. $rows = [[
  125. 'ID',
  126. '会员信息',
  127. '手机号',
  128. '商品',
  129. '时间',
  130. '表单',
  131. '表单值',
  132. ]];
  133. foreach($list as $item){
  134. if(empty($item['form']) || empty($item['form']['list'])){
  135. continue;
  136. }
  137. foreach ($item['form']['list'] as $i => $fitem) {
  138. $fitemVal = $fitem['default'];
  139. if($fitem['type'] == 'uploadImg'){
  140. $fitemVal = '<table><img src="'. $fitemVal .'"></table>';
  141. }
  142. if(is_array($fitemVal)){
  143. $fitemVal = implode(',', $fitemVal);
  144. }
  145. if($i == 0){
  146. $r = [
  147. $item['id'],
  148. $item['nickname'],
  149. $item['mobile'],
  150. $item['goods_name'],
  151. $item['created_at'],
  152. $item['form']['name'],
  153. '',
  154. ];
  155. $rows[] = $r;
  156. }
  157. $r = [
  158. '',
  159. '',
  160. '',
  161. '',
  162. '',
  163. $fitem['name'],
  164. $fitemVal,
  165. ];
  166. $rows[] = $r;
  167. }
  168. }
  169. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  170. ->addRows($rows)->toBrowser();
  171. }
  172. public function del() {
  173. $t = \Yii::$app->db->beginTransaction();
  174. try {
  175. $id = is_array($this->id) ? $this->id : explode(',', $this->id);
  176. FormData::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'id' => $id, 'is_delete' => 0]);
  177. $t->commit();
  178. return [
  179. 'code' => 0,
  180. 'msg' => '操作成功'
  181. ];
  182. } catch (\Exception $e) {
  183. $t->rollBack();
  184. return [
  185. 'code' => 1,
  186. 'msg' => $e->getMessage()
  187. ];
  188. }
  189. }
  190. }