Programme.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Goods;
  5. use app\common\model\GoodsInstall;
  6. use app\common\model\GoodsInstallLink;
  7. use app\common\model\GoodsSku;
  8. use app\common\model\OrderInfo;
  9. use app\common\model\Refund;
  10. use app\common\service\InstallService;
  11. use app\common\service\OrderService;
  12. use think\Db;
  13. use app\common\model\Orders as Order;
  14. /**
  15. * 快速报价接口
  16. * @inheritdoc
  17. */
  18. class Programme extends Api
  19. {
  20. protected $noNeedRight="*";
  21. /**
  22. * 预估报价
  23. * @ApiMethod (POST)
  24. * @ApiParams (name=sku,description="key为规格id,对象:num购买数量,num_install安装数量,{1:{num:1,num_install:2}})
  25. * ApiReturnParams (name=logo,description=logo)
  26. * @ApiReturnParams (name=name,description=商品名称)
  27. * @ApiReturnParams (name=sku,description=规格)
  28. * @ApiReturnParams (name=amount,description=单价)
  29. * @ApiReturnParams (name=amount_install,description=安装费)
  30. * @ApiReturnParams (name=amount_total,description=共计)
  31. *@ApiReturnParams (name=total,description=预估报价)
  32. * @ApiReturnParams (name=num,description=采购数量)
  33. * @ApiReturnParams (name=num_install,description=安装数量)
  34. * @ApiReturnParams (name=category_id,description=商品分类id)
  35. */
  36. public function calculate(){
  37. $sku = input('sku');
  38. if(!$sku){
  39. $this->error('请传商品信息');
  40. }
  41. $sku = htmlspecialchars_decode($sku);
  42. $sku = json_decode($sku,true);
  43. $goods = [];
  44. $goodsModel = new Goods();
  45. $goodsSkuModel = new GoodsSku();
  46. $total = 0;
  47. foreach ($sku as $k=>$v){
  48. $goodsSku = $goodsSkuModel->where('id',$k)->find();
  49. $nowGoods = $goodsModel->where('id',$goodsSku['goods_id'])->find();
  50. $goods[$k]['logo'] = $nowGoods['logo'];
  51. $goods[$k]['name'] = $nowGoods['name'];
  52. $goods[$k]['sku'] = $goodsSku['name'];
  53. $goods[$k]['num'] = $v['num'];
  54. $goods[$k]['category_id'] = $nowGoods['category_id'];
  55. $goods[$k]['num_install'] = $v['num_install'];
  56. $goods[$k]['amount'] = $goodsSku['amount'];//单价
  57. $goods[$k]['amount_install']=InstallService::getFee([$k=>$v]);//安装费
  58. $goods[$k]['amount_total']=bcmul($goodsSku['amount'],$v['num']);
  59. $goods[$k]['amount_total']=bcadd($goods[$k]['amount_total'],$goods[$k]['amount_install']);
  60. $total+=$goods[$k]['amount_total'];
  61. }
  62. $goods = array_values($goods);
  63. $result = ['goods'=>$goods,'total'=>$total];
  64. $this->success('',$result);
  65. }
  66. /**
  67. * 保存方案
  68. * @ApiMethod (POST)
  69. * @ApiParams (name=sku,description="key为规格id,对象:num购买数量,num_install安装数量,{1:{num:1,num_install:2}})
  70. * @ApiParams (name=name,description=方案名称)
  71. */
  72. public function submit(){
  73. $data=input();
  74. $this->validate($data,[
  75. 'sku|商品'=>['require','min:1'],
  76. 'name|方案名称'=>['require','max:100'],
  77. ]);
  78. Db::startTrans();
  79. $user=$this->auth->getUser();
  80. $data['createtime'] = time();
  81. $data['user_id'] = $user['id'];
  82. $reault = Db::name('Programme')->strict(false)->insert($data);
  83. $id = Db::name('Programme')->getLastInsID();
  84. $sku = htmlspecialchars_decode($data['sku']);
  85. $sku = json_decode($sku,true);
  86. $goods = [];
  87. $goodsModel = new Goods();
  88. $goodsSkuModel = new GoodsSku();
  89. $total = 0;
  90. foreach ($sku as $k=>$v){
  91. $goodsSku = $goodsSkuModel->where('id',$k)->find();
  92. $nowGoods = $goodsModel->where('id',$goodsSku['goods_id'])->find();
  93. $goods[$k]['programme_id'] = $id;
  94. $goods[$k]['goods_id'] = $goodsSku['goods_id'];
  95. $goods[$k]['sku_id'] = $k;
  96. $goods[$k]['logo'] = implode(',',$nowGoods['logo']);
  97. $goods[$k]['name'] = $nowGoods['name'];
  98. $goods[$k]['sku'] = $goodsSku['name'];
  99. $goods[$k]['num'] = $v['num'];
  100. $goods[$k]['amount'] = $goodsSku['amount'];//单价
  101. $goods[$k]['amount_install']=InstallService::getFee([$k=>$v]);//安装费
  102. $goods[$k]['amount_total']=bcmul($goodsSku['amount'],$v['num']);
  103. $goods[$k]['amount_total']=bcadd($goods[$k]['amount_total'],$goods[$k]['amount_install']);
  104. $total+=$goods[$k]['amount_total'];
  105. }
  106. $goods = array_values($goods);
  107. $goodsResult = Db::name('ProgrammeGoods')->strict(false)->insertAll($goods);
  108. $totalResult = Db::name('Programme')->where('id',$id)->setField('amount_total',$total);
  109. if(!$reault || !$goodsResult || !$totalResult){
  110. Db::rollback();
  111. $this->error('保存失败');
  112. }
  113. Db::commit();
  114. $this->success('保存成功');
  115. }
  116. /**
  117. * 用户方案列表
  118. * @ApiParams (name=page,description=分页)
  119. * @ApiParams (name=limit,description=分页)
  120. *ApiReturnParams (name=id,description=方案id)
  121. * ApiReturnParams (name=name,description=方案名称)
  122. * ApiReturnParams (name=createtime,description=创建时间)
  123. */
  124. public function lists(){
  125. $user=$this->auth->getUser();
  126. $lists=$user->programmes()
  127. ->order('id','desc')
  128. ->paginate(input('limit',15));
  129. $this->success('',$lists);
  130. }
  131. /**
  132. * 查看方案
  133. * @ApiParams (name=id,description=方案id)
  134. * @ApiParams (name=page,description=分页)
  135. * @ApiParams(name=limit,description=分页)
  136. * ApiReturnParams (name=goods_id,description=商品id)
  137. * ApiReturnParams (name=logo,description=logo)
  138. * ApiReturnParams (name=name,description=商品名称)
  139. * @ApiReturnParams (name=sku,description=规格)
  140. * @ApiReturnParams (name=amount,description=单价)
  141. * @ApiReturnParams (name=amount_install,description=安装费)
  142. * ApiReturnParams (name=createtime,description=创建时间)
  143. */
  144. public function detail(){
  145. $id = input('id');
  146. $user=$this->auth->getUser();
  147. $lists= Db('ProgrammeGoods')
  148. ->field('goods_id,logo,name,sku,amount,num,amount_install')
  149. ->where('programme_id',$id)
  150. ->order('id','desc')
  151. ->paginate(input('limit',15));
  152. $data = $lists->toArray();
  153. $list = $data['data'];
  154. foreach ($list as &$v){
  155. $v['logo'] = explode(',', $v['logo']);
  156. }
  157. $data['data'] = $list;
  158. $this->success('',$data);
  159. }
  160. /**
  161. * 删除方案
  162. *@ApiParams (name=id,description=方案id)
  163. */
  164. public function delete(){
  165. $id = input('id');
  166. Db('Programme')->where('id',$id)->delete();
  167. Db('ProgrammeGoods')->where('programme_id',$id)->delete();
  168. $this->success('删除成功');
  169. }
  170. /**
  171. * 删除方案商品
  172. *@ApiParams (name=id,description=方案id)
  173. * @ApiParams (name=goods_ids,description=商品id,删除多个商品用逗号分割)
  174. */
  175. public function deleteGoods(){
  176. $id = input('id');
  177. $goodsId = input('goods_ids');
  178. Db('ProgrammeGoods')->where('programme_id',$id)->where('goods_id','in',$goodsId)->delete();
  179. if(!Db('ProgrammeGoods')->where('programme_id',$id)->find()){
  180. Db('Programme')->where('id',$id)->delete();
  181. }
  182. $this->success('删除成功');
  183. }
  184. /**
  185. * 方案详情
  186. * @ApiParams (name=id,description=方案id)
  187. * ApiReturnParams (name=goods_id,description=商品id)
  188. * ApiReturnParams (name=logo,description=logo)
  189. * ApiReturnParams (name=name,description=商品名称)
  190. * @ApiReturnParams (name=sku,description=规格)
  191. * @ApiReturnParams (name=amount,description=单价)
  192. * @ApiReturnParams (name=amount_install,description=安装费)
  193. * ApiReturnParams (name=createtime,description=创建时间)
  194. */
  195. public function info(){
  196. $id = input('id');
  197. $goods = Db('ProgrammeGoods')->where('programme_id',$id)->select();
  198. $goodsIds = Db('ProgrammeGoods')->where('programme_id',$id)->column('goods_id');
  199. $left = [];
  200. foreach ($goodsIds as $k=>$v){
  201. $nowGoods = Db('ProgrammeGoods')->where('goods_id',$v)->where('programme_id',$id)->find();
  202. $categoryId = Db('Goods')->where('id',$v)->value('category_id');
  203. $cate = DB('category')->field('id,name')->where('id',$categoryId)->find();
  204. if(empty($left[$cate['id']])){
  205. $left[$cate['id']] = $cate;
  206. }
  207. array_push($left[$cate['id']],$nowGoods);
  208. }
  209. $left = array_values($left);
  210. dump($left);
  211. }
  212. }