GoodController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Good;
  4. use App\Trace;
  5. use Encore\Admin\Form;
  6. use Encore\Admin\Grid;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Layout\Content;
  9. use App\Http\Controllers\Controller;
  10. use Encore\Admin\Controllers\ModelForm;
  11. use Illuminate\Support\Facades\Artisan;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. use Vinkla\Hashids\Facades\Hashids;
  14. class GoodController extends Controller
  15. {
  16. use ModelForm;
  17. /**
  18. * Index interface.
  19. *
  20. * @return Content
  21. */
  22. public function index()
  23. {
  24. return Admin::content(function (Content $content) {
  25. $content->header('产品管理');
  26. $content->description('列表');
  27. $content->body($this->grid());
  28. });
  29. }
  30. /**
  31. * Edit interface.
  32. *
  33. * @param $id
  34. * @return Content
  35. */
  36. public function edit($id)
  37. {
  38. return Admin::content(function (Content $content) use ($id) {
  39. $content->header('产品管理');
  40. $content->description('编辑');
  41. $content->body($this->form()->edit($id));
  42. });
  43. }
  44. /**
  45. * Create interface.
  46. *
  47. * @return Content
  48. */
  49. public function create()
  50. {
  51. return Admin::content(function (Content $content) {
  52. $content->header('产品管理');
  53. $content->description('创建');
  54. $content->body($this->form());
  55. });
  56. }
  57. /**
  58. * Make a grid builder.
  59. *
  60. * @return Grid
  61. */
  62. protected function grid()
  63. {
  64. return Admin::grid(Good::class, function (Grid $grid) {
  65. $grid->id('ID')->sortable();
  66. $grid->name('品种名称');
  67. $grid->operator('生产经营者');
  68. $grid->product_name('产品类型');
  69. $grid->product_pici('产品批次');
  70. $grid->number('数量');
  71. $grid->group('分组单位');
  72. $grid->trace('防伪码数量');
  73. $grid->is_key('防伪码状态')->display(function ($v) {
  74. switch ($v) {
  75. case 'F':
  76. return '未生成';
  77. break;
  78. case 'D':
  79. return '正在生成';
  80. break;
  81. case 'T':
  82. return '已生成';
  83. break;
  84. }
  85. });
  86. $grid->created_at();
  87. $grid->updated_at();
  88. $grid->disableFilter();
  89. $grid->disableExport();
  90. $grid->actions(function ($actions) {
  91. if ($actions->row->group) {
  92. $actions->prepend('<a style="margin-right:3px;" href="/admin/code?gid=' . $actions->getKey() . '"><i class="fa fa-bullseye"></i></a>');
  93. }
  94. // append一个操作
  95. if ($actions->row->is_key !== 'G') {
  96. $actions->append('<a href="/admin/key/' . $actions->getKey() . '"><i class="fa fa-sitemap"></i></a>');
  97. }
  98. if ($actions->row->is_key === 'T') {
  99. $actions->append('<a target="_blank" style="margin-left:5px;" href="/admin/export/' . $actions->getKey() . '"><i class="fa fa-share"></i></a>');
  100. }
  101. });
  102. });
  103. }
  104. /**
  105. * Make a form builder.
  106. *
  107. * @return Form
  108. */
  109. protected function form()
  110. {
  111. return Admin::form(Good::class, function (Form $form) {
  112. $form->display('id', 'ID');
  113. //产品信息
  114. $form->text('name', '产品名称')->rules('required');
  115. $form->text('product_name', '产品类型');
  116. $form->text('product_pici', '产品批次');
  117. $form->text('standard', '包装规格');
  118. $form->text('package', '包装比例');
  119. $form->divide();
  120. //厂商
  121. $form->text('operator', '生产经营者')->rules('required');
  122. $form->text('product_area', '产地');
  123. $form->text('process_pici', '加工批次');
  124. $form->divide();
  125. //发货信息
  126. $form->text('consignor', '发货商');
  127. $form->date('delivery_time', '发货时间');
  128. $form->text('dealer', '经销商');
  129. $form->divide();
  130. //生产设置
  131. $form->text('higher_key', '上级码');
  132. // $form->textarea('data','扩展字段');
  133. $form->number('number', '数量');
  134. $form->text('skey', '首部添加关键码');
  135. $form->number('bit', '单元识别码的位数')->default(16);
  136. $form->number('group', '多少个分组一组')->help('[注:不分组不用填]');
  137. $form->select('templet', '前端模板')->options(collect(Good::$templetOption)->pluck('name', 'id'));
  138. $form->textarea('remark', '备注');
  139. $form->display('created_at', '创建时间');
  140. $form->display('updated_at', '更新时间');
  141. });
  142. }
  143. /**
  144. * 生成key
  145. * @param $id
  146. * @return \Illuminate\Http\RedirectResponse
  147. * User: Mead
  148. */
  149. public function key($id)
  150. {
  151. admin_toastr('正在生成!请稍后!', 'success');
  152. Artisan::call("generate:key", ['gid' => $id]);
  153. return redirect()->back();
  154. }
  155. /**
  156. * 导出excel
  157. * @param $id
  158. * @return mixed
  159. * User: Mead
  160. */
  161. public function export($id)
  162. {
  163. ini_set('memory_limit', '3072M');
  164. set_time_limit(0);
  165. return Excel::create(time(), function ($excel) use ($id) {
  166. $excel->sheet('Sheet1', function ($sheet) use ($id) {
  167. $sheet->setAutoSize(true);
  168. $sheet->cells('A', function ($cells) {
  169. $cells->setAlignment('center');
  170. });
  171. $sheet->cells('B', function ($cells) {
  172. $cells->setAlignment('center');
  173. });
  174. $sheet->cells('C', function ($cells) {
  175. $cells->setAlignment('center');
  176. });
  177. $sheet->cells('D', function ($cells) {
  178. $cells->setAlignment('center');
  179. });
  180. $sheet->setColumnFormat(array(
  181. 'B' => '0',
  182. 'A' => '0'
  183. ));
  184. $g = Trace::where('gid', $id)->distinct()->count();
  185. $info = Good::where('id', $id)->first();
  186. if ($g == 1) {
  187. Trace::where('gid', $id)->select('id', 'key', 'group')->chunk(1000, function ($trace) use ($sheet) {
  188. $sheet->rows($trace->toArray());
  189. });
  190. } else {
  191. $num = $info->group;
  192. Trace::where('gid', $id)->select('id', 'key', 'group', 'slug')->chunk($num, function ($trace) use ($sheet) {
  193. // $data = $trace->map(function ($v) {
  194. // $v['id'] = number_format(10000000 + $v['id'], 0, '', '');
  195. //
  196. //// $v['b'] = bcadd('1000000000000000', "{$v['id']}");
  197. //
  198. // return $v;
  199. // });
  200. $data = $trace->toArray();
  201. $data[] = ['', '', '', ''];
  202. $sheet->rows($data);
  203. });
  204. }
  205. $sheet->prependRow(1, ['序号', '编码', '分组', '网址']);
  206. });
  207. })->download('xls');
  208. }
  209. }