270 lines
10 KiB
PHP
270 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* SAAS应用系统 --- 十年开发经验汇集巨献!
|
|
* ==========================================================
|
|
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
|
|
* ----------------------------------------------------------
|
|
* 官方网址: https://www.zoomtk.com
|
|
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
|
|
* 任何企业和个人未经允许对程序代码以任何形式任何目的再发布传播。
|
|
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
|
|
* ==========================================================
|
|
*/
|
|
|
|
namespace addon\saas\model;
|
|
|
|
use addon\saas\model\Config as ConfigModel;
|
|
use app\model\DbModel;
|
|
use app\model\system\Address as AddressModel;
|
|
use think\facade\Db;
|
|
|
|
class RegionManage extends DbModel
|
|
{
|
|
public $manage_status_zh = [
|
|
1 => '正常',
|
|
0 => '冻结',
|
|
];
|
|
|
|
/**
|
|
* 添加经理
|
|
* @param $data
|
|
* @return mixed
|
|
*/
|
|
public function addManage($data)
|
|
{
|
|
if(!$data['province_id']){
|
|
return $this->error('', '请至少选择省份');
|
|
}
|
|
$or = [
|
|
['member_id', '=', $data['member_id']]
|
|
];
|
|
$condition = [
|
|
['province_id', '=', $data['province_id']],
|
|
['site_id', '=', $data['site_id']],
|
|
['is_delete', '=' , 1]
|
|
];
|
|
if($data['city_id']){
|
|
$condition[] = ['city_id', '=', $data['city_id']];
|
|
}
|
|
if($data['district_id']){
|
|
$condition[] = ['district_id', '=', $data['district_id']];
|
|
}else{
|
|
$condition[] = ['district_id', '=', 0];
|
|
}
|
|
$manage_info = model('region_manage')->getInfo(
|
|
$condition, 'id,member_id','a', null , null ,$or
|
|
);
|
|
if (!empty($manage_info)) return $this->error('', $manage_info['member_id'] == $data['member_id'] ? '已经是区域经理了' : '该区域已有区域经理');
|
|
$data = array_filter($data, function($value) {
|
|
return $value !== null && $value !== "";
|
|
});
|
|
$data['create_time'] = time();
|
|
$res = model('region_manage')->add($data);
|
|
if($res){
|
|
return $this->success($res);
|
|
}else{
|
|
return $this->error('', '添加失败');
|
|
}
|
|
}
|
|
|
|
public function editManage($data,$id){
|
|
$condition = [
|
|
['id' , '=' , $id]
|
|
];
|
|
$result = model('region_manage')->update($data, $condition);
|
|
return $this->success($result);
|
|
}
|
|
|
|
/**
|
|
* 获取经理分页列表
|
|
* @param array $condition
|
|
* @param number $page
|
|
* @param string $page_size
|
|
* @param string $order
|
|
* @param string $field
|
|
*/
|
|
public function getManagePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'f.id,f.member_id,f.site_id,f.level_id,f.province_id,f.city_id,f.district_id,f.total_commission,f.status,f.create_time,m.nickname,m.mobile,m.member_id,m.headimg,l.id as level_id,l.level_name,l.reward_type,l.site_rate')
|
|
{
|
|
$condition[] = ['f.is_delete', '=' , 1];
|
|
$alias = 'f';
|
|
$join = [
|
|
[
|
|
'member m',
|
|
'm.member_id = f.member_id',
|
|
'left'
|
|
],
|
|
[
|
|
'region_level l',
|
|
'l.id = f.level_id',
|
|
'left'
|
|
],
|
|
];
|
|
$list = model('region_manage')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
|
if ($list) {
|
|
$address_model = new AddressModel();
|
|
foreach ($list['list'] as &$item) {
|
|
$address = $item['province_id'] > 0 ? $address_model->getAreaName($item['province_id'])['data'] : '';
|
|
$address .= $item['city_id'] > 0 ? ' '.$address_model->getAreaName($item['city_id'])['data'] : '';
|
|
$address .= $item['district_id'] > 0 ? ' '.$address_model->getAreaName($item['district_id'])['data'] : '';
|
|
$item['address'] = $address;
|
|
}
|
|
}
|
|
return $this->success($list);
|
|
}
|
|
|
|
public function getManageInfo($condition, $field = 'f.id,f.member_id,f.site_id,f.level_id,f.province_id,f.city_id,f.district_id,f.account,f.total_commission,f.status,f.create_time,m.nickname,m.mobile,m.member_id,m.headimg,l.id as level_id,l.level_name,l.reward_type,l.site_rate')
|
|
{
|
|
$alias = 'f';
|
|
$join = [
|
|
[
|
|
'member m',
|
|
'm.member_id = f.member_id',
|
|
'left'
|
|
],
|
|
[
|
|
'region_level l',
|
|
'l.id = f.level_id',
|
|
'left'
|
|
],
|
|
];
|
|
$list = model('region_manage')->getInfo($condition, $field, $alias, $join);
|
|
return $this->success($list);
|
|
}
|
|
|
|
/**
|
|
* 变更等级
|
|
* @param array $condition
|
|
* @param string $field
|
|
* @param string $order
|
|
* @param string $limit
|
|
*/
|
|
public function changeManageLevel($data, $condition)
|
|
{
|
|
$result = model('region_manage')->update($data, $condition);
|
|
return $this->success($result);
|
|
}
|
|
|
|
/**
|
|
* 冻结
|
|
* @param $id
|
|
* @return array
|
|
*/
|
|
public function frozen($id,$status = 0)
|
|
{
|
|
$data = [
|
|
'status' => $status,
|
|
];
|
|
$res = model('region_manage')->update($data, [ [ 'id', '=', $id ] ]);
|
|
|
|
return $this->success($res);
|
|
}
|
|
|
|
public function deleteManage($id){
|
|
$data = [
|
|
'is_delete' => 0,
|
|
'delete_time' => time()
|
|
];
|
|
$res = model('region_manage')->update($data, [ [ 'id', '=', $id ] ]);
|
|
|
|
return $this->success($res);
|
|
}
|
|
|
|
public function calculate($order){
|
|
$province_id = $order['province_id'];
|
|
$city_id = $order['city_id'];
|
|
$district_id = $order['district_id'];
|
|
$site_id = $order['site_id'];
|
|
$prefix = config('database.connections.mysql.prefix').'region_manage';
|
|
$sql = "SELECT COALESCE((SELECT id FROM {$prefix} WHERE province_id = {$province_id} AND city_id = {$city_id} AND district_id = {$district_id} AND site_id = {$site_id} LIMIT 1),
|
|
(SELECT id FROM {$prefix} WHERE province_id = {$province_id} AND city_id = {$city_id} AND district_id = 0 AND site_id = {$site_id} LIMIT 1),
|
|
(SELECT id FROM {$prefix} WHERE province_id = {$province_id} AND city_id = 0 AND district_id = 0 AND site_id = {$site_id} LIMIT 1)) FROM {$prefix} LIMIT 1";
|
|
$info = model('region_manage')->query($sql);
|
|
$data = array_values($info[0]);
|
|
$manage_id = $data[0];
|
|
//判断是否有区域经理符合订单用户地址
|
|
if($manage_id > 0 && !is_null($manage_id)){
|
|
model('region_order')->startTrans();
|
|
try{
|
|
$manage_info = self::getManageInfo([['f.id', '=', $manage_id]])['data'];
|
|
$order_goods = model('order_goods')->getColumn([['order_id', '=', $order['order_id']]], '*', 'order_goods_id');//查询当前对应产品
|
|
foreach ($order_goods as $k => $orderGoodsInfo) {
|
|
$amount = $orderGoodsInfo['price'] * $orderGoodsInfo['num'];
|
|
if($manage_info['reward_type'] == 1){
|
|
$commission = round($orderGoodsInfo['goods_money'] * $manage_info['site_rate'] / 100, 2);
|
|
}else{
|
|
$commission = $manage_info['site_rate'];
|
|
}
|
|
$add = [
|
|
'order_id' => $order['order_id'],
|
|
'site_id' => $manage_info['site_id'],
|
|
'order_no' => $order['order_no'],
|
|
'order_goods_id' => $orderGoodsInfo['order_goods_id'],
|
|
'order_site_id' => $order['site_id'],
|
|
'goods_name' => $orderGoodsInfo['goods_name'],
|
|
'sku_name' => $orderGoodsInfo['sku_name'],
|
|
'sku_image' => $orderGoodsInfo['sku_image'],
|
|
'price' => $amount,
|
|
'num' => $orderGoodsInfo['num'],
|
|
'order_price' => $orderGoodsInfo['goods_money'],
|
|
'member_id' => $order['member_id'],
|
|
'full_address' => $order['full_address'] . $order['address'],
|
|
'manage_id' => $manage_id,
|
|
'commission' => $commission,
|
|
'reward_type' => $manage_info['reward_type'],
|
|
'rate' => $manage_info['site_rate'],
|
|
'create_time' => time(),
|
|
];
|
|
model("region_order")->add($add);
|
|
}
|
|
model('region_order')->commit();
|
|
}catch(\Exception $e){
|
|
model('region_order')->rollback();
|
|
return $this->error();
|
|
}
|
|
}
|
|
return $manage_id;
|
|
}
|
|
|
|
public function settlement($params){
|
|
if (isset($params['order_id'])) {
|
|
$where = [
|
|
['order_id', '=', $params['order_id']],
|
|
['is_settlement', '=', 0],
|
|
['is_refund', '=', 0],
|
|
];
|
|
$field = 'id,manage_id,commission,site_id';
|
|
$order_info = model('region_order')->getList($where, $field);
|
|
if($order_info){
|
|
model('region_order')->startTrans();
|
|
try{
|
|
$account = new RegionAccount();
|
|
model('region_order')->update(['is_settlement' => 1,'settlement_time' => time()], $where);//变更结算状态
|
|
foreach ($order_info as $k => $v) {
|
|
$manage_id = $v['manage_id'];
|
|
if($manage_id > 0 && $v['commission'] > 0){
|
|
$where = [
|
|
'id' => $manage_id,
|
|
'status' => 1,
|
|
'is_delete' => 1
|
|
];
|
|
$count = model('region_manage')->getCount($where);
|
|
if($count){
|
|
$account->addAccount($manage_id, 'order', $v['commission'], $v['id'], $v['site_id']);
|
|
}
|
|
}
|
|
}
|
|
model('region_order')->commit();
|
|
return $this->success('OrderSettlementByRegion');
|
|
}catch(\Exception $e){
|
|
model('region_order')->rollback();
|
|
return $this->error();
|
|
}
|
|
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|