CatPlat.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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['type']!=1) return error_show(1004,"平台为非对接平台");
  28. $rate =isset($post['rate'])&&$post['rate']!=="" ? floor($post['rate']):"";
  29. if($rate==""){
  30. return error_show(1004,"参数 rate 不能为空");
  31. }
  32. $order_rate =isset($post['order_rate'])&&$post['order_rate']!=="" ? floor($post['order_rate']):"";
  33. if($order_rate==""){
  34. return error_show(1004,"参数 order_rate 不能为空");
  35. }
  36. $money_rate =isset($post['money_rate'])&&$post['money_rate']!=="" ? floor($post['money_rate']):"";
  37. if($money_rate==""){
  38. return error_show(1004,"参数 money_rate 不能为空");
  39. }
  40. $sale_rate =isset($post['sale_rate'])&&$post['sale_rate']!=="" ? floor($post['sale_rate']):"";
  41. if($sale_rate==""){
  42. return error_show(1004,"参数 sale_rate 不能为空");
  43. }
  44. $low_rate =isset($post['low_rate'])&&$post['low_rate']!=="" ? floor($post['low_rate']):"";
  45. if($low_rate==""){
  46. return error_show(1004,"参数 low_rate 不能为空");
  47. }
  48. $fund_code =isset($post['fund_code'])&&$post['fund_code']!=="" ? $post['fund_code']:"";
  49. if($fund_code==""){
  50. return error_show(1004,"参数 fund_code 不能为空");
  51. }
  52. $data=[
  53. "cat_id"=>$catid,
  54. "platform_id"=>$plat_id,
  55. "rate"=>$rate,
  56. "order_rate"=>$order_rate,
  57. "money_rate"=>$money_rate,
  58. "sale_rate"=>$sale_rate,
  59. "low_rate"=>$low_rate,
  60. "fund_code"=>$fund_code,
  61. "status"=>1,
  62. "is_del"=>0,
  63. "apply_id"=>$this->uid,
  64. "apply_name"=>$this->uname,
  65. "addtime"=>date("Y-m-d H:i:s"),
  66. "updatetime"=>date("Y-m-d H:i:s"),
  67. ];
  68. $int = Db::name("cat_plat")->insert($data);
  69. return $int ? app_show(0,"新建成功"): error_show(1003,"新建失败");
  70. }
  71. public function edit(){
  72. $post = $this->post;
  73. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  74. if($catid==""){
  75. return error_show(1004,"参数 id 不能为空");
  76. }
  77. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  78. if($cat==false){
  79. return error_show(1004,"未找到平台分类信息");
  80. }
  81. $cat_id =isset($post['cat_id'])&&$post['cat_id']!=="" ? intval($post['cat_id']):"";
  82. if($cat_id==""){
  83. return error_show(1004,"参数 cat_id 不能为空");
  84. }
  85. $catinfo = Db::name("cat")->where(["id"=>$cat_id,"is_del"=>0])->find();
  86. if($catinfo==false){
  87. return error_show(1004,"分类信息不存在");
  88. }
  89. $plat_id =isset($post['platform_id'])&&$post['platform_id']!=="" ? intval($post['platform_id']):"";
  90. if($plat_id==""){
  91. return error_show(1004,"参数 platform_id 不能为空");
  92. }
  93. $platform = Db::name("platform")->where(["id"=>$plat_id,"is_del"=>0])->find();
  94. if($platform==false){
  95. return error_show(1004,"平台信息不存在");
  96. }
  97. if ($platform['type']!=1) return error_show(1004,"平台为非对接平台");
  98. $rate =isset($post['rate'])&&$post['rate']!=="" ? floor($post['rate']):"";
  99. if($rate==""){
  100. return error_show(1004,"参数 rate 不能为空");
  101. }
  102. $ist = Db::name("cat_plat")->where([["cat_id","=",$cat_id],["platform_id","=",$plat_id],["id","<>",$catid],["is_del","=",0]])->find();
  103. if($ist!=false){
  104. return error_show(1004,"该平台下已存在此分类");
  105. }
  106. $order_rate =isset($post['order_rate'])&&$post['order_rate']!=="" ? floor($post['order_rate']):"";
  107. if($order_rate==""){
  108. return error_show(1004,"参数 order_rate 不能为空");
  109. }
  110. $money_rate =isset($post['money_rate'])&&$post['money_rate']!=="" ? floor($post['money_rate']):"";
  111. if($money_rate==""){
  112. return error_show(1004,"参数 money_rate 不能为空");
  113. }
  114. $sale_rate =isset($post['sale_rate'])&&$post['sale_rate']!=="" ? floor($post['sale_rate']):"";
  115. if($sale_rate==""){
  116. return error_show(1004,"参数 sale_rate 不能为空");
  117. }
  118. $low_rate =isset($post['low_rate'])&&$post['low_rate']!=="" ? floor($post['low_rate']):"";
  119. if($low_rate==""){
  120. return error_show(1004,"参数 low_rate 不能为空");
  121. }
  122. $fund_code =isset($post['fund_code'])&&$post['fund_code']!=="" ? $post['fund_code']:"";
  123. if($fund_code==""){
  124. return error_show(1004,"参数 fund_code 不能为空");
  125. }
  126. $data=[
  127. "cat_id"=>$catid,
  128. "platform_id"=>$plat_id,
  129. "rate"=>$rate,
  130. "order_rate"=>$order_rate,
  131. "money_rate"=>$money_rate,
  132. "sale_rate"=>$sale_rate,
  133. "low_rate"=>$low_rate,
  134. "fund_code"=>$fund_code,
  135. "apply_id"=>$this->uid,
  136. "apply_name"=>$this->uname,
  137. "updatetime"=>date("Y-m-d H:i:s")
  138. ];
  139. $int = Db::name("cat_plat")->where($cat)->update($data);
  140. return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
  141. }
  142. /**列表
  143. * @return \think\response\Json|void
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function list(){
  149. $post=$this->post;
  150. $page = isset($post['page'])&&$post['page']!=""?intval($post['page']) :"1";
  151. $size = isset($post['size'])&&$post['size']!=""?intval($post['size']) :"15";
  152. $condition = [["is_del","=",0]];
  153. $catid= isset($post['cat_id'])&&$post['cat_id']!=""?intval($post['cat_id']) :"";
  154. if($catid!="") $condition[]=["cat_id","=",$catid];
  155. $platid= isset($post['platform_id'])&&$post['platform_id']!=""?intval($post['platform_id']) :"";
  156. if($platid!="") $condition[]=["platform_id","=",$platid];
  157. $status= isset($post['status'])&&$post['status']!==""?intval($post['status']) :"";
  158. if($status!=="") $condition[]=["status","=",$status];
  159. $count = Db::name("cat_plat")->where($condition)->count();
  160. $total = ceil($count/$size);
  161. $page = $total> $page ? $page: intval($total);
  162. $list =Db::name("cat_plat")->where($condition)->page($page,$size)->select()->toArray();
  163. $catKey=[];
  164. $platKey=[];
  165. if(!empty($list)){
  166. $catArr = array_column($list,"cat_id");
  167. $platArr = array_column($list,"platform_id");
  168. $catKey= Db::name("cat")->where(["id"=>$catArr,"is_del"=>0])->column("cat_name","id");
  169. $platKey= Db::name("platform")->where(["id"=>$platArr,"is_del"=>0])->column("platform_name","id");
  170. }
  171. $data=[];
  172. foreach ($list as $key=>$value){
  173. $value['cat_name'] = isset($catKey[$value['cat_id']]) ?$catKey[$value['cat_id']]:"";
  174. $value['platform_name'] = isset($platKey[$value['platform_id']]) ?$platKey[$value['platform_id']]:"";
  175. $data[]=$value;
  176. }
  177. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  178. }
  179. //启禁用分类平台信息
  180. public function status(){
  181. $post = $this->post;
  182. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  183. if($catid==""){
  184. return error_show(1004,"参数 id 不能为空");
  185. }
  186. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  187. if($cat==false){
  188. return error_show(1004,"未找到平台分类信息");
  189. }
  190. $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
  191. if($status===""){
  192. return error_show(1004,"参数 status 不能为空");
  193. }
  194. if(!in_array($status,[0,1])){
  195. return error_show(1004,"参数 status 无效值");
  196. }
  197. $cat['status']=$status;
  198. $cat['updatetime']=date("Y-m-d H:i:s");
  199. $int = Db::name("cat_plat")->save($cat);
  200. return $int ? app_show(0,"更新成功"): error_show(1003,"更新失败");
  201. }
  202. //删除分类平台信息
  203. public function delete(){
  204. $post = $this->post;
  205. $catid =isset($post['id'])&&$post['id']!=="" ? intval($post['id']):"";
  206. if($catid==""){
  207. return error_show(1004,"参数 id 不能为空");
  208. }
  209. $cat = Db::name("cat_plat")->where(["id"=>$catid,"is_del"=>0])->find();
  210. if($cat==false){
  211. return error_show(1004,"未找到平台分类信息");
  212. }
  213. $cat['is_del']=1;
  214. $cat['updatetime']=date("Y-m-d H:i:s");
  215. $int = Db::name("cat_plat")->save($cat);
  216. return $int ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  217. }
  218. //根据毛利率计算平台价格
  219. public function PlatPrice(){
  220. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=""? trim($this->post['spuCode']):"";
  221. if($spuCode==""){
  222. return error_show(1005,"参数spuCode不能为空");
  223. }
  224. $platid= isset($post['plat_id'])&&$post['plat_id']!=""?intval($post['plat_id']) :"";
  225. if($platid=="") return error_show(1005,"参数 plat_id 不能为空");
  226. $platform = Db::name("platform")->where(["id"=>$platid,"is_del"=>0])->find();
  227. if($platform==false){
  228. return error_show(1004,"平台信息不存在");
  229. }
  230. $num=isset($this->post['min_num'])&&$this->post['min_num']!=="" ? intval($this->post['min_num']):"";
  231. if($num==""){
  232. return error_show(1005,"参数min_num不能为空");
  233. }
  234. if ($platform['type']!=1) return error_show(1004,"平台为非对接平台");
  235. $good =Db::name("good_basic")->where(["spuCode"=>$spuCode,"is_del"=>0])->find();
  236. if($good==false){
  237. return error_show(1005,"商品数据未找到");
  238. }
  239. $nakelist = Db::name("good_nake")
  240. ->where(['spuCode'=>$spuCode,"is_del"=>0])
  241. ->where("min_num","<=",$num)
  242. ->order("min_num desc")
  243. ->find();
  244. if($nakelist==false){
  245. $nakelist = Db::name("good_nake")->where(['spuCode'=>$spuCode,"is_del"=>0])->order("min_num asc")->find();
  246. //非库存品的话,继续校验最小起订量
  247. //库存品的话,不足采购起订量的时候,取采购最小起订量
  248. if ($good['is_stock'] == 0) return error_show(1010, "起订量不足{$nakelist['min_num']}");
  249. }
  250. $catinfo = Db::name("cat_plat")->where(["cat_id"=>$good['cat_id'],"is_del"=>0,"platform_id"=>$platid])->find();
  251. if ($catinfo==false) return error_show(1005,"分类未设置平台毛利率");
  252. $budget = isset($catinfo['sale_rate']) ? $catinfo['sale_rate']/100:0;
  253. $top_cat_id = made($catinfo['cat_id']);//获取所有分类
  254. $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
  255. $sale_cost_fee = 0;
  256. if ($good['is_gold_price'] == 1 && $top_cat_id == 6) {
  257. // $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'];
  258. //系统售价=(贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用)/(1-成本售价/100)
  259. $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);
  260. //计算工艺费
  261. //销售工艺费=(( 商品重量* 供应商采购金价 + 采购成本工艺费* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)/(1-成本售价/100)-(包装费+加标费+证书费+产品裸价0+运费+其他费用) )/商品重量-供应商采购金价
  262. $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"];
  263. }else{
  264. // $saleprice = ($nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['delivery_fee'] + $nakelist['nake_fee']) / (1 - $budget);
  265. // (加标费+包装费+证书费+成本裸价+运费)/(1-成本售价/100)=系统售价
  266. // $saleprice = (加标费 + 包装费 + 证书费 + 运费 + 成本裸价+其他费用) / (1 - $budget);
  267. $saleprice = $nakelist['nake_total'] / (1 - $budget);
  268. }
  269. return app_show(0, "获取成功", ["sale_price" => round($saleprice, 2), 'new_cost_fee' => round($sale_cost_fee, 2)]);
  270. }
  271. }