123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace app\services\supplier;
- use app\dao\supplier\SystemSupplierDao;
- use app\dao\system\admin\SystemAdminDao;
- use app\services\BaseServices;
- use app\services\order\StoreOrderServices;
- use app\services\order\StoreOrderRefundServices;
- use app\services\product\branch\StoreBranchProductServices;
- use app\services\system\attachment\SystemAttachmentServices;
- use app\services\system\SystemUserApplyServices;
- use crmeb\exceptions\AdminException;
- use think\exception\ValidateException;
- class SystemSupplierServices extends BaseServices
- {
- protected $adminDao = null;
-
- public function __construct(SystemSupplierDao $dao, SystemAdminDao $adminDao)
- {
- $this->dao = $dao;
- $this->adminDao = $adminDao;
- }
-
- public function getSupplierInfo(int $id, string $field = '*', array $with = [])
- {
- $info = $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
- if (!$info) {
- throw new ValidateException('供应商不存在');
- }
- return $info;
- }
-
- public function getSupplierList(array $where, array $field = ['*'])
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getSupplierList($where, $field, $page, $limit);
- if ($list) {
- $prefix = config('admin.supplier_prefix');
- foreach ($list as &$item) {
- if (isset($item['add_time']) && $item['add_time']) $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
- $item['prefix'] = $prefix;
- }
- }
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
-
- public function create(array $data)
- {
- if ($this->adminDao->count(['account' => $data['account'], 'admin_type' => 4, 'is_del' => 0])) {
- throw new AdminException('管理员账号已存在');
- }
- return $this->transaction(function () use ($data) {
- $adminData = [
- 'pwd' => $this->passwordHash($data['pwd']),
- 'admin_type' => 4,
- 'account' => $data['account'],
- 'roles' => '',
- 'real_name' => $data['name'],
- 'phone' => $data['phone'],
- 'add_time' => time(),
- 'level' => 0
- ];
- unset($data['pwd'], $data['conf_pwd'], $data['account']);
-
- $res = $this->adminDao->save($adminData);
- if (!$res) throw new AdminException('管理员添加失败');
- $data['admin_id'] = (int)$res->id;
- $data['add_time'] = time();
-
- if ($data['valid_time'] ?? '') $data['valid_time'] = strtotime($data['valid_time']);
- $relation_id = $this->dao->save($data)->id;
- if (!$relation_id) throw new AdminException('供应商添加失败');
- $this->adminDao->update($res->id, ['relation_id' => $relation_id]);
- return $relation_id;
- });
- }
-
- public function save(int $id, array $data)
- {
- if (!$supplierInfo = $this->dao->get($id)) {
- throw new AdminException('供应商不存在,无法修改');
- }
- if ($supplierInfo->is_del) {
- throw new AdminException('供应商已经删除');
- }
- if (!$adminInfo = $this->adminDao->get($supplierInfo['admin_id'])) {
- throw new AdminException('管理员不存在,无法修改');
- }
- if ($adminInfo->is_del) {
- throw new AdminException('管理员已经删除');
- }
-
- if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->adminDao->isAccountUsable($data['account'], $supplierInfo['admin_id'], 4)) {
- throw new AdminException('管理员账号已存在');
- }
- return $this->transaction(function () use ($id, $data, $adminInfo, $supplierInfo) {
- $adminData = [
- 'pwd' => $this->passwordHash($data['pwd']),
- 'real_name' => $data['name'] ?? $adminInfo->real_name,
- 'phone' => $data['phone'] ?? $adminInfo->phone,
- 'account' => $data['account'] ?? $adminInfo->account
- ];
-
- $res = $this->adminDao->update($adminInfo['id'], $adminData);
- if (!$res) throw new AdminException('管理员修改失败');
-
- unset($data['pwd'], $data['conf_pwd'], $data['account']);
- if ($data['valid_time'] ?? '') $data['valid_time'] = strtotime($data['valid_time']);
- $this->dao->update($id, $data);
- $res1 = $supplierInfo->save();
- if (!$res1) throw new AdminException('供应商修改失败');
- return true;
- });
- }
-
- public function delete(int $id)
- {
- if (!$supplierInfo = $this->dao->get($id)) {
- throw new AdminException('供应商不存在,无法修改');
- }
- if ($supplierInfo->is_del) {
- throw new AdminException('供应商已经删除');
- }
- if (!$adminInfo = $this->adminDao->get($supplierInfo['admin_id'])) {
- throw new AdminException('管理员不存在,无法删除');
- }
- if ($adminInfo->is_del) {
- throw new AdminException('管理员已经删除');
- }
-
- $storeOrderServices = app()->make(StoreOrderServices::class);
- $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 0]);
- if (!$orderCount) {
- $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 1]);
- if (!$orderCount) {
- $orderCount = $storeOrderServices->count(['supplier_id' => $id, 'status' => 5]);
- }
- }
- if ($orderCount) {
- return $this->fail('删除失败,该供应商还有待处理订单');
- }
- return $this->transaction(function () use ($id, $supplierInfo, $adminInfo) {
- $adminInfo->status = 0;
- $adminInfo->is_del = 1;
-
- $res = $adminInfo->save();
- if (!$res) throw new AdminException('管理员删除失败');
- $supplierInfo->is_show = 0;
- $supplierInfo->is_del = 1;
-
- $res1 = $supplierInfo->save();
- if (!$res1) throw new AdminException('供应商删除失败');
-
- $storeBranchProducesServices = app()->make(StoreBranchProductServices::class);
-
- $storeBranchProducesServices->deleteProducts([], 2, $id);
-
- $attach = app()->make(SystemAttachmentServices::class);
-
- $attach->delAttachment([], 4, $id);
- return true;
- });
- }
-
- public function supplierChart(int $supplierId, array $time)
- {
- $list = $this->dao->getSupplierList(['is_del' => 0, 'is_show' => 1], ['id', 'supplier_name']);
-
- $orderServices = app()->make(StoreOrderServices::class);
-
- $orderRefundServices = app()->make(StoreOrderRefundServices::class);
- $where = ['time' => $time];
- $order_where = ['paid' => 1, 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
- $refund_where = ['refund_type' => 6];
- foreach ($list as &$item) {
- $supplier_where = ['supplier_id' => $item['id']];
- $item['order_price'] = $orderServices->sum($where + $supplier_where + $order_where, 'pay_price', true);
- $item['order_count'] = $orderServices->count($where + $supplier_where + $order_where);
- $item['refund_order_price'] = $orderRefundServices->sum($where + $supplier_where + $refund_where, 'refunded_price', true);
- $item['refund_order_count'] = $orderRefundServices->count($where + $supplier_where + $refund_where);
- }
- return $list;
- }
-
- public function getSupplierSearch(array $where, array $field = ['*'])
- {
- return $this->dao->getSupplierList($where, $field, 0, 0);
- }
-
- public function verifyAgreeCreate(int $applyId, array $info = [])
- {
- if (!$applyId) {
- throw new ValidateException('缺少申请ID');
- }
-
- $applyServices = app()->make(SystemUserApplyServices::class);
- if (!$info) {
- $info = $applyServices->get($applyId);
- if (!$info) {
- throw new ValidateException('申请数据不存在');
- }
- $info = $info->toArray();
- }
- $data = [
- 'supplier_name' => $info['system_name'],
- 'account' => $this->getAccount($info['phone']),
- 'phone' => $info['phone'],
- 'name' => $info['name'],
- 'pwd' => substr($info['phone'], -6)
- ];
- $supplier_id = $this->create($data);
- return $this->dao->get($supplier_id)->toArray();
- }
-
- public function getAccount(string $phone)
- {
- $account = '';
- if ($phone) {
-
- $adminDCount = $this->adminDao->count(['account' => $phone, 'admin_type' => 4, 'is_del' => 0]);
- $account = $phone;
- if ($adminDCount) {
- $account = $account . '_' . $adminDCount;
- }
- }
- return $account;
- }
- }
|