MobileFlow.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\library\Auth;
  4. use app\common\controller\Backend;
  5. use app\common\library\MobileConstant;
  6. use app\common\service\MobileImport;
  7. use app\common\service\MobilePriceLogService;
  8. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  9. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  10. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  11. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  12. use think\App;
  13. use think\Db;
  14. use think\Exception;
  15. use think\exception\PDOException;
  16. use think\Loader;
  17. use think\Url;
  18. /**
  19. *
  20. *
  21. * @icon fa fa-mobile
  22. */
  23. class MobileFlow extends Backend
  24. {
  25. /**
  26. * Mobile模型对象
  27. * @var \app\admin\model\Mobile
  28. */
  29. protected $model = null;
  30. protected $relationSearch=true;
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new \app\admin\model\Mobile;
  35. $this->assign('status',\app\common\model\Mobile::flowStatus());
  36. }
  37. public function import()
  38. {
  39. //MobileImport::import(input('file'),$this->auth->id,2);
  40. $this->success();
  41. }
  42. /**
  43. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  44. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  45. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  46. */
  47. /**
  48. * 查看
  49. */
  50. public function index()
  51. {
  52. //当前是否为关联查询
  53. $this->relationSearch = false;
  54. //设置过滤方法
  55. $this->request->filter(['strip_tags', 'trim']);
  56. if ($this->request->isAjax()) {
  57. //如果发送的来源是Selectpage,则转发到Selectpage
  58. if ($this->request->request('keyField')) {
  59. return $this->selectpage();
  60. }
  61. list($where, $sort, $order, $offset, $limit) = $this->buildindexparams();
  62. $list = $this->model
  63. ->with(['info'])
  64. ->where('type',2)
  65. ->where($where)
  66. ->order($sort, $order)
  67. ->paginate($limit);
  68. foreach ($list as $row) {
  69. $rules=[];
  70. foreach (MobileConstant::getFilters() as $rule=>$field){
  71. foreach (array_values($field) as $column){
  72. if($row[$column]==1){
  73. $rules[]=$rule;
  74. }
  75. }
  76. }
  77. $row['rules']=array_unique($rules);
  78. }
  79. $result = array("total" => $list->total(), "rows" => $list->items());
  80. return json($result);
  81. }
  82. $this->assign('no_type',array_column(MobileConstant::getNoType(),'name','id'));
  83. $this->assign('network',array_column(MobileConstant::getNetwork(),'name','id'));
  84. $this->assign('filters',MobileConstant::getFilters());
  85. return $this->view->fetch();
  86. }
  87. public static $rule=[
  88. 'name'=>['require'],
  89. 'brand'=>['require'],
  90. 'amount_base'=>['require','number','egt:0'],
  91. 'stock_num'=>['require','integer'],
  92. 'summary'=>['require','max:50'],
  93. 'logo'=>['require'],
  94. //'describe'=>['require'],
  95. ];
  96. public function add()
  97. {
  98. if($this->request->isGet()){
  99. return view();
  100. }else{
  101. $data=input('row/a');
  102. $this->validate($data,self::$rule);
  103. $describe=$data['describe']??'';
  104. $free_app=$data['free_app']??'';
  105. $content=$data['content']??'';
  106. $flow_images=$data['images']??[];
  107. foreach (self::$rule as $key=>$r){
  108. if(in_array($key,$this->model->getTableInfo('','fields'))) {
  109. $this->model[$key] = $data[$key];
  110. }
  111. }
  112. $this->model['type']=\app\common\model\Mobile::FLOW;
  113. $this->model->save();
  114. $this->model->info()->save(compact('describe','free_app','content','flow_images'));
  115. $this->success();
  116. }
  117. }
  118. public function constant(){
  119. return json([
  120. 'no_type'=>MobileConstant::getNoType(),
  121. ]);
  122. }
  123. public function status(){
  124. return \app\common\model\MobileOrder::flowStatus();
  125. }
  126. public function edit($ids=null){
  127. if($this->request->isGet()){
  128. $mobile=$this->model->find($ids);
  129. $this->assign('row',$mobile);
  130. return view();
  131. }else{
  132. $data=input('row/a');
  133. self::$rule['status']=['in:'.implode(',',array_keys(\app\common\model\Mobile::flowStatus()))];
  134. $this->validate($data,self::$rule);
  135. $describe=$data['describe']??'';
  136. $free_app=$data['free_app']??'';
  137. $content=$data['content']??'';
  138. $flow_images=$data['images']??[];
  139. $mobiles=$this->model->whereIn('id',$ids)->select();
  140. foreach ($mobiles as $mobile) {
  141. Db::startTrans();
  142. foreach (self::$rule as $key=>$r){
  143. if(in_array($key,$this->model->getTableInfo('','fields'))) {
  144. $mobile[$key] = $data[$key];
  145. }
  146. }
  147. $mobile->save();
  148. $mobile->info->save(compact('describe','free_app','content','flow_images'));
  149. Db::commit();
  150. }
  151. $this->success();
  152. }
  153. }
  154. protected function buildindexparams($searchfields = null, $relationSearch = null)
  155. {
  156. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  157. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  158. $search = $this->request->get("search", '');
  159. $filter = $this->request->get("filter", '');
  160. $op = $this->request->get("op", '', 'trim');
  161. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  162. $order = $this->request->get("order", "DESC");
  163. $offset = $this->request->get("offset/d", 0);
  164. $limit = $this->request->get("limit/d", 999999);
  165. //新增自动计算页码
  166. $page = $limit ? intval($offset / $limit) + 1 : 1;
  167. if ($this->request->has("page")) {
  168. $page = $this->request->get("page/d", 1);
  169. }
  170. $this->request->get([config('paginate.var_page') => $page]);
  171. $filter = (array)json_decode($filter, true);
  172. $op = (array)json_decode($op, true);
  173. $filter = $filter ? $filter : [];
  174. $where = [];
  175. $alias = [];
  176. $bind = [];
  177. $name = '';
  178. $aliasName = '';
  179. if (!empty($this->model) && $this->relationSearch) {
  180. $name = $this->model->getTable();
  181. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  182. $aliasName = $alias[$name] . '.';
  183. }
  184. $sortArr = explode(',', $sort);
  185. foreach ($sortArr as $index => & $item) {
  186. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  187. }
  188. unset($item);
  189. $sort = implode(',', $sortArr);
  190. $adminIds = $this->getDataLimitAdminIds();
  191. if (is_array($adminIds)) {
  192. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  193. }
  194. if ($search) {
  195. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  196. foreach ($searcharr as $k => &$v) {
  197. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  198. }
  199. unset($v);
  200. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  201. }
  202. $index = 0;
  203. foreach ($filter as $k => $v) {
  204. if($k=='rules'){
  205. continue;
  206. }
  207. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  208. continue;
  209. }
  210. $sym = isset($op[$k]) ? $op[$k] : '=';
  211. if (stripos($k, ".") === false) {
  212. $k = $aliasName . $k;
  213. }
  214. $v = !is_array($v) ? trim($v) : $v;
  215. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  216. //null和空字符串特殊处理
  217. if (!is_array($v)) {
  218. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  219. $sym = strtoupper($v);
  220. }
  221. if (in_array($v, ['""', "''"])) {
  222. $v = '';
  223. $sym = '=';
  224. }
  225. }
  226. switch ($sym) {
  227. case '=':
  228. case '<>':
  229. $where[] = [$k, $sym, (string)$v];
  230. break;
  231. case 'LIKE':
  232. case 'NOT LIKE':
  233. case 'LIKE %...%':
  234. case 'NOT LIKE %...%':
  235. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  236. break;
  237. case '>':
  238. case '>=':
  239. case '<':
  240. case '<=':
  241. $where[] = [$k, $sym, intval($v)];
  242. break;
  243. case 'FINDIN':
  244. case 'FINDINSET':
  245. case 'FIND_IN_SET':
  246. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  247. $findArr = array_values($v);
  248. foreach ($findArr as $idx => $item) {
  249. $bindName = "item_" . $index . "_" . $idx;
  250. $bind[$bindName] = $item;
  251. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  252. }
  253. break;
  254. case 'IN':
  255. case 'IN(...)':
  256. case 'NOT IN':
  257. case 'NOT IN(...)':
  258. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  259. break;
  260. case 'BETWEEN':
  261. case 'NOT BETWEEN':
  262. $arr = array_slice(explode(',', $v), 0, 2);
  263. if (stripos($v, ',') === false || !array_filter($arr)) {
  264. continue 2;
  265. }
  266. //当出现一边为空时改变操作符
  267. if ($arr[0] === '') {
  268. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  269. $arr = $arr[1];
  270. } elseif ($arr[1] === '') {
  271. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  272. $arr = $arr[0];
  273. }
  274. $where[] = [$k, $sym, $arr];
  275. break;
  276. case 'RANGE':
  277. case 'NOT RANGE':
  278. $v = str_replace(' - ', ',', $v);
  279. $arr = array_slice(explode(',', $v), 0, 2);
  280. if (stripos($v, ',') === false || !array_filter($arr)) {
  281. continue 2;
  282. }
  283. //当出现一边为空时改变操作符
  284. if ($arr[0] === '') {
  285. $sym = $sym == 'RANGE' ? '<=' : '>';
  286. $arr = $arr[1];
  287. } elseif ($arr[1] === '') {
  288. $sym = $sym == 'RANGE' ? '>=' : '<';
  289. $arr = $arr[0];
  290. }
  291. $tableArr = explode('.', $k);
  292. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  293. //修复关联模型下时间无法搜索的BUG
  294. $relation = Loader::parseName($tableArr[0], 1, false);
  295. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  296. }
  297. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  298. break;
  299. case 'NULL':
  300. case 'IS NULL':
  301. case 'NOT NULL':
  302. case 'IS NOT NULL':
  303. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  304. break;
  305. default:
  306. break;
  307. }
  308. $index++;
  309. }
  310. if (!empty($this->model)) {
  311. $this->model->alias($alias);
  312. }
  313. if(isset($filter['rules']) && $filter['rules']){
  314. $temp=[];
  315. foreach (MobileConstant::getFilters()[$filter['rules']] as $pos=>$column){
  316. $temp[]=$column;
  317. }
  318. $where[]=[implode("|",$temp),1];
  319. }
  320. $model = $this->model;
  321. $where = function ($query) use ($where, $alias, $bind, &$model) {
  322. if (!empty($model)) {
  323. $model->alias($alias);
  324. $model->bind($bind);
  325. }
  326. foreach ($where as $k => $v) {
  327. if (is_array($v)) {
  328. call_user_func_array([$query, 'where'], $v);
  329. } else {
  330. $query->where($v);
  331. }
  332. }
  333. };
  334. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  335. }
  336. public function multi_down($ids){
  337. $flow=$this->model->whereIn('id',$ids)->select();
  338. foreach ($flow as $mobile){
  339. $mobile->flowMakeDown();
  340. }
  341. $this->success();
  342. }
  343. public function multi_up($ids){
  344. $flow=$this->model->whereIn('id',$ids)->select();
  345. foreach ($flow as $mobile){
  346. $mobile->flowMakeUp();
  347. }
  348. $this->success();
  349. }
  350. }