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