Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\LogService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use app\main\constants\CacheConstants;
  7. use app\main\constants\PayConstants;
  8. use app\main\service\GoodsService;
  9. use think\Controller;
  10. use think\Db;
  11. use think\Request;
  12. use think\Exception;
  13. /**
  14. * 商品管理
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Goods extends Backend
  19. {
  20. /**
  21. * Goods模型对象
  22. */
  23. protected $model = null;
  24. protected $relationSearch = true;
  25. protected $categorylist = [];
  26. protected $categorydata = [];
  27. protected $category_pid = 14;//支付分类父级ID
  28. protected $searchFields = 'category_id';
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = model('Goods');
  33. $this->view->assign("typeList", $this->model->getTypeList());
  34. $this->view->assign("showTypeList", $this->model->getShowTypeList());
  35. $this->categorylist = model('category')->order('weigh desc,id desc')->where('pid', 'in', $this->category_pid)->select();
  36. $this->categorylist = $this->model->getCategoryList();
  37. foreach ($this->categorylist as $k => $v) {
  38. $this->categorydata[$v['id']] = ['name'=>$v['name'],'nickname'=>$v['nickname']];
  39. }
  40. $this->view->assign("parentList", $this->categorydata);
  41. }
  42. /**
  43. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  44. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  45. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  46. */
  47. public function index()
  48. {
  49. if ($this->request->isAjax()) {
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
  51. $total = $this->model
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->count();
  55. $list = $this->model
  56. ->where($where)
  57. ->with('category')
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->select();
  61. $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]);
  62. return json($result);
  63. }
  64. $this->assignconfig('category_list',model('Goods')->getCategoryList());
  65. $this->assignconfig('business',PayConstants::getBusinessLine());
  66. return $this->view->fetch();
  67. }
  68. public function select()
  69. {
  70. $params = $this->request->get();
  71. $goodsCategory = $params['goods_category'] ?? PayConstants::GOODS_CATEGORY_RECHARGE;
  72. $businessLine = $params['business_line'] ?? '-1';
  73. if ($this->request->isAjax()) {
  74. list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
  75. $adminId = $this->auth->id;
  76. $selectedIds = explode('_', $params['selected_ids']);
  77. if ($goodsCategory == PayConstants::GOODS_CATEGORY_RECHARGE) {
  78. $goodsTypes = [PayConstants::GOODS_TYPE_KD];
  79. if ($businessLine == PayConstants::BUSINESS_APP) {
  80. $goodsTypes[] = PayConstants::GOODS_TYPE_VIP;
  81. }
  82. if ($businessLine == PayConstants::BUSINESS_APP) {
  83. $goodsCategory = [$goodsCategory, 'test'];
  84. }
  85. } elseif ($goodsCategory == PayConstants::GOODS_CATEGORY_ACTIVITY) {
  86. $goodsTypes = [PayConstants::GOODS_TYPE_KD, PayConstants::GOODS_TYPE_VIP];
  87. } else {
  88. throw new Exception('参数错误');
  89. }
  90. $maps = [];
  91. if ($businessLine != '-1') {
  92. $maps[] = ['exp', "FIND_IN_SET('$businessLine', business_line)"];
  93. }
  94. //增加从充值渠道弹出商品列表逻辑,如果没有筛选条件&&是从商品列表tab弹出的框
  95. //如果recharge_show_type=-1 为 筛选已充值列表(包括已充值,全部,新用户,老用户);
  96. //recharge_show_type=-2 为 筛选未充值列表(包括未充值,全部,新用户,老用户)
  97. $rechargeShowType = $this->request->get("recharge_show_type", null);
  98. $filter = $this->request->get("filter", '');
  99. $filter = json_decode($filter, TRUE);
  100. $showType = $filter['show_type']??null;
  101. if(!$showType && $rechargeShowType){
  102. if($rechargeShowType == -1)
  103. {
  104. $maps['show_type'] =['neq',2];
  105. }
  106. elseif($rechargeShowType == -2)
  107. {
  108. $maps['show_type'] =['neq',1];
  109. }
  110. }
  111. List($list, $total) = GoodsService::instance()->getSelectGoodsList($adminId, $selectedIds, $where, $offset, $limit, $goodsCategory, $goodsTypes, $maps);
  112. $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]);
  113. return json($result);
  114. }
  115. $this->assignconfig('category_list',model('Goods')->getCategoryList());
  116. $this->assignconfig('business',PayConstants::getBusinessLine());
  117. $this->assignconfig('goods_category', $goodsCategory);
  118. $this->assignconfig('business_line', $businessLine);
  119. return $this->view->fetch();
  120. }
  121. public function add()
  122. {
  123. if ($this->request->isPost()) {
  124. $params = $this->request->post("row/a");
  125. if ($params) {
  126. if(empty($params['business_line']) || empty($params['category_id'])){
  127. $this->error("业务线或者分类不能为空");
  128. }
  129. if ($params['type'] == PayConstants::GOODS_TYPE_VIP) {
  130. $params['kandian'] = 0;
  131. $params['free_kandian'] = 0;
  132. } elseif ($params['type'] == PayConstants::GOODS_TYPE_KD) {
  133. $params['day'] = 0;
  134. }
  135. $params['business_line'] = implode(',',$params['business_line']);
  136. $params['category_id'] = implode(',',$params['category_id']);
  137. $result = $this->model->validate('goods.add')->save($params);
  138. if ($result === false) {
  139. $this->error($this->model->getError());
  140. }
  141. $this->success();
  142. }
  143. $this->error();
  144. }
  145. $this->assign('business',PayConstants::getBusinessLine());
  146. return $this->view->fetch();
  147. }
  148. /**
  149. * 编辑
  150. */
  151. public function edit($ids = NULL)
  152. {
  153. $row = $this->model->get(['id' => $ids]);
  154. if (!$row)
  155. $this->error(__('No Results were found'));
  156. if ($this->request->isPost()) {
  157. $params = $this->request->post("row/a");
  158. if(empty($params['business_line']) || empty($params['category_id'])){
  159. $this->error("业务线或者分类不能为空");
  160. }
  161. //如果原来的商品为VIP,现在改成不是VIP
  162. if($params['type'] != PayConstants::GOODS_TYPE_VIP && $row['type'] == PayConstants::GOODS_TYPE_VIP){
  163. $this->clearChannelVipConfig($row['id']);
  164. }
  165. //修改业务线的时候,不是微信商品
  166. if(
  167. !in_array(strval(PayConstants::BUSINESS_WECHAT),$params['business_line'])
  168. && in_array(strval(PayConstants::BUSINESS_WECHAT),explode(',',$row['business_line']))
  169. ){
  170. $this->clearChannelVipConfig($row['id']);
  171. }
  172. if($ids = model('Goods')->getGoodsCategoryIds([PayConstants::GOODS_CATEGORY_RECHARGE])){
  173. //修改分类,不是充值页
  174. if(
  175. !in_array(strval(current($ids)),$params['category_id'])
  176. && in_array(strval(current($ids)),explode(',',$row['category_id']))
  177. ){
  178. $this->clearChannelVipConfig($row['id']);
  179. }
  180. }
  181. $params['business_line'] = implode(',',$params['business_line']);
  182. $params['category_id'] = implode(',',$params['category_id']);
  183. if ($params['type'] == PayConstants::GOODS_TYPE_VIP) {
  184. $params['kandian'] = 0;
  185. $params['free_kandian'] = 0;
  186. } elseif ($params['type'] == PayConstants::GOODS_TYPE_KD) {
  187. $params['day'] = 0;
  188. }
  189. try {
  190. //是否采用模型验证
  191. if ($this->modelValidate) {
  192. $name = basename(str_replace('\\', '/', get_class($this->model)));
  193. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  194. $row->validate($validate);
  195. }
  196. GoodsService::instance()->removeCacheCustomGoods($row['id']);
  197. $result = $row->allowField(true)->save($params);
  198. if ($result !== false) {
  199. //清理下缓存吧
  200. Redis::instance()->del(CacheConstants::getGoodsInfoKey($row['id']));
  201. $this->success();
  202. } else {
  203. $this->error($row->getError());
  204. }
  205. } catch (\think\exception\PDOException $e) {
  206. $this->error($e->getMessage());
  207. }
  208. }
  209. $act_cate_id = model('Category')->where('type','default')->where('nickname','activity')->value('id');
  210. $row['business_line'] = explode(',',$row['business_line']);
  211. $row['category_id'] = explode(',',$row['category_id']);
  212. $is_show_free_day = (in_array($act_cate_id,$row['category_id']) && $row['type'] != PayConstants::GOODS_TYPE_VIP);
  213. $this->assign('is_show_free_day',$is_show_free_day);
  214. $this->assign('business',PayConstants::getBusinessLine());
  215. $this->view->assign("row", $row);
  216. return $this->view->fetch();
  217. }
  218. public function categorylist()
  219. {
  220. foreach ($this->categorydata as $key => $value) {
  221. $searchlist[] = ['id' => $key, 'name' => $value['name']];
  222. }
  223. $data = ['searchlist' => $searchlist];
  224. $this->success('', null, $data);
  225. return json($data);
  226. }
  227. private function clearChannelVipConfig($goods_id){
  228. //清空渠道的商品ID&关闭VIP状态
  229. $admin_ids = model('AdminConfig')->where('vip_goods_id',$goods_id)->column('admin_id');
  230. if($admin_ids){
  231. foreach($admin_ids as $admin_id){
  232. model('AdminConfig')->where('admin_id',$admin_id)->update(['vip_goods_id'=>0,'vip_state'=>0]);
  233. model('AdminConfig')->delAdminInfoAllCache($admin_id);
  234. }
  235. }
  236. }
  237. /**
  238. * 删除
  239. */
  240. public function del($ids = "")
  241. {
  242. if ($ids)
  243. {
  244. $pk = $this->model->getPk();
  245. $adminIds = $this->getDataLimitAdminIds();
  246. if (is_array($adminIds))
  247. {
  248. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  249. }
  250. $list = $this->model->where($pk, 'in', $ids)->select();
  251. $count = 0;
  252. foreach ($list as $k => $v)
  253. {
  254. //清空渠道的商品ID&关闭VIP状态
  255. $this->clearChannelVipConfig($v['id']);
  256. GoodsService::instance()->delCustomGoodsId($v['id']);
  257. $count += $v->delete();
  258. }
  259. if ($count)
  260. {
  261. $this->success();
  262. }
  263. else
  264. {
  265. $this->error(__('No rows were deleted'));
  266. }
  267. }
  268. $this->error(__('Parameter %s can not be empty', 'ids'));
  269. }
  270. }