123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- namespace app\admin\controller;
- use think\facade\Db;
- use think\App;class CatPlat extends Base{
- public function __construct(App $app) {parent::__construct($app);}
- /**@params cat_id plat_id rate 分类 平台 税率
- * @return \think\response\Json|void
- */
- public function add(){
- $post =$this->post;
- $catid =isset($post['cat_id'])&&$post['cat_id']!=="" ? intval($post['cat_id']):"";
- if($catid==""){
- return error_show(1004,"参数 cat_id 不能为空");
- }
- $catinfo = Db::name("cat")->where(["id"=>$catid,"is_del"=>0])->find();
- if($catinfo==false){
- return error_show(1004,"分类信息不存在");
- }
- $plat_id =isset($post['platform_id'])&&$post['platform_id']!=="" ? intval($post['platform_id']):"";
- if($plat_id==""){
- return error_show(1004,"参数 plat_id 不能为空");
- }
- $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
- if($platform==false){
- return error_show(1004,"平台信息不存在");
- }
- if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
- $ist = Db::name("cat_plat")->where([["cat_id","=",$catid],["platform_id","=",$plat_id],["is_del","=",0]])->find();
- if($ist!=false){
- return error_show(1004,"该平台下已存在此分类");
- }
- $rate =isset($post['rate'])&&$post['rate']!=="" ? floor($post['rate']):"";
- if($rate==""){
- return error_show(1004,"参数 rate 不能为空");
- }
- $order_rate =isset($post['order_rate'])&&$post['order_rate']!=="" ? floor($post['order_rate']):"";
- if($order_rate==""){
- return error_show(1004,"参数 order_rate 不能为空");
- }
- $money_rate =isset($post['money_rate'])&&$post['money_rate']!=="" ? floor($post['money_rate']):"";
- if($money_rate==""){
- return error_show(1004,"参数 money_rate 不能为空");
- }
- $sale_rate =isset($post['sale_rate'])&&$post['sale_rate']!=="" ? floor($post['sale_rate']):"";
- if($sale_rate==""){
- return error_show(1004,"参数 sale_rate 不能为空");
- }
- $low_rate =isset($post['low_rate'])&&$post['low_rate']!=="" ? floor($post['low_rate']):"";
- if($low_rate==""){
- return error_show(1004,"参数 low_rate 不能为空");
- }
- $fund_code =isset($post['fund_code'])&&$post['fund_code']!=="" ? $post['fund_code']:"";
- if($fund_code==""){
- return error_show(1004,"参数 fund_code 不能为空");
- }
- $data=[
- "cat_id"=>$catid,
- "platform_id"=>$plat_id,
- "rate"=>$rate,
- "order_rate"=>$order_rate,
- "money_rate"=>$money_rate,
- "sale_rate"=>$sale_rate,
- "low_rate"=>$low_rate,
- "fund_code"=>$fund_code,
- "status"=>1,
- "is_del"=>0,
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $int = Db::name("cat_plat")->insert($data);
- return $int ? app_show(0,"新建成功"): error_show(1003,"新建失败");
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function edit(){
- $post = $this->post;
- // $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
- // if($catid==""){
- // return error_show(1004,"参数 id 不能为空");
- // }
- // $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
- // if($cat==false){
- // return error_show(1004,"未找到平台分类信息");
- // }
- $cat_id =isset($post['cat_id'])&&$post['cat_id']!=="" ? intval($post['cat_id']):"";
- if($cat_id==""){
- return error_show(1004,"参数 cat_id 不能为空");
- }
- $catinfo = Db::name("cat")->where(["id"=>$cat_id,"is_del"=>0])->find();
- if($catinfo==false){
- return error_show(1004,"分类信息不存在");
- }
- $plat_id =isset($post['platform_id'])&&$post['platform_id']!=="" ? intval($post['platform_id']):"";
- if($plat_id==""){
- return error_show(1004,"参数 platform_id 不能为空");
- }
- $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
- if($platform==false){
- return error_show(1004,"平台信息不存在");
- }
- if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
- $rate =isset($post['rate'])&&$post['rate']!=="" ? floor($post['rate']):"";
- // if($rate==""){
- // return error_show(1004,"参数 rate 不能为空");
- // }
- $ist = Db::name("cat_plat")->where([["cat_id","=",$cat_id],["platform_id","=",$plat_id],["is_del","=",0]])->find();
- if($ist==false){
- return error_show(1004,"未找到平台下分类");
- }
- $order_rate =isset($post['order_rate'])&&$post['order_rate']!=="" ? floor($post['order_rate']):"";
- // if($order_rate==""){
- // return error_show(1004,"参数 order_rate 不能为空");
- // }
- $money_rate =isset($post['money_rate'])&&$post['money_rate']!=="" ? floor($post['money_rate']):"";
- // if($money_rate==""){
- // return error_show(1004,"参数 money_rate 不能为空");
- // }
- $sale_rate =isset($post['sale_rate'])&&$post['sale_rate']!=="" ? floor($post['sale_rate']):"";
- if($sale_rate==""){
- return error_show(1004,"参数 sale_rate 不能为空");
- }
- $low_rate =isset($post['low_rate'])&&$post['low_rate']!=="" ? floor($post['low_rate']):"";
- // if($low_rate==""){
- // return error_show(1004,"参数 low_rate 不能为空");
- // }
- $fund_code =isset($post['fund_code'])&&$post['fund_code']!=="" ? $post['fund_code']:"";
- // if($fund_code==""){
- // return error_show(1004,"参数 fund_code 不能为空");
- // }
- if($sale_rate> $ist['sale_rate']){
- $list= $this->checkGood($cat_id,$sale_rate,$plat_id);
- if(!empty($list)){
- return app_show(10004,"存在不满足毛利率得商品价格",["skuCode"=>$list]);
- }
- }
- $data=[
- "cat_id"=>$cat_id,
- "platform_id"=>$plat_id,
- "rate"=>$rate,
- "order_rate"=>$order_rate,
- "money_rate"=>$money_rate,
- "sale_rate"=>$sale_rate,
- "low_rate"=>$low_rate,
- "fund_code"=>$fund_code,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $int = Db::name("cat_plat")->where($ist)->update($data);
- return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
- }
- /**列表
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function list(){
- $post=$this->post;
- $page = isset($post['page'])&&$post['page']!=""?intval($post['page']) :"1";
- $size = isset($post['size'])&&$post['size']!=""?intval($post['size']) :"15";
- $condition = [["is_del","=",0]];
- $catid= isset($post['cat_id'])&&$post['cat_id']!=""?intval($post['cat_id']) :"";
- if($catid!="") $condition[]=["cat_id","=",$catid];
- $platid= isset($post['platform_id'])&&$post['platform_id']!=""?intval($post['platform_id']) :"";
- if($platid!="") $condition[]=["platform_id","=",$platid];
- $status= isset($post['status'])&&$post['status']!==""?intval($post['status']) :"";
- if($status!=="") $condition[]=["status","=",$status];
- $count = Db::name("cat_plat")->where($condition)->count();
- $total = ceil($count/$size);
- $page = $total> $page ? $page: intval($total);
- $list =Db::name("cat_plat")->where($condition)->page($page,$size)->select()->toArray();
- $catKey=[];
- $platKey=[];
- if(!empty($list)){
- $catArr = array_column($list,"cat_id");
- $platArr = array_column($list,"platform_id");
- $catKey= Db::name("cat")->where(["id"=>$catArr,"is_del"=>0])->column("cat_name","id");
- $platKey= Db::name("platform")->where(["id"=>$platArr,"is_del"=>0])->column("platform_name","id");
- }
- $data=[];
- foreach ($list as $key=>$value){
- $value['cat_name'] = isset($catKey[$value['cat_id']]) ?$catKey[$value['cat_id']]:"";
- $value['platform_name'] = isset($platKey[$value['platform_id']]) ?$platKey[$value['platform_id']]:"";
- $data[]=$value;
- }
- return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
- }
- //启禁用分类平台信息
- public function status(){
- $post = $this->post;
- $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
- if($catid==""){
- return error_show(1004,"参数 id 不能为空");
- }
- $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
- if($cat==false){
- return error_show(1004,"未找到平台分类信息");
- }
- $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
- if($status===""){
- return error_show(1004,"参数 status 不能为空");
- }
- if(!in_array($status,[0,1])){
- return error_show(1004,"参数 status 无效值");
- }
- $cat['status']=$status;
- $cat['updatetime']=date("Y-m-d H:i:s");
- $int = Db::name("cat_plat")->save($cat);
- return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
- }
- //删除分类平台信息
- public function delete(){
- $post = $this->post;
- $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
- if($catid==""){
- return error_show(1004,"参数 id 不能为空");
- }
- $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
- if($cat==false){
- return error_show(1004,"未找到平台分类信息");
- }
- $cat['is_del']=1;
- $cat['updatetime']=date("Y-m-d H:i:s");
- $int = Db::name("cat_plat")->save($cat);
- return $int ? app_show(0,"删除成功"): error_show(1003,"删除失败");
- }
- //根据毛利率计算平台价格
- public function PlatPrice(){
- $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=""? trim($this->post['spuCode']):"";
- if($spuCode==""){
- return error_show(1005,"参数spuCode不能为空");
- }
- $platid= isset($this->post['platform_id'])&&$this->post['platform_id']!=""?intval($this->post['platform_id']) :"";
- if($platid=="") return error_show(1005,"参数 platform_id 不能为空");
- $platform = Db::name("platform")->where(["id"=>$platid,"is_del"=>0])->find();
- if($platform==false){
- return error_show(1004,"平台信息不存在");
- }
- $num=isset($this->post['min_num'])&&$this->post['min_num']!=="" ? intval($this->post['min_num']):"";
- if($num==""){
- return error_show(1005,"参数min_num不能为空");
- }
- if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
- $good =Db::name("good_basic")->where(["spuCode"=>$spuCode,"is_del"=>0])->find();
- if($good==false){
- return error_show(1005,"商品数据未找到");
- }
- $nakelist = Db::name("good_nake")
- ->where(['spuCode'=>$spuCode,"is_del"=>0])
- ->where("min_num","<=",$num)
- ->order("min_num desc")
- ->find();
- if($nakelist==false){
- $nakelist = Db::name("good_nake")->where(['spuCode'=>$spuCode,"is_del"=>0])->order("min_num asc")->find();
- //非库存品的话,继续校验最小起订量
- //库存品的话,不足采购起订量的时候,取采购最小起订量
- if ($good['is_stock'] == 0) return error_show(1010, "起订量不足{$nakelist['min_num']}");
- }
- // $catinfo = Db::name("cat_plat")->where(["cat_id"=>$good['cat_id'],"is_del"=>0,"platform_id"=>$platid])->find();
- // if ($catinfo==false || $catinfo['sale_rate']==0) return error_show(1005,"分类未设置平台毛利率");
- $companyNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] !== "" ? trim($this->post['relaComNo']) : "";
- if($companyNo=='') return error_show(1004,'关联公司不能为空');
- //计算毛利率
- $budget = get_budget($good['cat_id'], $companyNo, $platid);
- // $budget = isset($catinfo['sale_rate']) ? $catinfo['sale_rate']/100:0;
- $top_cat_id = made($good['cat_id']);//获取所有分类
- $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
- $sale_cost_fee = 0;
- if ($good['is_gold_price'] == 1 && $top_cat_id == 6) {
- // $saleprice = $good['noble_weight']*$good["cgd_gold_price"] + $nakelist['cost_fee']/(1-$budget)*$good['noble_weight']+$nakelist['mark_fee']+$nakelist['package_fee']+$nakelist['cert_fee']+$nakelist['nake_fee']+$nakelist['delivery_fee'];
- //系统售价=(贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用)/(1-成本售价/100)
- $saleprice = ($good['noble_weight'] * $good["cgd_gold_price"] + $nakelist['cost_fee'] * $good['noble_weight'] + $nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['nake_fee'] + $nakelist['delivery_fee'] + $nakelist['other_fee']) / (1 - $budget);
- //计算工艺费
- //销售工艺费=(( 商品重量* 供应商采购金价 + 采购成本工艺费* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)/(1-成本售价/100)-(包装费+加标费+证书费+产品裸价0+运费+其他费用) )/商品重量-供应商采购金价
- $sale_cost_fee = (($good['noble_weight'] * $good["cgd_gold_price"] + $nakelist['cost_fee'] * $good['noble_weight'] + $nakelist['package_fee'] + $nakelist['mark_fee'] + $nakelist['cert_fee'] + $nakelist['nake_fee'] + $nakelist['other_fee'] + $nakelist['delivery_fee']) / (1 - $budget) - ($nakelist['package_fee'] + $nakelist['mark_fee'] + $nakelist['cert_fee'] + $nakelist['nake_fee'] + $nakelist['delivery_fee'] + $nakelist['other_fee'])) / $good['noble_weight'] - $good["cgd_gold_price"];
- }else{
- // $saleprice = ($nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['delivery_fee'] + $nakelist['nake_fee']) / (1 - $budget);
- // (加标费+包装费+证书费+成本裸价+运费)/(1-成本售价/100)=系统售价
- // $saleprice = (加标费 + 包装费 + 证书费 + 运费 + 成本裸价+其他费用) / (1 - $budget);
- $saleprice = $nakelist['nake_total'] / (1 - $budget);
- }
- return app_show(0, "获取成功", ["sale_price" => round($saleprice, 2), 'new_cost_fee' => round($sale_cost_fee, 2)]);
- }
- public function catlist(){
- $where =[["is_del","=",0]];
- $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? trim($this->post['cat_name']) :"";
- if($cat_name!==""){
- $where[]=['cat_name',"like","%$cat_name%"];
- }
- $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
- $plat_id=isset($this->post['platform_id']) && $this->post['platform_id'] !==""? intval($this->post['platform_id']) :"";
- if($pid!==""){
- $where[]=["pid","=",$pid];
- if ($pid!=0){
- $pcat = Db::name('cat')->where(['id'=>$pid,"is_del"=>0])->find();
- if($pcat==false){
- return error_show(1004,"未找到分类数据");
- }
- if($plat_id==''){
- return error_show(1004,"参数 platform_id 不能为空");
- }
- }
- }
- if($plat_id!=""){
- $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
- if($platform==false){
- return error_show(1004,"未找到平台数据");
- }
- }
- $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
- if($status!==""){
- $where[]=['status',"=",$status];
- }
- $page = isset($this->post['page'])&&$this->post['page']!=""?intval($this->post['page']) :"1";
- $size = isset($this->post['size'])&&$this->post['size']!=""?intval($this->post['size']) :"15";
- $count = Db::name("cat")->where($where)->count();
- $total = ceil($count/$size);
- $page = $total> $page ? $page: intval($total);
- $list = Db::name('cat')->where($where)->page($page,$size)->select();
- $data=[];
- foreach ($list as $key=>$value){
- $catplat= Db::name("cat_plat")->where(["cat_id"=>$value['id'],"platform_id"=>$plat_id,"is_del"=>0])
- ->find();
- if($catplat==false){
- continue;
- }
- $value['rate']= $catplat['rate'];
- $value['order_rate']= $catplat['order_rate'];
- $value['low_rate']= $catplat['low_rate'];
- $value['money_rate']= $catplat['money_rate'];
- $value['sale_rate']= $catplat['sale_rate'];
- $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
- $value['platform_id'] = $plat_id;
- $data[]=$value;
- }
- return app_show(0, "获取成功",["list"=>$data,"count"=>$count]);
- }
- public function checkGood($catid,$sale_rate,$plat_id){
- $good =Db::name("platform_youzan")->alias("a")->leftJoin("good_basic b","a.spuCode=b.spuCode")
- ->where(["b.cat_id"=>$catid,"a.platform_id"=>$plat_id,"a.exam_status"=>6,"a.is_del"=>0])->field("a.spuCode,a.skuCode,b.good_name,a.start_sale_num,final_price")
- ->select()->toArray();
- $spucode=[];
- if(!empty($good)){
- foreach ($good as $key=>$value){
- $salec=Db::name("good_nake")->where([["spuCode","=",$value['spuCode'],["min_num","<=",$value['start_sale_num']],["is_del","=",0]]])->find();
- if($salec){
- $rate =round( ($value['final_price'] - $salec['nake_total']) / $value['final_price'],2)/100;
- if ($rate>=$sale_rate){
- continue;
- }
- }
- $temp=["skuCode"=>$value['skuCode'],"good_name"=>$value['good_name'],"spuCode"=>$value['good_name']];
- $spucode[]=$temp;
- }
- }
- return $spucode;
- }
- }
|