123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\Goods;
- use app\common\model\GoodsInstall;
- use app\common\model\GoodsInstallLink;
- use app\common\model\GoodsSku;
- use app\common\model\OrderInfo;
- use app\common\model\Refund;
- use app\common\service\InstallService;
- use app\common\service\OrderService;
- use think\Db;
- use app\common\model\Orders as Order;
- /**
- * 快速报价接口
- * @inheritdoc
- */
- class Programme extends Api
- {
- protected $noNeedRight="*";
- /**
- * 预估报价
- * @ApiMethod (POST)
- * @ApiParams (name=sku,description="key为规格id,对象:num购买数量,num_install安装数量,{1:{num:1,num_install:2}})
- * ApiReturnParams (name=logo,description=logo)
- * @ApiReturnParams (name=name,description=商品名称)
- * @ApiReturnParams (name=sku,description=规格)
- * @ApiReturnParams (name=amount,description=单价)
- * @ApiReturnParams (name=amount_install,description=安装费)
- * @ApiReturnParams (name=amount_total,description=共计)
- *@ApiReturnParams (name=total,description=预估报价)
- * @ApiReturnParams (name=num,description=采购数量)
- * @ApiReturnParams (name=num_install,description=安装数量)
- * @ApiReturnParams (name=category_id,description=商品分类id)
- */
- public function calculate(){
- $sku = input('sku');
- if(!$sku){
- $this->error('请传商品信息');
- }
- $sku = htmlspecialchars_decode($sku);
- $sku = json_decode($sku,true);
- $goods = [];
- $goodsModel = new Goods();
- $goodsSkuModel = new GoodsSku();
- $total = 0;
- foreach ($sku as $k=>$v){
- $goodsSku = $goodsSkuModel->where('id',$k)->find();
- $nowGoods = $goodsModel->where('id',$goodsSku['goods_id'])->find();
- $goods[$k]['logo'] = $nowGoods['logo'];
- $goods[$k]['name'] = $nowGoods['name'];
- $goods[$k]['sku'] = $goodsSku['name'];
- $goods[$k]['num'] = $v['num'];
- $goods[$k]['category_id'] = $nowGoods['category_id'];
- $goods[$k]['num_install'] = $v['num_install'];
- $goods[$k]['amount'] = $goodsSku['amount'];//单价
- $goods[$k]['amount_install']=InstallService::getFee([$k=>$v]);//安装费
- $goods[$k]['amount_total']=bcmul($goodsSku['amount'],$v['num']);
- $goods[$k]['amount_total']=bcadd($goods[$k]['amount_total'],$goods[$k]['amount_install']);
- $total+=$goods[$k]['amount_total'];
- }
- $goods = array_values($goods);
- $result = ['goods'=>$goods,'total'=>$total];
- $this->success('',$result);
- }
- /**
- * 保存方案
- * @ApiMethod (POST)
- * @ApiParams (name=sku,description="key为规格id,对象:num购买数量,num_install安装数量,{1:{num:1,num_install:2}})
- * @ApiParams (name=name,description=方案名称)
- */
- public function submit(){
- $data=input();
- $this->validate($data,[
- 'sku|商品'=>['require','min:1'],
- 'name|方案名称'=>['require','max:100'],
- ]);
- Db::startTrans();
- $user=$this->auth->getUser();
- $data['createtime'] = time();
- $data['user_id'] = $user['id'];
- $reault = Db::name('Programme')->strict(false)->insert($data);
- $id = Db::name('Programme')->getLastInsID();
- $sku = htmlspecialchars_decode($data['sku']);
- $sku = json_decode($sku,true);
- $goods = [];
- $goodsModel = new Goods();
- $goodsSkuModel = new GoodsSku();
- $total = 0;
- foreach ($sku as $k=>$v){
- $goodsSku = $goodsSkuModel->where('id',$k)->find();
- $nowGoods = $goodsModel->where('id',$goodsSku['goods_id'])->find();
- $goods[$k]['programme_id'] = $id;
- $goods[$k]['goods_id'] = $goodsSku['goods_id'];
- $goods[$k]['sku_id'] = $k;
- $goods[$k]['logo'] = implode(',',$nowGoods['logo']);
- $goods[$k]['name'] = $nowGoods['name'];
- $goods[$k]['sku'] = $goodsSku['name'];
- $goods[$k]['num'] = $v['num'];
- $goods[$k]['amount'] = $goodsSku['amount'];//单价
- $goods[$k]['amount_install']=InstallService::getFee([$k=>$v]);//安装费
- $goods[$k]['amount_total']=bcmul($goodsSku['amount'],$v['num']);
- $goods[$k]['amount_total']=bcadd($goods[$k]['amount_total'],$goods[$k]['amount_install']);
- $total+=$goods[$k]['amount_total'];
- }
- $goods = array_values($goods);
- $goodsResult = Db::name('ProgrammeGoods')->strict(false)->insertAll($goods);
- $totalResult = Db::name('Programme')->where('id',$id)->setField('amount_total',$total);
- if(!$reault || !$goodsResult || !$totalResult){
- Db::rollback();
- $this->error('保存失败');
- }
- Db::commit();
- $this->success('保存成功');
- }
- /**
- * 用户方案列表
- * @ApiParams (name=page,description=分页)
- * @ApiParams (name=limit,description=分页)
- *ApiReturnParams (name=id,description=方案id)
- * ApiReturnParams (name=name,description=方案名称)
- * ApiReturnParams (name=createtime,description=创建时间)
- */
- public function lists(){
- $user=$this->auth->getUser();
- $lists=$user->programmes()
- ->order('id','desc')
- ->paginate(input('limit',15));
- $this->success('',$lists);
- }
- /**
- * 查看方案
- * @ApiParams (name=id,description=方案id)
- * @ApiParams (name=page,description=分页)
- * @ApiParams(name=limit,description=分页)
- * ApiReturnParams (name=goods_id,description=商品id)
- * ApiReturnParams (name=logo,description=logo)
- * ApiReturnParams (name=name,description=商品名称)
- * @ApiReturnParams (name=sku,description=规格)
- * @ApiReturnParams (name=amount,description=单价)
- * @ApiReturnParams (name=amount_install,description=安装费)
- * ApiReturnParams (name=createtime,description=创建时间)
- */
- public function detail(){
- $id = input('id');
- $user=$this->auth->getUser();
- $lists= Db('ProgrammeGoods')
- ->field('goods_id,logo,name,sku,amount,num,amount_install')
- ->where('programme_id',$id)
- ->order('id','desc')
- ->paginate(input('limit',15));
- $data = $lists->toArray();
- $list = $data['data'];
- foreach ($list as &$v){
- $v['logo'] = explode(',', $v['logo']);
- }
- $data['data'] = $list;
- $this->success('',$data);
- }
- /**
- * 删除方案
- *@ApiParams (name=id,description=方案id)
- */
- public function delete(){
- $id = input('id');
- Db('Programme')->where('id',$id)->delete();
- Db('ProgrammeGoods')->where('programme_id',$id)->delete();
- $this->success('删除成功');
- }
- /**
- * 删除方案商品
- *@ApiParams (name=id,description=方案id)
- * @ApiParams (name=goods_ids,description=商品id,删除多个商品用逗号分割)
- */
- public function deleteGoods(){
- $id = input('id');
- $goodsId = input('goods_ids');
- Db('ProgrammeGoods')->where('programme_id',$id)->where('goods_id','in',$goodsId)->delete();
- if(!Db('ProgrammeGoods')->where('programme_id',$id)->find()){
- Db('Programme')->where('id',$id)->delete();
- }
- $this->success('删除成功');
- }
- /**
- * 方案详情
- * @ApiParams (name=id,description=方案id)
- * ApiReturnParams (name=goods_id,description=商品id)
- * ApiReturnParams (name=logo,description=logo)
- * ApiReturnParams (name=name,description=商品名称)
- * @ApiReturnParams (name=sku,description=规格)
- * @ApiReturnParams (name=amount,description=单价)
- * @ApiReturnParams (name=amount_install,description=安装费)
- * ApiReturnParams (name=createtime,description=创建时间)
- */
- public function info(){
- $id = input('id');
- $goods = Db('ProgrammeGoods')->where('programme_id',$id)->select();
- $goodsIds = Db('ProgrammeGoods')->where('programme_id',$id)->column('goods_id');
- $left = [];
- foreach ($goodsIds as $k=>$v){
- $nowGoods = Db('ProgrammeGoods')->where('goods_id',$v)->where('programme_id',$id)->find();
- $categoryId = Db('Goods')->where('id',$v)->value('category_id');
- $cate = DB('category')->field('id,name')->where('id',$categoryId)->find();
- if(empty($left[$cate['id']])){
- $left[$cate['id']] = $cate;
- }
- array_push($left[$cate['id']],$nowGoods);
- }
- $left = array_values($left);
- dump($left);
- }
- }
|