CustomerSpec.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 'fast_mail' => $item['fast_mail']
  73. ]);
  74. $result++;
  75. }else{
  76. $row->box_id = $item['packet']??0;
  77. $row->price = $item['price']??0;
  78. $row->fast_mail = $item['fast_mail']??0;
  79. $row->status = 1;
  80. $row->save();
  81. $result++;
  82. }
  83. }
  84. if($this->callback){
  85. $callback=$this->callback;
  86. $callback($this->model);
  87. }
  88. Db::commit();
  89. } catch (\Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. return resp_json(200,'操作成功');
  94. }
  95. //删除
  96. #[Route("GET,POST","del")]
  97. public function del()
  98. {
  99. //通过定义callback回调函数来执行删除后的操作
  100. $this->callback=function ($ids){};
  101. return $this->_del();
  102. }
  103. //更新
  104. #[Route("GET,POST","multi")]
  105. public function multi()
  106. {
  107. //通过定义callback回调函数来执行更新后的操作
  108. $this->callback=function ($ids,$field,$value){};
  109. return $this->_multi();
  110. }
  111. #[Route('GET','get_box')]
  112. public function get_box()
  113. {
  114. if($this->request->isAjax()){
  115. $type_id = $this->request->get('type_id/d', 0);
  116. if(empty($type_id)) return resp_json(0, '参数错误');
  117. $list = ProductConfig::where('type_id', $type_id)->order('sort desc')->field('id, title')->select();
  118. return json($list, 200);
  119. }
  120. }
  121. }