CustomerSpec.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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('typeList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
  29. $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->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. $ids =$this->request->get('ids/d', 0);
  46. $rows = $this->model->where('customer_id', $ids)->where('status', 1)->select();
  47. $this->assign('ids', $ids);
  48. $this->assign('row', $rows);
  49. return $this->fetch();
  50. }
  51. $params = $this->request->post();
  52. if(empty($params['ids']) || count($params['all_data']) == 0) return resp_json(0,'请填写完整信息');
  53. $result = 0;
  54. Db::startTrans();
  55. try {
  56. //状态设置为0
  57. $this->model->where('customer_id', $params['ids'])->save(['status'=>0]);
  58. //然后根据选中的规格ID逐个判断是否存在,存在的更新状态、包装箱及发货价,不存在的则创建。 type_id
  59. foreach ($params['all_data'] as $item) {
  60. $row = $this->model
  61. ->where('customer_id',$params['ids'])
  62. ->where('type_id',$item['type_id'])
  63. ->where('product_id',$item['value'])
  64. ->findOrEmpty();
  65. if($row->isEmpty()){
  66. $this->model->create([
  67. 'customer_id' => $params['ids'],
  68. 'type_id' => $item['type_id'],
  69. 'product_id' => $item['value'],
  70. 'box_id' => $item['packet']??0,
  71. 'price' => $item['price']??0,
  72. ]);
  73. $result++;
  74. }else{
  75. $row->box_id = $item['packet']??0;
  76. $row->price = $item['price']??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. }