Kennel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * 商家模型
  4. */
  5. class Kennel extends MongoAr {
  6. public $_id;
  7. public $name; // String : 商家名
  8. public $avatar; // String : 商家头像,url
  9. public $status = 1; // Int : 状态
  10. public $join_time; // Int : 加入时间
  11. public $tags; // Array : 商家标签
  12. public $weight = 0; // Int : 排序权重
  13. public $desc; // String : 详细说明文字
  14. public $desc_pics; // Array : 详情图片
  15. public $region; // Object : 地区,{province: '', city: '', area: ''}
  16. public $address; // String : 地址,参考ROrder模型address字段
  17. public $phone; // String : 电话
  18. public $order_count = 0; // Int : 总订单数(包括取消、赔付的订单)
  19. public $finished_order = 0; // Int : 完成订单数
  20. public $average; // Float 平均分
  21. public $favorable_rate; // Float : 好评率
  22. public $comment_count; // Int : 评价总数
  23. public $favorable_count; // Int : 好评数
  24. public $video; // Object : 视频 {url:'',width:0,height:'',avatar:'',length:0}
  25. public $type=2; // Int : 商家类型,1认证商家,2普通商家
  26. // 审核相关
  27. public $business_license; // 营业执照,{url: '', width: '', height: ''}
  28. public $certificate; // 协会证书,{url: '', width: '', height: ''}
  29. // 账号相关
  30. public $apply_time; // 申请时间
  31. public $account_type; // 收款账号类型,参考ping++,alipay支付宝,wx微信支付,etc.
  32. public $account_name; // 收款账号姓名,参考ping++,alipay支付宝,wx微信支付,etc.
  33. public $account; // 账号
  34. public $have_change; // 最近信息变更状态
  35. public $last_deny_reason; // 最近一次审核未通过理由
  36. public $last_change_time;//最后一次修改时间
  37. public $from;//数据来源 wozhua xinchong chongwushichang
  38. public static $status_option = [
  39. 0 => ['name' => '待审核', 'wx' => 0],
  40. 1 => ['name' => '正常', 'wx' => 1],
  41. -1 => ['name' => '已删除', 'wx' => 0],
  42. -2 => ['name' => '审核未通过', 'wx' => 0],
  43. ];
  44. public static $from_option = [
  45. 'wozhua' => ['name' => 'wozhua'],
  46. 'xinchong' => ['name' => 'xinchong'],
  47. 'shichang' => ['name' => 'shichang'],
  48. ];
  49. /**
  50. * 商家申请状态设置
  51. */
  52. public static $apply_status_option = [
  53. 0 => ['name' => '未申请'],
  54. 1 => ['name' => '正在申请'],
  55. 2 => ['name' => '申请通过'],
  56. -1 => ['name' => '申请失败'],
  57. // status <= -2没有权限
  58. -2 => ['name' => '商家管理员资格正在审核'],
  59. -3 => ['name' => '管理员被取消授权'],
  60. -100 => ['name' => '商家已被删除'],
  61. ];
  62. /**
  63. * 商家标签,根据需要拓展或修改
  64. */
  65. public static $tag_option = [
  66. 1 => ['name' => '30天保障'],
  67. 2 => ['name' => '100%环境实拍'],
  68. 3 => ['name' => '①宠①拍']
  69. ];
  70. public static $type_option = [
  71. 0 => ['name' => '未知'],
  72. 1 => ['name' => '认证商家'],
  73. 2 => ['name' => '普通卖家'],
  74. ];
  75. public function __construct($scenario = 'insert') {
  76. $this->setMongoDBComponent(Yii::app()->getComponent('mongodb_o2o'));
  77. parent::__construct($scenario);
  78. }
  79. public function getCollectionName() {
  80. return 'kennels';
  81. }
  82. public static function model($className = __CLASS__) {
  83. return parent::model($className);
  84. }
  85. public static function get($_id) {
  86. if (CommonFn::isMongoId($_id)) {
  87. $criteria = new EMongoCriteria();
  88. $criteria->_id('==', $_id);
  89. $model = self::model()->find($criteria);
  90. return $model;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * 根据微信Userid获取商家
  97. */
  98. public static function getByUserid($weixin_userid) {
  99. $criteria = new EMongoCriteria();
  100. $criteria->weixin_userid('==', $weixin_userid);
  101. $model = self::model()->find($criteria);
  102. return $model;
  103. }
  104. /**
  105. * 修改微信端状态
  106. */
  107. /*public function updateWeixinStatus($status) {
  108. $weixin_userid = $this->weixin_userid;
  109. $weixin_enable = self::$status_option[$status]['wx'];
  110. $user_data = [
  111. 'userid' => $weixin_userid,
  112. 'enable' => $weixin_enable,
  113. ];
  114. $option = WechatConfig::getIns()->getLinkOption();
  115. $secret = WechatConfig::getIns()->getSecret('admin_dev');
  116. $wechat = new QyWechat($option);
  117. $wechat->checkAuth($option['appid'], $secret);
  118. return $wechat->updateUser($user_data);
  119. }*/
  120. /**
  121. * MongoDB-Distinct获取商家内宠物类型列表
  122. * 过Redis,过期时间7200秒
  123. *
  124. * @return Array $list : 宠物类型列表,包含宠物类型信息
  125. * $list = [
  126. * [
  127. * 'id' => '...',
  128. * 'name' => '...',
  129. * etc.
  130. * ]
  131. * ];
  132. */
  133. public function getPetTypes() {
  134. $key = 'pet_types_'.(string)$this->_id;
  135. $cache = VariableRedis::get($key);
  136. if (!$cache) {
  137. $cache_data = [];
  138. } else {
  139. $cache_data = unserialize($cache);
  140. }
  141. if (!$cache_data || $cache_data['expire'] < time()) {
  142. $list = [];
  143. } else {
  144. $list = $cache_data['data'];
  145. }
  146. if (!$list) {
  147. $distinct_query = ['status' => 1, 'kennel' => $this->_id];
  148. $mongo = new MongoClient(DB_CONNETC);
  149. $db = $mongo->deal;
  150. $collection = $db->selectCollection('pets');
  151. $result = $collection->distinct('pet_type', $distinct_query);
  152. //$models = PetTypes::find()->where(['_id' => $result])->all();
  153. $criteria = new EMongoCriteria;
  154. $criteria->_id('in',$result);
  155. $cursor = PetTypes::model()->findAll($criteria);
  156. $list = [];
  157. foreach ($cursor as $key => $item) {
  158. $list[] = PetTypes::model()->parseRow($item->attributes);
  159. }
  160. $cache_data = ['data' => $list, 'expire' => time() + 7200];
  161. VariableRedis::set('pet_types_' . (string)$this->_id, serialize($cache_data));
  162. }
  163. return $list;
  164. }
  165. /**
  166. * 获取商家宠物列表
  167. * 过Redis,过期时间7200秒
  168. *
  169. * @param Int $limit : 显示数目
  170. *
  171. * @return Array $data : 返回结果,包含宠物列表及商家宠物总数
  172. * $data = [
  173. * 'list' => [
  174. * ['id' => '...', 'name' => '...', etc.],
  175. * ],
  176. * 'count' => 100,
  177. * ];
  178. */
  179. public function getPetList($limit = 3) {
  180. $key = 'pet_list_' . (string)$this->_id;
  181. $cache = VariableRedis::get($key);
  182. if (!$cache) {
  183. $cache_data = [];
  184. } else {
  185. $cache_data = unserialize($cache);
  186. }
  187. if (!$cache_data || $cache_data['expire'] < time()) {
  188. $criteria = new EMongoCriteria();
  189. $criteria->status('==',1);
  190. $criteria->kennel('==',$this->_id);
  191. $criteria->sort('sort_weight',EMongoCriteria::SORT_DESC);
  192. $criteria->sort('view',EMongoCriteria::SORT_DESC);
  193. $criteria->limit($limit);
  194. $models = DealPet::model()->findAll($criteria);
  195. $count = $models->count();
  196. $list = [];
  197. foreach ($models as $item) {
  198. $list[] = DealPet::model()->parseRow($item->attributes,array('id', 'name', 'region', 'price','avatar'));
  199. }
  200. $data = ['list' => $list, 'count' => $count];
  201. $cache_data = ['data' => $data, 'expire' => time() + 7200];
  202. VariableRedis::set($key, serialize($cache_data), 0);
  203. } else {
  204. $data = $cache_data['data'];
  205. }
  206. return $data;
  207. }
  208. /**
  209. * 获取该商家的一条评价(根据权重排序)
  210. * 优先从缓存中获取数据
  211. */
  212. public function getCommentOne() {
  213. $key = 'kennel_comment_one_'.(string)$this->_id;
  214. $cache = VariableRedis::get($key);
  215. if (ENVIRONMENT != 'product') {
  216. $cache = '';
  217. }
  218. if (!$cache) {
  219. $cache_data = [];
  220. } else {
  221. $cache_data = unserialize($cache);
  222. }
  223. if (!$cache_data) {
  224. $criteria = new EMongoCriteria();
  225. $criteria->kennel('==',$this->_id);
  226. $criteria->status('==',1);
  227. $criteria->sort('weight',EMongoCriteria::SORT_DESC);
  228. $cursor = DealComment::model()->findAll($criteria);
  229. if (!$cursor->count()) {
  230. return (object)[];
  231. }
  232. foreach(CommonFn::getRowsFromCursor($cursor) as $value){
  233. $comment = $value;break;
  234. }
  235. $data = DealComment::model()->parseRow($cursor->attributes,array('user_info', 'pet_info', 'time_str'));
  236. VariableRedis::set($key, serialize($data), 7200);
  237. return $data;
  238. } else {
  239. return $cache_data;
  240. }
  241. }
  242. public function parseRow($row, $output = []) {
  243. $newRow = [];
  244. $newRow['id'] = (string)$row['_id'];
  245. $newRow['name'] = CommonFn::get_val_if_isset($row, 'name', '');
  246. $newRow['avatar'] = CommonFn::get_val_if_isset($row, 'avatar', Yii::app()->params['defaultImage']);
  247. $newRow['status'] = CommonFn::get_val_if_isset($row, 'status', 0);
  248. $newRow['status_str'] = self::$status_option[$newRow['status']]['name'];
  249. $newRow['join_time'] = CommonFn::get_val_if_isset($row, 'join_time', 0);
  250. $newRow['join_time_str'] = $newRow['join_time'] != 0 ? date('Y-m-d', $newRow['join_time']) : '';
  251. $newRow['tags'] = CommonFn::get_val_if_isset($row, 'tags', []);
  252. $newRow['weight'] = CommonFn::get_val_if_isset($row, 'weight', 0);
  253. $newRow['desc'] = CommonFn::get_val_if_isset($row, 'desc', '');
  254. $newRow['desc'] = str_replace("<br>","\n",$newRow['desc']);
  255. $newRow['desc_pics'] = CommonFn::get_val_if_isset($row, 'desc_pics', []);
  256. $newRow['region'] = CommonFn::get_val_if_isset($row, 'region', []);
  257. $newRow['address'] = CommonFn::get_val_if_isset($row, 'address', '');
  258. $newRow['phone'] = CommonFn::get_val_if_isset($row, 'phone', '');
  259. $newRow['order_count'] = CommonFn::get_val_if_isset($row, 'order_count', 0);
  260. $newRow['finished_order'] = CommonFn::get_val_if_isset($row, 'finished_order', 0);
  261. $newRow['favorable_rate'] = CommonFn::get_val_if_isset($row, 'favorable_rate', 0.0);
  262. $newRow['comment_count'] = CommonFn::get_val_if_isset($row, 'comment_count', 0);
  263. $newRow['favorable_count'] = CommonFn::get_val_if_isset($row, 'favorable_count', 0);
  264. $newRow['video'] = CommonFn::get_val_if_isset($row, 'video', []);
  265. $newRow['type'] = CommonFn::get_val_if_isset($row, 'type', 2);
  266. // 审核相关
  267. $newRow['business_license'] = CommonFn::get_val_if_isset($row, 'business_license', []);
  268. $newRow['certificate'] = CommonFn::get_val_if_isset($row, 'certificate', []);
  269. // 账号
  270. $newRow['account_type'] = CommonFn::get_val_if_isset($row, 'account_type', '');
  271. $newRow['account_name'] = CommonFn::get_val_if_isset($row, 'account_name', '');
  272. $newRow['account'] = CommonFn::get_val_if_isset($row, 'account', '');
  273. $newRow['last_deny_reason'] = CommonFn::get_val_if_isset($row, 'last_deny_reason', '');
  274. $newRow['have_change'] = CommonFn::get_val_if_isset($row, 'have_change', 0);
  275. $newRow['pet_types'] = $this->getPetTypes();
  276. if($newRow['favorable_rate']>0){
  277. $newRow['average'] = round($newRow['favorable_rate'] * 5);
  278. }else{
  279. $newRow['average'] = 5;
  280. }
  281. $cache = new ARedisCache();
  282. $key = 'pets_count_'.$newRow['id'];
  283. $count_cache = $cache->get($key);
  284. $_count=0;
  285. if($count_cache){
  286. $_count = $count_cache;
  287. }else{
  288. $criteria = new EMongoCriteria;
  289. $criteria->status('in',array(1, 2,));
  290. $criteria->kennel('==',$row['_id']);
  291. $cursor = DealPet::model()->findAll($criteria);
  292. $_count = $cursor->count();
  293. $cache->set($key,$_count,86400);
  294. }
  295. $newRow['pets_count'] = $_count;
  296. $manager = KennelManager::getByKennel($row['_id']);
  297. $manager_user_id = '';
  298. if ($manager) {
  299. $manager_user_id = $newRow['user_id'];
  300. }else{
  301. $manager_user_id = Yii::app()->params['kefu_user'];
  302. }
  303. if(APPLICATION=='admin'){
  304. $newRow['from'] = CommonFn::get_val_if_isset($row, 'from', '');
  305. if ($manager) {
  306. $newRow['id_card'] = $manager->id_card? $manager->id_card : ['url' => '', 'width' => 0, 'height' => 0];
  307. $newRow['id_card_inhand'] = $manager->id_card_inhand? $manager->id_card_inhand : ['url' => '', 'width' => 0, 'height' => 0];
  308. $newRow['user_id'] = (string)$manager->user;
  309. } else {
  310. $newRow['id_card'] = ['url' => '', 'width' => 0, 'height' => 0];
  311. $newRow['id_card_inhand'] = ['url' => '', 'width' => 0, 'height' => 0];
  312. $newRow['user_id'] = '';
  313. }
  314. }
  315. $newRow['manager_user_id'] = $manager_user_id;
  316. return $this->output($newRow, $output);
  317. }
  318. }