Synthesis.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductLists;
  5. use app\common\model\ProductOrder;
  6. use app\common\model\Synthesis as SynthesisModel;
  7. use Exception;
  8. use app\api\logic\WelfareLoginc;
  9. use app\common\model\UserModel;
  10. use think\Db;
  11. use think\Log;
  12. //合成管理
  13. class Synthesis extends Api
  14. {
  15. protected string $lan = '';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->lan = $this->request->getLan();
  20. }
  21. public function list(SynthesisModel $synthesis)
  22. {
  23. $list = $synthesis->alias('a')
  24. ->join("product_list b", "a.product_id = b.id", "left")
  25. ->field('a.id,a.title,a.start_time,a.end_time,b.thum as img_url')
  26. ->where('a.status', $synthesis::Normal)->where('a.to_lang', $this->lan)
  27. ->order('a.weigh desc')
  28. ->paginate($this->pageSize);
  29. $this->success('', $list);
  30. }
  31. /*
  32. * 合成详情
  33. */
  34. public function detail(ProductLists $productLists, ProductOrder $productOrder)
  35. {
  36. $id = $this->request->param('id', 0, 'intval');
  37. if(empty($id)) $this->error(__("参数有误,无可用产品"));
  38. $lan = $this->request->getLan();
  39. //合成详情
  40. $synthesis = SynthesisModel::getBySynthesis($id, $lan);
  41. if (!empty($synthesis)) {
  42. //条件一
  43. $material_one = $productLists->getBySynthesisProduct($synthesis->material_one, $lan);
  44. $synthesis->material_one = $productOrder->getByOrderProductNum($material_one, $synthesis->material_one_num, $this->auth->id);
  45. //条件二
  46. $material_two = $productLists->getBySynthesisProduct($synthesis->material_two, $lan);
  47. $synthesis->material_two = $productOrder->getByOrderProductNum($material_two, $synthesis->material_one_num, $this->auth->id);
  48. //条件三
  49. $material_three = $productLists->getBySynthesisProduct($synthesis->material_three, $lan);
  50. $synthesis->material_three = $productOrder->getByOrderProductNum($material_three, $synthesis->material_one_num, $this->auth->id);
  51. }
  52. $this->success('', $synthesis);
  53. }
  54. /*
  55. * 合成
  56. */
  57. public function create( ProductOrder $productOrder, UserModel $userModel)
  58. {
  59. $id = $this->request->post('id', 0, 'intval');
  60. $num = $this->request->post('num', 0, 'intval');
  61. $productList= $this->request->param('product_list/a'); //选中产品
  62. if(empty($id) || empty($num)) $this->error(__("参数有误,无可用产品"));
  63. //合成详情
  64. $synthesis = SynthesisModel::get($id);
  65. if (empty($synthesis)) $this->error(__("合成活动不存在"));
  66. $time = time();
  67. if ($synthesis->start_time > $time || $synthesis->end_time < $time)$this->error(__("合成活动已结束"));
  68. if ($synthesis->num + $num > $synthesis->stock)$this->error(__("库存不足"));
  69. Db::startTrans();
  70. try {
  71. // 合成订单
  72. $productOrder->setSynthesisOrder($synthesis, $productList, $num, $this->auth->id);
  73. // 添加新订单
  74. $result = WelfareLoginc::setUserWelfareLos($this->auth->id, $synthesis->product_id, $num, time(), $this->lan, $productOrder::Synthesi);
  75. //添加茶数量
  76. if($result['price'] > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, $num, '+');
  77. $synthesis->num += $num;
  78. $synthesis->save();
  79. // 提交事务
  80. Db::commit();
  81. } catch (Exception $e) {
  82. // 回滚事务
  83. Db::rollback();
  84. $this->error($e->getMessage(), null, $e->getCode());
  85. }
  86. $this->success('ok');
  87. }
  88. }