PrinterTemplateService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\sys;
  12. use app\model\sys\SysPrinterTemplate;
  13. use app\service\core\printer\CorePrinterService;
  14. use core\base\BaseAdminService;
  15. use core\exception\CommonException;
  16. /**
  17. * 小票打印模板服务层
  18. * Class PrinterTemplateService
  19. * @package app\service\admin\sys
  20. */
  21. class PrinterTemplateService extends BaseAdminService
  22. {
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. $this->model = new SysPrinterTemplate();
  27. }
  28. /**
  29. * 获取小票打印模板分页列表
  30. * @param array $where
  31. * @return array
  32. * @throws \think\db\exception\DbException
  33. */
  34. public function getPage(array $where = [])
  35. {
  36. $field = 'template_id,template_type,template_name,value,create_time';
  37. $order = 'create_time desc';
  38. $search_model = $this->model->where([ [ 'site_id', "=", $this->site_id ] ])->withSearch([ "template_id", "template_type", "template_name" ], $where)->field($field)->order($order)->append([ 'template_type_name' ]);
  39. $list = $this->pageQuery($search_model);
  40. return $list;
  41. }
  42. /**
  43. * 获取小票打印模板列表
  44. * @param array $where
  45. * @param string $field
  46. * @return array
  47. * @throws \think\db\exception\DbException
  48. */
  49. public function getList(array $where = [], $field = 'template_id,template_type,template_name,value,create_time')
  50. {
  51. $order = 'create_time desc';
  52. return $this->model->where([ [ 'site_id', "=", $this->site_id ] ])->withSearch([ "template_id", "template_type", "template_name" ], $where)->field($field)->order($order)->append([ 'template_type_name' ])->select()->toArray();
  53. }
  54. /**
  55. * 获取小票打印模板信息
  56. * @param int $id
  57. * @return array
  58. */
  59. public function getInfo(int $id)
  60. {
  61. $field = 'template_id,site_id,template_type,template_name,value';
  62. $info = $this->model->field($field)->where([ [ 'template_id', "=", $id ] ])->findOrEmpty()->toArray();
  63. return $info;
  64. }
  65. /**
  66. * 添加小票打印模板
  67. * @param array $data
  68. * @return mixed
  69. */
  70. public function add(array $data)
  71. {
  72. $data[ 'site_id' ] = $this->site_id;
  73. $res = $this->model->create($data);
  74. return $res->template_id;
  75. }
  76. /**
  77. * 小票打印模板编辑
  78. * @param int $id
  79. * @param array $data
  80. * @return bool
  81. */
  82. public function edit(int $id, array $data)
  83. {
  84. $this->model->where([ [ 'template_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
  85. return true;
  86. }
  87. /**
  88. * 删除小票打印模板
  89. * @param int $id
  90. * @return bool
  91. */
  92. public function del(int $id)
  93. {
  94. // 检测要删除的模板有没有被打印机使用
  95. $field = 'template_type';
  96. $info = $this->model->field($field)->where([ [ 'template_id', "=", $id ] ])->findOrEmpty()->toArray();
  97. $core_printer_service = new CorePrinterService();
  98. $printer_list = $core_printer_service->getList([
  99. [ 'site_id', '=', $this->site_id ],
  100. [ 'template_type', 'like', '%"' . $info[ 'template_type' ] . '"%' ]
  101. ], 'printer_id,printer_name,value');
  102. if (!empty($printer_list)) {
  103. foreach ($printer_list as $k => $v) {
  104. if (!empty($v[ 'value' ])) {
  105. foreach ($v[ 'value' ] as $value_k => $value_v) {
  106. foreach ($value_v as $trigger_k => $trigger_v) {
  107. if ($trigger_v[ 'template_id' ] == $id) {
  108. throw new CommonException("该模板已被打印机 [{$v['printer_name']}] 使用,无法删除");
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. $model = $this->model->where([ [ 'template_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->find();
  117. $res = $model->delete();
  118. return $res;
  119. }
  120. }