123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\store\model;
- use app\store\model\Goods as GoodsModel;
- use app\common\model\Delivery as DeliveryModel;
- use app\store\model\DeliveryRule as DeliveryRuleModel;
- /**
- * 配送模板模型
- * Class Delivery
- * @package app\common\model
- */
- class Delivery extends DeliveryModel
- {
- /**
- * 获取全部
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getAll()
- {
- $model = new static;
- return $model->where('is_delete', '=', 0)->order(['sort', $model->getPk()])->select();
- }
- /**
- * 获取列表
- * @param array $param
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList($param = [])
- {
- // 默认查询条件
- $params = $this->setQueryDefaultValue($param, ['search' => '']);
- // 检索查询条件
- $filter = [];
- !empty($params['search']) && $filter[] = ['name', 'like', "%{$params['search']}%"];
- // 查询列表数据
- return $this->with(['rule'])
- ->where($filter)
- ->where('is_delete', '=', 0)
- ->order(['sort', $this->getPk()])
- ->paginate(15);
- }
- /**
- * 添加新记录
- * @param array $data
- * @return bool
- */
- public function add(array $data)
- {
- // 表单验证
- if (!$this->onValidate($data)) {
- return false;
- }
- $data['store_id'] = self::$storeId;
- // 事务处理
- $this->transaction(function () use ($data) {
- // 保存数据
- $this->save($data);
- // 添加模板区域及运费
- DeliveryRuleModel::increased((int)$this['delivery_id'], $data['rules']);
- });
- return true;
- }
- /**
- * 编辑记录
- * @param array $data
- * @return bool
- */
- public function edit(array $data)
- {
- // 表单验证
- if (!$this->onValidate($data)) {
- return false;
- }
- // 事务处理
- $this->transaction(function () use ($data) {
- // 保存数据
- $this->save($data);
- // 更新模板区域及运费
- DeliveryRuleModel::updates((int)$this['delivery_id'], $data['rules']);
- });
- return true;
- }
- /**
- * 表单验证
- * @param $data
- * @return bool
- */
- private function onValidate(array $data)
- {
- if (!isset($data['rules']) || empty($data['rules'])) {
- $this->error = '请选择可配送区域';
- return false;
- }
- $method = $data['method'];
- if (count($data['rules'])){
- foreach ($data['rules'] as $rule){
- if (is_null($rule['first'])){
- if ($method == 10){
- $this->error = '首件不能为空';
- }
- if ($method == 20){
- $this->error = '首重不能为空';
- }
- return false;
- }
- if (is_null($rule['first_fee'])){
- $this->error = '运费不能为空';
- return false;
- }
- if (is_null($rule['additional'])){
- if ($method == 10){
- $this->error = '续件不能为空';
- }
- if ($method == 20){
- $this->error = '续重不能为空';
- }
- return false;
- }
- if (is_null($rule['additional_fee'])){
- $this->error = '续费不能为空';
- return false;
- }
- }
- }
- return true;
- }
- /**
- * 删除记录
- * @return bool
- */
- public function remove()
- {
- // 验证运费模板是否被商品使用
- if (!$this->checkIsUseGoods($this['delivery_id'])) {
- return false;
- }
- // 删除运费模板
- return $this->save(['is_delete' => 1]);
- }
- /**
- * 验证运费模板是否被商品使用
- * @param int $deliveryId
- * @return bool
- */
- private function checkIsUseGoods(int $deliveryId)
- {
- // 判断是否存在商品
- $goodsCount = (new GoodsModel)->getGoodsTotal(['delivery_id' => $deliveryId]);
- if ($goodsCount > 0) {
- $this->error = "该模板被{$goodsCount}个商品使用,不允许删除";
- return false;
- }
- return true;
- }
- }
|