DianqianApply.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace addons\Dianqian\common\models;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Download;
  7. use common\models\user\User;
  8. use Yii;
  9. use yii\db\Exception;
  10. /**
  11. * This is the model class for table "{{%dianqian_apply}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id 商城ID
  15. * @property int|null $user_id 用户ID
  16. * @property string|null $name 签约人姓名
  17. * @property int|null $mobile 签约人手机号
  18. * @property string|null $reason 被拒理由
  19. * @property string|null $remark 备注
  20. * @property int|null $sign_status 签约状态[-1:申请被拒,1=签约中,2=签约成功]
  21. * @property int|null $status 状态
  22. * @property int $created_at
  23. * @property int $updated_at
  24. */
  25. class DianqianApply extends \common\models\base\BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%dianqian_apply}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'user_id', 'mobile', 'sign_status', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['reason', 'remark'], 'string'],
  42. [['name'], 'string', 'max' => 100],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => '商城ID',
  53. 'user_id' => '用户ID',
  54. 'name' => '签约人姓名',
  55. 'mobile' => '签约人手机号',
  56. 'reason' => '被拒理由',
  57. 'remark' => '备注',
  58. 'sign_status' => '签约状态[-1:申请被拒,1=签约中,2=签约成功]',
  59. 'status' => '状态',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. /**
  65. * 关联用户
  66. * @Author: lun
  67. * @DateTime: 2023/5/12 0012 14:51
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @return \yii\db\ActiveQuery
  70. */
  71. public function getUser()
  72. {
  73. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url,parent_id');
  74. }
  75. /**
  76. * 保存数据
  77. * @Author: lun
  78. * @DateTime: 2023/5/12 0012 15:33
  79. * @Copyright: copyright (c) 2021 广东七件事集团
  80. * @param $params
  81. * @return bool
  82. */
  83. public static function setData($params)
  84. {
  85. try {
  86. if (!empty($params['id'])) {
  87. $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  88. if (empty($model)) throw new \Exception('暂无此申请');
  89. } else {
  90. $model = new self();
  91. $model->loadDefaultValues();
  92. }
  93. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  94. return true;
  95. } catch (\Exception $e) {
  96. self::setStaticError($e->getMessage());
  97. return false;
  98. }
  99. }
  100. /**
  101. * 申请数据提交
  102. * @Author: lun
  103. * @DateTime: 2023/5/12 0012 17:15
  104. * @Copyright: copyright (c) 2021 广东七件事集团
  105. * @param $params
  106. * @return bool
  107. */
  108. public static function apply($params)
  109. {
  110. try {
  111. $model = self::findOne(['user_id' => $params['user_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  112. if (empty($model)) {
  113. $model = new self();
  114. $model->loadDefaultValues();
  115. } else {
  116. if ($model->sign_status != -1) throw new \Exception('禁止重复提交!');
  117. }
  118. if ($model->sign_status == -1) $params['sign_status'] = 1;// 重新申请状态修改
  119. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  120. return true;
  121. } catch (\Exception $e) {
  122. self::setStaticError($e->getMessage());
  123. return false;
  124. }
  125. }
  126. /**
  127. * 导出字段
  128. * @Author: lun
  129. * @DateTime: 2023/5/13 0013 14:23
  130. * @Copyright: copyright (c) 2021 广东七件事集团
  131. * @param null $field
  132. * @return array|string[]
  133. *
  134. */
  135. public static function fieldsList($field = null){
  136. $fields = [
  137. 'user_id' => '用户ID',
  138. 'nickname' => '用户昵称',
  139. 'user_mobile' => '用户手机号',
  140. 'name' => '签约人姓名',
  141. 'mobile' => '签约人手机号',
  142. 'reason' => '被拒理由',
  143. 'sign_status' => '签约状态',
  144. 'remark' => '备注',
  145. 'created_at' => '创建时间',
  146. 'updated_at' => '修改时间',
  147. ];
  148. if ($field === null) return $fields;
  149. $temp = [];
  150. foreach ($field as $val) {
  151. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  152. }
  153. return $temp;
  154. }
  155. const REFUSE = -1;
  156. const SIGNING = 1;
  157. const SIGN_SUCCESS = 2;
  158. /**
  159. * 获取签约类型
  160. * @Author: lun
  161. * @DateTime: 2023/5/13 0013 14:41
  162. * @Copyright: copyright (c) 2021 广东七件事集团
  163. * @return string[]
  164. */
  165. public static function signMap()
  166. {
  167. return [
  168. self::REFUSE => '拒绝',
  169. self::SIGNING => '待签约',
  170. self::SIGN_SUCCESS => '签约成功',
  171. ];
  172. }
  173. /**
  174. * 导出
  175. * @Author: lun
  176. * @DateTime: 2023/5/13 0013 14:23
  177. * @Copyright: copyright (c) 2021 广东七件事集团
  178. * @param array $data
  179. * @return bool|string
  180. */
  181. public static function exportHandle(array $data) {
  182. date_default_timezone_set('Asia/Shanghai');
  183. $download_id = $data['download_id'] ?? 0;
  184. $download = Download::findOne(['id' => $download_id]);
  185. if ($download) {
  186. try {
  187. $params = json_decode($download->params,true);
  188. $filename = $download->name;
  189. $type = $download->type;
  190. $apply_model = self::find();
  191. $apply_model->where(['mall_id'=>$params['mall_id'], 'status' => StatusEnum::ENABLED]);
  192. // 根据条件查询组合
  193. if (!empty($params['user_id'])) $apply_model->andWhere(['=', 'user_id', $params['user_id']]);
  194. if (!empty($params['name'])) $apply_model->andWhere(['=', 'name', $params['name']]);
  195. if (!empty($params['mobile'])) $apply_model->andWhere(['=', 'mobile', $params['mobile']]);
  196. if (!empty($params['sign_status'])) $apply_model->andWhere(['=', 'sign_status', $params['sign_status']]);
  197. if (!empty($params['start'])) $apply_model->andWhere(['>=', 'created_at', strtotime($params['start'])]);
  198. if (!empty($params['end'])) $apply_model->andWhere(['<=', 'created_at', strtotime($params['end'])]);
  199. $apply_model->with(['user' => function($query) {
  200. $query->select('id,nickname,username,mobile');
  201. }]);
  202. $fields = $params['fields'];
  203. $fields_arr = self::fieldsList();
  204. $have_select_field_arr = [];
  205. foreach ($fields as $value) {
  206. if (isset($fields_arr[$value])) $have_select_field_arr[] = $fields_arr[$value];
  207. }
  208. $csv = new CsvTool();
  209. $csv->filename = $filename;
  210. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  211. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  212. $csv->header = $have_select_field_arr;
  213. $sign_map = self::signMap();
  214. foreach ($apply_model->batch() as $details) {
  215. $csv->data = array_map(function($detail)use($fields,$sign_map){
  216. $row = [];
  217. foreach ($fields as $field){
  218. switch (true){
  219. case (in_array($field, ['created_at'])):
  220. $row[] = !empty($detail[$field]) ? date('Y-m-d H:i:s',$detail[$field]) : '';
  221. break;
  222. case (in_array($field, ['updated_at'])):
  223. $row[] = !empty($detail[$field]) ? date('Y-m-d H:i:s',$detail[$field]) : '';
  224. break;
  225. case ($field == 'user_mobile'):
  226. $row[] = $detail['user']['mobile'] ?? '';
  227. break;
  228. case ($field == 'nickname'):
  229. $row[] = $detail['user']['nickname'] ?? '';
  230. break;
  231. case ($field == 'sign_status'):
  232. $row[] = empty($sign_map[$detail['sign_status']]) ? '' : $sign_map[$detail['sign_status']];
  233. break;
  234. default:
  235. $row[] = !empty($detail[$field]) ? $detail[$field] : '';
  236. }
  237. }
  238. return $row;
  239. }, $details);
  240. $csv->export();
  241. }
  242. $csv->updateDown($download,$params['mall_id']);
  243. return true;
  244. } catch (\Exception $e) {
  245. $download->status = 3;
  246. $res = $download->save();
  247. var_dump($e->getMessage().$e->getFile().$e->getLine());
  248. self::setStaticError($e->getMessage().$e->getFile().$e->getLine());
  249. return $e->getMessage();
  250. }
  251. }
  252. }
  253. }