Goldprice.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. //金价
  7. class Goldprice extends BaseController
  8. {
  9. public $post="";
  10. public $gold=[
  11. 1=>'18K',2=>'24K',3=>'白银'
  12. ];
  13. public $rate=[
  14. 1=> 0,2=>1,3=>3,4=>5,5=>6,6=>9,7=>13
  15. ];
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. $this->post=$this->request->post();
  20. }
  21. public function linst(){
  22. $data=[];
  23. foreach ($this->gold as $key=>$value){
  24. $v =[];
  25. $v['type']=$key;
  26. $v['type_cn']=$value;
  27. $data[]=$v;
  28. }
  29. return app_show(0,"获取成功",$data);
  30. }
  31. public function create(){
  32. $type = isset($this->post['type']) && $this->post['type'] !=="" ?intval($this->post['type']):"";
  33. if($type==""){
  34. return error_show(1002,"参数type不能为空");
  35. }
  36. $price = isset($this->post['price']) && $this->post['price'] !=="" ? floatval($this->post['price']):"";
  37. if($price==""){
  38. return error_show(1002,"参数price不能为空");
  39. }
  40. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  41. if($token==''){
  42. return error_show(105,"参数token不能为空");
  43. }
  44. $user =GetUserInfo($token);
  45. if(empty($user)||$user['code']!=0){
  46. return error_show(1002,"创建人数据不存在");
  47. }
  48. $action_id= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  49. $action_name= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  50. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"1";
  51. $data=[
  52. "type"=>$type,
  53. "price"=>$price,
  54. "action_name"=>$action_name,
  55. "action_id"=>$action_id,
  56. "status"=>$status,
  57. "is_del"=>0,
  58. "addtime"=>date("Y-m-d H:i:s")
  59. ];
  60. $datainfo = Db::name('gold_price1')->insert($data);
  61. if($datainfo){
  62. return error_show(0,"新建成功");
  63. }else{
  64. return error_show(1002,"新建失败");
  65. }
  66. }
  67. public function list(){
  68. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  69. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  70. $where =[["is_del","=",0]];
  71. $type = isset($this->post['type']) && $this->post['type'] !=="" ? trim($this->post['type']):"";
  72. if($type!=""){
  73. $where[]=['type',"=",$type];
  74. }
  75. $action_name= isset($this->post['action_name']) && $this->post['action_name'] !=="" ? trim($this->post['action_name']):"";
  76. if($action_name!=""){
  77. $where[]=['action_name',"like","%$action_name%"];
  78. }
  79. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  80. if($start!==""){
  81. $where[]=['addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  82. }
  83. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  84. if($end!==""){
  85. $where[]=['addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  86. }
  87. $count = Db::name('gold_price1') ->where($where)->count();
  88. $total = ceil($count / $size);
  89. $page = $page >= $total ? $total : $page;
  90. $list = Db::name('gold_price1')->where($where)->page($page,$size)->order("addtime desc")->select();
  91. $data=[];
  92. foreach ($list as $value){
  93. $value['type_cn']=$this->gold[$value['type']];
  94. $data[]=$value;
  95. }
  96. return app_show(0,"获取成功",['list'=>$data,'count'=>$count,]);
  97. }
  98. public function del(){
  99. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  100. if($id===""){
  101. return error_show(1004,"参数id不能为空");
  102. }
  103. $str= Db::name('gold_price1')->where(['id'=>$id,'is_del'=>0])->find();
  104. if(empty($str)){
  105. return error_show(1002,"未找到数据");
  106. }
  107. $end = Db::name('gold_price1')->update(['id'=>$id,'is_del'=>1]);
  108. if($end){
  109. return error_show(0,"删除成功");
  110. }else{
  111. return error_show(1002,"删除失败");
  112. }
  113. }
  114. public function status(){
  115. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  116. if($id==""){
  117. return error_show(1002,"参数id不能为空");
  118. }
  119. $info = Db::name("gold_price1")->where([["id","=",$id],["is_del","=",0]])->find();
  120. if(!$info){
  121. return error_show(1002,"未找到对应数据");
  122. }
  123. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  124. if($status===""){
  125. return error_show(1002,"参数status不能为空");
  126. }
  127. if(!in_array($status,[0,1])){
  128. return error_show(1002,"参数status无效");
  129. }
  130. $info['status']=$status;
  131. $msg = $status==1?"启用":"禁用";
  132. $update = Db::name("gold_price1")->save($info);
  133. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  134. }
  135. public function edit(){
  136. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  137. if($id==""){
  138. return error_show(1002,"参数id不能为空");
  139. }
  140. $info = Db::name("gold_price1")->where(['id'=>$id,"is_del"=>0])->find();
  141. if($info==""){
  142. return error_show(1003,"未找到数据");
  143. }
  144. // $type = isset($this->post['type']) && $this->post['type'] !=="" ?trim($this->post['type']):"";
  145. // if($type==""){
  146. // return error_show(1002,"参数type不能为空");
  147. // }
  148. $price = isset($this->post['price']) && $this->post['price'] !=="" ? floatval($this->post['price']):"";
  149. if($price==""){
  150. return error_show(1002,"参数price不能为空");
  151. }
  152. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  153. if($token==''){
  154. return error_show(105,"参数token不能为空");
  155. }
  156. $user =GetUserInfo($token);
  157. if(empty($user)||$user['code']!=0){
  158. return error_show(1002,"创建人数据不存在");
  159. }
  160. $action_id= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  161. $action_name= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  162. //$status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  163. $data=[
  164. "type"=>$info['type'],
  165. "price"=>$price,
  166. "action_name"=>$action_name,
  167. "action_id"=>$action_id,
  168. "status"=>1,
  169. "is_del"=>0,
  170. "addtime"=>date("Y-m-d H:i:s")
  171. ];
  172. $datainfo = Db::name('gold_price1')->insert($data);
  173. if($datainfo){
  174. return error_show(0,"新建成功");
  175. }else{
  176. return error_show(1002,"新建失败");
  177. }
  178. }
  179. public function lastlist(){
  180. $where = [['g.is_del', '=', 0]];
  181. $type = isset($this->post['type']) && $this->post['type'] != "" ? intval($this->post['type']) : "";
  182. if ($type !== "") {
  183. $where[] = ['g.type', '=', $type];
  184. }
  185. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  186. if ($company_name !== "") $where[] = ["g.action_id", 'in', get_company_item_user_by_name($company_name)];
  187. $ids = Db::name("gold_price1")
  188. ->where(['is_del' => 0])
  189. ->group("type")
  190. ->column("max(id) as id");
  191. $where[] = ['g.id', 'in', $ids];
  192. $list = Db::name("gold_price1")
  193. ->alias('g')
  194. ->where($where)
  195. ->select()
  196. ->toArray();
  197. $all_createrid = array_column($list,'action_id');
  198. $item = get_company_name_by_uid($all_createrid);
  199. $data = [];
  200. foreach ($list as $value) {
  201. $value['type_cn'] = $this->gold[$value['type']];
  202. $value['company_name'] = $item[$value['action_id']]??'';
  203. $data[] = $value;
  204. }
  205. return app_show(0, "获取成功", $data);
  206. }
  207. public function ratelist(){
  208. $data =[];
  209. foreach ($this->rate as $key=>$value){
  210. $v =[];
  211. $v['rate_cn']=$key;
  212. $v['rate']=$value;
  213. $data[]=$v;
  214. }
  215. return app_show(0,"获取成功",$data);
  216. }
  217. }