SystemSupplierServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\supplier;
  12. use app\dao\supplier\SystemSupplierDao;
  13. use app\dao\system\admin\SystemAdminDao;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderServices;
  16. use app\services\order\StoreOrderRefundServices;
  17. use app\services\product\branch\StoreBranchProductServices;
  18. use app\services\system\attachment\SystemAttachmentServices;
  19. use app\services\system\SystemUserApplyServices;
  20. use crmeb\exceptions\AdminException;
  21. use think\exception\ValidateException;
  22. /**
  23. * 供应商
  24. * Class SystemSupplierServices
  25. * @package app\services\supplier
  26. * @mixin SystemSupplierDao
  27. */
  28. class SystemSupplierServices extends BaseServices
  29. {
  30. protected $adminDao = null;
  31. /**
  32. * 构造方法
  33. * SystemSupplierServices constructor.
  34. * @param SystemSupplierDao $dao
  35. * @param SystemAdminDao $adminDao
  36. */
  37. public function __construct(SystemSupplierDao $dao, SystemAdminDao $adminDao)
  38. {
  39. $this->dao = $dao;
  40. $this->adminDao = $adminDao;
  41. }
  42. /**
  43. * 获取供应商
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\DataNotFoundException
  47. */
  48. public function getSupplierInfo(int $id, string $field = '*', array $with = [])
  49. {
  50. $info = $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  51. if (!$info) {
  52. throw new ValidateException('供应商不存在');
  53. }
  54. return $info;
  55. }
  56. /**
  57. * 供应商列表
  58. * @param array $where
  59. * @param array $field
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function getSupplierList(array $where, array $field = ['*'])
  66. {
  67. [$page, $limit] = $this->getPageValue();
  68. $list = $this->dao->getSupplierList($where, $field, $page, $limit);
  69. if ($list) {
  70. $prefix = config('admin.supplier_prefix');
  71. foreach ($list as &$item) {
  72. if (isset($item['add_time']) && $item['add_time']) $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  73. $item['prefix'] = $prefix;
  74. }
  75. }
  76. $count = $this->dao->count($where);
  77. return compact('list', 'count');
  78. }
  79. /**
  80. * 保存供应商
  81. * @param array $data
  82. * @return mixed
  83. */
  84. public function create(array $data)
  85. {
  86. if ($this->adminDao->count(['account' => $data['account'], 'admin_type' => 4, 'is_del' => 0])) {
  87. throw new AdminException('管理员账号已存在');
  88. }
  89. return $this->transaction(function () use ($data) {
  90. $adminData = [
  91. 'pwd' => $this->passwordHash($data['pwd']),
  92. 'admin_type' => 4,
  93. 'account' => $data['account'],
  94. 'roles' => '',
  95. 'real_name' => $data['name'],
  96. 'phone' => $data['phone'],
  97. 'add_time' => time(),
  98. 'level' => 0
  99. ];
  100. unset($data['pwd'], $data['conf_pwd'], $data['account']);
  101. // 创建管理员
  102. $res = $this->adminDao->save($adminData);
  103. if (!$res) throw new AdminException('管理员添加失败');
  104. $data['admin_id'] = (int)$res->id;
  105. $data['add_time'] = time();
  106. // 创建供应商
  107. if ($data['valid_time'] ?? '') $data['valid_time'] = strtotime($data['valid_time']);
  108. $relation_id = $this->dao->save($data)->id;
  109. if (!$relation_id) throw new AdminException('供应商添加失败');
  110. $this->adminDao->update($res->id, ['relation_id' => $relation_id]);
  111. return $relation_id;
  112. });
  113. }
  114. /**
  115. * 修改管理员
  116. * @param array $data
  117. * @return mixed
  118. */
  119. public function save(int $id, array $data)
  120. {
  121. if (!$supplierInfo = $this->dao->get($id)) {
  122. throw new AdminException('供应商不存在,无法修改');
  123. }
  124. if ($supplierInfo->is_del) {
  125. throw new AdminException('供应商已经删除');
  126. }
  127. if (!$adminInfo = $this->adminDao->get($supplierInfo['admin_id'])) {
  128. throw new AdminException('管理员不存在,无法修改');
  129. }
  130. if ($adminInfo->is_del) {
  131. throw new AdminException('管理员已经删除');
  132. }
  133. //修改账号
  134. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->adminDao->isAccountUsable($data['account'], $supplierInfo['admin_id'], 4)) {
  135. throw new AdminException('管理员账号已存在');
  136. }
  137. return $this->transaction(function () use ($id, $data, $adminInfo, $supplierInfo) {
  138. $adminData = [
  139. 'pwd' => $this->passwordHash($data['pwd']),
  140. 'real_name' => $data['name'] ?? $adminInfo->real_name,
  141. 'phone' => $data['phone'] ?? $adminInfo->phone,
  142. 'account' => $data['account'] ?? $adminInfo->account
  143. ];
  144. // 修改管理员
  145. $res = $this->adminDao->update($adminInfo['id'], $adminData);
  146. if (!$res) throw new AdminException('管理员修改失败');
  147. // 修改供应商
  148. unset($data['pwd'], $data['conf_pwd'], $data['account']);
  149. if ($data['valid_time'] ?? '') $data['valid_time'] = strtotime($data['valid_time']);
  150. $this->dao->update($id, $data);
  151. $res1 = $supplierInfo->save();
  152. if (!$res1) throw new AdminException('供应商修改失败');
  153. return true;
  154. });
  155. }
  156. /**
  157. * @param int $id
  158. * @return mixed
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function delete(int $id)
  164. {
  165. if (!$supplierInfo = $this->dao->get($id)) {
  166. throw new AdminException('供应商不存在,无法修改');
  167. }
  168. if ($supplierInfo->is_del) {
  169. throw new AdminException('供应商已经删除');
  170. }
  171. if (!$adminInfo = $this->adminDao->get($supplierInfo['admin_id'])) {
  172. throw new AdminException('管理员不存在,无法删除');
  173. }
  174. if ($adminInfo->is_del) {
  175. throw new AdminException('管理员已经删除');
  176. }
  177. /** @var StoreOrderServices $storeOrderServices */
  178. $storeOrderServices = app()->make(StoreOrderServices::class);
  179. $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 0]);
  180. if (!$orderCount) {
  181. $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 1]);
  182. if (!$orderCount) {
  183. $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 5]);
  184. }
  185. }
  186. if ($orderCount) {
  187. return $this->fail('删除失败,该供应商还有待处理订单');
  188. }
  189. return $this->transaction(function () use ($id, $supplierInfo, $adminInfo) {
  190. $adminInfo->status = 0;
  191. $adminInfo->is_del = 1;
  192. // 修改管理员
  193. $res = $adminInfo->save();
  194. if (!$res) throw new AdminException('管理员删除失败');
  195. $supplierInfo->is_show = 0;
  196. $supplierInfo->is_del = 1;
  197. // 修改供应商
  198. $res1 = $supplierInfo->save();
  199. if (!$res1) throw new AdminException('供应商删除失败');
  200. /** @var StoreBranchProductServices $storeBranchProducesServices */
  201. $storeBranchProducesServices = app()->make(StoreBranchProductServices::class);
  202. //删除供应商商品
  203. $storeBranchProducesServices->deleteProducts([], 2, $id);
  204. /** @var SystemAttachmentServices $attach */
  205. $attach = app()->make(SystemAttachmentServices::class);
  206. //删除附件
  207. $attach->delAttachment([], 4, $id);
  208. return true;
  209. });
  210. }
  211. /**
  212. * 平台供应商运营统计
  213. * @param int $supplierId
  214. * @param array $time
  215. * @return array
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\DbException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. */
  220. public function supplierChart(int $supplierId, array $time)
  221. {
  222. $list = $this->dao->getSupplierList(['is_del' => 0, 'is_show' => 1], ['id', 'supplier_name']);
  223. /** @var StoreOrderServices $orderServices */
  224. $orderServices = app()->make(StoreOrderServices::class);
  225. /** @var StoreOrderRefundServices $orderRefundServices */
  226. $orderRefundServices = app()->make(StoreOrderRefundServices::class);
  227. $where = ['time' => $time];
  228. $order_where = ['paid' => 1, 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  229. $refund_where = ['refund_type' => 6];
  230. foreach ($list as &$item) {
  231. $supplier_where = ['supplier_id' => $item['id']];
  232. $item['order_price'] = $orderServices->sum($where + $supplier_where + $order_where, 'pay_price', true);
  233. $item['order_count'] = $orderServices->count($where + $supplier_where + $order_where);
  234. $item['refund_order_price'] = $orderRefundServices->sum($where + $supplier_where + $refund_where, 'refunded_price', true);
  235. $item['refund_order_count'] = $orderRefundServices->count($where + $supplier_where + $refund_where);
  236. }
  237. return $list;
  238. }
  239. /**
  240. * 供应商选择列表
  241. * @param array $where
  242. * @param array $field
  243. * @return array
  244. * @throws \think\db\exception\DataNotFoundException
  245. * @throws \think\db\exception\DbException
  246. * @throws \think\db\exception\ModelNotFoundException
  247. */
  248. public function getSupplierSearch(array $where, array $field = ['*'])
  249. {
  250. return $this->dao->getSupplierList($where, $field, 0, 0);
  251. }
  252. /**
  253. * 供应商入住审核通过创建数据
  254. * @param int $applyId
  255. * @param array $info
  256. * @return array
  257. * @throws \think\db\exception\DataNotFoundException
  258. * @throws \think\db\exception\DbException
  259. * @throws \think\db\exception\ModelNotFoundException
  260. */
  261. public function verifyAgreeCreate(int $applyId, array $info = [])
  262. {
  263. if (!$applyId) {
  264. throw new ValidateException('缺少申请ID');
  265. }
  266. /** @var SystemUserApplyServices $applyServices */
  267. $applyServices = app()->make(SystemUserApplyServices::class);
  268. if (!$info) {
  269. $info = $applyServices->get($applyId);
  270. if (!$info) {
  271. throw new ValidateException('申请数据不存在');
  272. }
  273. $info = $info->toArray();
  274. }
  275. $data = [
  276. 'supplier_name' => $info['system_name'],
  277. 'account' => $this->getAccount($info['phone']),
  278. 'phone' => $info['phone'],
  279. 'name' => $info['name'],
  280. 'pwd' => substr($info['phone'], -6)
  281. ];
  282. $supplier_id = $this->create($data);
  283. return $this->dao->get($supplier_id)->toArray();
  284. }
  285. /**
  286. * 获取同意申请 创建账号
  287. * @param string $phone
  288. * @return string
  289. */
  290. public function getAccount(string $phone)
  291. {
  292. $account = '';
  293. if ($phone) {
  294. //当前手机号当作账号是否存在
  295. $adminDCount = $this->adminDao->count(['account' => $phone, 'admin_type' => 4, 'is_del' => 0]);
  296. $account = $phone;
  297. if ($adminDCount) {
  298. $account = $account . '_' . $adminDCount;
  299. }
  300. }
  301. return $account;
  302. }
  303. }