| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\SaasUser;
- use app\models\FormData;
- use app\models\Goods;
- use yii\base\Model;
- class FormDataForm extends Model
- {
- public $export;
- public $store_id;
-
- public $verify_code;
- public $id;
- public $goods_id;
- public $name; //名称
- public $gender;
- public $age;
- public $form;
- public $type;
- public $cat_id;
- public $user;
- public $user_id;
- public $worker_id;
- public $level;
- public $desc;
- public $tel;
- public $logo;
- public $status;
- public $open_status;
- public $reason;
- public $area;
- public $lat;
- public $lng;
- public $province_id;
- public $city_id;
- public $district_id;
- public $address;
- public $nickname;
- public $order_id;
- public $goods_name;
- public $score;
- public $begin;
- public $end;
- public $key_word;
- public $is_hide;
- public $setting;
- public $pic_url;
- public function rules()
- {
- return [
- [['id', 'user_id', 'level', 'type', 'status', 'province_id', 'city_id', 'district_id', 'open_status', 'gender', 'age'], 'integer'],
- [['desc', 'name', 'tel', 'logo', 'reason', 'area', 'lat', 'lng', 'address', 'nickname', 'verify_code', 'cat_id'], 'string'],
- [['user', 'form', 'order_id', 'worker_id', 'goods_name', 'score', 'begin', 'end', 'key_word', 'gender', 'is_hide', 'setting', 'pic_url', 'export'], 'safe'],
- ];
- }
- public function list() {
- try {
- $store_id = $this->store_id;
- $status = $this->status;
- $goods_name = $this->goods_name;
- $goods_id = $this->goods_id;
- $nickname = $this->nickname;
- $tel = $this->tel;
- $query = FormData::find()->alias('f')->where(['f.store_id' => $store_id, 'f.is_delete' => 0])
- ->leftJoin(['g' => Goods::tableName()], 'f.goods_id = g.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'f.saas_user_id = su.id');
- $query->andWhere(['>', 'f.goods_id', 0]);
- if (!is_null($status) && $status >= 0) {
- $query->andWhere(['f.status' => $status]);
- }
- if (is_null($status)) {
- $query->andWhere(['f.status' => FormData::STATUS_VALID]);
- }
- if ($goods_id) {
- if(is_array($goods_id)){
- $goods_id = explode(',', $goods_id);
- }
- $query->andWhere(['f.goods_id' => $goods_id]);
- }
- if ($goods_name) {
- $query->andWhere(['LIKE', 'g.name', $goods_name]);
- }
- if ($nickname) {
- $query->andWhere(['LIKE', 'su.name', $nickname]);
- }
- if ($tel) {
- $query->andWhere(['LIKE', 'su.mobile', $tel]);
- }
- if($this->begin){
- $query->andWhere(['>=', 'f.created_at', strtotime($this->begin)]);
- }
- if($this->end){
- $query->andWhere(['<=', 'f.created_at', strtotime($this->end)]);
- }
- $query->select('f.*, su.name nickname, su.avatar, su.mobile, g.name goods_name, g.cover_pic goods_cover_pic')
- ->orderBy('f.id desc');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['form'] = json_decode($item['form'], true);
- $item['status'] = (int)$item['status'];
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- if($this->export){
- return $this->export($list['list']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- 's' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function export($list) {
- $rows = [[
- 'ID',
- '会员信息',
- '手机号',
- '商品',
- '时间',
- '表单',
- '表单值',
- ]];
- foreach($list as $item){
- if(empty($item['form']) || empty($item['form']['list'])){
- continue;
- }
- foreach ($item['form']['list'] as $i => $fitem) {
- $fitemVal = $fitem['default'];
- if($fitem['type'] == 'uploadImg'){
- $fitemVal = '<table><img src="'. $fitemVal .'"></table>';
- }
- if(is_array($fitemVal)){
- $fitemVal = implode(',', $fitemVal);
- }
- if($i == 0){
- $r = [
- $item['id'],
- $item['nickname'],
- $item['mobile'],
- $item['goods_name'],
- $item['created_at'],
- $item['form']['name'],
- '',
- ];
- $rows[] = $r;
- }
- $r = [
- '',
- '',
- '',
- '',
- '',
- $fitem['name'],
- $fitemVal,
- ];
- $rows[] = $r;
- }
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- public function del() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $id = is_array($this->id) ? $this->id : explode(',', $this->id);
- FormData::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'id' => $id, 'is_delete' => 0]);
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|