CatPlat.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ErrorException;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. $data=[
  137. "cat_id"=>$cat_id,
  138. "platform_id"=>$plat_id,
  139. "rate"=>$rate,
  140. "order_rate"=>$order_rate,
  141. "money_rate"=>$money_rate,
  142. "sale_rate"=>$sale_rate,
  143. "low_rate"=>$low_rate,
  144. "fund_code"=>$fund_code,
  145. "updatetime"=>date("Y-m-d H:i:s")
  146. ];
  147. $int = Db::name("cat_plat")->where($ist)->update($data);
  148. return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
  149. }
  150. /**列表
  151. * @return \think\response\Json|void
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. */
  156. public function list(){
  157. $post=$this->post;
  158. $page = isset($post['page'])&&$post['page']!=""?intval($post['page']) :"1";
  159. $size = isset($post['size'])&&$post['size']!=""?intval($post['size']) :"15";
  160. $condition = [["is_del","=",0]];
  161. $catid= isset($post['cat_id'])&&$post['cat_id']!=""?intval($post['cat_id']) :"";
  162. if($catid!="") $condition[]=["cat_id","=",$catid];
  163. $platid= isset($post['platform_id'])&&$post['platform_id']!=""?intval($post['platform_id']) :"";
  164. if($platid!="") $condition[]=["platform_id","=",$platid];
  165. $status= isset($post['status'])&&$post['status']!==""?intval($post['status']) :"";
  166. if($status!=="") $condition[]=["status","=",$status];
  167. $count = Db::name("cat_plat")->where($condition)->count();
  168. $total = ceil($count/$size);
  169. $page = $total> $page ? $page: intval($total);
  170. $list =Db::name("cat_plat")->where($condition)->page($page,$size)->select()->toArray();
  171. $catKey=[];
  172. $platKey=[];
  173. if(!empty($list)){
  174. $catArr = array_column($list,"cat_id");
  175. $platArr = array_column($list,"platform_id");
  176. $catKey= Db::name("cat")->where(["id"=>$catArr,"is_del"=>0])->column("cat_name","id");
  177. $platKey= Db::name("platform")->where(["id"=>$platArr,"is_del"=>0])->column("platform_name","id");
  178. }
  179. $data=[];
  180. foreach ($list as $key=>$value){
  181. $value['cat_name'] = isset($catKey[$value['cat_id']]) ?$catKey[$value['cat_id']]:"";
  182. $value['platform_name'] = isset($platKey[$value['platform_id']]) ?$platKey[$value['platform_id']]:"";
  183. $data[]=$value;
  184. }
  185. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  186. }
  187. //启禁用分类平台信息
  188. public function status(){
  189. $post = $this->post;
  190. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  191. if($catid==""){
  192. return error_show(1004,"参数 id 不能为空");
  193. }
  194. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  195. if($cat==false){
  196. return error_show(1004,"未找到平台分类信息");
  197. }
  198. $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
  199. if($status===""){
  200. return error_show(1004,"参数 status 不能为空");
  201. }
  202. if(!in_array($status,[0,1])){
  203. return error_show(1004,"参数 status 无效值");
  204. }
  205. $cat['status']=$status;
  206. $cat['updatetime']=date("Y-m-d H:i:s");
  207. $int = Db::name("cat_plat")->save($cat);
  208. return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
  209. }
  210. //删除分类平台信息
  211. public function delete(){
  212. $post = $this->post;
  213. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  214. if($catid==""){
  215. return error_show(1004,"参数 id 不能为空");
  216. }
  217. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  218. if($cat==false){
  219. return error_show(1004,"未找到平台分类信息");
  220. }
  221. $cat['is_del']=1;
  222. $cat['updatetime']=date("Y-m-d H:i:s");
  223. $int = Db::name("cat_plat")->save($cat);
  224. return $int ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  225. }
  226. //根据毛利率计算平台价格
  227. public function PlatPrice(){
  228. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=""? trim($this->post['spuCode']):"";
  229. if($spuCode==""){
  230. return error_show(1005,"参数spuCode不能为空");
  231. }
  232. $platid= isset($this->post['platform_id'])&&$this->post['platform_id']!=""?intval($this->post['platform_id']) :"";
  233. if($platid=="") return error_show(1005,"参数 platform_id 不能为空");
  234. $platform = Db::name("platform")->where(["id"=>$platid,"is_del"=>0])->find();
  235. if($platform==false){
  236. return error_show(1004,"平台信息不存在");
  237. }
  238. $num=isset($this->post['min_num'])&&$this->post['min_num']!=="" ? intval($this->post['min_num']):"";
  239. if($num==""){
  240. return error_show(1005,"参数min_num不能为空");
  241. }
  242. if ($platform['platform_type']!=1) return error_show(1004,"平台为非对接平台");
  243. $good =Db::name("good_basic")->where(["spuCode"=>$spuCode,"is_del"=>0])->find();
  244. if($good==false){
  245. return error_show(1005,"商品数据未找到");
  246. }
  247. $nakelist = Db::name("good_nake")
  248. ->where(['spuCode'=>$spuCode,"is_del"=>0])
  249. ->where("min_num","<=",$num)
  250. ->order("min_num desc")
  251. ->find();
  252. if($nakelist==false){
  253. $nakelist = Db::name("good_nake")->where(['spuCode'=>$spuCode,"is_del"=>0])->order("min_num asc")->find();
  254. //非库存品的话,继续校验最小起订量
  255. //库存品的话,不足采购起订量的时候,取采购最小起订量
  256. if ($good['is_stock'] == 0) return error_show(1010, "起订量不足{$nakelist['min_num']}");
  257. }
  258. $catinfo = Db::name("cat_plat")->where(["cat_id"=>$good['cat_id'],"is_del"=>0,"platform_id"=>$platid])->find();
  259. if ($catinfo==false) return error_show(1005,"分类未设置平台毛利率");
  260. $budget = isset($catinfo['sale_rate']) ? $catinfo['sale_rate']/100:0;
  261. $top_cat_id = made($catinfo['cat_id']);//获取所有分类
  262. $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
  263. $sale_cost_fee = 0;
  264. if ($good['is_gold_price'] == 1 && $top_cat_id == 6) {
  265. // $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'];
  266. //系统售价=(贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用)/(1-成本售价/100)
  267. $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);
  268. //计算工艺费
  269. //销售工艺费=(( 商品重量* 供应商采购金价 + 采购成本工艺费* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)/(1-成本售价/100)-(包装费+加标费+证书费+产品裸价0+运费+其他费用) )/商品重量-供应商采购金价
  270. $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"];
  271. }else{
  272. // $saleprice = ($nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['delivery_fee'] + $nakelist['nake_fee']) / (1 - $budget);
  273. // (加标费+包装费+证书费+成本裸价+运费)/(1-成本售价/100)=系统售价
  274. // $saleprice = (加标费 + 包装费 + 证书费 + 运费 + 成本裸价+其他费用) / (1 - $budget);
  275. $saleprice = $nakelist['nake_total'] / (1 - $budget);
  276. }
  277. return app_show(0, "获取成功", ["sale_price" => round($saleprice, 2), 'new_cost_fee' => round($sale_cost_fee, 2)]);
  278. }
  279. public function catlist(){
  280. $where =[["is_del","=",0]];
  281. $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? trim($this->post['cat_name']) :"";
  282. if($cat_name!==""){
  283. $where[]=['cat_name',"like","%$cat_name%"];
  284. }
  285. $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
  286. $plat_id=isset($this->post['platform_id']) && $this->post['platform_id'] !==""? intval($this->post['platform_id']) :"";
  287. if($pid!==""){
  288. $where[]=["pid","=",$pid];
  289. if ($pid!=0){
  290. $pcat = Db::name('cat')->where(['id'=>$pid,"is_del"=>0])->find();
  291. if($pcat==false){
  292. return error_show(1004,"未找到分类数据");
  293. }else{
  294. if($pcat['level']==2){
  295. if($plat_id==""){
  296. return error_show(1004,"参数 platform_id 不能为空");
  297. }
  298. }
  299. }
  300. }
  301. }
  302. if($plat_id!=""){
  303. $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
  304. }
  305. $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
  306. if($status!==""){
  307. $where[]=['status',"=",$status];
  308. }
  309. $page = isset($post['page'])&&$post['page']!=""?intval($post['page']) :"1";
  310. $size = isset($post['size'])&&$post['size']!=""?intval($post['size']) :"15";
  311. $count = Db::name("cat")->where($where)->count();
  312. $total = ceil($count/$size);
  313. $page = $total> $page ? $page: intval($total);
  314. $list = Db::name('cat')->where($where)->page($page,$size)->select();
  315. $data=[];
  316. foreach ($list as $key=>$value){
  317. if($value['level']==3){
  318. $catplat= Db::name("cat_plat")->where(["cat_id"=>$value['id'],"platform_id"=>$plat_id,"is_del"=>0])
  319. ->find();
  320. if($catplat==false){
  321. continue;
  322. }
  323. $value['rate']= $catplat['rate'];
  324. $value['order_rate']= $catplat['order_rate'];
  325. $value['low_rate']= $catplat['low_rate'];
  326. $value['money_rate']= $catplat['money_rate'];
  327. $value['sale_rate']= $catplat['sale_rate'];
  328. }
  329. $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
  330. $value['platform_id'] = $plat_id;
  331. $data[]=$value;
  332. }
  333. return app_show(0, "获取成功",["list"=>$data,"count"=>$count]);
  334. }
  335. }