CustomerSpec.php 5.2 KB

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