File: /www/wwwroot/www.fpngv.com/application/admin/controller/XxlData.php
<?php
// +----------------------------------------------------------------------
// | Author: Moli <332287662@qq.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 信息流数据列表
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;
class XxlData extends Adminbase
{
protected function initialize()
{
parent::initialize();
// 公司数据
$companydata = [""];
$result = Db::name('xxl_company')->select();;
foreach ($result as $k => $v) {
$companydata[$v['id']] = $v['title'];
}
$this->assign('companydata', $companydata);
// 项目数据
$projectdata = [""];
$result = Db::name('xxl_project')->select();;
foreach ($result as $k => $v) {
$projectdata[$v['id']] = $v['title'];
}
$this->assign('projectdata', $projectdata);
// 地区数据
$areadata = [""];
$result = Db::name('xxl_area')->select();;
foreach ($result as $k => $v) {
$areadata[$v['id']] = $v['title'];
}
$this->assign('areadata', $areadata);
// 渠道平台
$channeldata = [""];
$result = Db::name('xxl_channel')->select();;
foreach ($result as $k => $v) {
$channeldata[$v['id']] = $v['title'];
}
$this->assign('channeldata', $channeldata);
// 币种
$currencydata = [""];
$result = Db::name('xxl_currency')->select();;
foreach ($result as $k => $v) {
$currencydata[$v['id']] = $v['title'];
}
$this->assign('currencydata', $currencydata);
}
// 信息流每日数据列表
public function index()
{
if ($this->request->isAjax()) {
$data = $this->request->get('');
$page = $data['page'];
$limit = $data['limit'];
// 获取搜索条件
$filter = $data['filter'];
if ($filter) {
$filter = json_decode($filter, true);
// 开始时间
$startTime = $filter['startTime'];
// 结束时间
$endTime = $filter['endTime'];
$userInfo = $this->_userinfo = Session::get('admin');
$roleid = $userInfo['roleid'];
if ($roleid != 1 && $roleid != 2) {
$where[] = ['pitcher', '=', $userInfo['nickname']];
}
if (!empty($startTime && $endTime)) {
$where[] = ['Ttime', 'between time', [$startTime, $endTime]];
}
$list = Db::name('xxl_data')
->where($where)
->order(array('id' => 'DESC'))
->select();
$total = count($list);
$result = array("code" => 0, 'count' => $total, "data" => $list);
return json($result);
} else {
// 获取用户的权限组 如果是超级管理员,那么可以看到所有用户的列表,如果不是,则看不到
$userInfo = $this->_userinfo = Session::get('admin');
$roleid = $userInfo['roleid'];
if ($roleid === 1 || $roleid === 2) {
$list = Db::name('xxl_data')->order(array('id' => 'DESC'))->select();
$_list = Db::name('xxl_data')->order(array('id' => 'DESC'))->page($page, $limit)->select();
$total = count($list);
$result = array("code" => 0, 'count' => $total, "data" => $_list);
return json($result);
} else {
$list = Db::name('xxl_data')->order(array('id' => 'DESC'))->where('pitcher', $userInfo['nickname'])->select();
$_list = Db::name('xxl_data')->order(array('id' => 'DESC'))->where('pitcher', $userInfo['nickname'])->page($page, $limit)->select();
$total = count($list);
$result = array("code" => 0, 'count' => $total, "data" => $_list);
return json($result);
}
}
} else {
return $this->fetch();
}
}
// 信息流每日数据添加
public function add()
{
if ($this->request->isPost()) {
$data = $this->request->post('');
// 时间
$Ttime = $data['Ttime'];
for ($i = 0; $i < count($Ttime); $i++) {
// 根据公司id查找公司名称
$company[$i] = Db::name('xxl_company')->where('id', $data['companyid'][$i])->value('title');
// 项目
$project[$i] = Db::name('xxl_project')->where('id', $data['projectid'][$i])->value('title');
// 地区
$area[$i] = Db::name('xxl_area')->where('id', $data['areaid'][$i])->value('title');
// 渠道平台
$channel[$i] = Db::name('xxl_channel')->where('id', $data['channelid'][$i])->value('title');
// 币种
$currency[$i] = Db::name('xxl_currency')->where('id', $data['currencyid'][$i])->value('title');
// 消费
$consume[$i] = $data['consume'][$i];
// 税后消费
$consume_after[$i] = $data['consume_after'][$i];
// 加粉数量
$add_fan[$i] = $data['add_fan'][$i];
// 计算成本,使用税后消费除以加粉数量,得到成本
if($add_fan[$i]!=0){
$cost[$i] = $consume_after[$i] / $add_fan[$i];
}else{
$cost[$i]=$add_fan[$i];
}
// 判断币种,转换成人民币税后消费以及成本
if ($currency[$i] === '台币') {
$consume_rmb[$i] = sprintf("%1\$.2f", $consume_after[$i] / 4.4);
$cost_rmb[$i] = sprintf("%1\$.2f", $cost[$i] / 4.4);
} else if ($currency[$i] === '美金') {
$consume_rmb[$i] = sprintf("%1\$.2f", $consume_after[$i] * 7);
$cost_rmb[$i] = sprintf("%1\$.2f", $cost[$i] * 7);
} else if ($currency[$i] === '日元') {
$consume_rmb[$i] = sprintf("%1\$.2f", $consume_after[$i] / 15.68);
$cost_rmb[$i] = sprintf("%1\$.2f", $cost[$i] / 15.68);
}else {
$consume_rmb[$i] = sprintf("%1\$.2f", $consume_after[$i]);
$cost_rmb[$i] = sprintf("%1\$.2f", $cost[$i]);;
}
// 获取投手
$userInfo = $this->_userinfo = Session::get('admin');
$this->assign('userInfo', $this->_userinfo);
$pitcher = $userInfo['nickname'];
// 创建时间
$creattime = date('Y-m-d H:i:s', time());
$xxl_data[$i] = [
'Ttime' => $Ttime[$i],
'company' => $company[$i],
'project' => $project[$i],
'area' => $area[$i],
'channel' => $channel[$i],
'currency' => $currency[$i],
'consume' => $consume[$i],
'consume_after' => $consume_after[$i],
'add_fan' => $add_fan[$i],
'cost' => $cost[$i],
'pitcher' => $pitcher,
'creat_time' => $creattime,
'consume_rmb' => $consume_rmb[$i],
'cost_rmb' => $cost_rmb[$i]
];
$result = Db::name('xxl_data')
->data($xxl_data[$i])
->insert();
}
if ($result) {
$this->success("添加数据成功!", url('admin/xxl_data/index'));
} else {
$this->error("添加数据失败!");
}
}
return $this->fetch();
}
// 信息流每日数据编辑
public function edit()
{
if ($this->request->isPost()) {
$data = $this->request->post('');
// 时间
$Ttime = $data['Ttime'];
// 根据公司id查找公司名称
$company = Db::name('xxl_company')->where('id', $data['companyid'])->value('title');
// 项目
$project = Db::name('xxl_project')->where('id', $data['projectid'])->value('title');
// 地区
$area = Db::name('xxl_area')->where('id', $data['areaid'])->value('title');
// 渠道平台
$channel = Db::name('xxl_channel')->where('id', $data['channelid'])->value('title');
// 币种
$currency = Db::name('xxl_currency')->where('id', $data['currencyid'])->value('title');
// 消费
$consume = $data['consume'];
// 税后消费
$consume_after = $data['consume_after'];
// 加粉数量
$add_fan = $data['add_fan'];
// $cost = $consume_after / $add_fan;
// 计算成本,使用税后消费除以加粉数量,得到成本
if($add_fan!=0){
$cost = $consume_after / $add_fan;
}else{
$cost=$add_fan;
}
// 判断币种,转换成人民币税后消费以及成本
if ($currency === '台币') {
$consume_rmb = sprintf("%1\$.2f", $consume_after / 4.4);
$cost_rmb = sprintf("%1\$.2f", $cost / 4.4);
} else if ($currency === '美金') {
$consume_rmb = sprintf("%1\$.2f", $consume_after * 7);
$cost_rmb = sprintf("%1\$.2f", $cost * 7);
}else if ($currency === '日元') {
$consume_rmb = sprintf("%1\$.2f", $consume_after / 15.68);
$cost_rmb = sprintf("%1\$.2f", $cost /15.68);
} else {
$consume_rmb = sprintf("%1\$.2f", $consume_after);
$cost_rmb = sprintf("%1\$.2f", $cost);;
}
// 获取投手
// $userInfo = $this->_userinfo = Session::get('admin');
// $this->assign('userInfo', $this->_userinfo);
// $pitcher = $userInfo['nickname'];
// 更新时间
$update_time = date('Y-m-d H:i:s', time());
$xxl_data = [
'id' => $data['id'],
'Ttime' => $Ttime,
'company' => $company,
'project' => $project,
'area' => $area,
'channel' => $channel,
'currency' => $currency,
'consume' => $consume,
'consume_after' => $consume_after,
'add_fan' => $add_fan,
'cost' => $cost,
// 'pitcher' => $pitcher,
'update_time' => $update_time,
'consume_rmb' => $consume_rmb,
'cost_rmb' => $cost_rmb
];
$result = Db::name('xxl_data')
->update($xxl_data);
if ($result) {
$this->success("修改成功!");
}
} else {
$id = $this->request->param('id/d');
$ishas = Db::name('xxl_data')->where('id', $id)->find();
if (empty($ishas)) {
$this->error('该信息不存在!');
}
// 根据公司名称查公司id
$companyid = Db::name('xxl_company')->where('title', $ishas['company'])->value('id');
// 项目
$projectid = Db::name('xxl_project')->where('title', $ishas['project'])->value('id');
// 地区
$areaid = Db::name('xxl_area')->where('title', $ishas['area'])->value('id');
// 渠道平台
$channelid = Db::name('xxl_channel')->where('title', $ishas['channel'])->value('id');
// 币种
$currencyid = Db::name('xxl_currency')->where('title', $ishas['currency'])->value('id');
$data = [
'id' => $ishas['id'],
'Ttime' => $ishas['Ttime'],
'companyid' => $companyid,
'projectid' => $projectid,
'areaid' => $areaid,
'channelid' => $channelid,
'currencyid' => $currencyid,
'consume' => $ishas['consume'],
'consume_after' => $ishas['consume_after'],
'add_fan' => $ishas['add_fan'],
'cost' => $ishas['cost']
];
$this->assign("data", $data);
return $this->fetch();
}
}
// 删除数据
public function del()
{
$id = $this->request->param('id/d');
if (empty($id)) {
$this->error('请指定需要删除的数据ID!');
}
$ishas = Db::name('xxl_data')->where('id', $id)->find();
if ($ishas) {
Db::name('xxl_data')
->where('id', $id)
->delete();
$this->success("删除成功!");
}
}
}