Programme.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * ApiReturnParams (name=sku_id,description=规格低)
  124. * ApiReturnParams (name=sku,description=规格名称)
  125. * ApiReturnParams (name=num,description=数量)
  126. */
  127. public function lists(){
  128. $user=$this->auth->getUser();
  129. $lists=$user->programmes()
  130. ->order('id','desc')
  131. ->paginate(input('limit',15));
  132. $data = $lists->toArray();
  133. $list = $data['data'];
  134. foreach ($list as &$v){
  135. $v['goods'] = Db('ProgrammeGoods')->field('sku_id,sku,num')->where('programme_id',$v['id'])->select();
  136. }
  137. $data['data'] = $list;
  138. $this->success('',$data);
  139. }
  140. /**
  141. * 查看方案
  142. * @ApiParams (name=id,description=方案id)
  143. * @ApiParams (name=page,description=分页)
  144. * @ApiParams(name=limit,description=分页)
  145. * ApiReturnParams (name=goods_id,description=商品id)
  146. * ApiReturnParams (name=logo,description=logo)
  147. * ApiReturnParams (name=name,description=商品名称)
  148. * @ApiReturnParams (name=sku,description=规格)
  149. * @ApiReturnParams (name=amount,description=单价)
  150. * @ApiReturnParams (name=amount_install,description=安装费)
  151. * ApiReturnParams (name=createtime,description=创建时间)
  152. */
  153. public function detail(){
  154. $id = input('id');
  155. $user=$this->auth->getUser();
  156. $lists= Db('ProgrammeGoods')
  157. ->field('goods_id,logo,name,sku,amount,num,amount_install')
  158. ->where('programme_id',$id)
  159. ->order('id','desc')
  160. ->paginate(input('limit',15));
  161. $data = $lists->toArray();
  162. $list = $data['data'];
  163. foreach ($list as &$v){
  164. $v['logo'] = explode(',', $v['logo']);
  165. }
  166. $data['data'] = $list;
  167. $this->success('',$data);
  168. }
  169. /**
  170. * 删除方案
  171. *@ApiParams (name=id,description=方案id)
  172. */
  173. public function delete(){
  174. $id = input('id');
  175. Db('Programme')->where('id',$id)->delete();
  176. Db('ProgrammeGoods')->where('programme_id',$id)->delete();
  177. $this->success('删除成功');
  178. }
  179. /**
  180. * 删除方案商品
  181. *@ApiParams (name=id,description=方案id)
  182. * @ApiParams (name=goods_ids,description=商品id,删除多个商品用逗号分割)
  183. */
  184. public function deleteGoods(){
  185. $id = input('id');
  186. $goodsId = input('goods_ids');
  187. Db('ProgrammeGoods')->where('programme_id',$id)->where('goods_id','in',$goodsId)->delete();
  188. if(!Db('ProgrammeGoods')->where('programme_id',$id)->find()){
  189. Db('Programme')->where('id',$id)->delete();
  190. }
  191. $this->success('删除成功');
  192. }
  193. /**
  194. * 方案详情
  195. * @ApiParams (name=id,description=方案id)
  196. * ApiReturnParams (name=goods_id,description=商品id)
  197. * ApiReturnParams (name=logo,description=logo)
  198. * ApiReturnParams (name=name,description=商品名称)
  199. * @ApiReturnParams (name=sku,description=规格)
  200. * @ApiReturnParams (name=amount,description=单价)
  201. * @ApiReturnParams (name=amount_install,description=安装费)
  202. * ApiReturnParams (name=createtime,description=创建时间)
  203. */
  204. public function info(){
  205. $id = input('id');
  206. $goods = Db('ProgrammeGoods')->where('programme_id',$id)->select();
  207. $goodsIds = Db('ProgrammeGoods')->where('programme_id',$id)->column('goods_id');
  208. $left = [];
  209. foreach ($goodsIds as $k=>$v){
  210. $nowGoods = Db('ProgrammeGoods')->where('goods_id',$v)->where('programme_id',$id)->find();
  211. $categoryId = Db('Goods')->where('id',$v)->value('category_id');
  212. $cate = DB('category')->field('id,name')->where('id',$categoryId)->find();
  213. if(empty($left[$cate['id']])){
  214. $left[$cate['id']] = $cate;
  215. }
  216. array_push($left[$cate['id']],$nowGoods);
  217. }
  218. $left = array_values($left);
  219. dump($left);
  220. }
  221. }