* @date 2020/6/19 17:08 * * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function createSpec() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'name|规格名称' => 'require', ]); (true !== $valid) && IResponse::failure($valid); // 1. 存在性 $model = model('common/GoodsSpec'); if ($spec = $model->where(['spec_name' => $params['name']])->find()) { //直接返回id return IResponse::success($spec); } // 不存在则新增 return IResponse::success($model->create([ 'spec_name' => $params['name'] ],true)); } /** * 新增规格属性 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/19 17:10 * * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function createSpecValue() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'spec_id|规格ID' => 'require', 'name|规格属性名称' => 'require', ]); (true !== $valid) && IResponse::failure($valid); // 1. 存在性 $model = model('common/GoodsSpecValue'); if ($value = $model->where(['spec_id' => $params['spec_id'],'value_name' => $params['name']])->find()) { //直接返回id return IResponse::success($value); } // 不存在则新增 return IResponse::success($model->create([ 'spec_id' => $params['spec_id'], 'value_name' => $params['name'] ],true)); } /** * 取得商品规格 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/19 17:00 * * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getSkuById($id) { // 获取规格 $skus = model('common/GoodsSku') ->where(['goods_id' => $id]) ->select(); $spec = []; $values = []; $ret = []; // 取出这个产品的SPEC SPEC_VALUE foreach ($skus as $sku) { $ret['sku'][] = [ 'ids' => dejson($sku['spec_param']), 'price' => $sku['price'], 'pin_price' => $sku['pin_price'], 'instock_price' => $sku['instock_price'], 'stock' => $sku['stock'], ]; foreach (dejson($sku['spec_param']) as $sp) { foreach ($sp as $sid => $vid) { $spec[] = model('GoodsSpec')->where(['id' => $sid])->find(); $values[] = model('GoodsSpecValue')->where(['spec_id' => $sid, 'id' => $vid])->find(); } } } // 构造 foreach (array_unique($spec) as $s => $k) { $sub = []; foreach (array_unique($values) as $i => $v) { if ($v['spec_id'] == $k['id']) { $sub[] = [ 'id' => $v['id'], 'name' => $v['value_name'] ]; } } $ret['spec'][] = [ 'id' => $k['id'], 'name' => $k['spec_name'], 'sub' => $sub ]; } return IResponse::success($ret); } /** * 获取商品信息 * * @return mixed * @throws \think\exception\DbException */ public function index() { $where = []; //组合搜索 !empty(input('name')) && $where[] = ['name', 'like', '%' . input('name') . '%']; !empty(input('seller_id')) && $where[] = ['seller_id', 'eq', input('seller_id')]; !empty(input('category_id')) && $where[] = ['category_id', 'eq', input('category_id')]; !empty(input('area')) && $where[] = ['area_id', 'eq', input('area')]; $product = model('common/Goods'); return IResponse::paginate($product->where($where)->with(['category'])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * 新增产品 * * @return mixed */ public function save() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'cover_img|产品主图' => 'require', 'name|产品名称' => 'require|unique:Goods', 'sku|产品规格' => 'require', 'is_pin|是否拼团' => 'require', 'pin_sum|拼团人数' => 'requireWith:is_pin', 'pin_number|成团人数' => 'requireWith:is_pin', 'content|产品详情' => 'require', 'sku| 商品规格参数' => 'require' ],[ 'email.unique' => '重复产品名称!' ]); (true !== $valid) && IResponse::failure($valid); Db::startTrans(); // 保存数据 $goodsId = model('common/Goods')->storeBy($params); try { // sku处理 $insku = dejson($params['sku']); //库存 $stock = 0; foreach ($insku as &$sku){ $sku['goods_id'] = $goodsId; $sku['spec_param'] = enjson($sku['ids']); $sku['param'] = enjson($sku['param']); $stock += $sku['stock']; model('common/GoodsSku')::create($sku,true); } if($stock == 0){ throw new EvidentException([ 'errmsg' => '当前商品库存不能未0!' ]); } // 进货价 $iprice = array_column($insku, 'instock_price'); sort($iprice); // 价格 $price = array_column($insku, 'price'); sort($price); // 拼团价格 $pprice = array_column($insku, 'pin_price'); sort($pprice); // 更新信息 model('common/Goods')->updateBy($goodsId, [ 'stock' => $stock, 'instock_price' => $iprice[0], 'price' => $price[0], 'pin_price' => $pprice[0] ]); Db::commit(); }catch (\Exception $e){ Db::rollback(); } return $goodsId ? IResponse::success([],'新增产品成功'): IResponse::failure('新增产品异常'); } /** * 更新产品信息 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/2 22:13 * * @param $id * @return mixed * @throws \think\Exception * @throws \think\exception\PDOException */ public function update($id) { // 接收数据 $params = $this->request->param(); // 查询 $goods = model('common/Goods')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params, [ 'cover_img|产品主图' => 'require', 'name|产品名称' => 'require|unique:Goods', 'sku|产品规格' => 'require', 'is_pin|是否拼团' => 'require', 'pin_sum|拼团人数' => 'requireWith:is_pin', 'pin_number|成团人数' => 'requireWith:is_pin', 'content|产品详情' => 'require', 'sku| 商品规格参数' => 'require' ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 删除旧数据 //TODO 模型方法有bug,等待优化 $this->app->db()->name('goods_sku')->where(['goods_id' => $goods['id']])->delete(); // sku处理 $insku = dejson($params['sku']); $stock = 0; foreach ($insku as &$sku){ $sku['goods_id'] = $goods['id']; $sku['spec_param'] = enjson($sku['ids']); $sku['param'] = enjson($sku['param']); $stock += $sku['stock']; model('common/GoodsSku')::create($sku,true); } if($stock == 0){ throw new EvidentException([ 'errmsg' => '当前商品库存不能未0!' ]); } // 库存 $params['stock'] = $stock; // 进货价 $iprice = array_column($insku, 'instock_price'); sort($iprice); $params['instock_price'] = $iprice[0]; // 价格 $price = array_column($insku, 'price'); sort($price); $params['price'] = $price[0]; // 拼团价格 $pprice = array_column($insku, 'pin_price'); sort($pprice); $params['pin_price'] = $pprice[0]; // 更新信息 $goods->updateBy($id, $params); return IResponse::success('更新产品信息成功'); } /** * 删除产品 * * @param $id * @return \think\response\Json */ public function delete($id) { model('common/Goods')->deleteBy($id); return IResponse::success([],'删除产品成功'); } /*********************订单**********************/ /** * * @return mixed * @throws \think\exception\DbException */ public function order() { $where = []; //组合搜索 !empty(input('name')) && $where[] = ['name', 'like', '%' . input('name') . '%']; (!empty(input('pin')) || input('pin') == '0') && $where[] = ['is_pin', 'eq', input('pin')]; (!empty(input('status')) || input('status') == '0') && $where[] = ['status', 'eq', input('status')]; !empty(input('area')) && $where[] = ['area_id', 'eq', input('area')]; !empty(input('keywords')) && $where[] = ['user_id|order_no', 'like', '%' . input('keywords') . '%']; // 时间处理 if (!empty(input('created_at'))){ list($start, $end) = str2arr(input('created_at'),'-'); $where[] = ['created_at', 'between', [strtotime($start), strtotime($end)]]; } $order = model('common/GoodsOrder'); return IResponse::paginate($order->where($where)->with(['user','seller','addr','detail']) ->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } public function getOrderDetailById($id){ (new IDMustBePositiveInt())->valid(); // $order = model('common/GoodsOrderDetail'); // TODO 商家要依据自己的ID查询 return IResponse::paginate($order ->where(['order_id' => $id]) ->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * 获取售后 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/10/26 17:07 * * @return \think\response\Json * @throws \think\exception\DbException */ public function getOrderService(){ $where = []; (!empty(input('status')) || input('status') == '0') && $where[] = ['status', 'eq', input('status')]; (!empty(input('action')) || input('action') == '0') && $where[] = ['action', 'eq', input('action')]; !empty(input('pay_type')) && $where[] = ['pay_type', 'eq', input('pay_type')]; !empty(input('keywords')) && $where[] = ['user_id|order_id|mobile', 'like', '%' . input('keywords') . '%']; // 时间处理 if (!empty(input('created_at'))){ list($start, $end) = str2arr(input('created_at'),'-'); $where[] = ['created_at', 'between', [strtotime($start), strtotime($end)]]; } $service = model('common/GoodsOrderService')->with(['user','seller','order']); // TODO 商家要依据自己的ID查询 return IResponse::paginate($service->where($where)->order(['created_at' => "desc"]) ->paginate(input('limit'),false)); } /** * 服务单处理 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/10/26 18:16 * * @param $id * @return \think\response\Json */ public function actionServiceOrder($id) { $param = $this->request->param(); $valid = $this->validate($param, [ 'action|操作状态' => 'require' ]); // 错误返回 (true !== $valid) && $this->ApiErr($valid); $model = model('common/GoodsOrderService'); $service = $model->findBy($id); $msg = "操作"; switch ($param['action']) { case "review": $msg = "审核订单"; $model->updateBy($id, [ 'status' => 3 ]); $order = model('common/GoodsOrder')->findBy($service['order_id']); // 退钱 model('common/Users')->changeBalance( $order['user_id'], $order['pay_price'], "售后订单退款", true); // 订单改服务状态 model('common/GoodsOrder')->updateBy($order['id'], [ 'in_service' => 2, // 已退款 'service_rec' => 1, // 标记售后 // 'status' => 0 // 关闭 ]); break; case "reject": $msg = "拒绝订单"; $model->updateBy($id, [ 'status' => 2 ]); // 订单改服务状态 model('common/GoodsOrder')->updateBy($id, [ 'in_service' => 0 ]); break; } return IResponse::success([],$msg . '成功'); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/15 15:59 * * @return mixed */ public function plectronOrder(){ // 收参数 $params = $this->request->param(); foreach (str2arr($params['ids']) as $id){ $user = model('common/GoodsOrder')->getBy($id); if ($this->request->isDelete()){ $user->deleteBy($id); } $user->allowField(true)->updateBy($id, $params); } return IResponse::success([],'操作成功'); } /** * 订单操作 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/30 16:42 * * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function actionOrder($id) { $param = $this->request->param(); $valid = $this->validate($param, [ 'action|操作状态' => 'require' ]); // 查订单 $order = model('common/GoodsOrder')->where(['id' => $id])->find(); if ($order['status'] == 0){ return IResponse::failure("当前订单已关闭"); } // 错误返回 (true !== $valid) && IResponse::failure($valid); $model = model('common/GoodsOrder'); switch ($param['action']){ case "ship": $msg = "发货"; // 拦截确认过的订单 if ($order['status'] >= 3){ return IResponse::failure("当前订单已发货"); } // 状态以及时间 $model->updateBy($id,[ 'shipping_at' => time(), 'status' => 3 ]); break; case "confirm": $msg = "确认"; // 拦截确认过的订单 if ($order['status'] >= 4){ return IResponse::failure("当前订单已确认"); } // 查商品信息 取得商品进货价 // 计算利润 // 利润的百分几 // 利润 count * ( total_price - instock ) // 总利润 $cgo_total = 0; // 子单明细 $its = model('common/GoodsOrderDetail')->where(['order_id' => $order['id']])->select(); foreach ($its as $itsv) { // 产品 $goin = model('common/GoodsSku')->where(['goods_id' => $itsv['goods_id'],'param' => enjson(str2arr($itsv['spec'],','))])->value('instock_price'); $cgo_total += sprintf("%.2f", round($itsv['total_price'] - $goin, 2)); } // 奖励期权条件 -- 资产消费不得期权 if ($order && $order['is_pin'] !== 1 && $order['pay_type'] !== 'property'){ // 用户期权 // order_property_reward $order_property_reward = sys_config('order_property_reward','store'); // 计算 交易金额$order['total_price'] * order_property_reward //2020 12 09改为利润$cgo_total $reward = sprintf("%.3f", round(((float)$order_property_reward / 100) * $cgo_total, 3)); if($reward > 0){// 更新 model('common/Users')->changeProperty( $order['user_id'], $reward, '订单完成,奖励资产【' . $reward . "】", true ); } } // 查找上级 $parent = model('common/UsersInviteRelation')->getBy(['invite_id' => $order['user_id']]); // 上级推荐奖励 if ($parent['form_id'] !== 0) { // 取得收益百分几 $user_spread_reward_scale = sys_config('user_spread_reward_scale','user'); // 计算收益 $reward = sprintf("%.2f", round(((float)$user_spread_reward_scale / 100) * $cgo_total, 2)); model('common/Users')->changeBalance( $parent['form_id'], $reward, "下级消费奖励,奖金【" . $reward .'】', true ); } // 系统帮助用户自主确认订单 $model->updateBy($id,[ 'received_at' => time(), 'status' => 4 ]); break; case "delete": $msg = "删除"; $model->deleteBy($id); break; } return IResponse::success([],$msg . '成功'); } /************ 分类 **********************/ public function categories() { $where = []; //组合搜索 $user = model('common/GoodsCategory'); return IResponse::paginate($user->where($where)->order(['parent_id' => 'asc']) ->paginate(input('limit'),false)); } /** * 新增分类 * * @return mixed */ public function category() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'name|产品分类' => 'require|unique:GoodsCategory', ],[ 'email.unique' => '重复产品分类名称!' ]); (true !== $valid) && IResponse::failure($valid); // 保存数据 $goodsId = model('common/GoodsCategory')->storeBy($params); return $goodsId ? IResponse::success([],'新增产品分类成功'): IResponse::failure('新增产品分类异常'); } /** * 更新分类 * * @return mixed */ public function updateCategory($id) { // 接收数据 $params = $this->request->param(); // 查询 $goods = model('common/GoodsCategory')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params,[ 'name|产品分类' => 'require', ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 更新用户信息 $goods->updateBy($id, $params); return IResponse::success('更新分类信息成功'); } /** * 删除分类 * @param $id * * @return mixed */ public function deleteCategory($id) { model('common/GoodsCategory')->deleteBy($id); return IResponse::success([],'删除产品分类成功'); } }