123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- <?php
- /**
- * 配送方式设置
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/10/31
- * Time: 15:53
- */
- namespace JinDouYun\Model\System;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\System\DDeliveryRule;
- use JinDouYun\Dao\System\DDeliverySelfRule;
- use JinDouYun\Dao\System\DExpressCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\System\DDeliverySetting;
- use JinDouYun\Dao\System\DAdminSetting;
- class MDeliverySetting
- {
- private $objDDeliverySetting;
- private $objDAdminSetting;
- private $onlineUserId;
- private $onlineEnterpriseId;
- /**
- * @var DDeliveryRule
- */
- private $objDDeliveryRule;
- /**
- * @var DDeliverySelfRule
- */
- private $objDDeliverySelfRule;
- /**
- * MDeliverySetting constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- */
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->objDDeliverySetting = new DDeliverySetting('default');
- $this->objDAdminSetting = new DAdminSetting('default');
- $this->objDDeliveryRule = new DDeliveryRule();
- $this->objDDeliveryRule->setTable('qianniao_delivery_rule_' . $this->onlineEnterpriseId);
- $this->objDDeliverySelfRule = new DDeliverySelfRule();
- $this->objDDeliverySelfRule->setTable('qianniao_delivery_self_rule_' . $this->onlineEnterpriseId);
- self::initDelivery();
- }
- /**
- * Doc: (des="初始化配送方式")
- * User: XMing
- * Date: 2020/9/7
- * Time: 10:33 上午
- */
- private function init($shopId = 0)
- {
- $adminLists = $this->objDAdminSetting->select(['type' => StatusCode::$adminSettingType['delivery']]);
- if ($adminLists === false) {
- return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
- }
- if (empty($adminLists)) {
- return ResultWrapper::success(true);
- }
- $allSignIds = [];
- foreach ($adminLists as $value) {
- $allSignIds[] = $value['signId'];
- }
- $deliveryLists = $this->objDDeliverySetting->select(['shopId' => $shopId, 'enterpriseId' => $this->onlineEnterpriseId, 'deliveryType' => $allSignIds]);
- if ($deliveryLists === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- $insert = [];
- if (empty($deliveryLists)) {
- foreach ($adminLists as $item) {
- $insert[] = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'settingId' => $item['id'],
- 'defaultStatus' => $item['signId'] == StatusCode::$deliveryType['selfMention'] ? StatusCode::$standard : StatusCode::$delete,
- 'enableStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'deliveryType' => $item['signId'],
- 'title' => $item['title'],
- 'shopId' => $shopId
- ];
- }
- } else {
- $deliverySignIds = [];
- foreach ($deliveryLists as $value) {
- $deliverySignIds[] = $value['deliveryType'];
- }
- foreach ($adminLists as $item) {
- if (!in_array($item['signId'], $deliverySignIds)) {
- $insert[] = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'settingId' => $item['id'],
- 'defaultStatus' => StatusCode::$delete,
- 'enableStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'deliveryType' => $item['signId'],
- 'title' => $item['title'],
- 'shopId' => $shopId
- ];
- }
- }
- }
- if (!empty($insert)) {
- $insertResult = $this->objDDeliverySetting->insert($insert, true);
- if ($insertResult === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- }
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="配置配送方式")
- * User: XMing
- * Date: 2020/9/7
- * Time: 10:40 上午
- * @param $params
- * @param int $id
- * @return ResultWrapper
- */
- public function setData($params)
- {
- $shopId = $params['shopId'];
- unset($params['shopId']);
- if (isset($params['id']) && !empty($params['id'])) {
- $id = $params['id'];
- unset($params['id']);
- $result = $this->objDDeliveryRule->update(['shopId' => $shopId, 'setData' => json_encode($params), 'updateTime' => time()], ['id' => $id]);
- } else {
- $result = $this->objDDeliveryRule->insert(['shopId' => $shopId, 'setData' => json_encode($params), 'createTime' => time()]);
- }
- if ($result === false) {
- return ResultWrapper::fail($this->objDDeliveryRule->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="设置自提点")
- * User: XMing
- * Date: 2020/9/9
- * Time: 10:33 上午
- * @param $params
- * @return ResultWrapper
- */
- public function setSelfData($params)
- {
- $shopId = $params['shopId'];
- unset($params['shopId']);
- if (isset($params['id']) && !empty($params['id'])) {
- $id = $params['id'];
- unset($params['id']);
- $result = $this->objDDeliverySelfRule->update(['setData' => json_encode($params), 'shopId' => $shopId, 'updateTime' => time()], ['id' => $id]);
- } else {
- $result = $this->objDDeliverySelfRule->insert(['setData' => json_encode($params), 'shopId' => $shopId, 'createTime' => time()]);
- }
- if ($result === false) {
- return ResultWrapper::fail($this->objDDeliverySelfRule->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="删除运费模版规则")
- * User: XMing
- * Date: 2020/9/7
- * Time: 4:57 下午
- * @param int $id
- * @return ResultWrapper
- */
- public function delDeliveryRule(int $id)
- {
- $result = $this->objDDeliveryRule->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['id' => $id]);
- if ($result === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="删除自提点")
- * User: XMing
- * Date: 2020/9/9
- * Time: 10:56 上午
- * @param int $id
- * @return ResultWrapper
- */
- public function delDeliverySelfRule(int $id)
- {
- $result = $this->objDDeliverySelfRule->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['id' => $id]);
- if ($result === false) {
- return ResultWrapper::fail($this->objDDeliverySelfRule->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * 配送方式列表
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllDelivery($selectParams)
- {
- !empty($selectParams['shopId']) && self::init($selectParams['shopId']);
- $fields = 'd.id,d.defaultStatus,d.enableStatus,d.deliveryType,d.setData,a.title,a.signId';
- $sql = 'SELECT ' . $fields . ' FROM qianniao_delivery_setting as d
- LEFT JOIN qianniao_admin_setting as a
- ON a.signId = d.deliveryType WHERE d.deleteStatus = ' . StatusCode::$standard . '
- AND d.enterpriseId = ' . $this->onlineEnterpriseId . ' AND a.type = ' . StatusCode::$adminSettingType['delivery'];
- if (isset($selectParams['shopId']) && !empty($selectParams['shopId'])) {
- $sql .= ' AND d.shopId = ' . $selectParams['shopId'];
- } else {
- $sql .= ' AND d.shopId = 0 ';
- }
- $lists = $this->objDDeliverySetting->query($sql);
- if ($lists === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- if(empty($lists)){
- return ResultWrapper::success(['data'=>[],'total'=>0]);
- }
- foreach ($lists as $key => $value){
- $lists[$key]['setData'] = !empty($value['setData']) ? json_decode($value['setData'], true) : (object)[];
- }
- $returnData = [
- 'data' => $lists,
- 'total' => count((array)$lists),
- ];
- return ResultWrapper::success($returnData);
- }
- /**
- * @param $data
- * @return mixed
- */
- private function formatDelivery($data)
- {
- if (empty($data)) return $data;
- foreach ($data as $key => &$val) {
- if (!isset($val['deliveryType'])) {
- unset($data[$key]);
- continue;
- }
- if (isset($val['deliveryType'])) {
- $val['id'] = $val['deliveryType'];
- }
- $data[$key]['setData'] = json_decode($val['setData'], true);
- }
- return $data;
- }
- /**
- * 配送方式,启用/禁用
- * @param $params
- * @return ResultWrapper
- */
- public function updateEnableStatus($params)
- {
- // 判断自提点是否为空,为空则不设置;
- $ifExtraction = self::getInfo($params['id'],0);
- if(empty($ifExtraction)){
- return ResultWrapper::success('当前自提点未设置,不能勾选');
- }
- $finallyResult = $this->objDDeliverySetting->update(['enableStatus' => $params['enableStatus'], 'updateTime' => time()], ['id' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
- if ($finallyResult === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * 更新配送方式的默认状态
- * @param $params
- * @return ResultWrapper
- */
- public function updateDefaultStatus($params)
- {
- if ($params['defaultStatus'] == StatusCode::$standard) {
- $updateResult = $this->objDDeliverySetting->update(['defaultStatus' => StatusCode::$delete], ['enterpriseId' => $this->onlineEnterpriseId, 'shopId' => $params['shopId']]);
- if ($updateResult === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- }
- $finallyResult = $this->objDDeliverySetting->update(['defaultStatus' => $params['defaultStatus'], 'updateTime' => time()], ['id' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
- if ($finallyResult === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($finallyResult);
- }
- /**
- * 用于获取配送方式(设置->配送方式列表不要使用此接口)
- * @param int $shopId
- * @return array|ResultWrapper
- */
- public function allDelivery($shopId = 0)
- {
- $sql = 'SELECT a.title,d.deliveryType,d.defaultStatus,d.setData FROM qianniao_delivery_setting as d LEFT JOIN qianniao_admin_setting AS a ON d.settingId=a.id WHERE d.enterpriseId=' . $this->onlineEnterpriseId . ' AND deleteStatus=' . StatusCode::$standard . ' AND a.type=' . StatusCode::$adminSettingType['delivery'] . ' AND enableStatus=' . StatusCode::$standard;
- if (empty($shopId)) {
- $sql .= ' AND d.shopId = 0';
- } else {
- $sql .= ' AND d.shopId = ' . (int)$shopId;
- }
- $dbResult = $this->objDDeliverySetting->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- if( empty($dbResult) ){
- return ResultWrapper::success(['data'=>[]]);
- }
- $returnData = [
- 'data' => self::formatDelivery($dbResult)
- ];
- return ResultWrapper::success($returnData);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/9/7
- * Time: 12:30 下午
- * @param int $id
- * @param int $shopId
- * @return ResultWrapper
- */
- public function getInfo(int $id, $shopId = 0): ResultWrapper
- {
- $info = $this->objDDeliverySetting->get(['id' => $id]);
- if ($info === false) {
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }
- if (empty($info)) {
- return ResultWrapper::success([]);
- }
- $ruleLists = [];
- if ($info['deliveryType'] == StatusCode::$deliveryType['goodsDelivery']) {
- $where = ['deleteStatus' => StatusCode::$standard];
- $where['shopId'] = $shopId;
- $ruleLists = $this->objDDeliveryRule->select($where);
- if ($ruleLists === false) {
- return ResultWrapper::fail($this->objDDeliveryRule->error(), ErrorCode::$dberror);
- }
- } else {
- //平台可以管理所有自提点
- $where = ['deleteStatus' => StatusCode::$standard];
- if (!empty($shopId)) {
- $where['shopId'] = $shopId;
- }
- $ruleLists = $this->objDDeliverySelfRule->select($where);
- if ($ruleLists === false) {
- return ResultWrapper::fail($this->objDDeliverySelfRule->error(), ErrorCode::$dberror);
- }
- }
- $mapRule = [];
- foreach ($ruleLists as $key => $list) {
- $mapRule[$key] = empty($list['setData']) ? [] : json_decode($list['setData'], true);
- $mapRule[$key]['id'] = $list['id'];
- $mapRule[$key]['createTime'] = $list['createTime'];
- if ($info['deliveryType'] == StatusCode::$deliveryType['goodsDelivery']) {
- $mapRule[$key]['isDefault'] = $list['isDefault'];
- }
- }
- $info['setData'] = $mapRule;
- return ResultWrapper::success($info);
- }
- /**
- * Doc: (des="获取运费模版详情")
- * User: XMing
- * Date: 2020/9/8
- * Time: 6:26 下午
- * @param $id
- * @return ResultWrapper
- */
- public function getRuleInfo($id)
- {
- $ruleInfo = $this->objDDeliveryRule->get(['id' => $id]);
- if ($ruleInfo === false) {
- return ResultWrapper::fail($this->objDDeliveryRule->error(), ErrorCode::$dberror);
- }
- if (empty($ruleInfo)) {
- return ResultWrapper::success([]);
- }
- $ruleInfo['setData'] = json_decode($ruleInfo['setData'], true);
- $ruleInfo['setData']['id'] = $id;
- return ResultWrapper::success($ruleInfo);
- }
- /**
- * Doc: (des="获取自提点详情")
- * User: XMing
- * Date: 2020/9/9
- * Time: 10:49 上午
- * @param $id
- * @return ResultWrapper
- */
- public function getSelfRuleInfo($id)
- {
- $ruleInfo = $this->objDDeliverySelfRule->get(['id' => $id]);
- if ($ruleInfo === false) {
- return ResultWrapper::fail($this->objDDeliverySelfRule->error(), ErrorCode::$dberror);
- }
- if (empty($ruleInfo)) {
- return ResultWrapper::success([]);
- }
- $ruleInfo['setData'] = json_decode($ruleInfo['setData'], true);
- $ruleInfo['setData']['id'] = $id;
- return ResultWrapper::success($ruleInfo);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/9/7
- * Time: 6:20 下午
- */
- public function getAllExpressRule()
- {
- $ruleLists = $this->objDDeliveryRule->select(['deleteStatus' => StatusCode::$standard], 'setData,id,isDefault');
- if ($ruleLists === false) {
- return ResultWrapper::fail($this->objDDeliveryRule->error(), ErrorCode::$dberror);
- }
- $map = [];
- foreach ($ruleLists as $list) {
- $rule = empty($list['setData']) ? [] : json_decode($list['setData'], true);
- $map[] = [
- 'id' => $list['id'],
- 'title' => isset($rule['name']) ? $rule['name'] : '',
- 'isDefault' => $list['isDefault']
- ];
- }
- $ret = [
- 'total' => count($map),
- 'data' => $map
- ];
- return ResultWrapper::success($ret);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/9/9
- * Time: 12:09 下午
- */
- public function getAllSelfExpressRule($shopId = 0)
- {
- $where = [
- 'deleteStatus' => StatusCode::$standard,
- 'enableStatus' => StatusCode::$standard
- ];
- if (!empty($shopId)){
- $where['shopId'] = $shopId;
- }
- $ruleLists = $this->objDDeliverySelfRule->select($where);
- if ($ruleLists === false) {
- return ResultWrapper::fail($this->objDDeliverySelfRule->error(), ErrorCode::$dberror);
- }
- if (empty($ruleLists)) {
- return ResultWrapper::success([]);
- }
- foreach ($ruleLists as &$list) {
- $list['setData'] = empty($list['setData']) ? [] : json_decode($list['setData'], true);
- }
- return ResultWrapper::success($ruleLists);
- }
- /**
- * Doc: (des="初始化配送方式")
- * User: XMing
- * Date: 2020/7/13
- * Time: 6:08 下午
- */
- public function initDelivery()
- {
- $settingAll = $this->objDAdminSetting->select(['type' => StatusCode::$adminSettingType['delivery']]);
- if ($settingAll === false) {
- Logger::logs(E_USER_ERROR, 'sql错误', __CLASS__, __LINE__, $this->objDAdminSetting->error());
- return false;
- }
- if (empty($settingAll)) {
- return true;
- }
- $Delivery = $this->objDDeliverySetting->select(['enterpriseId' => $this->onlineEnterpriseId], 'settingId');
- if ($Delivery === false) {
- Logger::logs(E_USER_ERROR, 'sql错误', __CLASS__, __LINE__, $this->objDAdminSetting->error());
- return false;
- }
- $insert = [];
- $allSettingIds = empty($Delivery) ? [] : array_column($Delivery, 'settingId');
- foreach ($settingAll as $key => $value) {
- if (!in_array($value['id'], $allSettingIds)) {
- $insert [$key] = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'deliveryType' => $value['signId'],
- 'settingId' => $value['id'],
- 'defaultStatus' => StatusCode::$delete
- ];
- if ($value['signId'] == StatusCode::$deliveryType['logistics']) {
- //代码更新后没有物流这个配送方式
- $insert[$key]['deleteStatus'] = StatusCode::$delete;
- }
- if ($value['signId'] == StatusCode::$deliveryType['selfMention']) {
- $insert[$key]['defaultStatus'] = StatusCode::$standard;
- }
- }
- }
- $insert = array_values($insert);
- if (!empty($insert)) {
- $result = $this->objDDeliverySetting->insert($insert, true);
- if ($result === false) {
- Logger::logs(E_USER_ERROR, 'sql错误', __CLASS__, __LINE__, $this->objDDeliverySetting->error());
- return false;
- }
- return true;
- }
- }
- /**
- * Doc: (des="设置默认规则")
- * User: XMing
- * Date: 2020/9/10
- * Time: 2:09 下午
- * @param int $id
- * @return ResultWrapper
- */
- public function setDefaultRule(int $id, $shopId)
- {
- $updateResult = $this->objDDeliveryRule->update(['isDefault' => StatusCode::$delete], ['deleteStatus' => StatusCode::$standard, 'shopId' => $shopId]);
- $this->objDDeliveryRule->beginTransaction();
- if ($updateResult === false) {
- $this->objDDeliveryRule->rollBack();
- return ResultWrapper::fail($this->objDDeliveryRule->error(), ErrorCode::$dberror);
- }
- unset($updateResult);
- $updateResult = $this->objDDeliveryRule->update(['isDefault' => StatusCode::$standard], ['id' => $id]);
- if ($updateResult === false) {
- $this->objDDeliveryRule->rollBack();
- return ResultWrapper::fail($this->objDDeliveryRule->error(), ErrorCode::$dberror);
- }
- $this->objDDeliveryRule->commit();
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/9/8
- * Time: 6:54 下午
- */
- public function getAllExpress()
- {
- $objDExpressCode = new DExpressCode();
- $expressLists = $objDExpressCode->select([], '*', 'id DESC');
- if ($expressLists === false) {
- return ResultWrapper::fail($objDExpressCode->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success([
- 'total' => count($expressLists),
- 'data' => $expressLists
- ]);
- }
- /**
- * 物流提醒
- * @param $reminderData
- * @return ResultWrapper
- */
- public function updateLogisticsReminder($reminderData)
- {
- $reminderDataId = $reminderData['id'];
- unset($reminderData['id']);
- $dbResult = $this->objDDeliverySetting->update(['setData'=>json_encode($reminderData)],['id'=>$reminderDataId]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDDeliverySetting->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
-
- }
-
-
- }
|