CustomerSpec.php 5.4 KB

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