CustomerSpec.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\shop;
  4. use app\common\controller\Backend;
  5. use app\admin\traits\Actions;
  6. use think\annotation\route\Group;
  7. use think\annotation\route\Route;
  8. use app\common\model\Customer;
  9. use app\common\model\StockConfig;
  10. use think\facade\Db;
  11. use app\common\model\ProductConfig;
  12. use app\common\model\CustomerSpec as CustomerSpecModel;
  13. #[Group("shop/customer_spec")]
  14. class CustomerSpec extends Backend
  15. {
  16. use Actions{
  17. index as private _index;
  18. add as private _add;
  19. edit as private _edit;
  20. del as private _del;
  21. multi as private _multi;
  22. }
  23. protected function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new CustomerSpecModel();
  27. $this->assign('customerList', Customer::where('status', 1)->column('name','id'));
  28. $this->assign('fieldList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
  29. }
  30. //查看
  31. #[Route("GET,JSON","index")]
  32. public function index()
  33. {
  34. return $this->_index();
  35. }
  36. /**
  37. * 添加
  38. */
  39. #[Route('GET,POST','add')]
  40. public function add()
  41. {
  42. if (false === $this->request->isPost()) {
  43. return $this->fetch();
  44. }
  45. $customer_id =$this->request->post('customer_id/d', 0);
  46. $type_box =$this->request->post('type_box');
  47. $type_list =$this->request->post('type_list');
  48. if(empty($customer_id) || empty($type_box) || empty($type_list)) return resp_json(0,'请填写完整信息');
  49. $specsList = [];
  50. foreach ($type_box as $item) {
  51. if(empty($item['price'])) return resp_json(0, '请填写发货价格');
  52. $specsList[] = ['type_id'=> $item['type_id'], 'type_name'=> $item['type_name'],'specs_id'=>$item['value'],'specs_name'=>$item['name'], 'price'=> $item['price']];
  53. }
  54. if($this->model->where('customer_id', $customer_id)->count()>0) return resp_json(0,'客户信息已存在');
  55. $result = false;
  56. Db::startTrans();
  57. try {
  58. $result = $this->model->save([
  59. 'customer_id'=> $customer_id,
  60. 'variety' => json_encode($type_list, JSON_UNESCAPED_UNICODE),
  61. 'specs' => json_encode($type_box, JSON_UNESCAPED_UNICODE),
  62. ]);
  63. if($this->callback){
  64. $callback=$this->callback;
  65. $callback($this->model);
  66. }
  67. Db::commit();
  68. } catch (\Exception $e) {
  69. Db::rollback();
  70. $this->error($e->getMessage());
  71. }
  72. if ($result === false) {
  73. $this->error(__('没有新增任何数据'));
  74. }
  75. return resp_json(200,'操作成功');
  76. }
  77. /**
  78. * 编辑
  79. */
  80. #[Route('GET,POST','edit')]
  81. public function edit(mixed $row=null)
  82. {
  83. if (false === $this->request->isPost()) {
  84. $ids = $this->request->get('ids');
  85. if(!$row || is_array($row)){
  86. $row = $this->model->find($ids);
  87. }
  88. if (!$row) {
  89. $this->error(__('没有找到记录'));
  90. }
  91. $row->customer_id = (string)$row->customer_id;
  92. $row->specs = json_decode($row->specs, true);
  93. $row->variety = json_decode($row->variety, true);
  94. $this->assign('row', $row);
  95. return $this->fetch();
  96. }
  97. $params = $this->request->post("");
  98. if(empty($params['ids']) || empty($params['type_box']) || empty($params['type_list'])) return resp_json(0,'请填写完整信息');
  99. $specsList = [];
  100. foreach ($params['type_box'] as $item) {
  101. if(empty($item['price'])) return resp_json(0, '请填写发货价格');
  102. $specsList[] = ['type_id'=> $item['type_id'], 'type_name'=> $item['type_name'],'specs_id'=>$item['value'],'specs_name'=>$item['name'], 'price'=> $item['price']];
  103. }
  104. $result = false;
  105. Db::startTrans();
  106. try {
  107. $result = $this->model->where('id', $params['ids'])->save([
  108. 'variety' => json_encode($params['type_list'], JSON_UNESCAPED_UNICODE),
  109. 'specs' => json_encode($params['type_box'], JSON_UNESCAPED_UNICODE),
  110. ]);
  111. if($this->callback){
  112. $callback=$this->callback;
  113. $callback($row);
  114. }
  115. Db::commit();
  116. } catch (\Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if (false === $result) {
  121. $this->error(__('没有数据被更新'));
  122. }
  123. return resp_json(200,'操作成功');
  124. }
  125. //删除
  126. #[Route("GET,POST","del")]
  127. public function del()
  128. {
  129. //通过定义callback回调函数来执行删除后的操作
  130. $this->callback=function ($ids){};
  131. return $this->_del();
  132. }
  133. //更新
  134. #[Route("GET,POST","multi")]
  135. public function multi()
  136. {
  137. //通过定义callback回调函数来执行更新后的操作
  138. $this->callback=function ($ids,$field,$value){};
  139. return $this->_multi();
  140. }
  141. #[Route('GET','get_box')]
  142. public function get_box()
  143. {
  144. if($this->request->isAjax()){
  145. $type_id = $this->request->get('type_id/d', 0);
  146. if(empty($type_id)) return resp_json(0, '参数错误');
  147. $list = ProductConfig::where('type_id', $type_id)->order('sort desc')->field('id, title')->select();
  148. return json($list, 200);
  149. }
  150. }
  151. }