| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace addons\Dianqian\common\models;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\models\common\Download;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%dianqian_apply}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int|null $user_id 用户ID
- * @property string|null $name 签约人姓名
- * @property int|null $mobile 签约人手机号
- * @property string|null $reason 被拒理由
- * @property string|null $remark 备注
- * @property int|null $sign_status 签约状态[-1:申请被拒,1=签约中,2=签约成功]
- * @property int|null $status 状态
- * @property int $created_at
- * @property int $updated_at
- */
- class DianqianApply extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%dianqian_apply}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'mobile', 'sign_status', 'status', 'created_at', 'updated_at'], 'integer'],
- [['reason', 'remark'], 'string'],
- [['name'], 'string', 'max' => 100],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户ID',
- 'name' => '签约人姓名',
- 'mobile' => '签约人手机号',
- 'reason' => '被拒理由',
- 'remark' => '备注',
- 'sign_status' => '签约状态[-1:申请被拒,1=签约中,2=签约成功]',
- 'status' => '状态',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 关联用户
- * @Author: lun
- * @DateTime: 2023/5/12 0012 14:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url,parent_id');
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2023/5/12 0012 15:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function setData($params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) throw new \Exception('暂无此申请');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 申请数据提交
- * @Author: lun
- * @DateTime: 2023/5/12 0012 17:15
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function apply($params)
- {
- try {
- $model = self::findOne(['user_id' => $params['user_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- } else {
- if ($model->sign_status != -1) throw new \Exception('禁止重复提交!');
- }
- if ($model->sign_status == -1) $params['sign_status'] = 1;// 重新申请状态修改
- if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 导出字段
- * @Author: lun
- * @DateTime: 2023/5/13 0013 14:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param null $field
- * @return array|string[]
- *
- */
- public static function fieldsList($field = null){
- $fields = [
- 'user_id' => '用户ID',
- 'nickname' => '用户昵称',
- 'user_mobile' => '用户手机号',
- 'name' => '签约人姓名',
- 'mobile' => '签约人手机号',
- 'reason' => '被拒理由',
- 'sign_status' => '签约状态',
- 'remark' => '备注',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- const REFUSE = -1;
- const SIGNING = 1;
- const SIGN_SUCCESS = 2;
- /**
- * 获取签约类型
- * @Author: lun
- * @DateTime: 2023/5/13 0013 14:41
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return string[]
- */
- public static function signMap()
- {
- return [
- self::REFUSE => '拒绝',
- self::SIGNING => '待签约',
- self::SIGN_SUCCESS => '签约成功',
- ];
- }
- /**
- * 导出
- * @Author: lun
- * @DateTime: 2023/5/13 0013 14:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $data
- * @return bool|string
- */
- public static function exportHandle(array $data) {
- date_default_timezone_set('Asia/Shanghai');
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params,true);
- $filename = $download->name;
- $type = $download->type;
- $apply_model = self::find();
- $apply_model->where(['mall_id'=>$params['mall_id'], 'status' => StatusEnum::ENABLED]);
- // 根据条件查询组合
- if (!empty($params['user_id'])) $apply_model->andWhere(['=', 'user_id', $params['user_id']]);
- if (!empty($params['name'])) $apply_model->andWhere(['=', 'name', $params['name']]);
- if (!empty($params['mobile'])) $apply_model->andWhere(['=', 'mobile', $params['mobile']]);
- if (!empty($params['sign_status'])) $apply_model->andWhere(['=', 'sign_status', $params['sign_status']]);
- if (!empty($params['start'])) $apply_model->andWhere(['>=', 'created_at', strtotime($params['start'])]);
- if (!empty($params['end'])) $apply_model->andWhere(['<=', 'created_at', strtotime($params['end'])]);
- $apply_model->with(['user' => function($query) {
- $query->select('id,nickname,username,mobile');
- }]);
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- if (isset($fields_arr[$value])) $have_select_field_arr[] = $fields_arr[$value];
- }
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- $sign_map = self::signMap();
- foreach ($apply_model->batch() as $details) {
- $csv->data = array_map(function($detail)use($fields,$sign_map){
- $row = [];
- foreach ($fields as $field){
- switch (true){
- case (in_array($field, ['created_at'])):
- $row[] = !empty($detail[$field]) ? date('Y-m-d H:i:s',$detail[$field]) : '';
- break;
- case (in_array($field, ['updated_at'])):
- $row[] = !empty($detail[$field]) ? date('Y-m-d H:i:s',$detail[$field]) : '';
- break;
- case ($field == 'user_mobile'):
- $row[] = $detail['user']['mobile'] ?? '';
- break;
- case ($field == 'nickname'):
- $row[] = $detail['user']['nickname'] ?? '';
- break;
- case ($field == 'sign_status'):
- $row[] = empty($sign_map[$detail['sign_status']]) ? '' : $sign_map[$detail['sign_status']];
- break;
- default:
- $row[] = !empty($detail[$field]) ? $detail[$field] : '';
- }
- }
- return $row;
- }, $details);
- $csv->export();
- }
- $csv->updateDown($download,$params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- var_dump($e->getMessage().$e->getFile().$e->getLine());
- self::setStaticError($e->getMessage().$e->getFile().$e->getLine());
- return $e->getMessage();
- }
- }
- }
- }
|