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