Synthesis.php 3.8 KB

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