Files
tougao/application/api/controller/PromotionFactory.php
2026-04-20 15:33:59 +08:00

282 lines
9.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\controller;
use think\Db;
use think\Validate;
class PromotionFactory extends Base
{
public function __construct(\think\Request $request = null)
{
parent::__construct($request);
}
/**
* 工厂列表
* 参数: journal_id, state(-1=全部), page, per_page
*/
public function getList()
{
$journalId = intval($this->request->param('journal_id', 0));
$editor_id = $this->request->param("user_id",0);
$state = $this->request->param('state', '-1');
$page = max(1, intval($this->request->param('page', 1)));
$perPage = max(1, min(intval($this->request->param('per_page', 20)), 100));
// if (!$journalId) {
// return jsonError('journal_id is required');
// }
if($journalId!==0){
$where[] = ['journal_id', '=', $journalId];
}else{
$journalIds = $this->journal_obj->where("editor_id",$editor_id)->where("state",0)->column("journal_id");
$where[] = ['journal_id',"in",$journalIds];
}
if ($state !== '-1' && $state !== '') {
$where[] = ['state', '=', intval($state)];
}
$countQuery = Db::name('promotion_factory');
foreach ($where as $w) {
$countQuery->where($w[0], $w[1], $w[2]);
}
$total = $countQuery->count();
$listQuery = Db::name('promotion_factory');
foreach ($where as $w) {
$listQuery->where($w[0], $w[1], $w[2]);
}
$list = $listQuery
->order('promotion_factory_id desc')
->page($page, $perPage)
->select();
foreach ($list as &$item) {
$item['ctime_text'] = $item['ctime'] ? date('Y-m-d H:i:s', $item['ctime']) : '';
$item['fetch_fields'] = $this->resolveFetchFields($item['fetch_ids']);
$item['country_scope_label'] = $this->buildCountryScopeLabel(
isset($item['target_partitions']) ? (string)$item['target_partitions'] : '',
isset($item['target_country_ids']) ? (string)$item['target_country_ids'] : ''
);
}
return jsonSuccess([
'list' => $list,
'total' => $total,
'page' => $page,
'per_page' => $perPage,
'total_pages' => $total > 0 ? ceil($total / $perPage) : 0,
]);
}
/**
* 工厂详情
* 参数: promotion_factory_id
*/
public function getDetail()
{
$id = intval($this->request->param('promotion_factory_id', 0));
if (!$id) {
return jsonError('promotion_factory_id is required');
}
$row = Db::name('promotion_factory')->where('promotion_factory_id', $id)->find();
if (!$row) {
return jsonError('Factory not found');
}
$row['ctime_text'] = $row['ctime'] ? date('Y-m-d H:i:s', $row['ctime']) : '';
$row['fetch_fields'] = $this->resolveFetchFields($row['fetch_ids']);
$row['country_scope_label'] = $this->buildCountryScopeLabel(
isset($row['target_partitions']) ? (string)$row['target_partitions'] : '',
isset($row['target_country_ids']) ? (string)$row['target_country_ids'] : ''
);
return jsonSuccess($row);
}
/**
* 新增工厂
*/
public function add()
{
$data = $this->request->post();
$journalId = intval(isset($data['journal_id']) ? $data['journal_id'] : 0);
if (!$journalId) {
return jsonError('journal_id is required');
}
$journal = Db::name('journal')->where('journal_id', $journalId)->where('state', 0)->find();
if (!$journal) {
return jsonError('Journal not found');
}
$now = time();
$id = Db::name('promotion_factory')->insertGetId([
'journal_id' => $journalId,
'type' => intval(isset($data['type']) ? $data['type'] : 0),
'expert_type' => intval(isset($data['expert_type']) ? $data['expert_type'] : 5),
'email_ids' => trim(isset($data['email_ids']) ? (string)$data['email_ids'] : ''),
'send_count' => max(1, intval(isset($data['send_count']) ? $data['send_count'] : 100)),
'template_id' => intval(isset($data['template_id']) ? $data['template_id'] : 0),
'style_id' => intval(isset($data['style_id']) ? $data['style_id'] : 0),
'fetch_ids' => trim(isset($data['fetch_ids']) ? (string)$data['fetch_ids'] : ''),
'target_partitions' => trim(isset($data['target_partitions']) ? (string)$data['target_partitions'] : ''),
'target_country_ids' => trim(isset($data['target_country_ids']) ? (string)$data['target_country_ids'] : ''),
'state' => 0,
'ctime' => $now,
]);
return jsonSuccess(['promotion_factory_id' => $id]);
}
public function getCountForPromotionEmailIds(){
$data = $this->request->post();
$rule = new Validate([
"ids"=>"require"
]);
if(!$rule->check($data)){
return jsonError($rule->getError());
}
$ids = explode(",",$data["ids"]);
$emails = Db::name("journal_email")->whereIn("j_email_id",$ids)->select();
$count = 0;
foreach ($emails as $item){
$count += $item["daily_limit"];
}
return jsonSuccess(['limit_count'=>$count]);
}
/**
* 编辑工厂
*/
public function edit()
{
$data = $this->request->post();
$id = intval(isset($data['promotion_factory_id']) ? $data['promotion_factory_id'] : 0);
if (!$id) {
return jsonError('promotion_factory_id is required');
}
$row = Db::name('promotion_factory')->where('promotion_factory_id', $id)->find();
if (!$row) {
return jsonError('Factory not found');
}
$update = [];
$allowedFields = [
'type', 'expert_type', 'email_ids', 'send_count',
'template_id', 'style_id', 'fetch_ids',
'target_partitions', 'target_country_ids',
];
$intFields = ['type', 'expert_type', 'send_count', 'template_id', 'style_id'];
foreach ($allowedFields as $f) {
if (isset($data[$f])) {
$update[$f] = in_array($f, $intFields) ? intval($data[$f]) : trim((string)$data[$f]);
}
}
if (isset($update['send_count'])) {
$update['send_count'] = max(1, $update['send_count']);
}
if (empty($update)) {
return jsonError('没有可更新的字段');
}
Db::name('promotion_factory')->where('promotion_factory_id', $id)->update($update);
return jsonSuccess([]);
}
/**
* 删除工厂(软删除 state=1
*/
public function delete()
{
$id = intval($this->request->param('promotion_factory_id', 0));
if (!$id) {
return jsonError('promotion_factory_id is required');
}
$row = Db::name('promotion_factory')->where('promotion_factory_id', $id)->find();
if (!$row) {
return jsonError('Factory not found');
}
Db::name('promotion_factory')->where('promotion_factory_id', $id)->update(['state' => 1]);
return jsonSuccess([]);
}
/**
* 启用/禁用切换
*/
public function toggle()
{
$id = intval($this->request->param('promotion_factory_id', 0));
if (!$id) {
return jsonError('promotion_factory_id is required');
}
$row = Db::name('promotion_factory')->where('promotion_factory_id', $id)->find();
if (!$row) {
return jsonError('Factory not found');
}
$newState = ($row['state'] == 0) ? 1 : 0;
Db::name('promotion_factory')->where('promotion_factory_id', $id)->update(['state' => $newState]);
return jsonSuccess(['state' => $newState]);
}
/**
* 根据 fetch_ids 解析出领域名称列表
*/
private function resolveFetchFields($fetchIds)
{
$fetchIds = trim((string)$fetchIds);
if ($fetchIds === '') {
return [];
}
$ids = array_map('intval', explode(',', $fetchIds));
$ids = array_filter($ids);
if (empty($ids)) {
return [];
}
return Db::name('expert_fetch')
->where('expert_fetch_id', 'in', $ids)
->column('field', 'expert_fetch_id');
}
/**
* 构建国家范围描述标签
*/
private function buildCountryScopeLabel($targetPartitions, $targetCountryIds)
{
$tp = trim($targetPartitions);
$tc = trim($targetCountryIds);
if ($tp === '' && $tc === '') {
return '全部国家';
}
$parts = [];
if ($tp !== '') {
$parts[] = '分区' . $tp;
}
if ($tc !== '') {
$cids = array_map('intval', explode(',', $tc));
$names = Db::name('country')->where('country_id', 'in', $cids)->column('zh_name');
if (!empty($names)) {
$parts[] = implode(',', $names);
}
}
return implode(' + ', $parts);
}
}