CatPlat.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\App;class CatPlat extends Base{
  5. public function __construct(App $app) {parent::__construct($app);}
  6. /**@params cat_id plat_id rate 分类 平台 税率
  7. * @return \think\response\Json|void
  8. */
  9. public function add(){
  10. $post =$this->post;
  11. $catid =isset($post['cat_id'])&&$post['cat_id']!=="" ? intval($post['cat_id']):"";
  12. if($catid==""){
  13. return error_show(1004,"参数 cat_id 不能为空");
  14. }
  15. $catinfo = Db::name("cat")->where(["id"=>$catid,"is_del"=>0])->find();
  16. if($catinfo==false){
  17. return error_show(1004,"分类信息不存在");
  18. }
  19. $plat_id =isset($post['platform_id'])&&$post['platform_id']!=="" ? intval($post['platform_id']):"";
  20. if($plat_id==""){
  21. return error_show(1004,"参数 plat_id 不能为空");
  22. }
  23. $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
  24. if($platform==false){
  25. return error_show(1004,"平台信息不存在");
  26. }
  27. if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
  28. $ist = Db::name("cat_plat")->where([["cat_id","=",$catid],["platform_id","=",$plat_id],["is_del","=",0]])->find();
  29. if($ist!=false){
  30. return error_show(1004,"该平台下已存在此分类");
  31. }
  32. $rate =isset($post['rate'])&&$post['rate']!=="" ? floor($post['rate']):"";
  33. if($rate==""){
  34. return error_show(1004,"参数 rate 不能为空");
  35. }
  36. $order_rate =isset($post['order_rate'])&&$post['order_rate']!=="" ? floor($post['order_rate']):"";
  37. if($order_rate==""){
  38. return error_show(1004,"参数 order_rate 不能为空");
  39. }
  40. $money_rate =isset($post['money_rate'])&&$post['money_rate']!=="" ? floor($post['money_rate']):"";
  41. if($money_rate==""){
  42. return error_show(1004,"参数 money_rate 不能为空");
  43. }
  44. $sale_rate =isset($post['sale_rate'])&&$post['sale_rate']!=="" ? floor($post['sale_rate']):"";
  45. if($sale_rate==""){
  46. return error_show(1004,"参数 sale_rate 不能为空");
  47. }
  48. $low_rate =isset($post['low_rate'])&&$post['low_rate']!=="" ? floor($post['low_rate']):"";
  49. if($low_rate==""){
  50. return error_show(1004,"参数 low_rate 不能为空");
  51. }
  52. $fund_code =isset($post['fund_code'])&&$post['fund_code']!=="" ? $post['fund_code']:"";
  53. if($fund_code==""){
  54. return error_show(1004,"参数 fund_code 不能为空");
  55. }
  56. $data=[
  57. "cat_id"=>$catid,
  58. "platform_id"=>$plat_id,
  59. "rate"=>$rate,
  60. "order_rate"=>$order_rate,
  61. "money_rate"=>$money_rate,
  62. "sale_rate"=>$sale_rate,
  63. "low_rate"=>$low_rate,
  64. "fund_code"=>$fund_code,
  65. "status"=>1,
  66. "is_del"=>0,
  67. "apply_id"=>$this->uid,
  68. "apply_name"=>$this->uname,
  69. "addtime"=>date("Y-m-d H:i:s"),
  70. "updatetime"=>date("Y-m-d H:i:s"),
  71. ];
  72. $int = Db::name("cat_plat")->insert($data);
  73. return $int ? app_show(0,"新建成功"): error_show(1003,"新建失败");
  74. }
  75. /**
  76. * @return \think\response\Json|void
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function edit(){
  82. $post = $this->post;
  83. // $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  84. // if($catid==""){
  85. // return error_show(1004,"参数 id 不能为空");
  86. // }
  87. // $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  88. // if($cat==false){
  89. // return error_show(1004,"未找到平台分类信息");
  90. // }
  91. $cat_id =isset($post['cat_id'])&&$post['cat_id']!=="" ? intval($post['cat_id']):"";
  92. if($cat_id==""){
  93. return error_show(1004,"参数 cat_id 不能为空");
  94. }
  95. $catinfo = Db::name("cat")->where(["id"=>$cat_id,"is_del"=>0])->find();
  96. if($catinfo==false){
  97. return error_show(1004,"分类信息不存在");
  98. }
  99. $plat_id =isset($post['platform_id'])&&$post['platform_id']!=="" ? intval($post['platform_id']):"";
  100. if($plat_id==""){
  101. return error_show(1004,"参数 platform_id 不能为空");
  102. }
  103. $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
  104. if($platform==false){
  105. return error_show(1004,"平台信息不存在");
  106. }
  107. if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
  108. $rate =isset($post['rate'])&&$post['rate']!=="" ? floor($post['rate']):"";
  109. // if($rate==""){
  110. // return error_show(1004,"参数 rate 不能为空");
  111. // }
  112. $ist = Db::name("cat_plat")->where([["cat_id","=",$cat_id],["platform_id","=",$plat_id],["is_del","=",0]])->find();
  113. if($ist==false){
  114. return error_show(1004,"未找到平台下分类");
  115. }
  116. $order_rate =isset($post['order_rate'])&&$post['order_rate']!=="" ? floor($post['order_rate']):"";
  117. // if($order_rate==""){
  118. // return error_show(1004,"参数 order_rate 不能为空");
  119. // }
  120. $money_rate =isset($post['money_rate'])&&$post['money_rate']!=="" ? floor($post['money_rate']):"";
  121. // if($money_rate==""){
  122. // return error_show(1004,"参数 money_rate 不能为空");
  123. // }
  124. $sale_rate =isset($post['sale_rate'])&&$post['sale_rate']!=="" ? floor($post['sale_rate']):"";
  125. if($sale_rate==""){
  126. return error_show(1004,"参数 sale_rate 不能为空");
  127. }
  128. $low_rate =isset($post['low_rate'])&&$post['low_rate']!=="" ? floor($post['low_rate']):"";
  129. // if($low_rate==""){
  130. // return error_show(1004,"参数 low_rate 不能为空");
  131. // }
  132. $fund_code =isset($post['fund_code'])&&$post['fund_code']!=="" ? $post['fund_code']:"";
  133. // if($fund_code==""){
  134. // return error_show(1004,"参数 fund_code 不能为空");
  135. // }
  136. if($sale_rate> $ist['sale_rate']){
  137. $list= $this->checkGood($cat_id,$sale_rate,$plat_id);
  138. if(!empty($list)){
  139. return app_show(10004,"存在不满足毛利率得商品价格",["skuCode"=>$list]);
  140. }
  141. }
  142. $data=[
  143. "cat_id"=>$cat_id,
  144. "platform_id"=>$plat_id,
  145. "rate"=>$rate,
  146. "order_rate"=>$order_rate,
  147. "money_rate"=>$money_rate,
  148. "sale_rate"=>$sale_rate,
  149. "low_rate"=>$low_rate,
  150. "fund_code"=>$fund_code,
  151. "updatetime"=>date("Y-m-d H:i:s")
  152. ];
  153. $int = Db::name("cat_plat")->where($ist)->update($data);
  154. return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
  155. }
  156. /**列表
  157. * @return \think\response\Json|void
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function list(){
  163. $post=$this->post;
  164. $page = isset($post['page'])&&$post['page']!=""?intval($post['page']) :"1";
  165. $size = isset($post['size'])&&$post['size']!=""?intval($post['size']) :"15";
  166. $condition = [["is_del","=",0]];
  167. $catid= isset($post['cat_id'])&&$post['cat_id']!=""?intval($post['cat_id']) :"";
  168. if($catid!="") $condition[]=["cat_id","=",$catid];
  169. $platid= isset($post['platform_id'])&&$post['platform_id']!=""?intval($post['platform_id']) :"";
  170. if($platid!="") $condition[]=["platform_id","=",$platid];
  171. $status= isset($post['status'])&&$post['status']!==""?intval($post['status']) :"";
  172. if($status!=="") $condition[]=["status","=",$status];
  173. $count = Db::name("cat_plat")->where($condition)->count();
  174. $total = ceil($count/$size);
  175. $page = $total> $page ? $page: intval($total);
  176. $list =Db::name("cat_plat")->where($condition)->page($page,$size)->select()->toArray();
  177. $catKey=[];
  178. $platKey=[];
  179. if(!empty($list)){
  180. $catArr = array_column($list,"cat_id");
  181. $platArr = array_column($list,"platform_id");
  182. $catKey= Db::name("cat")->where(["id"=>$catArr,"is_del"=>0])->column("cat_name","id");
  183. $platKey= Db::name("platform")->where(["id"=>$platArr,"is_del"=>0])->column("platform_name","id");
  184. }
  185. $data=[];
  186. foreach ($list as $key=>$value){
  187. $value['cat_name'] = isset($catKey[$value['cat_id']]) ?$catKey[$value['cat_id']]:"";
  188. $value['platform_name'] = isset($platKey[$value['platform_id']]) ?$platKey[$value['platform_id']]:"";
  189. $data[]=$value;
  190. }
  191. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  192. }
  193. //启禁用分类平台信息
  194. public function status(){
  195. $post = $this->post;
  196. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  197. if($catid==""){
  198. return error_show(1004,"参数 id 不能为空");
  199. }
  200. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  201. if($cat==false){
  202. return error_show(1004,"未找到平台分类信息");
  203. }
  204. $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
  205. if($status===""){
  206. return error_show(1004,"参数 status 不能为空");
  207. }
  208. if(!in_array($status,[0,1])){
  209. return error_show(1004,"参数 status 无效值");
  210. }
  211. $cat['status']=$status;
  212. $cat['updatetime']=date("Y-m-d H:i:s");
  213. $int = Db::name("cat_plat")->save($cat);
  214. return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
  215. }
  216. //删除分类平台信息
  217. public function delete(){
  218. $post = $this->post;
  219. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  220. if($catid==""){
  221. return error_show(1004,"参数 id 不能为空");
  222. }
  223. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  224. if($cat==false){
  225. return error_show(1004,"未找到平台分类信息");
  226. }
  227. $cat['is_del']=1;
  228. $cat['updatetime']=date("Y-m-d H:i:s");
  229. $int = Db::name("cat_plat")->save($cat);
  230. return $int ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  231. }
  232. //根据毛利率计算平台价格
  233. public function PlatPrice(){
  234. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=""? trim($this->post['spuCode']):"";
  235. if($spuCode==""){
  236. return error_show(1005,"参数spuCode不能为空");
  237. }
  238. $platid= isset($this->post['platform_id'])&&$this->post['platform_id']!=""?intval($this->post['platform_id']) :"";
  239. if($platid=="") return error_show(1005,"参数 platform_id 不能为空");
  240. $platform = Db::name("platform")->where(["id"=>$platid,"is_del"=>0])->find();
  241. if($platform==false){
  242. return error_show(1004,"平台信息不存在");
  243. }
  244. $num=isset($this->post['min_num'])&&$this->post['min_num']!=="" ? intval($this->post['min_num']):"";
  245. if($num==""){
  246. return error_show(1005,"参数min_num不能为空");
  247. }
  248. if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
  249. $good =Db::name("good_basic")->where(["spuCode"=>$spuCode,"is_del"=>0])->find();
  250. if($good==false){
  251. return error_show(1005,"商品数据未找到");
  252. }
  253. $nakelist = Db::name("good_nake")
  254. ->where(['spuCode'=>$spuCode,"is_del"=>0])
  255. ->where("min_num","<=",$num)
  256. ->order("min_num desc")
  257. ->find();
  258. if($nakelist==false){
  259. $nakelist = Db::name("good_nake")->where(['spuCode'=>$spuCode,"is_del"=>0])->order("min_num asc")->find();
  260. //非库存品的话,继续校验最小起订量
  261. //库存品的话,不足采购起订量的时候,取采购最小起订量
  262. if ($good['is_stock'] == 0) return error_show(1010, "起订量不足{$nakelist['min_num']}");
  263. }
  264. // $catinfo = Db::name("cat_plat")->where(["cat_id"=>$good['cat_id'],"is_del"=>0,"platform_id"=>$platid])->find();
  265. // if ($catinfo==false || $catinfo['sale_rate']==0) return error_show(1005,"分类未设置平台毛利率");
  266. $companyNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] !== "" ? trim($this->post['relaComNo']) : "";
  267. if($companyNo=='') return error_show(1004,'关联公司不能为空');
  268. //计算毛利率
  269. $budget = get_budget($good['cat_id'], $companyNo, $platid);
  270. if($budget===false) $this->error('未找到业务公司有关的平台分类毛利信息');
  271. // $budget = isset($catinfo['sale_rate']) ? $catinfo['sale_rate']/100:0;
  272. $top_cat_id = made($good['cat_id']);//获取所有分类
  273. $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
  274. $sale_cost_fee = 0;
  275. if ($good['is_gold_price'] == 1 && $top_cat_id == 6) {
  276. // $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'];
  277. //系统售价=(贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用)/(1-成本售价/100)
  278. $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);
  279. //计算工艺费
  280. //销售工艺费=(( 商品重量* 供应商采购金价 + 采购成本工艺费* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)/(1-成本售价/100)-(包装费+加标费+证书费+产品裸价0+运费+其他费用) )/商品重量-供应商采购金价
  281. $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"];
  282. }else{
  283. // $saleprice = ($nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['delivery_fee'] + $nakelist['nake_fee']) / (1 - $budget);
  284. // (加标费+包装费+证书费+成本裸价+运费)/(1-成本售价/100)=系统售价
  285. // $saleprice = (加标费 + 包装费 + 证书费 + 运费 + 成本裸价+其他费用) / (1 - $budget);
  286. $saleprice = $nakelist['nake_total'] / (1 - $budget);
  287. }
  288. return app_show(0, "获取成功", ["sale_price" => round($saleprice, 2), 'new_cost_fee' => round($sale_cost_fee, 2)]);
  289. }
  290. public function catlist(){
  291. $where =[["is_del","=",0]];
  292. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? trim($this->post['cat_name']) :"";
  293. if($cat_name!==""){
  294. $where[]=['cat_name',"like","%$cat_name%"];
  295. }
  296. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  297. $plat_id=isset($this->post['platform_id']) && $this->post['platform_id'] !==""? intval($this->post['platform_id']) :"";
  298. if($pid!==""){
  299. $where[]=["pid","=",$pid];
  300. if ($pid!=0){
  301. $pcat = Db::name('cat')->where(['id'=>$pid,"is_del"=>0])->find();
  302. if($pcat==false){
  303. return error_show(1004,"未找到分类数据");
  304. }
  305. if($plat_id==''){
  306. return error_show(1004,"参数 platform_id 不能为空");
  307. }
  308. }
  309. }
  310. if($plat_id!=""){
  311. $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
  312. if($platform==false){
  313. return error_show(1004,"未找到平台数据");
  314. }
  315. }
  316. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  317. if($status!==""){
  318. $where[]=['status',"=",$status];
  319. }
  320. $page = isset($this->post['page'])&&$this->post['page']!=""?intval($this->post['page']) :"1";
  321. $size = isset($this->post['size'])&&$this->post['size']!=""?intval($this->post['size']) :"15";
  322. $count = Db::name("cat")->where($where)->count();
  323. $total = ceil($count/$size);
  324. $page = $total> $page ? $page: intval($total);
  325. $list = Db::name('cat')->where($where)->page($page,$size)->select();
  326. $data=[];
  327. foreach ($list as $key=>$value){
  328. $catplat= Db::name("cat_plat")->where(["cat_id"=>$value['id'],"platform_id"=>$plat_id,"is_del"=>0])
  329. ->find();
  330. if($catplat==false){
  331. continue;
  332. }
  333. $value['rate']= $catplat['rate'];
  334. $value['order_rate']= $catplat['order_rate'];
  335. $value['low_rate']= $catplat['low_rate'];
  336. $value['money_rate']= $catplat['money_rate'];
  337. $value['sale_rate']= $catplat['sale_rate'];
  338. $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
  339. $value['platform_id'] = $plat_id;
  340. $data[]=$value;
  341. }
  342. return app_show(0, "获取成功",["list"=>$data,"count"=>$count]);
  343. }
  344. public function checkGood($catid,$sale_rate,$plat_id){
  345. $good =Db::name("platform_youzan")->alias("a")->leftJoin("good_basic b","a.spuCode=b.spuCode")
  346. ->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")
  347. ->select()->toArray();
  348. $spucode=[];
  349. if(!empty($good)){
  350. foreach ($good as $key=>$value){
  351. $salec=Db::name("good_nake")->where([["spuCode","=",$value['spuCode'],["min_num","<=",$value['start_sale_num']],["is_del","=",0]]])->find();
  352. if($salec){
  353. $rate =round( ($value['final_price'] - $salec['nake_total']) / $value['final_price'],2)/100;
  354. if ($rate>=$sale_rate){
  355. continue;
  356. }
  357. }
  358. $temp=["skuCode"=>$value['skuCode'],"good_name"=>$value['good_name'],"spuCode"=>$value['good_name']];
  359. $spucode[]=$temp;
  360. }
  361. }
  362. return $spucode;
  363. }
  364. }