CustomerSpec.php 5.2 KB

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