SystemSupplierServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. $relation_id = $this->dao->save($data)->id;
  108. if (!$relation_id) throw new AdminException('供应商添加失败');
  109. $this->adminDao->update($res->id, ['relation_id' => $relation_id]);
  110. return $relation_id;
  111. });
  112. }
  113. /**
  114. * 修改管理员
  115. * @param array $data
  116. * @return mixed
  117. */
  118. public function save(int $id, array $data)
  119. {
  120. if (!$supplierInfo = $this->dao->get($id)) {
  121. throw new AdminException('供应商不存在,无法修改');
  122. }
  123. if ($supplierInfo->is_del) {
  124. throw new AdminException('供应商已经删除');
  125. }
  126. if (!$adminInfo = $this->adminDao->get($supplierInfo['admin_id'])) {
  127. throw new AdminException('管理员不存在,无法修改');
  128. }
  129. if ($adminInfo->is_del) {
  130. throw new AdminException('管理员已经删除');
  131. }
  132. //修改账号
  133. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->adminDao->isAccountUsable($data['account'], $supplierInfo['admin_id'], 4)) {
  134. throw new AdminException('管理员账号已存在');
  135. }
  136. return $this->transaction(function () use ($id, $data, $adminInfo, $supplierInfo) {
  137. $adminData = [
  138. 'pwd' => $this->passwordHash($data['pwd']),
  139. 'real_name' => $data['name'] ?? $adminInfo->real_name,
  140. 'phone' => $data['phone'] ?? $adminInfo->phone,
  141. 'account' => $data['account'] ?? $adminInfo->account
  142. ];
  143. // 修改管理员
  144. $res = $this->adminDao->update($adminInfo['id'], $adminData);
  145. if (!$res) throw new AdminException('管理员修改失败');
  146. // 修改供应商
  147. unset($data['pwd'], $data['conf_pwd'], $data['account']);
  148. $this->dao->update($id, $data);
  149. $res1 = $supplierInfo->save();
  150. if (!$res1) throw new AdminException('供应商修改失败');
  151. return true;
  152. });
  153. }
  154. /**
  155. * @param int $id
  156. * @return mixed
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function delete(int $id)
  162. {
  163. if (!$supplierInfo = $this->dao->get($id)) {
  164. throw new AdminException('供应商不存在,无法修改');
  165. }
  166. if ($supplierInfo->is_del) {
  167. throw new AdminException('供应商已经删除');
  168. }
  169. if (!$adminInfo = $this->adminDao->get($supplierInfo['admin_id'])) {
  170. throw new AdminException('管理员不存在,无法删除');
  171. }
  172. if ($adminInfo->is_del) {
  173. throw new AdminException('管理员已经删除');
  174. }
  175. /** @var StoreOrderServices $storeOrderServices */
  176. $storeOrderServices = app()->make(StoreOrderServices::class);
  177. $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 0]);
  178. if (!$orderCount) {
  179. $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 1]);
  180. if (!$orderCount) {
  181. $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 5]);
  182. }
  183. }
  184. if ($orderCount) {
  185. return $this->fail('删除失败,该供应商还有待处理订单');
  186. }
  187. return $this->transaction(function () use ($id, $supplierInfo, $adminInfo) {
  188. $adminInfo->status = 0;
  189. $adminInfo->is_del = 1;
  190. // 修改管理员
  191. $res = $adminInfo->save();
  192. if (!$res) throw new AdminException('管理员删除失败');
  193. $supplierInfo->is_show = 0;
  194. $supplierInfo->is_del = 1;
  195. // 修改供应商
  196. $res1 = $supplierInfo->save();
  197. if (!$res1) throw new AdminException('供应商删除失败');
  198. /** @var StoreBranchProductServices $storeBranchProducesServices */
  199. $storeBranchProducesServices = app()->make(StoreBranchProductServices::class);
  200. //删除供应商商品
  201. $storeBranchProducesServices->deleteProducts([], 2, $id);
  202. /** @var SystemAttachmentServices $attach */
  203. $attach = app()->make(SystemAttachmentServices::class);
  204. //删除附件
  205. $attach->delAttachment([], 4, $id);
  206. return true;
  207. });
  208. }
  209. /**
  210. * 平台供应商运营统计
  211. * @param int $supplierId
  212. * @param array $time
  213. * @return array
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\DbException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. */
  218. public function supplierChart(int $supplierId, array $time)
  219. {
  220. $list = $this->dao->getSupplierList(['is_del' => 0, 'is_show' => 1], ['id', 'supplier_name']);
  221. /** @var StoreOrderServices $orderServices */
  222. $orderServices = app()->make(StoreOrderServices::class);
  223. /** @var StoreOrderRefundServices $orderRefundServices */
  224. $orderRefundServices = app()->make(StoreOrderRefundServices::class);
  225. $where = ['time' => $time];
  226. $order_where = ['paid' => 1, 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  227. $refund_where = ['refund_type' => 6];
  228. foreach ($list as &$item) {
  229. $supplier_where = ['supplier_id' => $item['id']];
  230. $item['order_price'] = $orderServices->sum($where + $supplier_where + $order_where, 'pay_price', true);
  231. $item['order_count'] = $orderServices->count($where + $supplier_where + $order_where);
  232. $item['refund_order_price'] = $orderRefundServices->sum($where + $supplier_where + $refund_where, 'refunded_price', true);
  233. $item['refund_order_count'] = $orderRefundServices->count($where + $supplier_where + $refund_where);
  234. }
  235. return $list;
  236. }
  237. /**
  238. * 供应商选择列表
  239. * @param array $where
  240. * @param array $field
  241. * @return array
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\DbException
  244. * @throws \think\db\exception\ModelNotFoundException
  245. */
  246. public function getSupplierSearch(array $where, array $field = ['*'])
  247. {
  248. return $this->dao->getSupplierList($where, $field, 0, 0);
  249. }
  250. /**
  251. * 供应商入住审核通过创建数据
  252. * @param int $applyId
  253. * @param array $info
  254. * @return array
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function verifyAgreeCreate(int $applyId, array $info = [])
  260. {
  261. if (!$applyId) {
  262. throw new ValidateException('缺少申请ID');
  263. }
  264. /** @var SystemUserApplyServices $applyServices */
  265. $applyServices = app()->make(SystemUserApplyServices::class);
  266. if (!$info) {
  267. $info = $applyServices->get($applyId);
  268. if (!$info) {
  269. throw new ValidateException('申请数据不存在');
  270. }
  271. $info = $info->toArray();
  272. }
  273. $data = [
  274. 'supplier_name' => $info['system_name'],
  275. 'account' => $this->getAccount($info['phone']),
  276. 'phone' => $info['phone'],
  277. 'name' => $info['name'],
  278. 'pwd' => substr($info['phone'], -6)
  279. ];
  280. $supplier_id = $this->create($data);
  281. return $this->dao->get($supplier_id)->toArray();
  282. }
  283. /**
  284. * 获取同意申请 创建账号
  285. * @param string $phone
  286. * @return string
  287. */
  288. public function getAccount(string $phone)
  289. {
  290. $account = '';
  291. if ($phone) {
  292. //当前手机号当作账号是否存在
  293. $adminDCount = $this->adminDao->count(['account' => $phone, 'admin_type' => 4, 'is_del' => 0]);
  294. $account = $phone;
  295. if ($adminDCount) {
  296. $account = $account . '_' . $adminDCount;
  297. }
  298. }
  299. return $account;
  300. }
  301. }