CustomerSpec.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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->assign('fastmailList', explode("\n", site_config('addonsd.fast_mail')));
  31. $this->relationField=['customer'];
  32. }
  33. //查看
  34. #[Route("GET,JSON","index")]
  35. public function index()
  36. {
  37. return $this->_index();
  38. }
  39. /**
  40. * 添加
  41. */
  42. #[Route('GET,POST','add')]
  43. public function add()
  44. {
  45. if (false === $this->request->isPost()) {
  46. $ids =$this->request->get('ids/d', 0);
  47. $rows = $this->model->where('customer_id', $ids)->where('status', 1)->select();
  48. $this->assign('ids', $ids);
  49. $this->assign('row', $rows);
  50. return $this->fetch();
  51. }
  52. $params = $this->request->post();
  53. if(empty($params['ids']) || count($params['all_data']) == 0) return resp_json(0,'请填写完整信息');
  54. $result = 0;
  55. Db::startTrans();
  56. try {
  57. //状态设置为0
  58. $this->model->where('customer_id', $params['ids'])->save(['status'=>0]);
  59. //然后根据选中的规格ID逐个判断是否存在,存在的更新状态、包装箱及发货价,不存在的则创建。 type_id
  60. foreach ($params['all_data'] as $item) {
  61. $row = $this->model
  62. ->where('customer_id',$params['ids'])
  63. ->where('type_id',$item['type_id'])
  64. ->where('product_id',$item['value'])
  65. ->findOrEmpty();
  66. if($row->isEmpty()){
  67. $this->model->create([
  68. 'customer_id' => $params['ids'],
  69. 'type_id' => $item['type_id'],
  70. 'product_id' => $item['value'],
  71. 'box_id' => $item['packet']??0,
  72. 'price' => $item['price']??0,
  73. 'fast_mail' => $item['fast_mail']
  74. ]);
  75. $result++;
  76. }else{
  77. $row->box_id = $item['packet']??0;
  78. $row->price = $item['price']??0;
  79. $row->fast_mail = $item['fast_mail']??0;
  80. $row->status = 1;
  81. $row->save();
  82. $result++;
  83. }
  84. }
  85. if($this->callback){
  86. $callback=$this->callback;
  87. $callback($this->model);
  88. }
  89. Db::commit();
  90. } catch (\Exception $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. }
  94. return resp_json(200,'操作成功');
  95. }
  96. //删除
  97. #[Route("GET,POST","del")]
  98. public function del()
  99. {
  100. //通过定义callback回调函数来执行删除后的操作
  101. $this->callback=function ($ids){};
  102. return $this->_del();
  103. }
  104. //更新
  105. #[Route("GET,POST","multi")]
  106. public function multi()
  107. {
  108. //通过定义callback回调函数来执行更新后的操作
  109. $this->callback=function ($ids,$field,$value){};
  110. return $this->_multi();
  111. }
  112. #[Route('GET','get_box')]
  113. public function get_box()
  114. {
  115. if($this->request->isAjax()){
  116. $type_id = $this->request->get('type_id/d', 0);
  117. if(empty($type_id)) return resp_json(0, '参数错误');
  118. $list = ProductConfig::where('type_id', $type_id)->order('sort desc')->field('id, title')->select();
  119. return json($list, 200);
  120. }
  121. }
  122. }