123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- <?php
- namespace app\admin\controller\system;
- use app\admin\controller\AuthController;
- use app\admin\model\store\StoreCategory;
- use app\admin\model\system\SystemAdmin;
- use app\admin\model\system\SystemStoreBill;
- use app\admin\model\system\SystemStorePoint;
- use app\admin\model\system\SystemStoreProductStock;
- use app\admin\model\system\SystemStoreProductStockLog;
- use app\admin\model\system\SystemStoreSender;
- use crmeb\services\FormBuilder;
- use crmeb\services\JsonService;
- use crmeb\services\JsonService as Json;
- use app\admin\model\system\SystemStore as SystemStoreModel;
- use crmeb\services\UtilService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Route;
- /**
- * 门店管理控制器
- * Class SystemAttachment
- * @package app\admin\controller\system
- *
- */
- class SystemStore extends AuthController
- {
- /**
- * 门店列表
- */
- public function list()
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['name', ''],
- ['excel', 0],
- ['type', $this->request->param('type')]
- ]);
- JsonService::successlayui(SystemStoreModel::getStoreList($where));
- }
- /**
- * 门店设置
- * @return string
- */
- public function index()
- {
- $type = $this->request->param('type');
- $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
- $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店
- $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店
- if ($type == null) $type = 1;
- $this->assign(compact('type', 'show', 'hide', 'recycle'));
- return $this->fetch();
- }
- /**
- * 门店添加
- * @param int $id
- * @return string
- */
- public function add($id = 0)
- {
- $store = SystemStoreModel::getStoreDispose($id);
- $this->assign(compact('store'));
- return $this->fetch();
- }
- /**
- * 删除恢复门店
- * @param $id
- */
- public function delete($id)
- {
- if (!$id) return $this->failed('数据不存在');
- if (!SystemStoreModel::be(['id' => $id])) return $this->failed('产品数据不存在');
- if (SystemStoreModel::be(['id' => $id, 'is_del' => 1])) {
- $data['is_del'] = 0;
- if (!SystemStoreModel::edit($data, $id))
- return Json::fail(SystemStoreModel::getErrorInfo('恢复失败,请稍候再试!'));
- else
- return Json::successful('恢复门店成功!');
- } else {
- $data['is_del'] = 1;
- if (!SystemStoreModel::edit($data, $id))
- return Json::fail(SystemStoreModel::getErrorInfo('删除失败,请稍候再试!'));
- else
- return Json::successful('删除门店成功!');
- }
- }
- /**
- * 设置单个门店是否显示
- * @param string $is_show
- * @param string $id
- * @return json
- */
- public function set_show($is_show = '', $id = '')
- {
- ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
- $res = SystemStoreModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
- } else {
- return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
- }
- }
- /**
- * 位置选择
- * @return string|void
- */
- public function select_address()
- {
- $key = sys_config('tengxun_map_key');
- if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
- $this->assign(compact('key'));
- return $this->fetch();
- }
- /**
- * 保存修改门店信息
- * @param int $id
- */
- public function save($id = 0)
- {
- $data = UtilService::postMore([
- ['name', ''],
- ['introduction', ''],
- ['image', ''],
- ['phone', ''],
- ['address', ''],
- ['detailed_address', ''],
- ['latlng', ''],
- ['valid_time', []],
- ['day_time', []],
- ['radius', 0],
- ['leader', ''],
- ['pictures', []],
- ['recharge_award_ratio', 0],
- ['terminal_number', ''],
- ['sh_start_money', 0],
- ['sh_start_distance', 0],
- ['sh_step', 0],
- ['sh_step_money', 0],
- ['sharing', 0],
- ['store_user', 0],
- ['see', 0],
- ['pay', 0],
- ]);
- SystemStoreModel::beginTrans();
- try {
- $data['address'] = implode(',', $data['address']);
- $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
- if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
- if ($data['sh_step'] <= 0) JsonService::fail('超过距离基数不能小于等于0');
- $data['latitude'] = $data['latlng'][0];
- $data['longitude'] = $data['latlng'][1];
- $data['valid_time'] = implode(' - ', $data['valid_time']);
- $data['day_time'] = implode(' - ', $data['day_time']);
- unset($data['latlng']);
- if ($data['image'] && strstr($data['image'], 'http') === false) {
- $site_url = sys_config('site_url');
- $data['image'] = $site_url . $data['image'];
- }
- $new = [];
- foreach ($data['pictures'] as $v) {
- $site_url = sys_config('site_url');
- $new[] = $site_url . $v;
- }
- $data['pictures'] = implode(',', $new);
- if ($id) {
- if (SystemStoreModel::where('id', $id)->update($data)) {
- SystemStoreModel::commitTrans();
- JsonService::success('修改成功');
- } else {
- SystemStoreModel::rollbackTrans();
- JsonService::fail('修改失败或者您没有修改什么!');
- }
- } else {
- $data['add_time'] = time();
- $data['is_show'] = 1;
- if ($res = SystemStoreModel::create($data)) {
- SystemAdmin::create([
- 'account' => 'store_admin_' . $res->id,
- 'pwd' => md5('123456'),
- 'real_name' => $data['leader'],
- 'roles' => sys_config('default_store_admin', 7),
- 'add_time' => time(),
- 'status' => 1,
- 'level' => 2,
- 'store_id' => $res->id,
- ]);
- SystemStoreModel::commitTrans();
- JsonService::success('保存成功', ['id' => $res->id]);
- } else {
- SystemStoreModel::rollbackTrans();
- JsonService::fail('保存失败!');
- }
- }
- } catch (\Exception $e) {
- SystemStoreModel::rollbackTrans();
- JsonService::fail($e->getMessage());
- }
- }
- /**
- * @param $id
- * @return string
- * @throws Exception
- */
- public function storeProductInfo($id)
- {
- $this->assign('id', $id);
- $this->assign('year', get_month());
- return $this->fetch();
- }
- public function stock_list($id)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['data', ''],
- ['limit', 20],
- ['product_id', ''],
- ['excel', 0],
- ]);
- JsonService::successlayui(SystemStoreProductStock::getStockList($where, $id));
- }
- public function add_stock($id)
- {
- if (!$id) {
- return $this->failed('请选择门店');
- }
- $where = UtilService::getMore([
- ['product_id', 0],
- ['unique', ''],
- ]);
- if (!$where['product_id'] || !$where['unique']) {
- $field = [];
- $field[] = FormBuilder::frameImages('product', '选择商品', Route::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
- $field[] = FormBuilder::hidden('product_id', '');
- $field[] = FormBuilder::number('price', '价格', 0)->step(0.01);
- $field[] = FormBuilder::hidden('unique', '');
- } else {
- $field = [];
- $field[] = FormBuilder::hidden('product_id', $where['product_id']);
- $field[] = FormBuilder::number('price', '价格', SystemStoreProductStock::where(['product_id' => $where['product_id'], 'unique' => $where['unique'], 'store_id' => $id])->value('price'))->step(0.01);
- $field[] = FormBuilder::hidden('unique', $where['unique']);
- }
- $field[] = FormBuilder::number('stock', '库存增加量', 0);
- $form = FormBuilder::make_post_form('添加库存', $field, Route::buildUrl('save_stock', ['id' => $id]), 3);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- /**
- * @return string
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws Exception
- */
- public function productList()
- {
- $cate = StoreCategory::getTierList(null, 1);
- $this->assign('cate', $cate);
- return $this->fetch();
- }
- public function save_stock($id)
- {
- if (!$id) {
- Json::fail('请选择门店');
- }
- list($product_id, $unique, $stock, $price) = UtilService::postMore([['product_id', []], ['unique', []], ['stock', 0], ['price', 0]], $this->request, true);
- if (!count($product_id)) {
- Json::fail('请选择补货商品');
- }
- $res = true;
- SystemStoreProductStockLog::beginTrans();
- foreach ($product_id as $k => $v) {
- if ($stock > 0) {
- $res = $res && SystemStoreProductStockLog::income($id, $v, $unique[$k], 'add_stock', 0, $stock, $this->adminId, '后台补货' . $stock . '件', 1, $price);
- } else {
- $res = $res && SystemStoreProductStockLog::expend($id, $v, $unique[$k], 'dec_stock', 0, abs($stock), $this->adminId, '后台减货' . $stock . '件', 1, $price);
- }
- }
- if ($res) {
- SystemStoreProductStockLog::commitTrans();
- Json::success('补货成功');
- } else {
- SystemStoreProductStockLog::rollbackTrans();
- Json::fail(SystemStoreProductStockLog::getErrorInfo('补货失败'));
- }
- }
- /**
- * @param $id
- * @return string
- * @throws Exception
- */
- public function stock_log($id)
- {
- $this->assign('id', $id);
- return $this->fetch();
- }
- public function stock_log_list($id)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ]);
- JsonService::successlayui(SystemStoreProductStockLog::getList($where, $id));
- }
- /**
- * @param $id
- * @return string
- * @throws Exception
- */
- public function store_bill_log($id)
- {
- $this->assign('id', $id);
- $this->assign('all', SystemStoreModel::get($id)['brokerage_price']);
- return $this->fetch();
- }
- public function store_bill_log_list($id)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ]);
- JsonService::successlayui(SystemStoreBill::getList($where, $id));
- }
- /**
- * @param $id
- * @return string
- * @throws Exception
- */
- public function store_point($id)
- {
- $type = $this->request->param('type');
- $show = SystemStorePoint::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
- $hide = SystemStorePoint::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
- $recycle = SystemStorePoint::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
- if ($type == null) $type = 1;
- $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
- return $this->fetch();
- }
- public function store_point_list($id)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['name', ''],
- ['excel', 0],
- ['type', $this->request->param('type')]
- ]);
- $where['store_id'] = $id;
- JsonService::successlayui(SystemStorePoint::getStoreList($where));
- }
- /**
- * 门店添加
- * @param int $id
- * @return string
- */
- public function add_point($id = 0, $store_id = 0)
- {
- $store = SystemStorePoint::getStoreDispose($id);
- $this->assign(compact('store', 'store_id'));
- return $this->fetch();
- }
- /**
- * 保存修改门店信息
- * @param int $id
- */
- public function save_point($id = 0)
- {
- $data = UtilService::postMore([
- ['name', ''],
- ['phone', ''],
- ['address', ''],
- ['detailed_address', ''],
- ['latlng', ''],
- ['valid_time', []],
- ['day_time', []],
- ['store_id', 0],
- ]);
- // var_dump($data['store_id']);
- if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
- SystemStorePoint::beginTrans();
- try {
- $data['address'] = implode(',', $data['address']);
- $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
- if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
- $data['latitude'] = $data['latlng'][0];
- $data['longitude'] = $data['latlng'][1];
- $data['valid_time'] = implode(' - ', $data['valid_time']);
- $data['day_time'] = implode(' - ', $data['day_time']);
- unset($data['latlng']);
- if ($id) {
- if (SystemStorePoint::where('id', $id)->update($data)) {
- SystemStorePoint::commitTrans();
- JsonService::success('修改成功');
- } else {
- SystemStorePoint::rollbackTrans();
- JsonService::fail('修改失败或者您没有修改什么!');
- }
- } else {
- $data['add_time'] = time();
- $data['is_show'] = 1;
- if ($res = SystemStorePoint::create($data)) {
- SystemStorePoint::commitTrans();
- JsonService::success('保存成功', ['id' => $res->id]);
- } else {
- SystemStorePoint::rollbackTrans();
- JsonService::fail('保存失败!');
- }
- }
- } catch (\Exception $e) {
- SystemStorePoint::rollbackTrans();
- JsonService::fail($e->getMessage());
- }
- }
- /**
- * 设置单个门店是否显示
- * @param string $is_show
- * @param string $id
- * @return json
- */
- public function set_point_show($is_show = '', $id = '')
- {
- ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
- $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
- } else {
- return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
- }
- }
- /**
- * 删除恢复门店
- * @param $id
- */
- public function point_delete($id)
- {
- if (!$id) return $this->failed('数据不存在');
- if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在');
- if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
- $data['is_del'] = 0;
- if (!SystemStorePoint::edit($data, $id))
- return Json::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
- else
- return Json::successful('恢复门店成功!');
- } else {
- $data['is_del'] = 1;
- if (!SystemStorePoint::edit($data, $id))
- return Json::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
- else
- return Json::successful('删除门店成功!');
- }
- }
- /**
- * @param $id
- * @return string
- * @throws Exception
- */
- public function store_sender($id)
- {
- $type = $this->request->param('type');
- $show = SystemStoreSender::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
- $hide = SystemStoreSender::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
- $recycle = SystemStoreSender::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
- if ($type == null) $type = 1;
- $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
- return $this->fetch();
- }
- public function store_sender_list($id)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['name', ''],
- ['excel', 0],
- ['type', $this->request->param('type')]
- ]);
- $where['store_id'] = $id;
- JsonService::successlayui(SystemStoreSender::getStoreList($where));
- }
- /**
- * 门店添加
- * @param int $id
- * @return string
- */
- public function add_sender($id = 0, $store_id = 0)
- {
- $store = SystemStoreSender::getStoreDispose($id);
- $this->assign(compact('store', 'store_id'));
- return $this->fetch();
- }
- /**
- * 保存修改门店信息
- * @param int $id
- */
- public function save_sender($id = 0)
- {
- $data = UtilService::postMore([
- ['name', ''],
- ['phone', ''],
- ['store_id', 0],
- ]);
- // var_dump($data['store_id']);
- if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
- SystemStoreSender::beginTrans();
- try {
- if ($id) {
- if (SystemStoreSender::where('id', $id)->update($data)) {
- SystemStoreSender::commitTrans();
- JsonService::success('修改成功');
- } else {
- SystemStoreSender::rollbackTrans();
- JsonService::fail('修改失败或者您没有修改什么!');
- }
- } else {
- $data['add_time'] = time();
- $data['is_show'] = 1;
- if ($res = SystemStoreSender::create($data)) {
- SystemStoreSender::commitTrans();
- JsonService::success('保存成功', ['id' => $res->id]);
- } else {
- SystemStoreSender::rollbackTrans();
- JsonService::fail('保存失败!');
- }
- }
- } catch (\Exception $e) {
- SystemStoreSender::rollbackTrans();
- JsonService::fail($e->getMessage());
- }
- }
- /**
- * 设置单个门店是否显示
- * @param string $is_show
- * @param string $id
- * @return json
- */
- public function set_sender_show($is_show = '', $id = '')
- {
- ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
- $res = SystemStoreSender::where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
- } else {
- return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
- }
- }
- /**
- * 删除恢复门店
- * @param $id
- */
- public function sender_delete($id)
- {
- if (!$id) return $this->failed('数据不存在');
- if (!SystemStoreSender::be(['id' => $id])) return $this->failed('数据不存在');
- if (SystemStoreSender::be(['id' => $id, 'is_del' => 1])) {
- $data['is_del'] = 0;
- if (!SystemStoreSender::edit($data, $id))
- return Json::fail(SystemStoreSender::getErrorInfo('恢复失败,请稍候再试!'));
- else
- return Json::successful('恢复门店成功!');
- } else {
- $data['is_del'] = 1;
- if (!SystemStoreSender::edit($data, $id))
- return Json::fail(SystemStoreSender::getErrorInfo('删除失败,请稍候再试!'));
- else
- return Json::successful('删除门店成功!');
- }
- }
- }
|