Common.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\constant\ApplyConstant;
  4. use app\common\constant\CommonConstant;
  5. use app\common\constant\ContractConstant;
  6. use app\common\constant\EvectionConstant;
  7. use app\common\constant\LeaveConstant;
  8. use app\common\constant\MaintainConstant;
  9. use app\common\constant\OfferConstant;
  10. use hg\apidoc\annotation as Apidoc;
  11. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  12. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  13. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  14. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  15. use think\db\exception\BindParamException;
  16. use think\Exception;
  17. use think\exception\PDOException;
  18. /**
  19. * @Apidoc\Title("公用")
  20. * @Apidoc\Group("api")
  21. * @Apidoc\Sort("4")
  22. */
  23. class Common extends Base
  24. {
  25. public function initialize()
  26. {
  27. parent::initialize();
  28. }
  29. /**
  30. * 类型列表
  31. *
  32. * @Apidoc\Method("POST")
  33. * @Apidoc\Returned("module_list", type="array", desc="模块列表")
  34. * @Apidoc\Returned("degree_list", type="array", desc="缓急程度列表")
  35. * @Apidoc\Returned("pay_type_list", type="array", desc="采购支付方式列表")
  36. * @Apidoc\Returned("time_list", type="array", desc="请假周期列表")
  37. * @Apidoc\Returned("data1", type="array", desc="采购类型列表")
  38. * @Apidoc\Returned("data2", type="array", desc="呈批类型列表")
  39. * @Apidoc\Returned("data5", type="array", desc="出差类型列表")
  40. * @Apidoc\Returned("data6", type="array", desc="请假类型列表")
  41. * @Apidoc\Returned("data8", type="array", desc="维修类型列表")
  42. * @Apidoc\Returned("data9", type="array", desc="合同类型列表")
  43. */
  44. public function get_type_list()
  45. {
  46. $module_list = get_one_two_array(CommonConstant::get_module_list(), 'id', 'name');
  47. $degree_list = get_one_two_array(OfferConstant::get_degree_list(), 'id', 'name');
  48. $pay_type_list = get_one_two_array(ApplyConstant::get_pay_type_list(), 'id', 'name');
  49. $time_list = get_one_two_array(LeaveConstant::get_time_list(), 'id', 'name');
  50. $data1 = get_one_two_array(ApplyConstant::get_type_list(), 'id', 'name');
  51. $data2 = get_one_two_array(OfferConstant::get_type_list(), 'id', 'name');
  52. $data5 = get_one_two_array(EvectionConstant::get_type_list(), 'id', 'name');
  53. $data6 = get_one_two_array(LeaveConstant::get_type_list(), 'id', 'name');
  54. $data8 = get_one_two_array(MaintainConstant::get_type_list(), 'id', 'name');
  55. $data9 = get_one_two_array(ContractConstant::get_type_list(), 'id', 'name');
  56. $data = compact("module_list", "degree_list", "pay_type_list", "time_list", "data1", "data2", "data5", "data6", "data8", "data9");
  57. $this->success('类型列表', $data);
  58. }
  59. /**
  60. * 导入
  61. *
  62. * @Apidoc\Method("POST")
  63. * @Apidoc\Param("file", type="string", require=true, desc="文件地址")
  64. * @return void
  65. * @throws PDOException
  66. * @throws BindParamException
  67. */
  68. public function import()
  69. {
  70. set_time_limit(0);
  71. $file = $this->request->post('file');
  72. if (!$file) {
  73. $this->error(__('Parameter %s can not be empty', 'file'));
  74. }
  75. $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  76. if (!is_file($filePath)) {
  77. $this->error(__('No results were found'));
  78. }
  79. //实例化reader
  80. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  81. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  82. $this->error(__('Unknown data format'));
  83. }
  84. if ($ext === 'csv') {
  85. $file = fopen($filePath, 'r');
  86. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  87. $fp = fopen($filePath, 'w');
  88. $n = 0;
  89. while ($line = fgets($file)) {
  90. $line = rtrim($line, "\n\r\0");
  91. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  92. if ($encoding !== 'utf-8') {
  93. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  94. }
  95. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  96. fwrite($fp, $line . "\n");
  97. } else {
  98. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  99. }
  100. $n++;
  101. }
  102. fclose($file) || fclose($fp);
  103. $reader = new Csv();
  104. } elseif ($ext === 'xls') {
  105. $reader = new Xls();
  106. } else {
  107. $reader = new Xlsx();
  108. }
  109. //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
  110. $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
  111. $table = $this->model->getQuery()->getTable();
  112. $database = \think\Config::get('database.database');
  113. $fieldArr = [];
  114. $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
  115. foreach ($list as $k => $v) {
  116. if ($importHeadType == 'comment') {
  117. $v['COLUMN_COMMENT'] = explode(':', $v['COLUMN_COMMENT'])[0]; //字段备注有:时截取
  118. $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
  119. } else {
  120. $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
  121. }
  122. }
  123. //加载文件
  124. $insert = [];
  125. try {
  126. if (!$PHPExcel = $reader->load($filePath)) {
  127. $this->error(__('Unknown data format'));
  128. }
  129. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  130. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  131. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  132. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  133. $fields = [];
  134. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  135. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  136. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  137. $fields[] = $val;
  138. }
  139. }
  140. $product = Product::column('id,name'); // 产品列表
  141. $status_list = config('bill_status_array');
  142. $status_list_flip = array_flip($status_list); // 交换数组中的键和值
  143. $status_value = array_values($status_list);
  144. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  145. $values = [];
  146. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  147. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  148. $values[] = is_null($val) ? '' : $val;
  149. }
  150. $row = [];
  151. $temp = array_combine($fields, $values);
  152. foreach ($temp as $k => $v) {
  153. if (isset($fieldArr[$k]) && $k !== '') {
  154. $row[$fieldArr[$k]] = $v;
  155. }
  156. }
  157. if ($row) {
  158. if(isset($row['date']) && !empty($row['date'])){
  159. // 状态 转为状态码
  160. if(in_array($row['status'],$status_value)){
  161. $row['status'] = $status_list_flip[$row['status']];
  162. }
  163. // 产品名称 转为产品ID
  164. if(in_array($row['product_id'],$product)){
  165. foreach ($product as $kk=>$vv){
  166. if($row['product_id'] == $vv){
  167. $row['product_id'] = $kk;
  168. }
  169. }
  170. } else{
  171. $row['product_id'] = 0;
  172. }
  173. $insert[] = $row;
  174. }
  175. }
  176. }
  177. } catch (Exception $exception) {
  178. $this->error($exception->getMessage());
  179. }
  180. if (!$insert) {
  181. $this->error(__('No rows were updated'));
  182. }
  183. try {
  184. $this->model->saveAll($insert);
  185. $msg = '导入成功';
  186. } catch (PDOException $exception) {
  187. $msg = $exception->getMessage();
  188. $this->error($msg);
  189. } catch (Exception $e) {
  190. $this->error($e->getMessage());
  191. }
  192. $this->success($msg);
  193. }
  194. }