| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- <?php
- namespace app\admin\controller\store;
- use app\common\controller\AdminController;
- use app\http\IResponse;
- use think\Db;
- use Lettered\Support\Exceptions\EvidentException;
- use app\common\validate\IDMustBePositiveInt;
- class Product extends AdminController
- {
- /**
- * 新增规格
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @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([],'删除产品分类成功');
- }
- }
|