CatPlat.php 15 KB

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