MerchantDao.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-16
  7. *
  8. *
  9. */
  10. namespace app\common\dao\system\merchant;
  11. use app\common\dao\BaseDao;
  12. use app\common\enum\merchant\MerchantEnum;
  13. use app\common\model\system\merchant\Merchant;
  14. use app\common\model\user\User;
  15. use app\entity\CommonEntity;
  16. use app\entity\data\merchant\MerchantEntity;
  17. use think\db\BaseQuery;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\Model;
  22. class MerchantDao extends BaseDao
  23. {
  24. /**
  25. * @return string
  26. * @author xaboy
  27. * @day 2020-04-16
  28. */
  29. protected function getModel(): string
  30. {
  31. return Merchant::class;
  32. }
  33. /**
  34. * @param array $where
  35. * @return BaseQuery
  36. * @author xaboy
  37. * @day 2020-04-16
  38. */
  39. public function search(array $where, $is_del = 0)
  40. {
  41. $query = Merchant::getDB()->when($is_del !== null, function ($query) use ($is_del) {
  42. $query->where('is_del', $is_del);
  43. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  44. getModelTime($query, $where['date']);
  45. });
  46. if (isset($where['keyword']) && $where['keyword'])
  47. $query->whereLike('mer_name|mer_keyword|mer_phone', "%{$where['keyword']}%");
  48. if (isset($where['status']) && $where['status'])
  49. $query->where('status', $where['status']);
  50. if (isset($where['mer_state']) && $where['mer_state'])
  51. $query->where('mer_state', $where['mer_state']);
  52. if (isset($where['category_id']) && $where['category_id'])
  53. $query->where('category_id', $where['category_id']);
  54. if (isset($where['city_id']) && $where['city_id'])
  55. $query->where('city_id', $where['city_id']);
  56. $order = $where['order'] ?? '';
  57. $query->when($order, function ($query) use ($order) {
  58. if (in_array($order, ['rate', 'sales']) && $order == 'rate')
  59. $query->order('is_best DESC, product_score DESC,service_score DESC,postage_score DESC,sort DESC');
  60. else
  61. $query->order('is_best DESC, sales DESC,sort DESC');
  62. }, function ($query) use ($order) {
  63. $query->order('is_best DESC, sort DESC,sales DESC');
  64. })->when(isset($where['account']) && $where['account'] !== '', function ($query) use ($where) {
  65. $uid = User::where('account|nickname|phone', 'like', "%{$where['account']}%")
  66. ->column('uid');
  67. $query->where('uid', 'in', $uid);
  68. });
  69. return $query;
  70. }
  71. /**
  72. * @param array $where
  73. * @param int $is_del
  74. * @return BaseQuery
  75. */
  76. public function searchLeft(array $where, $is_del = 0)
  77. {
  78. $query = Merchant::getDB()->when($is_del !== null, function ($query) use ($is_del) {
  79. $query->where('merchant.is_del', $is_del);
  80. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  81. getModelTime($query, $where['date'], 'merchant.create_time');
  82. });
  83. if (isset($where['keyword']) && $where['keyword'])
  84. $query->whereLike('merchant.mer_name|merchant.mer_keyword|merchant.mer_phone', "%{$where['keyword']}%");
  85. if (isset($where['status']) && $where['status'])
  86. $query->where('merchant.status', $where['status']);
  87. if (isset($where['mer_state']) && $where['mer_state'])
  88. $query->where('merchant.mer_state', $where['mer_state']);
  89. if (isset($where['category_id']) && $where['category_id'])
  90. $query->where('merchant.category_id', $where['category_id']);
  91. if (isset($where['city_id']) && $where['city_id'])
  92. $query->where('merchant.city_id', $where['city_id']);
  93. $order = $where['order'] ?? '';
  94. $query->when($order, function ($query) use ($order) {
  95. if (in_array($order, ['rate', 'sales']) && $order == 'rate')
  96. $query->order('merchant.is_best DESC, merchant.product_score DESC,merchant.service_score DESC,merchant.postage_score DESC,merchant.sort DESC');
  97. else
  98. $query->order('merchant.is_best DESC, merchant.sales DESC,merchant.sort DESC');
  99. }, function ($query) use ($order) {
  100. $query->order('merchant.is_best DESC, merchant.sort DESC,merchant.sales DESC');
  101. })->when(isset($where['account']) && $where['account'] !== '', function ($query) use ($where) {
  102. $uid = User::where('account|nickname|phone', 'like', "%{$where['account']}%")
  103. ->column('uid');
  104. $query->where('merchant.uid', 'in', $uid);
  105. });
  106. $query->where('merchantIntention.status', 1);
  107. return $query;
  108. }
  109. /**
  110. * @param array $where
  111. * @return BaseQuery
  112. * @author cabbage
  113. * @day 2020-10-31
  114. */
  115. public function search_mer(array $where, $is_del = 0)
  116. {
  117. $query = Merchant::getDB()->when($is_del !== null, function ($query) use ($is_del) {
  118. $query->where('is_del', $is_del);
  119. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  120. getModelTime($query, $where['date']);
  121. });
  122. if (isset($where['keyword']) && $where['keyword'])
  123. $query->whereLike('mer_name|mer_keyword|mer_phone', "%{$where['keyword']}%");
  124. if (isset($where['status']) && $where['status'])
  125. $query->where('status', $where['status']);
  126. if (isset($where['mer_state']) && $where['mer_state'])
  127. $query->where('mer_state', $where['mer_state']);
  128. if (isset($where['category_id']) && $where['category_id'])
  129. $query->where('category_id', $where['category_id']);
  130. if (isset($where['sxy_submerchant_id'])){
  131. $query->whereNotNull('sxy_submerchant_id');
  132. }
  133. return $query;
  134. }
  135. /**
  136. * @param int $id
  137. * @return array|Model|null
  138. * @throws DataNotFoundException
  139. * @throws DbException
  140. * @throws ModelNotFoundException
  141. * @author xaboy
  142. * @day 2020-04-17
  143. */
  144. public function get( $id)
  145. {
  146. return Merchant::getInstance()->where('is_del', 0)->find($id);
  147. }
  148. /**
  149. * @param $id
  150. * @author Qinii
  151. */
  152. public function apiGetOne($id)
  153. {
  154. return Merchant::getInstance()->where(['is_del'=>0,'status' => 1,'mer_state' => 1])->find($id);
  155. }
  156. /**
  157. * @param $id
  158. * @author Qinii
  159. */
  160. public function addrenqi($id)
  161. {
  162. return Merchant::getInstance()->where('mer_id',$id)->inc('renqizhi',1)->update();
  163. }
  164. /**
  165. * @param int $merId
  166. * @author Qinii
  167. */
  168. public function incCareCount(int $merId)
  169. {
  170. ($this->getModel()::getDB())->where($this->getPk(),$merId)->inc('care_count',1)->update();
  171. }
  172. /**
  173. * @param int $merId
  174. * @author Qinii
  175. */
  176. public function decCareCount(int $merId)
  177. {
  178. ($this->getModel()::getDB())->where($this->getPk(),$merId)->where('care_count','>',0)->dec('care_count',1)->update();
  179. }
  180. public function dateMerchantNum($date)
  181. {
  182. return Merchant::getDB()->where('is_del', 0)->when($date, function ($query, $date) {
  183. getModelTime($query, $date);
  184. })->count();
  185. }
  186. /**
  187. * TODO 获取复制商品次数
  188. * @param int $merId
  189. * @return mixed
  190. * @author Qinii
  191. * @day 2020-08-06
  192. */
  193. public function getCopyNum(int $merId)
  194. {
  195. return Merchant::getDB()->where('mer_id',$merId)->value('copy_product_num');
  196. }
  197. /**
  198. * TODO 变更复制次数
  199. * @param int $merId
  200. * @param int $num 正负数
  201. * @return mixed
  202. * @author Qinii
  203. * @day 2020-08-06
  204. */
  205. public function changeCopyNum(int $merId,int $num)
  206. {
  207. return $this->getModel()::where('mer_id',$merId)->inc('copy_product_num',$num)->update();
  208. }
  209. public function getConsumptionScoreMer(){
  210. return $this->getModel()::where('consumption_score',1)->field('mer_id,uid') -> select();
  211. }
  212. /**
  213. * 根据 商户ID 获取 未删除的 商户信息
  214. * @param int $merId
  215. * @return \app\entity\CommonEntity
  216. */
  217. public function getMerchantEntityByMerchantId(int $merId): \app\entity\CommonEntity
  218. {
  219. $data = [];
  220. try {
  221. /** @var Merchant $model */
  222. $model = $this->getModel();
  223. $dataRes = $model::getDB()
  224. ->where('mer_id', '=', $merId)
  225. ->where('is_del', '=', MerchantEnum::IS_DEL['No']['code'])
  226. ->find();
  227. if (!empty($dataRes)) {
  228. $data = $dataRes->toArray();
  229. }
  230. } catch (\Exception $e) {
  231. }
  232. return MerchantEntity::newInstance($data);
  233. }
  234. /**
  235. * @param array $idList
  236. * @return CommonEntity[]
  237. */
  238. public function getMerchantEntityListByMerchantIdList(array $idList): array
  239. {
  240. $entityList = [];
  241. try {
  242. /** @var Merchant $model */
  243. $model = $this->getModel();
  244. $dataRes = $model::getDB()
  245. ->whereIn('mer_id', $idList)
  246. ->where('is_del', '=', MerchantEnum::IS_DEL['No']['code'])
  247. ->select()
  248. ->toArray();
  249. if (!empty($dataRes)) {
  250. $entityList = array_map(function ($data) {
  251. return MerchantEntity::newInstance($data);
  252. }, $dataRes);
  253. }
  254. } catch (\Exception $e) {
  255. }
  256. return $entityList;
  257. }
  258. }