Programme.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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]['sku_id'] = $k;
  54. $goods[$k]['num'] = $v['num'];
  55. $goods[$k]['category_id'] = $nowGoods['category_id'];
  56. $goods[$k]['num_install'] = $v['num_install'];
  57. $goods[$k]['amount'] = $goodsSku['amount'];//单价
  58. $goods[$k]['amount_install']=InstallService::getFee([$k=>$v]);//安装费
  59. $goods[$k]['amount_total']=bcmul($goodsSku['amount'],$v['num']);
  60. $goods[$k]['amount_total']=bcadd($goods[$k]['amount_total'],$goods[$k]['amount_install']);
  61. $total+=$goods[$k]['amount_total'];
  62. }
  63. $goods = array_values($goods);
  64. $result = ['goods'=>$goods,'total'=>$total];
  65. $this->success('',$result);
  66. }
  67. /**
  68. * 保存方案
  69. * @ApiMethod (POST)
  70. * @ApiParams (name=sku,description="key为规格id,对象:num购买数量,num_install安装数量,{1:{num:1,num_install:2}})
  71. * @ApiParams (name=name,description=方案名称)
  72. */
  73. public function submit(){
  74. $data=input();
  75. $this->validate($data,[
  76. 'sku|商品'=>['require','min:1'],
  77. 'name|方案名称'=>['require','max:100'],
  78. ]);
  79. Db::startTrans();
  80. $user=$this->auth->getUser();
  81. $data['createtime'] = time();
  82. $data['user_id'] = $user['id'];
  83. $reault = Db::name('Programme')->strict(false)->insert($data);
  84. $id = Db::name('Programme')->getLastInsID();
  85. $sku = htmlspecialchars_decode($data['sku']);
  86. $sku = json_decode($sku,true);
  87. $goods = [];
  88. $goodsModel = new Goods();
  89. $goodsSkuModel = new GoodsSku();
  90. $total = 0;
  91. foreach ($sku as $k=>$v){
  92. $goodsSku = $goodsSkuModel->where('id',$k)->find();
  93. $nowGoods = $goodsModel->where('id',$goodsSku['goods_id'])->find();
  94. $goods[$k]['programme_id'] = $id;
  95. $goods[$k]['goods_id'] = $goodsSku['goods_id'];
  96. $goods[$k]['sku_id'] = $k;
  97. $goods[$k]['logo'] = implode(',',$nowGoods['logo']);
  98. $goods[$k]['name'] = $nowGoods['name'];
  99. $goods[$k]['sku'] = $goodsSku['name'];
  100. $goods[$k]['num'] = $v['num'];
  101. $goods[$k]['amount'] = $goodsSku['amount'];//单价
  102. $goods[$k]['amount_install']=InstallService::getFee([$k=>$v]);//安装费
  103. $goods[$k]['amount_total']=bcmul($goodsSku['amount'],$v['num']);
  104. $goods[$k]['amount_total']=bcadd($goods[$k]['amount_total'],$goods[$k]['amount_install']);
  105. $total+=$goods[$k]['amount_total'];
  106. }
  107. $goods = array_values($goods);
  108. $goodsResult = Db::name('ProgrammeGoods')->strict(false)->insertAll($goods);
  109. $totalResult = Db::name('Programme')->where('id',$id)->setField('amount_total',$total);
  110. if(!$reault || !$goodsResult || !$totalResult){
  111. Db::rollback();
  112. $this->error('保存失败');
  113. }
  114. Db::commit();
  115. $this->success('保存成功');
  116. }
  117. /**
  118. * 用户方案列表
  119. * @ApiParams (name=page,description=分页)
  120. * @ApiParams (name=limit,description=分页)
  121. *ApiReturnParams (name=id,description=方案id)
  122. * ApiReturnParams (name=name,description=方案名称)
  123. * ApiReturnParams (name=createtime,description=创建时间)
  124. * ApiReturnParams (name=sku_id,description=规格低)
  125. * ApiReturnParams (name=sku,description=规格名称)
  126. * ApiReturnParams (name=num,description=数量)
  127. */
  128. public function lists(){
  129. $user=$this->auth->getUser();
  130. $lists=$user->programmes()
  131. ->order('id','desc')
  132. ->paginate(input('limit',15));
  133. $data = $lists->toArray();
  134. $list = $data['data'];
  135. foreach ($list as &$v){
  136. $v['goods'] = Db('ProgrammeGoods')->field('sku_id,sku,num')->where('programme_id',$v['id'])->select();
  137. }
  138. $data['data'] = $list;
  139. $this->success('',$data);
  140. }
  141. /**
  142. * 查看方案
  143. * @ApiParams (name=id,description=方案id)
  144. * @ApiParams (name=page,description=分页)
  145. * @ApiParams(name=limit,description=分页)
  146. * ApiReturnParams (name=goods_id,description=商品id)
  147. * ApiReturnParams (name=logo,description=logo)
  148. * ApiReturnParams (name=name,description=商品名称)
  149. * @ApiReturnParams (name=sku,description=规格)
  150. * @ApiReturnParams (name=amount,description=单价)
  151. * @ApiReturnParams (name=amount_install,description=安装费)
  152. * ApiReturnParams (name=createtime,description=创建时间)
  153. */
  154. public function detail(){
  155. $id = input('id');
  156. $user=$this->auth->getUser();
  157. $lists= Db('ProgrammeGoods')
  158. ->field('goods_id,logo,name,sku,amount,num,amount_install')
  159. ->where('programme_id',$id)
  160. ->order('id','desc')
  161. ->paginate(input('limit',15));
  162. $data = $lists->toArray();
  163. $list = $data['data'];
  164. foreach ($list as &$v){
  165. $v['logo'] = explode(',', $v['logo']);
  166. }
  167. $data['data'] = $list;
  168. $this->success('',$data);
  169. }
  170. /**
  171. * 删除方案
  172. *@ApiParams (name=id,description=方案id)
  173. */
  174. public function delete(){
  175. $id = input('id');
  176. Db('Programme')->where('id',$id)->delete();
  177. Db('ProgrammeGoods')->where('programme_id',$id)->delete();
  178. $this->success('删除成功');
  179. }
  180. /**
  181. * 删除方案商品
  182. *@ApiParams (name=id,description=方案id)
  183. * @ApiParams (name=goods_ids,description=商品id,删除多个商品用逗号分割)
  184. */
  185. public function deleteGoods(){
  186. $id = input('id');
  187. $goodsId = input('goods_ids');
  188. Db('ProgrammeGoods')->where('programme_id',$id)->where('goods_id','in',$goodsId)->delete();
  189. if(!Db('ProgrammeGoods')->where('programme_id',$id)->find()){
  190. Db('Programme')->where('id',$id)->delete();
  191. }
  192. $this->success('删除成功');
  193. }
  194. /**
  195. * 方案详情
  196. * @ApiParams (name=id,description=方案id)
  197. * ApiReturnParams (name=goods_id,description=商品id)
  198. * ApiReturnParams (name=logo,description=logo)
  199. * ApiReturnParams (name=name,description=商品名称)
  200. * @ApiReturnParams (name=sku,description=规格)
  201. * @ApiReturnParams (name=amount,description=单价)
  202. * @ApiReturnParams (name=amount_install,description=安装费)
  203. * ApiReturnParams (name=createtime,description=创建时间)
  204. */
  205. public function info(){
  206. $id = input('id');
  207. $goods = Db('ProgrammeGoods')->where('programme_id',$id)->select();
  208. $goodsIds = Db('ProgrammeGoods')->where('programme_id',$id)->column('goods_id');
  209. $left = [];
  210. foreach ($goodsIds as $k=>$v){
  211. $nowGoods = Db('ProgrammeGoods')->where('goods_id',$v)->where('programme_id',$id)->find();
  212. $categoryId = Db('Goods')->where('id',$v)->value('category_id');
  213. $cate = DB('category')->field('id,name')->where('id',$categoryId)->find();
  214. if(empty($left[$cate['id']])){
  215. $left[$cate['id']] = $cate;
  216. }
  217. array_push($left[$cate['id']],$nowGoods);
  218. }
  219. $left = array_values($left);
  220. dump($left);
  221. }
  222. }