PrintTemplateService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerLogisticPrintTemplate;
  4. use App\Traits\ServiceAppAop;
  5. use App\PrintTemplate;
  6. class PrintTemplateService
  7. {
  8. use ServiceAppAop;
  9. protected $modelClass = PrintTemplate::class;
  10. public function saveRelation($saveRelation, $relations, $printTemplateId)
  11. {
  12. $relation_arr = [];
  13. foreach ($relations as $relation) {
  14. $owner_id = $relation['owner_id'];
  15. $logistic_id = $relation['logistic_id'];
  16. if(!array_key_exists($owner_id,$relation_arr)) $relation_arr[$owner_id] = [];
  17. $relation_arr[$owner_id][$logistic_id] = ['name' => $relation->logistic->name];
  18. }
  19. $delete_items = [
  20. 'owner_id' => [],
  21. 'items' => [],
  22. ];
  23. $params = [];
  24. foreach ($saveRelation as $owner_id => $data) {
  25. if(array_key_exists($owner_id,$relation_arr) && $data['isActivation'] == false){
  26. $delete_items['owner_id'][] = $owner_id;
  27. continue;
  28. }
  29. $logistic_relation = $relation_arr[$owner_id] ?? [];
  30. foreach ($data['logistics'] as $logistic_id => $logistic) {
  31. if ($logistic['isActivation'] == false){
  32. // 未激活
  33. if(array_key_exists($logistic_id,$logistic_relation)){
  34. $delete_items['items'][] = [
  35. 'owner_id' => $owner_id,
  36. 'logistic_id' => $logistic_id
  37. ];
  38. }
  39. } else if($logistic['isActivation'] == true){
  40. // 激活
  41. if (!array_key_exists($logistic_id,$logistic_relation)){
  42. $params[] = [
  43. 'owner_name'=>$data['name'],
  44. 'logistic_name'=>$logistic['name'],
  45. 'owner_id' => $owner_id,
  46. 'logistic_id' => $logistic_id,
  47. 'print_template_id' => $printTemplateId,
  48. ];
  49. }
  50. }
  51. }
  52. }
  53. if ($delete_items['owner_id']) OwnerLogisticPrintTemplate::query()->whereIn('owner_id', $delete_items['owner_id'])->where('print_template_id', $printTemplateId)->delete();
  54. if ($delete_items['items']) {
  55. foreach ($delete_items['items'] as $item) {
  56. OwnerLogisticPrintTemplate::query()->where('owner_id', $item['owner_id'])->where('logistic_id', $item['logistic_id'])->delete();
  57. }
  58. }
  59. if (!$params) return;
  60. OwnerLogisticPrintTemplate::query()->insert($params);
  61. }
  62. public function getParts(): array
  63. {
  64. return [
  65. [
  66. 'name' => '背景',
  67. 'value' => $this->getBg()
  68. ],
  69. [
  70. 'name' => '文本框',
  71. 'value' => $this->getTextBox()
  72. ],
  73. [
  74. 'name' => '条纹码',
  75. 'value' => $this->getStripeCode()
  76. ],
  77. [
  78. 'name' => '二维码',
  79. 'value' => $this->getQrCode()
  80. ],
  81. [
  82. 'name' => '图片',
  83. 'value' => $this->getImage()
  84. ]
  85. ];
  86. }
  87. private function getTextBox(): array
  88. {
  89. return [
  90. 'type' => 'textBox',
  91. 'border-style' => 'none',
  92. 'border-width' => 1,
  93. 'font-size' => 12,
  94. 'width' => 250,
  95. 'height' => 50,
  96. 'left' => '',
  97. 'top' => '',
  98. 'white-space' => 'pre',
  99. 'justify-content' => 'center',
  100. // flex-start 开头|flex-end 结尾|center 居中|space-between|space-around|initial|inherit;
  101. 'align-items' => 'center'
  102. // stretch 拉伸平铺|center居中|flex-start 容器开头|flex-end容器结尾|baseline容器基线|initial|inherit
  103. ];
  104. }
  105. private function getStripeCode(): array
  106. {
  107. return [
  108. 'type' => 'stripeCode',
  109. 'width' => 404,
  110. 'left' => 100,
  111. 'top' => 0,
  112. 'scale' => 1,
  113. ];
  114. }
  115. private function getBg(): array
  116. {
  117. return [
  118. 'type' => 'bg',
  119. 'width' => 600,
  120. 'height' => 900,
  121. ];
  122. }
  123. private function getQrCode(): array
  124. {
  125. return [
  126. 'type' => 'qRCode',
  127. 'width' => 100,
  128. 'height' => 100,
  129. 'left' => 0,
  130. 'top' => 0,
  131. ];
  132. }
  133. private function getImage(): array
  134. {
  135. return [
  136. 'type' => 'image',
  137. 'width' => 100,
  138. 'height' => 100,
  139. 'left' => 0,
  140. 'top' => 0,
  141. 'value' => 'none',
  142. 'scale' => 1,
  143. ];
  144. }
  145. }