Excel.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/5/17
  6. * Time: 上午11:34
  7. */
  8. namespace common\helpers;
  9. use common\helpers\http\RemoteUploadApi;
  10. use common\models\ExcelAddUser;
  11. use common\models\ExcelChangeBalance;
  12. use common\models\ExcelImport;
  13. use common\models\ExcelRegInfo;
  14. use common\models\ExcelWithdrawPaidFalse;
  15. use common\models\Export;
  16. use common\models\ExportFile;
  17. use common\models\forms\AdminAddUserForm;
  18. use common\models\forms\ChangeBalanceForm;
  19. use common\models\forms\ExcelOrderDecForm;
  20. use common\models\forms\ExcelOrderShopForm;
  21. use common\models\forms\ExcelOrderStandardForm;
  22. use common\models\forms\RegInfoAuditForm;
  23. use common\models\forms\WithdrawForm;
  24. use common\models\Uploads;
  25. use common\models\UserNetwork;
  26. use yii\base\BaseObject;
  27. use yii\base\Exception;
  28. use yii\base\StaticInstanceTrait;
  29. class Excel extends BaseObject {
  30. use StaticInstanceTrait;
  31. const CHILD_PATH = 'excel_export';
  32. public $isRemote = true;
  33. public $uploadInfo;
  34. public $exportInfo;
  35. private $_errors = [];
  36. const EXCEL_STRUCTURE = [
  37. 'addUser' => [
  38. 'formClass' => AdminAddUserForm::class,
  39. 'formScenario' => 'add',
  40. 'formAction' => 'edit',
  41. 'errorTipField' => 'USER_NAME',
  42. 'excelTableClass' => ExcelAddUser::class,
  43. 'excelTableField' => [
  44. '序号' => 'SORT',
  45. '会员编号' => 'USER_NAME',
  46. '会员姓名' => 'REAL_NAME',
  47. '会员级别' => 'DEC_LV',
  48. '身份证' => 'ID_CARD',
  49. '手机' => 'MOBILE',
  50. '备用手机号' => 'TEL',
  51. '省/市' => 'AREA_PROVINCE',
  52. '市/区' => 'AREA_CITY',
  53. '区/县' => 'AREA_COUNTY',
  54. '身份证地址' => 'ADDRESS',
  55. '开户银行' => 'OPEN_BANK',
  56. '开户行地址' => 'BANK_ADDRESS',
  57. '银行账号' => 'BANK_NO',
  58. '银行 省/市' => 'BANK_PROVINCE',
  59. '银行 市/区' => 'BANK_CITY',
  60. '银行 区/县' => 'BANK_COUNTY',
  61. '是否报单中心' => 'IS_DEC',
  62. '报单中心级别' => 'DEC_ROLE',
  63. '所属报单中心编号' => 'DEC_USER_NAME',
  64. '生日' => 'BIRTHDAY',
  65. '接点人' => 'CON_USER_NAME',
  66. '开拓人' => 'REC_USER_NAME',
  67. '区位' => 'LOCATION',
  68. ],
  69. 'formField' => [
  70. 'USER_NAME' => [
  71. 'name' => 'userName',
  72. 'type' => 'common',
  73. ],
  74. 'REAL_NAME' => [
  75. 'name' => 'realName',
  76. 'type' => 'common',
  77. ],
  78. 'DEC_LV' => [
  79. 'name' => 'decLv',
  80. 'type' => 'table',
  81. 'table' => 'DECLARATION_LEVEL',
  82. 'field' => 'LEVEL_NAME',
  83. 'select' => 'ID',
  84. ],
  85. 'ID_CARD' => [
  86. 'name' => 'idCard',
  87. 'type' => 'common',
  88. ],
  89. 'MOBILE' => [
  90. 'name' => 'mobile',
  91. 'type' => 'common',
  92. ],
  93. 'TEL' => [
  94. 'name' => 'tel',
  95. 'type' => 'common',
  96. ],
  97. 'AREA_PROVINCE' => [
  98. 'name' => 'areaSelected',
  99. 'isArray' => true,
  100. 'type' => 'table',
  101. 'table' => 'REGION',
  102. 'field' => 'REGION_NAME',
  103. 'where' => 'DEEP=2',
  104. 'select' => 'REGION_CODE',
  105. ],
  106. 'AREA_CITY' => [
  107. 'name' => 'areaSelected',
  108. 'isArray' => true,
  109. 'type' => 'table',
  110. 'table' => 'REGION',
  111. 'field' => 'REGION_NAME',
  112. 'where' => 'DEEP=3',
  113. 'select' => 'REGION_CODE',
  114. ],
  115. 'AREA_COUNTY' => [
  116. 'name' => 'areaSelected',
  117. 'isArray' => true,
  118. 'type' => 'table',
  119. 'table' => 'REGION',
  120. 'field' => 'REGION_NAME',
  121. 'where' => 'DEEP=4',
  122. 'select' => 'REGION_CODE',
  123. ],
  124. 'ADDRESS' => [
  125. 'name' => 'address',
  126. 'type' => 'common',
  127. ],
  128. 'OPEN_BANK' => [
  129. 'name' => 'openBank',
  130. 'type' => 'table',
  131. 'table' => 'OPEN_BANK',
  132. 'field' => 'BANK_NAME',
  133. 'select' => 'BANK_CODE',
  134. ],
  135. 'BANK_ADDRESS' => [
  136. 'name' => 'bankAddress',
  137. 'type' => 'common',
  138. ],
  139. 'BANK_NO' => [
  140. 'name' => 'bankNo',
  141. 'type' => 'common',
  142. ],
  143. 'BANK_PROVINCE' => [
  144. 'name' => 'bankAreaSelected',
  145. 'isArray' => true,
  146. 'type' => 'table',
  147. 'table' => 'REGION',
  148. 'field' => 'REGION_NAME',
  149. 'where' => 'DEEP=2',
  150. 'select' => 'REGION_CODE',
  151. ],
  152. 'BANK_CITY' => [
  153. 'name' => 'bankAreaSelected',
  154. 'isArray' => true,
  155. 'type' => 'table',
  156. 'table' => 'REGION',
  157. 'field' => 'REGION_NAME',
  158. 'where' => 'DEEP=3',
  159. 'select' => 'REGION_CODE',
  160. ],
  161. 'BANK_COUNTY' => [
  162. 'name' => 'bankAreaSelected',
  163. 'isArray' => true,
  164. 'type' => 'table',
  165. 'table' => 'REGION',
  166. 'field' => 'REGION_NAME',
  167. 'where' => 'DEEP=4',
  168. 'select' => 'REGION_CODE',
  169. ],
  170. 'IS_DEC' => [
  171. 'name' => 'isDec',
  172. 'type' => 'bool',
  173. ],
  174. 'DEC_ROLE' => [
  175. 'name' => 'decRoleId',
  176. 'type' => 'table',
  177. 'table' => 'DEC_ROLE',
  178. 'field' => 'ROLE_NAME',
  179. 'select' => 'ID',
  180. 'isAllowNull' => true,
  181. ],
  182. 'DEC_USER_NAME' => [
  183. 'name' => 'decUserName',
  184. 'type' => 'common',
  185. ],
  186. 'BIRTHDAY' => [
  187. 'name' => 'birthday',
  188. 'type' => 'common',
  189. ],
  190. 'CON_USER_NAME' => [
  191. 'name' => 'conUserName',
  192. 'type' => 'common',
  193. ],
  194. 'REC_USER_NAME' => [
  195. 'name' => 'recUserName',
  196. 'type' => 'common',
  197. ],
  198. 'LOCATION' => [
  199. 'name' => 'location',
  200. 'type' => 'int',
  201. ],
  202. ]
  203. ],
  204. 'withdrawPaidFalse' => [
  205. 'formClass' => WithdrawForm::class,
  206. 'formScenario' => 'excelPaidFalse',
  207. 'formAction' => 'excelPaidFalse',
  208. 'errorTipField' => 'SN',
  209. 'excelTableClass' => ExcelWithdrawPaidFalse::class,
  210. 'excelTableField' => [
  211. '序号' => 'SORT',
  212. '提现流水号' => 'SN',
  213. '提现期数' => 'WITHDRAW_PERIOD_NUM',
  214. '付款日期' => 'PAID_AT',
  215. '付款失败原因' => 'PAID_FAIL_REMARK',
  216. '会员编号' => 'USER_NAME',
  217. '会员姓名' => 'REAL_NAME',
  218. '实付金额' => 'AMOUNT',
  219. '实时开户名' => 'BANK_REAL_NAME',
  220. '实时银行账户' => 'BANK_NO',
  221. ],
  222. 'formField' => [
  223. 'SN' => [
  224. 'name' => 'sn',
  225. 'type' => 'common',
  226. ],
  227. 'WITHDRAW_PERIOD_NUM' => [
  228. 'name' => 'withdrawPeriodNum',
  229. 'type' => 'common',
  230. ],
  231. 'PAID_AT' => [
  232. 'name' => 'paidAt',
  233. 'type' => 'common',
  234. ],
  235. 'PAID_FAIL_REMARK' => [
  236. 'name' => 'paidFailRemark',
  237. 'type' => 'common',
  238. ],
  239. 'USER_NAME' => [
  240. 'name' => 'userName',
  241. 'type' => 'common',
  242. ],
  243. 'REAL_NAME' => [
  244. 'name' => 'realName',
  245. 'type' => 'common',
  246. ],
  247. 'AMOUNT' => [
  248. 'name' => 'amount',
  249. 'type' => 'common',
  250. ],
  251. 'BANK_REAL_NAME' => [
  252. 'name' => 'bankRealName',
  253. 'type' => 'common',
  254. ],
  255. 'BANK_NO' => [
  256. 'name' => 'bankNo',
  257. 'type' => 'common',
  258. ],
  259. ]
  260. ],
  261. 'changeBalance' => [
  262. 'formClass' => ChangeBalanceForm::class,
  263. 'formScenario' => 'excelChangeBalance',
  264. 'formAction' => 'excelChangeBalance',
  265. 'errorTipField' => 'USER_NAME',
  266. 'excelTableClass' => ExcelChangeBalance::class,
  267. 'excelTableField' => [
  268. '序号' => 'SORT',
  269. '会员编号' => 'USER_NAME',
  270. '会员姓名' => 'REAL_NAME',
  271. '账户类型' => 'TYPE',
  272. '交易类型' => 'DEAL_TYPE',
  273. '调整金额' => 'AMOUNT',
  274. '备注' => 'REMARK',
  275. '备注是否前台显示' => 'REMARK_IS_SHOW',
  276. ],
  277. 'formField' => [
  278. 'USER_NAME' => [
  279. 'name' => 'userName',
  280. 'type' => 'common',
  281. ],
  282. 'REAL_NAME' => [
  283. 'name' => 'realName',
  284. 'type' => 'common',
  285. ],
  286. 'TYPE' => [
  287. 'name' => 'type',
  288. 'type' => 'param',
  289. 'index' => 'bonusWalletType',
  290. ],
  291. 'DEAL_TYPE' => [
  292. 'name' => 'dealType',
  293. 'type' => 'table',
  294. 'db' => 'db',
  295. 'table' => 'DEAL_TYPE',
  296. 'field' => 'TYPE_NAME',
  297. 'select' => 'ID',
  298. ],
  299. 'AMOUNT' => [
  300. 'name' => 'amount',
  301. 'type' => 'common',
  302. ],
  303. 'REMARK' => [
  304. 'name' => 'remark',
  305. 'type' => 'common',
  306. ],
  307. 'REMARK_IS_SHOW' => [
  308. 'name' => 'isShow',
  309. 'type' => 'bool',
  310. ],
  311. ]
  312. ],
  313. 'regInfo' => [
  314. 'formClass' => RegInfoAuditForm::class,
  315. 'formScenario' => 'excelAdd',
  316. 'formAction' => 'excelAdd',
  317. 'errorTipField' => 'USER_NAME',
  318. 'excelTableClass' => ExcelRegInfo::class,
  319. 'excelTableField' => [
  320. '序号' => 'SORT',
  321. '会员编号' => 'USER_NAME',
  322. '会员姓名' => 'REAL_NAME',
  323. '身份证' => 'ID_CARD',
  324. '注册类型' => 'REG_TYPE',
  325. '注册名称' => 'REG_NAME',
  326. '统一社会信用代码' => 'CREDIT_CODE',
  327. '经营场所' => 'PREMISES',
  328. '注册有效期' => 'REG_EXPIRES',
  329. '法人' => 'LEGAL_PERSON',
  330. '企业开户名称' => 'OPEN_NAME',
  331. '企业开户银行' => 'OPEN_BANK',
  332. '企业开户行地址' => 'BANK_ADDRESS',
  333. '企业银行账号' => 'BANK_NO',
  334. '企业银行 省/市' => 'BANK_PROVINCE',
  335. '企业银行 市/区' => 'BANK_CITY',
  336. '企业银行 区/县' => 'BANK_COUNTY',
  337. '备注' => 'REMARK',
  338. ],
  339. 'formField' => [
  340. 'USER_NAME' => [
  341. 'name' => 'userName',
  342. 'type' => 'common',
  343. ],
  344. 'REAL_NAME' => [
  345. 'name' => 'realName',
  346. 'type' => 'common',
  347. ],
  348. 'ID_CARD' => [
  349. 'name' => 'idCard',
  350. 'type' => 'common',
  351. ],
  352. 'REG_TYPE' => [
  353. 'name' => 'type',
  354. 'type' => 'table',
  355. 'db' => 'db',
  356. 'table' => 'REG_TYPE',
  357. 'field' => 'TYPE_NAME',
  358. 'select' => 'ID',
  359. ],
  360. 'REG_NAME' => [
  361. 'name' => 'regName',
  362. 'type' => 'common',
  363. ],
  364. 'CREDIT_CODE' => [
  365. 'name' => 'creditCode',
  366. 'type' => 'common',
  367. ],
  368. 'PREMISES' => [
  369. 'name' => 'premises',
  370. 'type' => 'common',
  371. ],
  372. 'REG_EXPIRES' => [
  373. 'name' => 'regExpires',
  374. 'type' => 'common',
  375. ],
  376. 'LEGAL_PERSON' => [
  377. 'name' => 'legalPerson',
  378. 'type' => 'common',
  379. ],
  380. 'OPEN_NAME' => [
  381. 'name' => 'openName',
  382. 'type' => 'common',
  383. ],
  384. 'OPEN_BANK' => [
  385. 'name' => 'openBank',
  386. 'type' => 'table',
  387. 'table' => 'OPEN_BANK',
  388. 'field' => 'BANK_NAME',
  389. 'select' => 'BANK_CODE',
  390. 'isAllowNull' => true,
  391. ],
  392. 'BANK_ADDRESS' => [
  393. 'name' => 'bankAddress',
  394. 'type' => 'common',
  395. 'isAllowNull' => true,
  396. ],
  397. 'BANK_NO' => [
  398. 'name' => 'bankNo',
  399. 'type' => 'common',
  400. 'isAllowNull' => true,
  401. ],
  402. 'BANK_PROVINCE' => [
  403. 'name' => 'bankAreaSelected',
  404. 'isArray' => true,
  405. 'type' => 'table',
  406. 'table' => 'REGION',
  407. 'field' => 'REGION_NAME',
  408. 'where' => 'DEEP=2',
  409. 'select' => 'REGION_CODE',
  410. 'isAllowNull' => true,
  411. ],
  412. 'BANK_CITY' => [
  413. 'name' => 'bankAreaSelected',
  414. 'isArray' => true,
  415. 'type' => 'table',
  416. 'table' => 'REGION',
  417. 'field' => 'REGION_NAME',
  418. 'where' => 'DEEP=3',
  419. 'select' => 'REGION_CODE',
  420. 'isAllowNull' => true,
  421. ],
  422. 'BANK_COUNTY' => [
  423. 'name' => 'bankAreaSelected',
  424. 'isArray' => true,
  425. 'type' => 'table',
  426. 'table' => 'REGION',
  427. 'field' => 'REGION_NAME',
  428. 'where' => 'DEEP=4',
  429. 'select' => 'REGION_CODE',
  430. 'isAllowNull' => true,
  431. ],
  432. 'REMARK' => [
  433. 'name' => 'createRemark',
  434. 'type' => 'common',
  435. ],
  436. ]
  437. ],
  438. 'orderShop' => [
  439. 'excelTableClass' => ExcelOrderShopForm::class,
  440. 'excelTableAction' => 'run',
  441. ],
  442. 'orderDec' => [
  443. 'excelTableClass' => ExcelOrderDecForm::class,
  444. 'excelTableAction' => 'run',
  445. ],
  446. 'orderShopStandard' => [
  447. 'excelTableClass' => ExcelOrderStandardForm::class,
  448. 'excelTableAction' => 'run',
  449. ],
  450. ];
  451. public function init() {
  452. parent::init();
  453. $this->isRemote = \Yii::$app->params['isRemoteUpload'];
  454. }
  455. /**
  456. * 加入错误错误
  457. * @param $attr
  458. * @param $error
  459. */
  460. public function addError($attr, $error) {
  461. $this->_errors[$attr][] = $error;
  462. }
  463. /**
  464. * 获取错误信息
  465. * @return array
  466. */
  467. public function getErrors() {
  468. return $this->_errors;
  469. }
  470. /**
  471. * 导出到文件
  472. * @param $exportId
  473. * @param array $dataArr
  474. * @param array $columns
  475. * @param $fileName
  476. * @param callable|null $callBack
  477. * @return bool
  478. */
  479. public function exportToFile($exportId, array $dataArr, array $columns, $fileName, callable $callBack = null) {
  480. $result = true;
  481. $headers = $columns;
  482. $columns = array_keys($columns);
  483. $fileName = $fileName . '_export_' . date('YmdHis') . uniqid() . '.xlsx';
  484. $path = \Yii::getAlias('@common/runtime/' . self::CHILD_PATH . '/');
  485. if (!is_dir($path)) {
  486. mkdir($path, 0755);
  487. }
  488. try {
  489. \sunmoon\phpspreadsheet\Excel::export([
  490. 'models' => $dataArr,
  491. 'columns' => $columns,
  492. 'headers' => $headers,
  493. 'savePath' => $path,
  494. 'asAttachment' => false,
  495. 'format' => 'Xlsx',
  496. 'fileName' => $fileName,
  497. 'exit' => false,
  498. 'exportIterationCallback' => $callBack,
  499. ]);
  500. // 把导出的文件上传至静态文件服务器
  501. if ($this->isRemote) {
  502. $remoteUploadApi = RemoteUploadApi::instance();
  503. if ($uploadResult = $remoteUploadApi->upload($path . $fileName)) {
  504. $this->exportInfo = [
  505. 'fileName' => $uploadResult['name'],
  506. 'url' => $uploadResult['url'],
  507. 'fileSize' => $uploadResult['size'] ?? null,
  508. 'md5' => $uploadResult['md5'] ?? null,
  509. ];
  510. } else {
  511. throw new Exception('文件远程上传失败');
  512. }
  513. // 删除本地临时文件
  514. unlink($path . $fileName);
  515. } else {
  516. $this->exportInfo = [
  517. 'fileName' => $fileName,
  518. 'url' => $path . $fileName,
  519. 'fileSize' => null,
  520. 'md5' => null,
  521. ];
  522. }
  523. // 把文件对应的相关资料存入数据库中
  524. $uploads = new ExportFile();
  525. $uploads->FILE_NAME = $this->exportInfo['fileName'];
  526. $uploads->EXPORT_ID = $exportId;
  527. $uploads->URL = $this->exportInfo['url'];
  528. $uploads->FILE_SIZE = $this->exportInfo['fileSize'] ?? null;
  529. $uploads->MD5 = $this->exportInfo['md5'] ?? null;
  530. $uploads->CREATED_AT = Date::nowTime();
  531. if (!$uploads->save()) {
  532. throw new Exception(Form::formatErrorsForApi($uploads->getErrors()));
  533. }
  534. } catch (\Exception $e) {
  535. $this->addError('export', $e->getMessage());
  536. $result = false;
  537. }
  538. return $result;
  539. }
  540. /**
  541. * 过滤读取到的数据把空的未填写的行都删掉
  542. * @param $data
  543. * @return mixed
  544. */
  545. public static function filterData(&$data) {
  546. foreach ($data as $key => $value) {
  547. $isAllNull = true;
  548. foreach ($value as $k => $v) {
  549. if ($v !== null) {
  550. $isAllNull = false;
  551. }
  552. }
  553. if ($isAllNull) {
  554. unset($data[$key]);
  555. }
  556. }
  557. return $data;
  558. }
  559. /**
  560. * 分页导入数据
  561. * @param $excelStructureKey
  562. * @param $excelImportId
  563. * @param int $rowCount
  564. * @param int $startRow
  565. * @param int $limit
  566. * @return int
  567. * @throws Exception
  568. */
  569. public function pageImportDataFromExcel($excelStructureKey, $excelImportId, $rowCount = 1000, $startRow = 1, $limit = 1000) {
  570. if ($startRow > 1) {
  571. $startRow = $startRow + 1;
  572. $limit = $limit - 1;
  573. }
  574. $fileNameArray = ExcelImport::find()->select('U.FILE_NAME')->from(ExcelImport::tableName() . ' AS ET')->join('LEFT JOIN', Uploads::tableName() . ' AS U', 'ET.UPLOAD_ID=U.ID')->where('ET.ID=:ID', [':ID' => $excelImportId])->asArray()->one();
  575. $filePath = \Yii::getAlias('@common/runtime/uploads/' . $fileNameArray['FILE_NAME']);
  576. if ($startRow > $rowCount) {
  577. return 0;
  578. }
  579. try {
  580. // 临时文件不存在则创建
  581. $tempFileName = \Yii::getAlias('@common/runtime/uploads/' . 'importTemp.txt');
  582. if (!file_exists($tempFileName)) {
  583. $fp = fopen($tempFileName, 'w+');
  584. fclose($fp);
  585. } else {
  586. if ($startRow == 1) {
  587. file_put_contents($tempFileName, '');
  588. }
  589. }
  590. $data = \sunmoon\phpspreadsheet\Excel::import($filePath, [
  591. 'setFirstRecordAsKeys' => true,
  592. 'readStartRow' => $startRow,
  593. 'readEndRow' => $startRow + $limit,
  594. 'storeFile' => $tempFileName,
  595. 'dropKeysRow' => $startRow == 1 ? false : true,
  596. ]);
  597. } catch (\Exception $e) {
  598. throw new Exception($e->getMessage());
  599. }
  600. self::filterData($data);
  601. if ($data) {
  602. // if($startRow != 1){
  603. // unset($data[0]);
  604. // }
  605. // if(!$data) return 0;
  606. // self::filterData($data);
  607. $this->importDataToExcelTable($excelStructureKey, $excelImportId, $data);
  608. return 1;
  609. } else {
  610. return 0;
  611. }
  612. }
  613. /**
  614. * Excel数据导入到待导入的表中
  615. * @param $excelStructureKey
  616. * @param $excelImportId
  617. * @param $excelData
  618. */
  619. public function importDataToExcelTable($excelStructureKey, $excelImportId, $excelData) {
  620. $tableClass = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableClass'];
  621. $tableIndex = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableField'];
  622. $insertData = [];
  623. foreach ($excelData as $data) {
  624. $oneInsertData = [
  625. 'EXCEL_IMPORT_ID' => $excelImportId,
  626. 'CREATED_AT' => Date::nowTime(),
  627. ];
  628. foreach ($data as $key => $value) {
  629. $oneInsertData[$tableIndex[$key]] = $value;
  630. }
  631. $insertData[] = $oneInsertData;
  632. }
  633. $tableClass::batchInsert($insertData);
  634. }
  635. /**
  636. * 分页从待导入表中导入数据到真实数据中
  637. * @param $excelStructureKey
  638. * @param $excelImportId
  639. * @param int $offset
  640. * @param int $limit
  641. * @return int
  642. * @throws Exception
  643. */
  644. public function pageImportDataFromExcelTable($excelStructureKey, $excelImportId, $offset = 0, $limit = 1000) {
  645. $tableClass = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableClass'];
  646. // 获取1000条数据
  647. $allData = $tableClass::find()->where('EXCEL_IMPORT_ID=:EXCEL_IMPORT_ID', [':EXCEL_IMPORT_ID' => $excelImportId])->orderBy('SORT ASC')->offset($offset)->limit($limit)->asArray()->all();
  648. if ($allData) {
  649. $this->importToDbWithForm($excelStructureKey, $allData);
  650. return 1;
  651. }
  652. return 0;
  653. }
  654. /**
  655. * 通过表单类导入
  656. * @param $excelStructureKey
  657. * @param $excelDatas
  658. * @throws Exception
  659. */
  660. public function importToDbWithForm($excelStructureKey, $excelDatas) {
  661. foreach ($excelDatas as $value) {
  662. $formClassName = self::EXCEL_STRUCTURE[$excelStructureKey]['formClass'];
  663. $formScenario = self::EXCEL_STRUCTURE[$excelStructureKey]['formScenario'];
  664. $formAction = self::EXCEL_STRUCTURE[$excelStructureKey]['formAction'];
  665. $formData = $this->excelDataToFormData($excelStructureKey, $value);
  666. if ($formData) {
  667. $form = new $formClassName();
  668. $form->scenario = $formScenario;
  669. foreach ($formData as $formKey => $formValue) {
  670. $form->$formKey = $formValue;
  671. }
  672. $errorTipField = self::EXCEL_STRUCTURE[$excelStructureKey]['errorTipField'];
  673. $tableClass = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableClass'];
  674. if (!$form->$formAction()) {
  675. // 失败信息写入到待导入数据库excel表中
  676. $tableClass::updateAll(['STATUS' => 2, 'ERROR_REMARK' => Form::formatErrorsForApi($form->getErrors())], 'ID=:ID', [':ID' => $value['ID']]);
  677. throw new Exception('错误序号【'.$value['SORT'].'】会员编号【'.$value['USER_NAME'].'】。导入' . $value[$errorTipField] . '发生错误,原因:' . Form::formatErrorsForApi($form->getErrors()));
  678. } else {
  679. $tableClass::updateAll(['STATUS' => 1], 'ID=:ID', [':ID' => $value['ID']]);
  680. }
  681. }
  682. }
  683. }
  684. /**
  685. * excel数据转为表单可提交的数据
  686. * @param $excelStructureKey
  687. * @param $excelData
  688. * @return array
  689. * @throws Exception
  690. */
  691. public function excelDataToFormData($excelStructureKey, $excelData) {
  692. $formFieldArr = self::EXCEL_STRUCTURE[$excelStructureKey]['formField'];
  693. $excelTableField = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableField'];
  694. $sort = $excelData['SORT'];
  695. $result = [];
  696. foreach ($excelData as $key => $value) {
  697. if (in_array($key, ['ID', 'EXCEL_IMPORT_ID', 'STATUS', 'ERROR_REMARK', 'CREATED_AT', 'SORT', 'ROWNUMID'])) continue;
  698. if (!array_key_exists($key, $formFieldArr)) {
  699. throw new Exception('excel表格字段不符合要求,字段为:' . $key);
  700. }
  701. $resultKey = $formFieldArr[$key]['name'];
  702. // 这里要判断是否允许为空值,如果允许则可以直接付空值
  703. if (isset($formFieldArr[$key]['isAllowNull']) && $formFieldArr[$key]['isAllowNull'] && $value === null) {
  704. $result[$resultKey] = $value;
  705. } elseif ($formFieldArr[$key]['type'] === 'common') {
  706. $result[$resultKey] = $value;
  707. } elseif ($formFieldArr[$key]['type'] === 'bool') {
  708. $result[$resultKey] = intval($value == '是');
  709. } elseif ($formFieldArr[$key]['type'] === 'int') {
  710. $result[$resultKey] = intval($value);
  711. } elseif ($formFieldArr[$key]['type'] === 'param') {
  712. // 从param的数据中获取
  713. $tempArray = \Yii::$app->params[$formFieldArr[$key]['index']];
  714. if ($formFieldArr[$key]['name'] === 'nation') {
  715. array_unshift($tempArray, [
  716. 'id' => '0',
  717. 'name' => '',
  718. ]);
  719. $nationsArray = array_column($tempArray, 'name');
  720. $tempResultIndex = array_search($value, $nationsArray);
  721. if ($tempResultIndex === false){
  722. if($errTableField = array_search($key, $excelTableField)){
  723. throw new Exception('序号' . $sort . '的表格【' . $errTableField . '】值填写有误,系统中未找到填写的"' . $value . '"');
  724. }else{
  725. throw new Exception('序号' . $sort . '的表格' . $key . '值填写有误,系统中未找到"' . $value . '"');
  726. }
  727. }
  728. $result[$resultKey] = $tempArray[$tempResultIndex]['id'];
  729. } elseif ($formFieldArr[$key]['index'] === 'bonusWalletType') {
  730. $nationsArray = array_column($tempArray, 'name');
  731. $tempResultIndex = array_search($value, $nationsArray);
  732. if ($tempResultIndex === false){
  733. if($errTableField = array_search($key, $excelTableField)){
  734. throw new Exception('序号' . $sort . '的表格【' . $errTableField . '】值填写有误,系统中未找到填写的"' . $value . '"');
  735. }else{
  736. throw new Exception('序号' . $sort . '的表格' . $key . '值填写有误,系统中未找到"' . $value . '"');
  737. }
  738. }
  739. $result[$resultKey] = $nationsArray[$tempResultIndex];
  740. }
  741. } elseif ($formFieldArr[$key]['type'] === 'table') {
  742. // 从数据库中获取对应的参数值
  743. $db = $formFieldArr[$key]['db'] ?? 'db';
  744. $field = $formFieldArr[$key]['field'];
  745. $select = $formFieldArr[$key]['select'];
  746. $table = $formFieldArr[$key]['table'];
  747. $where = $field . '=:' . $field;
  748. $where .= (isset($formFieldArr[$key]['where']) && $formFieldArr[$key]['where']) ? ' AND ' . $formFieldArr[$key]['where'] : '';
  749. $bindValues = [$field => $value];
  750. $tempResult = \Yii::$app->$db->createCommand("SELECT $select FROM {{%$table}} WHERE $where")->bindValues($bindValues)->queryOne();
  751. if (!$tempResult){
  752. if($errTableField = array_search($key, $excelTableField)){
  753. throw new Exception('序号' . $sort . '的表格【' . $errTableField . '】值填写有误,系统中未找到填写的"' . $value . '"');
  754. }else{
  755. throw new Exception('序号' . $sort . '的表格' . $key . '值填写有误,系统中未找到"' . $value . '"');
  756. }
  757. }
  758. if (isset($formFieldArr[$key]['isArray']) && $formFieldArr[$key]['isArray']) {
  759. $result[$resultKey][] = $tempResult[$select];
  760. } else {
  761. $result[$resultKey] = $tempResult[$select];
  762. }
  763. }
  764. }
  765. return $result;
  766. }
  767. /**
  768. * 分页导入数据
  769. * @param $excelStructureKey
  770. * @param $excelImportId
  771. * @param int $rowCount
  772. * @param int $startRow
  773. * @param int $limit
  774. * @param int $orderDay
  775. * @param int $orderType
  776. * @return int
  777. * @throws Exception
  778. */
  779. public function pageImportCustomDataFromExcel($excelStructureKey, $excelImportId, $rowCount, $startRow, $limit, $orderDay, $orderType = 'cash') {
  780. if ($startRow > 1) {
  781. $startRow = $startRow + 1;
  782. $limit = $limit - 1;
  783. }
  784. $fileNameArray = ExcelImport::find()->select('U.FILE_NAME')->from(ExcelImport::tableName() . ' AS ET')->join('LEFT JOIN', Uploads::tableName() . ' AS U', 'ET.UPLOAD_ID=U.ID')->where('ET.ID=:ID', [':ID' => $excelImportId])->asArray()->one();
  785. $filePath = \Yii::$app->params['localUpload']['dns'] . $fileNameArray['FILE_NAME'];
  786. // $filePath = '/ng-stage/Volumes/HDD/workshop/old/ar.upload.ming/files/' . $fileNameArray['FILE_NAME'];
  787. if ($startRow > $rowCount) {
  788. return 0;
  789. }
  790. try {
  791. // 临时文件不存在则创建
  792. $tempFileName = \Yii::getAlias('@common/runtime/uploads/' . 'import'.$excelStructureKey.'Temp.txt');
  793. if (!file_exists($tempFileName)) {
  794. $fp = fopen($tempFileName, 'w+');
  795. fclose($fp);
  796. } else {
  797. if ($startRow == 1) {
  798. file_put_contents($tempFileName, '');
  799. }
  800. }
  801. $data = \sunmoon\phpspreadsheet\Excel::import($filePath, [
  802. 'setFirstRecordAsKeys' => true,
  803. 'readStartRow' => $startRow,
  804. 'readEndRow' => $startRow + $limit,
  805. 'storeFile' => $tempFileName,
  806. 'dropKeysRow' => $startRow == 1 ? false : true,
  807. ]);
  808. } catch (\Exception $e) {
  809. throw new Exception($e->getMessage());
  810. }
  811. self::filterData($data);
  812. if ($data) {
  813. $tableClass = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableClass'];
  814. $tableAction = self::EXCEL_STRUCTURE[$excelStructureKey]['excelTableAction'];
  815. $form = new $tableClass();
  816. if (!$form->$tableAction($data,$orderDay,$orderType)) {
  817. return 0;
  818. }
  819. return 1;
  820. } else {
  821. return 0;
  822. }
  823. }
  824. /**
  825. * 分页导入数据
  826. * @param string $fileName
  827. * @param int $rowCount
  828. * @param int $startRow
  829. * @param int $limit
  830. * @return array
  831. * @throws Exception
  832. */
  833. public function pageImportDataByExcelFile($fileName, $rowCount = 1000, $startRow = 1, $limit = 1000) {
  834. if ($startRow > 1) {
  835. $startRow = $startRow + 1;
  836. $limit = $limit - 1;
  837. }
  838. $filePath = \Yii::getAlias('@common/runtime/uploads/' . $fileName);
  839. if ($startRow > $rowCount) {
  840. return [];
  841. }
  842. try {
  843. // 临时文件不存在则创建
  844. $tempFileName = \Yii::getAlias('@common/runtime/uploads/' . 'importTemp.txt');
  845. if (!file_exists($tempFileName)) {
  846. $fp = fopen($tempFileName, 'w+');
  847. fclose($fp);
  848. } else {
  849. if ($startRow == 1) {
  850. file_put_contents($tempFileName, '');
  851. }
  852. }
  853. $data = \sunmoon\phpspreadsheet\Excel::import($filePath, [
  854. 'setFirstRecordAsKeys' => true,
  855. 'readStartRow' => $startRow,
  856. 'readEndRow' => $startRow + $limit,
  857. 'storeFile' => $tempFileName,
  858. 'dropKeysRow' => $startRow == 1 ? false : true,
  859. ]);
  860. } catch (\Exception $e) {
  861. throw new Exception($e->getMessage());
  862. }
  863. self::filterData($data);
  864. if ($data) {
  865. return $data;
  866. } else {
  867. return [];
  868. }
  869. }
  870. }