CustomerSpec.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\StockConfig;
  9. use think\facade\Db;
  10. use app\common\model\ProductConfig;
  11. use app\common\model\CustomerSpec as CustomerSpecModel;
  12. #[Group("shop/customer_spec")]
  13. class CustomerSpec extends Backend
  14. {
  15. use Actions{
  16. index as private _index;
  17. add as private _add;
  18. edit as private _edit;
  19. del as private _del;
  20. multi as private _multi;
  21. }
  22. protected function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new CustomerSpecModel();
  26. $this->assign('typeList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
  27. $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title','id'));
  28. $this->relationField=['customer'];
  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. $ids =$this->request->get('ids/d', 0);
  44. $rows = $this->model->where('customer_id', $ids)->where('status', 1)->select();
  45. $this->assign('ids', $ids);
  46. $this->assign('row', $rows);
  47. return $this->fetch();
  48. }
  49. $params = $this->request->post();
  50. if(empty($params['ids']) || count($params['all_data']) == 0) return resp_json(0,'请填写完整信息');
  51. $result = 0;
  52. Db::startTrans();
  53. try {
  54. //状态设置为0
  55. $this->model->where('customer_id', $params['ids'])->save(['status'=>0]);
  56. //然后根据选中的规格ID逐个判断是否存在,存在的更新状态、包装箱及发货价,不存在的则创建。 type_id
  57. foreach ($params['all_data'] as $item) {
  58. $row = $this->model
  59. ->where('customer_id',$params['ids'])
  60. ->where('type_id',$item['type_id'])
  61. ->where('product_id',$item['value'])
  62. ->findOrEmpty();
  63. if($row->isEmpty()){
  64. $this->model->create([
  65. 'customer_id' => $params['ids'],
  66. 'type_id' => $item['type_id'],
  67. 'product_id' => $item['value'],
  68. 'box_id' => $item['packet']??0,
  69. 'price' => $item['price']??0,
  70. 'fast_mail' => $item['fast_mail']
  71. ]);
  72. $result++;
  73. }else{
  74. $row->box_id = $item['packet']??0;
  75. $row->price = $item['price']??0;
  76. $row->fast_mail = $item['fast_mail']??0;
  77. $row->status = 1;
  78. $row->save();
  79. $result++;
  80. }
  81. }
  82. if($this->callback){
  83. $callback=$this->callback;
  84. $callback($this->model);
  85. }
  86. Db::commit();
  87. } catch (\Exception $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. }
  91. return resp_json(200,'操作成功');
  92. }
  93. //删除
  94. #[Route("GET,POST","del")]
  95. public function del()
  96. {
  97. //通过定义callback回调函数来执行删除后的操作
  98. $this->callback=function ($ids){};
  99. return $this->_del();
  100. }
  101. //更新
  102. #[Route("GET,POST","multi")]
  103. public function multi()
  104. {
  105. //通过定义callback回调函数来执行更新后的操作
  106. $this->callback=function ($ids,$field,$value){};
  107. return $this->_multi();
  108. }
  109. #[Route('GET','get_box')]
  110. public function get_box()
  111. {
  112. if($this->request->isAjax()){
  113. $type_id = $this->request->get('type_id/d', 0);
  114. if(empty($type_id)) return resp_json(0, '参数错误');
  115. $list = ProductConfig::where('type_id', $type_id)->order('sort desc')->field('id, title')->select();
  116. return json($list, 200);
  117. }
  118. }
  119. }