diff --git a/.gitignore b/.gitignore index b7c8c7e..55e3fff 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,9 @@ backend-java/target/ backend-java/data/ backend-java/*.log backend-java/logs/ +logs/ +aiimage-backend.log* +backend/tmp/ # backend-java local config backend-java/src/main/resources/application-local.yml @@ -39,3 +42,4 @@ backend/__pycache__ app/assets/ app/new_web_source app/user_data/ +OPS_REDIS_MYSQL_OPTIMIZATION_NOTES.md diff --git a/app/__pycache__/ali_oss.cpython-312.pyc b/app/__pycache__/ali_oss.cpython-312.pyc index 553a210..eba58bb 100644 Binary files a/app/__pycache__/ali_oss.cpython-312.pyc and b/app/__pycache__/ali_oss.cpython-312.pyc differ diff --git a/app/__pycache__/app.cpython-312.pyc b/app/__pycache__/app.cpython-312.pyc index 3ca4159..3ee40b5 100644 Binary files a/app/__pycache__/app.cpython-312.pyc and b/app/__pycache__/app.cpython-312.pyc differ diff --git a/app/__pycache__/main.cpython-312.pyc b/app/__pycache__/main.cpython-312.pyc index 2e240fb..c6b2c4d 100644 Binary files a/app/__pycache__/main.cpython-312.pyc and b/app/__pycache__/main.cpython-312.pyc differ diff --git a/app/amazon/__pycache__/approve - 副本.cpython-312.pyc b/app/amazon/__pycache__/approve - 副本.cpython-312.pyc new file mode 100644 index 0000000..51270b4 Binary files /dev/null and b/app/amazon/__pycache__/approve - 副本.cpython-312.pyc differ diff --git a/app/amazon/__pycache__/backend_api.cpython-312.pyc b/app/amazon/__pycache__/backend_api.cpython-312.pyc new file mode 100644 index 0000000..f6066b8 Binary files /dev/null and b/app/amazon/__pycache__/backend_api.cpython-312.pyc differ diff --git a/app/amazon/__pycache__/price_match.cpython-312.pyc b/app/amazon/__pycache__/price_match.cpython-312.pyc new file mode 100644 index 0000000..56c0306 Binary files /dev/null and b/app/amazon/__pycache__/price_match.cpython-312.pyc differ diff --git a/app/app.py b/app/app.py index 751bbbc..237ee51 100644 --- a/app/app.py +++ b/app/app.py @@ -16,8 +16,6 @@ from blueprints.admin import admin_bp from blueprints.image import image_bp from blueprints.brand import brand_bp from blueprints.communication import communication_bp - - def create_app(): app = Flask(__name__, template_folder=BASE_DIR, static_folder=BASE_DIR) frontend_origin = os.environ.get('FRONTEND_ORIGIN', '*') diff --git a/app/assets/convert.js b/app/assets/convert.js index 75f22f4..9363954 100644 --- a/app/assets/convert.js +++ b/app/assets/convert.js @@ -1 +1 @@ -import{d as A,o as H,a as n,c as o,b as z,B as K,e as s,F as w,r as F,t as r,f as _,n as E,w as M,s as j,u as q,g as u,h as T,x as J,i as k,E as i,y as L,z as O,A as G,_ as Q,p as W,q as X}from"./pywebview-ClWy2SbE.js";import{e as Y}from"./brand-DBpLCgAF.js";const Z={class:"page-shell module-page"},ee={class:"main-content"},se={class:"left-panel"},te={class:"upload-zone"},ae={class:"selected-files clean-placeholder"},le={key:0,class:"more-line"},ne={key:1},oe={class:"option-group"},ie=["value"],re={class:"template-item-content"},ce={class:"label"},ue={key:0,class:"template-tag"},de={key:1,class:"template-tag muted"},ve={key:2,class:"template-tag custom"},pe={class:"desc"},_e={key:0,class:"empty-option"},fe={class:"option-group"},me={class:"radio-item active"},he={class:"desc"},ye={class:"run-row"},ge=["disabled"],be={class:"loading-msg"},we={class:"right-panel"},ke={class:"task-list-wrap"},Ce={class:"clean-result-summary"},xe={class:"summary-card"},Fe={class:"summary-card"},Pe={class:"summary-card"},Ee={class:"result-list-wrap"},Te={key:0,class:"empty-tasks"},Ie={key:1,class:"task-list clean-result-list"},Be={class:"left"},Ne=["title"],$e={key:0,class:"files"},De={key:1,class:"files"},Re={class:"task-right"},Se=["onClick"],Ue=["onClick"],Ve=A({__name:"BrandConvertTab",setup(He){const v=u([]),g=u(""),f=u([]),m=u(!1),b=u([]),h=u({total:0,successCount:0,failedCount:0,items:[]}),d=u([]),c=u(""),C=T(()=>v.value.slice(0,8)),I=T(()=>d.value.find(a=>a.id===c.value)||null);async function P(a){const e=k();if(!e?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const l of a){const p=typeof l=="string"?l:l.absolutePath,V=typeof l=="string"?void 0:l.relativePath,y=await e.upload_file_to_java(p,V);if(!y?.success||!y.data)throw new Error(y?.error||y?.message||`上传失败:${p}`);t.push(y.data)}return t}async function B(a){const e=await J();d.value=e||[];const t=d.value.find(p=>p.id===c.value);if(t){c.value=t.id;return}const l=d.value.find(p=>p.isDefault);if(l){c.value=l.id;return}c.value=d.value[0]?.id||""}async function N(a,e){v.value=a,g.value="",f.value=await P(a),i.success(e)}async function $(){const a=k();if(!a?.select_brand_xlsx_files){i.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const e=await a.select_brand_xlsx_files();if(!e?.length)return;await N(e,`已选择 ${e.length} 个待转换文件`)}catch(e){i.error(e instanceof Error?e.message:"选择失败")}}async function D(){const a=k();if(!a?.select_brand_folder){i.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const e=await a.select_brand_folder();if(!e)return;g.value=e.split(/[/\\]/).filter(Boolean).pop()||"";const t=await Y(e);if(t.success&&t.items?.length){v.value=t.items.map(l=>l.relativePath),f.value=await P(t.items),i.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`);return}i.warning(t.error||"该文件夹下没有 xlsx 文件")}catch(e){i.error(e instanceof Error?e.message:"选择失败")}}async function R(){if(!f.value.length){i.warning("请先选择待转换 Excel 文件或文件夹");return}if(!c.value){i.warning("请先选择模板");return}try{m.value=!0;const a=await L({files:f.value.map(e=>({fileKey:e.fileKey,originalFilename:e.originalFilename,relativePath:e.relativePath})),templateId:c.value,archiveName:f.value.some(e=>!!e.relativePath)&&g.value?g.value:void 0});h.value=a,b.value=a.items||[],await x(),i.success("格式转换完成")}catch(a){i.error(a instanceof Error?a.message:"格式转换失败")}finally{m.value=!1}}async function x(){try{const a=await O();b.value=a.items||[],h.value={total:a.items?.length||0,successCount:a.items?.filter(e=>e.success).length||0,failedCount:a.items?.filter(e=>!e.success).length||0,items:a.items||[]}}catch{}}async function S(a){try{await G(a),await x(),i.success("已删除")}catch(e){i.error(e instanceof Error?e.message:"删除失败")}}async function U(a){const e=k();if(!a.downloadUrl){i.warning("当前结果没有下载地址");return}const t=a.outputFilename||`${new Date().toISOString().slice(0,10).replace(/-/g,"")}.txt`;if(e?.save_file_from_url_new){const l=await e.save_file_from_url_new(a.downloadUrl,t);l.success?i.success(`已保存:${l.path||t}`):l.error&&l.error!=="用户取消"&&i.error(l.error);return}}return H(()=>{B().catch(()=>{}),x().catch(()=>{})}),(a,e)=>(n(),o("div",Z,[z(K,{active:"convert"}),s("div",ee,[s("aside",se,[e[5]||(e[5]=s("div",{class:"section-title"},"选择文件",-1)),s("div",te,[e[1]||(e[1]=s("div",{class:"hint"},"选择需要转换的 Excel 文件或文件夹,将按所选模板生成 txt 文件。",-1)),s("div",{class:"btns"},[s("button",{type:"button",class:"opt-btn",onClick:$},"选择 Excel 文件"),s("button",{type:"button",class:"opt-btn",onClick:D},"选择文件夹")]),s("div",ae,[v.value.length?(n(),o(w,{key:0},[(n(!0),o(w,null,F(C.value,t=>(n(),o("span",{key:t},r(t),1))),128)),v.value.length>C.value.length?(n(),o("span",le," 还有 "+r(v.value.length-C.value.length)+" 个文件未展开显示 ",1)):_("",!0)],64)):(n(),o("span",ne,"暂未选择待转换文件"))])]),e[6]||(e[6]=s("div",{class:"section-title"},"模板选择",-1)),s("div",oe,[(n(!0),o(w,null,F(d.value,t=>(n(),o("label",{key:t.id,class:E(["radio-item",{active:c.value===t.id}])},[M(s("input",{"onUpdate:modelValue":e[0]||(e[0]=l=>c.value=l),type:"radio",value:t.id,name:"convert-template"},null,8,ie),[[j,c.value]]),s("div",re,[s("div",null,[s("span",ce,[q(r(t.templateName)+" ",1),t.isDefault?(n(),o("span",ue,"默认")):t.builtIn?(n(),o("span",de,"内置")):(n(),o("span",ve,"自定义"))]),s("div",pe,"输出文件:"+r(t.outputFilename),1)])])],2))),128)),d.value.length?_("",!0):(n(),o("div",_e,"暂无可用模板"))]),e[7]||(e[7]=s("div",{class:"section-title"},"转换说明",-1)),s("div",fe,[s("label",me,[e[3]||(e[3]=s("input",{type:"checkbox",checked:"",disabled:""},null,-1)),s("div",null,[e[2]||(e[2]=s("span",{class:"label"},"按当前模板输出",-1)),s("div",he,"当前模板:"+r(I.value?.templateName||"未加载模板"),1)])]),e[4]||(e[4]=s("label",{class:"radio-item active"},[s("input",{type:"checkbox",checked:"",disabled:""}),s("div",null,[s("span",{class:"label"},"SKU 使用雪花ID-序号"),s("div",{class:"desc"},"例如 1773816146796-1")])],-1))]),s("div",ye,[s("button",{type:"button",class:"btn-run",disabled:m.value,onClick:R},r(m.value?"转换中...":"开始转换"),9,ge),s("span",be,r(m.value?"正在生成并上传结果,请稍候…":"选择文件后即可执行格式转换,结果会显示在右侧供下载"),1)])]),s("section",we,[e[12]||(e[12]=s("div",{class:"panel-header"},"转换结果",-1)),s("div",ke,[s("div",Ce,[s("div",xe,[e[8]||(e[8]=s("span",{class:"summary-label"},"已处理文件",-1)),s("strong",null,r(h.value.total),1)]),s("div",Fe,[e[9]||(e[9]=s("span",{class:"summary-label"},"成功结果",-1)),s("strong",null,r(h.value.successCount),1)]),s("div",Pe,[e[10]||(e[10]=s("span",{class:"summary-label"},"失败文件",-1)),s("strong",null,r(h.value.failedCount),1)])]),s("div",Ee,[e[11]||(e[11]=s("div",{class:"result-list-header"},[s("span",null,"生成的结果压缩包列表")],-1)),b.value.length===0?(n(),o("div",Te," 暂无转换结果,完成格式转换后会在这里展示结果压缩包 ")):(n(),o("ul",Ie,[(n(!0),o(w,null,F(b.value,t=>(n(),o("li",{key:`${t.resultId||t.outputFilename||t.sourceFilename}`,class:"task-item"},[s("div",Be,[s("span",{class:"id",title:t.sourceFilename},r(t.sourceFilename||"-"),9,Ne),t.outputFilename?(n(),o("div",$e,"压缩包文件:"+r(t.outputFilename),1)):_("",!0),t.error?(n(),o("div",De,"错误信息:"+r(t.error),1)):_("",!0)]),s("div",Re,[s("span",{class:E(["status",t.success?"success":"failed"])},r(t.success?"已完成":"失败"),3),t.success&&t.downloadUrl?(n(),o("button",{key:0,type:"button",class:"download",onClick:l=>U(t)}," 下载压缩包 ",8,Se)):_("",!0),t.resultId?(n(),o("button",{key:1,type:"button",class:"btn-delete",onClick:l=>S(t.resultId)}," 删除 ",8,Ue)):_("",!0)])]))),128))]))])])])])]))}}),Ae=Q(Ve,[["__scopeId","data-v-7c280b44"]]);W(Ae).use(X).mount("#app"); +import{d as A,o as H,a as n,c as o,b as z,B as K,e as s,F as w,r as F,t as r,f as _,n as E,w as M,s as j,u as q,g as u,h as T,x as J,i as k,E as i,y as L,z as O,A as G,_ as Q,p as W,q as X}from"./pywebview-DuyK2jB1.js";import{e as Y}from"./brand-CKgrwtni.js";const Z={class:"page-shell module-page"},ee={class:"main-content"},se={class:"left-panel"},te={class:"upload-zone"},ae={class:"selected-files clean-placeholder"},le={key:0,class:"more-line"},ne={key:1},oe={class:"option-group"},ie=["value"],re={class:"template-item-content"},ce={class:"label"},ue={key:0,class:"template-tag"},de={key:1,class:"template-tag muted"},ve={key:2,class:"template-tag custom"},pe={class:"desc"},_e={key:0,class:"empty-option"},fe={class:"option-group"},me={class:"radio-item active"},he={class:"desc"},ye={class:"run-row"},ge=["disabled"],be={class:"loading-msg"},we={class:"right-panel"},ke={class:"task-list-wrap"},Ce={class:"clean-result-summary"},xe={class:"summary-card"},Fe={class:"summary-card"},Pe={class:"summary-card"},Ee={class:"result-list-wrap"},Te={key:0,class:"empty-tasks"},Ie={key:1,class:"task-list clean-result-list"},Be={class:"left"},Ne=["title"],$e={key:0,class:"files"},De={key:1,class:"files"},Re={class:"task-right"},Se=["onClick"],Ue=["onClick"],Ve=A({__name:"BrandConvertTab",setup(He){const v=u([]),g=u(""),f=u([]),m=u(!1),b=u([]),h=u({total:0,successCount:0,failedCount:0,items:[]}),d=u([]),c=u(""),C=T(()=>v.value.slice(0,8)),I=T(()=>d.value.find(a=>a.id===c.value)||null);async function P(a){const e=k();if(!e?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const l of a){const p=typeof l=="string"?l:l.absolutePath,V=typeof l=="string"?void 0:l.relativePath,y=await e.upload_file_to_java(p,V);if(!y?.success||!y.data)throw new Error(y?.error||y?.message||`上传失败:${p}`);t.push(y.data)}return t}async function B(a){const e=await J();d.value=e||[];const t=d.value.find(p=>p.id===c.value);if(t){c.value=t.id;return}const l=d.value.find(p=>p.isDefault);if(l){c.value=l.id;return}c.value=d.value[0]?.id||""}async function N(a,e){v.value=a,g.value="",f.value=await P(a),i.success(e)}async function $(){const a=k();if(!a?.select_brand_xlsx_files){i.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const e=await a.select_brand_xlsx_files();if(!e?.length)return;await N(e,`已选择 ${e.length} 个待转换文件`)}catch(e){i.error(e instanceof Error?e.message:"选择失败")}}async function D(){const a=k();if(!a?.select_brand_folder){i.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const e=await a.select_brand_folder();if(!e)return;g.value=e.split(/[/\\]/).filter(Boolean).pop()||"";const t=await Y(e);if(t.success&&t.items?.length){v.value=t.items.map(l=>l.relativePath),f.value=await P(t.items),i.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`);return}i.warning(t.error||"该文件夹下没有 xlsx 文件")}catch(e){i.error(e instanceof Error?e.message:"选择失败")}}async function R(){if(!f.value.length){i.warning("请先选择待转换 Excel 文件或文件夹");return}if(!c.value){i.warning("请先选择模板");return}try{m.value=!0;const a=await L({files:f.value.map(e=>({fileKey:e.fileKey,originalFilename:e.originalFilename,relativePath:e.relativePath})),templateId:c.value,archiveName:f.value.some(e=>!!e.relativePath)&&g.value?g.value:void 0});h.value=a,b.value=a.items||[],await x(),i.success("格式转换完成")}catch(a){i.error(a instanceof Error?a.message:"格式转换失败")}finally{m.value=!1}}async function x(){try{const a=await O();b.value=a.items||[],h.value={total:a.items?.length||0,successCount:a.items?.filter(e=>e.success).length||0,failedCount:a.items?.filter(e=>!e.success).length||0,items:a.items||[]}}catch{}}async function S(a){try{await G(a),await x(),i.success("已删除")}catch(e){i.error(e instanceof Error?e.message:"删除失败")}}async function U(a){const e=k();if(!a.downloadUrl){i.warning("当前结果没有下载地址");return}const t=a.outputFilename||`${new Date().toISOString().slice(0,10).replace(/-/g,"")}.txt`;if(e?.save_file_from_url_new){const l=await e.save_file_from_url_new(a.downloadUrl,t);l.success?i.success(`已保存:${l.path||t}`):l.error&&l.error!=="用户取消"&&i.error(l.error);return}}return H(()=>{B().catch(()=>{}),x().catch(()=>{})}),(a,e)=>(n(),o("div",Z,[z(K,{active:"convert"}),s("div",ee,[s("aside",se,[e[5]||(e[5]=s("div",{class:"section-title"},"选择文件",-1)),s("div",te,[e[1]||(e[1]=s("div",{class:"hint"},"选择需要转换的 Excel 文件或文件夹,将按所选模板生成 txt 文件。",-1)),s("div",{class:"btns"},[s("button",{type:"button",class:"opt-btn",onClick:$},"选择 Excel 文件"),s("button",{type:"button",class:"opt-btn",onClick:D},"选择文件夹")]),s("div",ae,[v.value.length?(n(),o(w,{key:0},[(n(!0),o(w,null,F(C.value,t=>(n(),o("span",{key:t},r(t),1))),128)),v.value.length>C.value.length?(n(),o("span",le," 还有 "+r(v.value.length-C.value.length)+" 个文件未展开显示 ",1)):_("",!0)],64)):(n(),o("span",ne,"暂未选择待转换文件"))])]),e[6]||(e[6]=s("div",{class:"section-title"},"模板选择",-1)),s("div",oe,[(n(!0),o(w,null,F(d.value,t=>(n(),o("label",{key:t.id,class:E(["radio-item",{active:c.value===t.id}])},[M(s("input",{"onUpdate:modelValue":e[0]||(e[0]=l=>c.value=l),type:"radio",value:t.id,name:"convert-template"},null,8,ie),[[j,c.value]]),s("div",re,[s("div",null,[s("span",ce,[q(r(t.templateName)+" ",1),t.isDefault?(n(),o("span",ue,"默认")):t.builtIn?(n(),o("span",de,"内置")):(n(),o("span",ve,"自定义"))]),s("div",pe,"输出文件:"+r(t.outputFilename),1)])])],2))),128)),d.value.length?_("",!0):(n(),o("div",_e,"暂无可用模板"))]),e[7]||(e[7]=s("div",{class:"section-title"},"转换说明",-1)),s("div",fe,[s("label",me,[e[3]||(e[3]=s("input",{type:"checkbox",checked:"",disabled:""},null,-1)),s("div",null,[e[2]||(e[2]=s("span",{class:"label"},"按当前模板输出",-1)),s("div",he,"当前模板:"+r(I.value?.templateName||"未加载模板"),1)])]),e[4]||(e[4]=s("label",{class:"radio-item active"},[s("input",{type:"checkbox",checked:"",disabled:""}),s("div",null,[s("span",{class:"label"},"SKU 使用雪花ID-序号"),s("div",{class:"desc"},"例如 1773816146796-1")])],-1))]),s("div",ye,[s("button",{type:"button",class:"btn-run",disabled:m.value,onClick:R},r(m.value?"转换中...":"开始转换"),9,ge),s("span",be,r(m.value?"正在生成并上传结果,请稍候…":"选择文件后即可执行格式转换,结果会显示在右侧供下载"),1)])]),s("section",we,[e[12]||(e[12]=s("div",{class:"panel-header"},"转换结果",-1)),s("div",ke,[s("div",Ce,[s("div",xe,[e[8]||(e[8]=s("span",{class:"summary-label"},"已处理文件",-1)),s("strong",null,r(h.value.total),1)]),s("div",Fe,[e[9]||(e[9]=s("span",{class:"summary-label"},"成功结果",-1)),s("strong",null,r(h.value.successCount),1)]),s("div",Pe,[e[10]||(e[10]=s("span",{class:"summary-label"},"失败文件",-1)),s("strong",null,r(h.value.failedCount),1)])]),s("div",Ee,[e[11]||(e[11]=s("div",{class:"result-list-header"},[s("span",null,"生成的结果压缩包列表")],-1)),b.value.length===0?(n(),o("div",Te," 暂无转换结果,完成格式转换后会在这里展示结果压缩包 ")):(n(),o("ul",Ie,[(n(!0),o(w,null,F(b.value,t=>(n(),o("li",{key:`${t.resultId||t.outputFilename||t.sourceFilename}`,class:"task-item"},[s("div",Be,[s("span",{class:"id",title:t.sourceFilename},r(t.sourceFilename||"-"),9,Ne),t.outputFilename?(n(),o("div",$e,"压缩包文件:"+r(t.outputFilename),1)):_("",!0),t.error?(n(),o("div",De,"错误信息:"+r(t.error),1)):_("",!0)]),s("div",Re,[s("span",{class:E(["status",t.success?"success":"failed"])},r(t.success?"已完成":"失败"),3),t.success&&t.downloadUrl?(n(),o("button",{key:0,type:"button",class:"download",onClick:l=>U(t)}," 下载压缩包 ",8,Se)):_("",!0),t.resultId?(n(),o("button",{key:1,type:"button",class:"btn-delete",onClick:l=>S(t.resultId)}," 删除 ",8,Ue)):_("",!0)])]))),128))]))])])])])]))}}),Ae=Q(Ve,[["__scopeId","data-v-7c280b44"]]);W(Ae).use(X).mount("#app"); diff --git a/app/assets/dedupe.js b/app/assets/dedupe.js index 9637696..fd8a71d 100644 --- a/app/assets/dedupe.js +++ b/app/assets/dedupe.js @@ -1 +1 @@ -import{d as z,o as W,a as n,c as o,b as q,B as J,e as s,F as C,r as F,t as i,f as b,n as I,w as P,v as B,g as u,h as L,i as x,E as r,j as O,k as G,l as Q,m as X,_ as Y,p as Z,q as ee}from"./pywebview-ClWy2SbE.js";import{e as se}from"./brand-DBpLCgAF.js";const te={class:"page-shell module-page"},le={class:"main-content"},ae={class:"left-panel"},ne={class:"upload-zone"},oe={class:"selected-files clean-placeholder"},re={key:0,class:"more-line"},ie={key:1},ue={class:"option-group column-option-group"},ce={class:"column-toolbar"},de={class:"column-count"},ve={key:0,class:"column-grid"},pe=["value"],fe={key:1,class:"empty-column-state"},_e={class:"option-group"},me={class:"run-row"},he=["disabled"],ge={class:"loading-msg"},ye={class:"right-panel"},be={class:"task-list-wrap"},we={class:"clean-result-summary"},ke={class:"summary-card"},Ce={class:"summary-card"},Ie={class:"summary-card"},xe={class:"result-list-wrap"},Ee={key:0,class:"empty-tasks"},De={key:1,class:"task-list clean-result-list"},Fe={class:"left"},Pe=["title"],Be={key:0,class:"files"},Se={key:1,class:"files"},Ue={class:"task-right"},Ne=["onClick"],$e=["onClick"],Ae=z({__name:"BrandDedupeTab",setup(Ke){const p=u([]),c=u([]),f=u([]),w=u(""),d=u([]),S=u(!1),_=u(!0),m=u(!0),h=u(!1),k=u([]),g=u({total:0,successCount:0,failedCount:0,items:[]}),E=L(()=>f.value.slice(0,8));function $(){c.value=[...p.value]}function A(){c.value=[]}async function U(l){const e=x();if(!e?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const a of l){const v=typeof a=="string"?a:a.absolutePath,j=typeof a=="string"?void 0:a.relativePath,y=await e.upload_file_to_java(v,j);if(!y?.success||!y.data)throw new Error(y?.error||y?.message||`上传失败:${v}`);t.push(y.data)}return t}async function N(l){const e=await X(l);if(!e.headers?.length){r.error("读取 Excel 表头失败"),p.value=[],c.value=[];return}const t=["id","ASIN","国家","价格","卖家名称"],a=[...t.filter(v=>e.headers?.includes(v)),...(e.headers||[]).filter(v=>!t.includes(v))];p.value=a,c.value=t.filter(v=>a.includes(v))}async function H(l,e){f.value=l,w.value="",d.value=await U(l),d.value.length>0&&await N(d.value[0].fileKey),r.success(e)}async function K(){const l=x();if(!l?.select_brand_xlsx_files){r.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const e=await l.select_brand_xlsx_files();if(!e?.length)return;await H(e,`已选择 ${e.length} 个待清洗文件`)}catch(e){r.error(e instanceof Error?e.message:"选择失败")}}async function R(){const l=x();if(!l?.select_brand_folder){r.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const e=await l.select_brand_folder();if(!e)return;w.value=e.split(/[/\\]/).filter(Boolean).pop()||"";const t=await se(e);if(t.success&&t.items?.length){f.value=t.items.map(a=>a.relativePath),d.value=await U(t.items),d.value.length>0&&await N(d.value[0].fileKey),r.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`);return}r.warning(t.error||"该文件夹下没有 xlsx 文件")}catch(e){r.error(e instanceof Error?e.message:"选择失败")}}async function V(){if(!d.value.length){r.warning("请先选择待清洗 Excel 文件或文件夹");return}if(!c.value.length){r.warning("请至少选择一列保留列");return}if(!S.value&&!_.value&&!m.value){r.warning("请至少选择一种 ID 保留规则");return}try{h.value=!0;const l=await O({files:d.value.map(e=>({fileKey:e.fileKey,originalFilename:e.originalFilename,relativePath:e.relativePath})),selectedColumns:c.value,keepIntegerIds:S.value,keepUnderscoreIds:_.value,keepIntegerMainIdsWhenNoSubIds:m.value,archiveName:d.value.some(e=>!!e.relativePath)&&w.value?w.value:void 0});g.value=l,k.value=l.items||[],await D(),r.success("数据去重完成")}catch(l){r.error(l instanceof Error?l.message:"去重失败")}finally{h.value=!1}}async function D(){try{const l=await G();k.value=l.items||[],g.value={total:l.items?.length||0,successCount:l.items?.filter(e=>e.success).length||0,failedCount:l.items?.filter(e=>!e.success).length||0,items:l.items||[]}}catch{}}async function M(l){try{await Q(l),await D(),r.success("已删除")}catch(e){r.error(e instanceof Error?e.message:"删除失败")}}async function T(l){const e=x();if(!l.downloadUrl){r.warning("当前结果没有下载地址");return}const t=l.outputFilename||`${new Date().toISOString().slice(0,10).replace(/-/g,"")}_cleaned.xlsx`;if(e?.save_file_from_url_new){const a=await e.save_file_from_url_new(l.downloadUrl,t);a.success?r.success(`已保存:${a.path||t}`):a.error&&a.error!=="用户取消"&&r.error(a.error);return}}return W(()=>{D().catch(()=>{})}),(l,e)=>(n(),o("div",te,[q(J,{active:"dedupe"}),s("div",le,[s("aside",ae,[e[7]||(e[7]=s("div",{class:"section-title"},"选择文件",-1)),s("div",ne,[e[3]||(e[3]=s("div",{class:"hint"},"选择需要清洗的 Excel 文件或文件夹,后续将按所选规则输出处理结果。",-1)),s("div",{class:"btns"},[s("button",{type:"button",class:"opt-btn",onClick:K},"选择 Excel 文件"),s("button",{type:"button",class:"opt-btn",onClick:R},"选择文件夹")]),s("div",oe,[f.value.length?(n(),o(C,{key:0},[(n(!0),o(C,null,F(E.value,t=>(n(),o("span",{key:t},i(t),1))),128)),f.value.length>E.value.length?(n(),o("span",re," 还有 "+i(f.value.length-E.value.length)+" 个文件未展开显示 ",1)):b("",!0)],64)):(n(),o("span",ie,"暂未选择待清洗文件"))])]),e[8]||(e[8]=s("div",{class:"section-title"},"保留列设置",-1)),s("div",ue,[s("div",ce,[s("span",de,"已选择 "+i(c.value.length)+" / "+i(p.value.length)+" 列",1),s("div",{class:"column-actions"},[s("button",{type:"button",class:"opt-btn small",onClick:$},"全选"),s("button",{type:"button",class:"opt-btn small",onClick:A},"清空")])]),p.value.length?(n(),o("div",ve,[(n(!0),o(C,null,F(p.value,t=>(n(),o("label",{key:t,class:I(["column-item",{active:c.value.includes(t)}])},[P(s("input",{"onUpdate:modelValue":e[0]||(e[0]=a=>c.value=a),type:"checkbox",value:t},null,8,pe),[[B,c.value]]),s("span",null,i(t),1)],2))),128))])):(n(),o("div",fe,"请选择 Excel 文件后读取表头并填充可保留列")),e[4]||(e[4]=s("div",{class:"desc"},"读取首个 Excel 文件的第一行作为表头,用于动态选择需要保留的列。",-1))]),e[9]||(e[9]=s("div",{class:"section-title"},"ID 保留规则",-1)),s("div",_e,[s("label",{class:I(["radio-item",{active:_.value}])},[P(s("input",{"onUpdate:modelValue":e[1]||(e[1]=t=>_.value=t),type:"checkbox"},null,512),[[B,_.value]]),e[5]||(e[5]=s("div",null,[s("span",{class:"label"},"保留子链接"),s("div",{class:"desc"},"例如 1_1、2_3 这种带下划线的子项 ID")],-1))],2),s("label",{class:I(["radio-item",{active:m.value}])},[P(s("input",{"onUpdate:modelValue":e[2]||(e[2]=t=>m.value=t),type:"checkbox"},null,512),[[B,m.value]]),e[6]||(e[6]=s("div",null,[s("span",{class:"label"},"仅剩主链接无子链接"),s("div",{class:"desc"},"当某个主链接只有 13 这类主 ID、没有 13_1 这类对应子 ID 时,自动保留该主 ID")],-1))],2)]),s("div",me,[s("button",{type:"button",class:"btn-run",disabled:h.value,onClick:V},i(h.value?"清洗中...":"开始清洗"),9,he),s("span",ge,i(h.value?"正在处理文件并上传结果,请稍候…":"选择文件、列和 ID 规则后即可执行清洗,结果会显示在右侧供下载"),1)])]),s("section",ye,[e[14]||(e[14]=s("div",{class:"panel-header"},"去重结果",-1)),s("div",be,[s("div",we,[s("div",ke,[e[10]||(e[10]=s("span",{class:"summary-label"},"已处理文件",-1)),s("strong",null,i(g.value.total),1)]),s("div",Ce,[e[11]||(e[11]=s("span",{class:"summary-label"},"成功结果",-1)),s("strong",null,i(g.value.successCount),1)]),s("div",Ie,[e[12]||(e[12]=s("span",{class:"summary-label"},"失败文件",-1)),s("strong",null,i(g.value.failedCount),1)])]),s("div",xe,[e[13]||(e[13]=s("div",{class:"result-list-header"},[s("span",null,"处理后的 Excel 列表")],-1)),k.value.length===0?(n(),o("div",Ee," 暂无去重结果,完成数据去重后会在这里展示输出文件 ")):(n(),o("ul",De,[(n(!0),o(C,null,F(k.value,t=>(n(),o("li",{key:`${t.resultId||t.outputFilename||t.sourceFilename}`,class:"task-item"},[s("div",Fe,[s("span",{class:"id",title:t.sourceFilename},i(t.sourceFilename||"-"),9,Pe),t.outputFilename?(n(),o("div",Be,"输出文件:"+i(t.outputFilename),1)):b("",!0),t.error?(n(),o("div",Se,"错误信息:"+i(t.error),1)):b("",!0)]),s("div",Ue,[s("span",{class:I(["status",t.success?"success":"failed"])},i(t.success?"已完成":"失败"),3),t.success&&t.downloadUrl?(n(),o("button",{key:0,type:"button",class:"download",onClick:a=>T(t)}," 下载文件 ",8,Ne)):b("",!0),t.resultId?(n(),o("button",{key:1,type:"button",class:"btn-delete",onClick:a=>M(t.resultId)}," 删除 ",8,$e)):b("",!0)])]))),128))]))])])])])]))}}),He=Y(Ae,[["__scopeId","data-v-bdaefe1c"]]);Z(He).use(ee).mount("#app"); +import{d as z,o as W,a as n,c as o,b as q,B as J,e as s,F as C,r as F,t as i,f as b,n as I,w as P,v as B,g as u,h as L,i as x,E as r,j as O,k as G,l as Q,m as X,_ as Y,p as Z,q as ee}from"./pywebview-DuyK2jB1.js";import{e as se}from"./brand-CKgrwtni.js";const te={class:"page-shell module-page"},le={class:"main-content"},ae={class:"left-panel"},ne={class:"upload-zone"},oe={class:"selected-files clean-placeholder"},re={key:0,class:"more-line"},ie={key:1},ue={class:"option-group column-option-group"},ce={class:"column-toolbar"},de={class:"column-count"},ve={key:0,class:"column-grid"},pe=["value"],fe={key:1,class:"empty-column-state"},_e={class:"option-group"},me={class:"run-row"},he=["disabled"],ge={class:"loading-msg"},ye={class:"right-panel"},be={class:"task-list-wrap"},we={class:"clean-result-summary"},ke={class:"summary-card"},Ce={class:"summary-card"},Ie={class:"summary-card"},xe={class:"result-list-wrap"},Ee={key:0,class:"empty-tasks"},De={key:1,class:"task-list clean-result-list"},Fe={class:"left"},Pe=["title"],Be={key:0,class:"files"},Se={key:1,class:"files"},Ue={class:"task-right"},Ne=["onClick"],$e=["onClick"],Ae=z({__name:"BrandDedupeTab",setup(Ke){const p=u([]),c=u([]),f=u([]),w=u(""),d=u([]),S=u(!1),_=u(!0),m=u(!0),h=u(!1),k=u([]),g=u({total:0,successCount:0,failedCount:0,items:[]}),E=L(()=>f.value.slice(0,8));function $(){c.value=[...p.value]}function A(){c.value=[]}async function U(l){const e=x();if(!e?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const a of l){const v=typeof a=="string"?a:a.absolutePath,j=typeof a=="string"?void 0:a.relativePath,y=await e.upload_file_to_java(v,j);if(!y?.success||!y.data)throw new Error(y?.error||y?.message||`上传失败:${v}`);t.push(y.data)}return t}async function N(l){const e=await X(l);if(!e.headers?.length){r.error("读取 Excel 表头失败"),p.value=[],c.value=[];return}const t=["id","ASIN","国家","价格","卖家名称"],a=[...t.filter(v=>e.headers?.includes(v)),...(e.headers||[]).filter(v=>!t.includes(v))];p.value=a,c.value=t.filter(v=>a.includes(v))}async function H(l,e){f.value=l,w.value="",d.value=await U(l),d.value.length>0&&await N(d.value[0].fileKey),r.success(e)}async function K(){const l=x();if(!l?.select_brand_xlsx_files){r.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const e=await l.select_brand_xlsx_files();if(!e?.length)return;await H(e,`已选择 ${e.length} 个待清洗文件`)}catch(e){r.error(e instanceof Error?e.message:"选择失败")}}async function R(){const l=x();if(!l?.select_brand_folder){r.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const e=await l.select_brand_folder();if(!e)return;w.value=e.split(/[/\\]/).filter(Boolean).pop()||"";const t=await se(e);if(t.success&&t.items?.length){f.value=t.items.map(a=>a.relativePath),d.value=await U(t.items),d.value.length>0&&await N(d.value[0].fileKey),r.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`);return}r.warning(t.error||"该文件夹下没有 xlsx 文件")}catch(e){r.error(e instanceof Error?e.message:"选择失败")}}async function V(){if(!d.value.length){r.warning("请先选择待清洗 Excel 文件或文件夹");return}if(!c.value.length){r.warning("请至少选择一列保留列");return}if(!S.value&&!_.value&&!m.value){r.warning("请至少选择一种 ID 保留规则");return}try{h.value=!0;const l=await O({files:d.value.map(e=>({fileKey:e.fileKey,originalFilename:e.originalFilename,relativePath:e.relativePath})),selectedColumns:c.value,keepIntegerIds:S.value,keepUnderscoreIds:_.value,keepIntegerMainIdsWhenNoSubIds:m.value,archiveName:d.value.some(e=>!!e.relativePath)&&w.value?w.value:void 0});g.value=l,k.value=l.items||[],await D(),r.success("数据去重完成")}catch(l){r.error(l instanceof Error?l.message:"去重失败")}finally{h.value=!1}}async function D(){try{const l=await G();k.value=l.items||[],g.value={total:l.items?.length||0,successCount:l.items?.filter(e=>e.success).length||0,failedCount:l.items?.filter(e=>!e.success).length||0,items:l.items||[]}}catch{}}async function M(l){try{await Q(l),await D(),r.success("已删除")}catch(e){r.error(e instanceof Error?e.message:"删除失败")}}async function T(l){const e=x();if(!l.downloadUrl){r.warning("当前结果没有下载地址");return}const t=l.outputFilename||`${new Date().toISOString().slice(0,10).replace(/-/g,"")}_cleaned.xlsx`;if(e?.save_file_from_url_new){const a=await e.save_file_from_url_new(l.downloadUrl,t);a.success?r.success(`已保存:${a.path||t}`):a.error&&a.error!=="用户取消"&&r.error(a.error);return}}return W(()=>{D().catch(()=>{})}),(l,e)=>(n(),o("div",te,[q(J,{active:"dedupe"}),s("div",le,[s("aside",ae,[e[7]||(e[7]=s("div",{class:"section-title"},"选择文件",-1)),s("div",ne,[e[3]||(e[3]=s("div",{class:"hint"},"选择需要清洗的 Excel 文件或文件夹,后续将按所选规则输出处理结果。",-1)),s("div",{class:"btns"},[s("button",{type:"button",class:"opt-btn",onClick:K},"选择 Excel 文件"),s("button",{type:"button",class:"opt-btn",onClick:R},"选择文件夹")]),s("div",oe,[f.value.length?(n(),o(C,{key:0},[(n(!0),o(C,null,F(E.value,t=>(n(),o("span",{key:t},i(t),1))),128)),f.value.length>E.value.length?(n(),o("span",re," 还有 "+i(f.value.length-E.value.length)+" 个文件未展开显示 ",1)):b("",!0)],64)):(n(),o("span",ie,"暂未选择待清洗文件"))])]),e[8]||(e[8]=s("div",{class:"section-title"},"保留列设置",-1)),s("div",ue,[s("div",ce,[s("span",de,"已选择 "+i(c.value.length)+" / "+i(p.value.length)+" 列",1),s("div",{class:"column-actions"},[s("button",{type:"button",class:"opt-btn small",onClick:$},"全选"),s("button",{type:"button",class:"opt-btn small",onClick:A},"清空")])]),p.value.length?(n(),o("div",ve,[(n(!0),o(C,null,F(p.value,t=>(n(),o("label",{key:t,class:I(["column-item",{active:c.value.includes(t)}])},[P(s("input",{"onUpdate:modelValue":e[0]||(e[0]=a=>c.value=a),type:"checkbox",value:t},null,8,pe),[[B,c.value]]),s("span",null,i(t),1)],2))),128))])):(n(),o("div",fe,"请选择 Excel 文件后读取表头并填充可保留列")),e[4]||(e[4]=s("div",{class:"desc"},"读取首个 Excel 文件的第一行作为表头,用于动态选择需要保留的列。",-1))]),e[9]||(e[9]=s("div",{class:"section-title"},"ID 保留规则",-1)),s("div",_e,[s("label",{class:I(["radio-item",{active:_.value}])},[P(s("input",{"onUpdate:modelValue":e[1]||(e[1]=t=>_.value=t),type:"checkbox"},null,512),[[B,_.value]]),e[5]||(e[5]=s("div",null,[s("span",{class:"label"},"保留子链接"),s("div",{class:"desc"},"例如 1_1、2_3 这种带下划线的子项 ID")],-1))],2),s("label",{class:I(["radio-item",{active:m.value}])},[P(s("input",{"onUpdate:modelValue":e[2]||(e[2]=t=>m.value=t),type:"checkbox"},null,512),[[B,m.value]]),e[6]||(e[6]=s("div",null,[s("span",{class:"label"},"仅剩主链接无子链接"),s("div",{class:"desc"},"当某个主链接只有 13 这类主 ID、没有 13_1 这类对应子 ID 时,自动保留该主 ID")],-1))],2)]),s("div",me,[s("button",{type:"button",class:"btn-run",disabled:h.value,onClick:V},i(h.value?"清洗中...":"开始清洗"),9,he),s("span",ge,i(h.value?"正在处理文件并上传结果,请稍候…":"选择文件、列和 ID 规则后即可执行清洗,结果会显示在右侧供下载"),1)])]),s("section",ye,[e[14]||(e[14]=s("div",{class:"panel-header"},"去重结果",-1)),s("div",be,[s("div",we,[s("div",ke,[e[10]||(e[10]=s("span",{class:"summary-label"},"已处理文件",-1)),s("strong",null,i(g.value.total),1)]),s("div",Ce,[e[11]||(e[11]=s("span",{class:"summary-label"},"成功结果",-1)),s("strong",null,i(g.value.successCount),1)]),s("div",Ie,[e[12]||(e[12]=s("span",{class:"summary-label"},"失败文件",-1)),s("strong",null,i(g.value.failedCount),1)])]),s("div",xe,[e[13]||(e[13]=s("div",{class:"result-list-header"},[s("span",null,"处理后的 Excel 列表")],-1)),k.value.length===0?(n(),o("div",Ee," 暂无去重结果,完成数据去重后会在这里展示输出文件 ")):(n(),o("ul",De,[(n(!0),o(C,null,F(k.value,t=>(n(),o("li",{key:`${t.resultId||t.outputFilename||t.sourceFilename}`,class:"task-item"},[s("div",Fe,[s("span",{class:"id",title:t.sourceFilename},i(t.sourceFilename||"-"),9,Pe),t.outputFilename?(n(),o("div",Be,"输出文件:"+i(t.outputFilename),1)):b("",!0),t.error?(n(),o("div",Se,"错误信息:"+i(t.error),1)):b("",!0)]),s("div",Ue,[s("span",{class:I(["status",t.success?"success":"failed"])},i(t.success?"已完成":"失败"),3),t.success&&t.downloadUrl?(n(),o("button",{key:0,type:"button",class:"download",onClick:a=>T(t)}," 下载文件 ",8,Ne)):b("",!0),t.resultId?(n(),o("button",{key:1,type:"button",class:"btn-delete",onClick:a=>M(t.resultId)}," 删除 ",8,$e)):b("",!0)])]))),128))]))])])])])]))}}),He=Y(Ae,[["__scopeId","data-v-bdaefe1c"]]);Z(He).use(ee).mount("#app"); diff --git a/app/assets/delete-brand.js b/app/assets/delete-brand.js index 169f21a..ffe6881 100644 --- a/app/assets/delete-brand.js +++ b/app/assets/delete-brand.js @@ -1 +1 @@ -import{d as We,o as Ye,I as Ze,a as d,c as f,b as es,B as ss,e as l,F as H,r as de,t as v,f as h,J as ts,K as Ae,n as De,g as y,h as J,L as ns,i as G,E as p,M as as,N as ls,O as rs,P as os,_ as us,p as is,q as cs}from"./pywebview-ClWy2SbE.js";import{e as ds}from"./brand-DBpLCgAF.js";const fs={class:"page-shell module-page"},vs={class:"main-content"},hs={class:"left-panel"},ps={class:"upload-zone"},_s={class:"selected-files clean-placeholder split-selected-files"},ys={key:0,class:"more-line"},gs={key:1},ms={class:"run-row"},ks=["disabled"],ws=["disabled"],Is={class:"loading-msg"},bs={key:0,class:"queue-debug-card"},Ss={key:0,class:"queue-debug-line"},Ts={key:1,class:"queue-debug-payload"},Cs={class:"right-panel"},Es={class:"task-list-wrap"},Fs={class:"clean-result-summary"},Ps={class:"summary-card"},Rs={class:"summary-card"},xs={class:"summary-card"},Ns={class:"result-list-wrap"},$s={key:0,class:"empty-tasks"},As={key:0,class:"result-subsection"},Ds={class:"task-list clean-result-list"},Ms={class:"left split-result-main"},Bs=["title"],qs={class:"files"},Ls={class:"files"},Us={key:0,class:"files"},Os={key:1,class:"files"},Ks={key:2,class:"files"},Hs={key:3,class:"time"},Js={key:4,class:"delete-brand-progress-block"},Gs={class:"delete-brand-progress-header"},js={class:"delete-brand-progress-bar"},zs={class:"files delete-brand-progress"},Vs={key:5,class:"files split-entry-list delete-brand-preview"},Qs={key:6,class:"files"},Xs={class:"task-right split-result-actions"},Ws=["onClick"],Ys=["onClick"],Zs={key:1,class:"result-subsection"},et={class:"task-list clean-result-list"},st={class:"left split-result-main"},tt=["title"],nt={class:"files"},at={class:"files"},lt={key:0,class:"files"},rt={key:1,class:"files"},ot={key:2,class:"time"},ut={key:3,class:"delete-brand-progress-block"},it={class:"delete-brand-progress-header"},ct={class:"delete-brand-progress-bar"},dt={class:"files delete-brand-progress"},ft={key:4,class:"files split-entry-list delete-brand-preview"},vt={key:5,class:"files"},ht={class:"task-right split-result-actions"},pt=["onClick"],_t=["onClick"],yt=We({__name:"BrandDeleteBrandTab",setup(mt){function $(){return`delete-brand:current-tasks:${typeof window<"u"&&window.localStorage.getItem("uid")||"0"}`}const A=y([]),j=y([]),B=y(!1),fe=y([]),i=y([]),q=y([]),z=y({total:0,successCount:0,failedCount:0,items:[]}),b=y(""),S=y(""),g=y({}),T=y(null),ee=y(!1),V=y(!1),P=y(!1),D=y(""),Q=y(!1),L=y(null),se=J(()=>A.value.slice(0,10)),E=J(()=>{const e=[];return i.value.forEach(s=>{e.push(...s.items)}),e});function Me(){try{const e=typeof window<"u"?window.localStorage.getItem($()):null;if(e){if(i.value=JSON.parse(e),i.value.length>0){const s=i.value[i.value.length-1];b.value=s.queuePushResult||"",S.value=s.queuePayloadText||""}}else i.value=[]}catch{i.value=[]}}function te(e,s,t,n){if(!s?.length)return;const a=i.value.findIndex(c=>c.taskId===e),o=a>=0?i.value[a]:null,r=s.map(c=>{const _=o?.items.find(Xe=>M(Xe,c)),k=Array.isArray(c.countries)?c.countries:void 0,m=Array.isArray(_?.countries)?_?.countries:void 0,w=k&&k.length>0?k:m&&m.length>0?m:k??m,I=Array.isArray(c.previewRows)?c.previewRows:void 0,C=Array.isArray(_?.previewRows)?_?.previewRows:void 0,ce=I&&I.length>0?I:C&&C.length>0?C:I??C;return{...c,countries:w,previewRows:ce,_pushed:_?._pushed??c._pushed??!1,_completed:_?._completed??c._completed??!1}}),u={taskId:e,items:r,createdAt:o?.createdAt||Date.now(),queuePushResult:t??o?.queuePushResult,queuePayloadText:n??o?.queuePayloadText};a>=0?i.value[a]=u:i.value.push(u),typeof window<"u"&&window.localStorage.setItem($(),JSON.stringify(i.value))}function ve(e){const s=i.value.length;i.value=i.value.filter(t=>t.taskId!==e),i.value.length!==s&&typeof window<"u"&&window.localStorage.setItem($(),JSON.stringify(i.value))}function he(e){return e==="SUCCESS"||e==="FAILED"||e==="COMPLETED"}function Be(e){if(!e?.length)return!1;const s=new Set;if(e.forEach(n=>{n.taskId&&he(n.taskStatus)&&s.add(n.taskId)}),!s.size)return!1;const t=i.value.length;return i.value=i.value.filter(n=>!s.has(n.taskId)),i.value.length!==t?(ne(),!0):!1}const X=J(()=>q.value.filter(e=>!E.value.some(s=>M(s,e)))),qe=J(()=>E.value.length>0||X.value.length>0),Le=J(()=>E.value.filter(e=>Re(e)&&F(e)==="未入队"));function ne(){typeof window<"u"&&(i.value.length===0?window.localStorage.removeItem($()):window.localStorage.setItem($(),JSON.stringify(i.value)))}function R(e){return e?(i.value.find(t=>t.taskId===e)?.items.length||0)>1:!1}function ae(e){return e&&i.value.find(s=>s.taskId===e)||null}function M(e,s){return!e||!s?!1:e.resultId!=null&&s.resultId!=null?e.resultId===s.resultId:e.fileKey&&s.fileKey?e.fileKey===s.fileKey:e.sourceFilename===s.sourceFilename}function x(e,s){const t=ae(e);return t&&t.items.find(n=>M(n,s))||null}function pe(e){if(!e)return!1;const s=ae(e);if(!s)return!1;let t=!1;const n=g.value[e]?.line_progress?.info,a=R(e);if(n?.file_name&&n.current_line!=null&&n.total_lines!=null&&n.total_lines>0&&n.current_line>=n.total_lines){const o=s.items.find(r=>r.sourceFilename===n.file_name);o&&!o._completed&&(o._completed=!0,t=!0)}if(!a){const o=g.value[e]?.task?.status;if((o==="SUCCESS"||o==="FAILED")&&s.items.length===1){const r=s.items[0];r&&!r._completed&&(r._completed=!0,t=!0)}}return t&&ne(),t}function _e(e){return e&&g.value[e]?.task?.status||""}function W(e){if(e.error)return e.error;if(e.matchStatus&&e.matchStatus!=="MATCHED")return e.matchMessage||"";const s=e.taskId;return s&&g.value[s]?.task?.errorMessage||""}function ye(e){const s=e.taskId;if(!s)return!1;const t=g.value[s]?.line_progress,n=_e(s),a=R(s),o=F(e);if(a){const r=x(s,e),u=K(e)===D.value,c=!!(t?.has_progress&&t?.info?.file_name===e.sourceFilename);return u||c||r?._pushed||(n==="RUNNING"||n==="PENDING"||!n)&&r?._completed?!0:{success:!1,retryable:!0}}return o==="本文件解析完毕"||o==="已被全局判定为终态"?!1:t?.has_progress||t?.info?!0:o==="已入队"||o==="处理中"}function ge(e){return e.map(s=>s.matchStatus==="MATCHED"?{...s,_pushed:!1,_completed:!1}:{...s,matched:!1,openStoreUrl:void 0,error:s.error||void 0,_pushed:!1,_completed:!1})}function me(e){return e.matchStatus==="MATCHED"?`已匹配 ${e.shopId||""}`.trim():e.matchMessage?e.matchMessage:e.matchStatus==="CONFLICT"?"存在多个同名店铺,请人工确认":e.matchStatus==="PENDING"||e.matchStatus==="INDEX_STALE"?"店铺索引暂未就绪":e.matchStatus?e.matchStatus:e.matched?`已匹配 ${e.shopId||""}`.trim():"待匹配"}function Ue(){fe.value=[...E.value,...X.value]}function N(){Ue(),Oe(fe.value)}function Oe(e){z.value={total:e.length,successCount:e.filter(s=>s.success).length,failedCount:e.filter(s=>!s.success).length,items:e}}function le(e){const s=e.taskId;return s&&(g.value[s]?.fileProgress||[]).find(a=>a.fileKey&&e.fileKey?a.fileKey===e.fileKey:a.sourceFilename&&e.sourceFilename?a.sourceFilename===e.sourceFilename:!1)||null}function ke(e){const s=e.taskId;if(!s)return"";const t=g.value[s],n=t?.line_progress?.info,a=le(e);if(R(s)){const u=x(s,e),c=t?.task?.status,_=!!(n?.file_name&&n.file_name===e.sourceFilename);let k=u?._completed?"COMPLETED":(u?._pushed,"PENDING"),m=0,w=Math.max(0,e.totalRows??a?.totalRows??0);c==="SUCCESS"||c==="FAILED"&&u?._completed?(k="COMPLETED",m=w):_?(k="RUNNING",m=Math.max(0,Math.min(w||Number.MAX_SAFE_INTEGER,n?.current_line??0)),!w&&n?.total_lines&&n.total_lines>0&&(w=n.total_lines)):u?._completed?(k="COMPLETED",m=w):u?._pushed&&(k="PENDING",m=0);const I=[];if(I.push(`文件状态:${k}`),I.push(`文件进度:${m}/${w}`),n?.finished_files!=null){const C=n.file_total&&n.file_total>0?`/${n.file_total}`:"";I.push(`已完成文件:${n.finished_files}${C}`)}return n?.phase&&I.push(`阶段:${n.phase}`),I.join("|")}if(a){const u=[],c=a.processedRows??0,_=a.totalRows??0;if(a.status&&u.push(`文件状态:${a.status}`),u.push(`文件进度:${c}/${_}`),n?.finished_files!=null){const k=n.file_total&&n.file_total>0?`/${n.file_total}`:"";u.push(`已完成文件:${n.finished_files}${k}`)}return n?.phase&&u.push(`阶段:${n.phase}`),u.join("|")}if(!t?.line_progress?.has_progress||!n){const u=t?.task?.status;return u?`任务状态:${u}`:""}const r=[];if(n.phase&&r.push(`阶段:${n.phase}`),n.file_index&&n.file_total&&r.push(`文件:${n.file_index}/${n.file_total}`),n.file_name&&r.push(`当前文件:${n.file_name}`),n.current_line!=null&&n.total_lines!=null&&r.push(`${R(s)?"当前文件进度":"进度"}:${n.current_line}/${n.total_lines}`),n.current_country&&r.push(`国家:${n.current_country}`),n.current_asin&&r.push(`ASIN:${n.current_asin}`),n.finished_files!=null){const u=n.file_total&&n.file_total>0?`/${n.file_total}`:"";r.push(`已完成文件:${n.finished_files}${u}`)}return r.join("|")}function Y(e){const s=e.taskId;if(!s)return 0;const t=g.value[s]?.line_progress?.info;if(R(s)){const r=x(s,e);if(g.value[s]?.task?.status==="SUCCESS"||r?._completed)return 100;const c=Math.max(0,e.totalRows??le(e)?.totalRows??0);if(!!(t?.file_name&&t.file_name===e.sourceFilename)){if(t?.current_line!=null&&t?.total_lines&&t.total_lines>0)return Math.max(0,Math.min(100,Math.round(t.current_line/t.total_lines*100)));if(t?.current_line!=null&&c>0)return Math.max(0,Math.min(100,Math.round(t.current_line/c*100)))}return 0}const a=le(e);return a?Math.max(0,Math.min(100,a.percent??0)):t?t.current_line!=null&&t.total_lines&&t.total_lines>0?Math.max(0,Math.min(100,Math.round(t.current_line/t.total_lines*100))):t.finished_files!=null&&t.file_total&&t.file_total>0?Math.max(0,Math.min(100,Math.round(t.finished_files/t.file_total*100))):g.value[s]?.task?.status==="SUCCESS"?100:0:0}function we(e){const s=g.value[e]?.task?.status;return he(s)}function U(){const e=new Set;return i.value.forEach(s=>{s.items.some(t=>t._pushed)&&e.add(s.taskId)}),Array.from(e)}function F(e){const s=e.taskId;if(!s||!ae(s))return"";const n=x(s,e),a=_e(s),o=g.value[s]?.line_progress,r=R(s);if(!r&&(a==="SUCCESS"||a==="FAILED"||e.taskStatus==="SUCCESS"||e.taskStatus==="FAILED"))return"已被全局判定为终态";if(n?._completed)return"本文件解析完毕";if(a==="RUNNING"&&o?.has_progress&&o?.info?.file_name===e.sourceFilename)return"处理中";if(r&&!n?._pushed&&!n?._completed)return"未入队";if(r&&a==="SUCCESS")return"本文件解析完毕";if(!r){const u=g.value[s];if(u?.items&&u.items.find(_=>M(_,e))&&a==="SUCCESS")return"本文件解析完毕"}return n?._pushed?"已入队":"未入队"}function Ie(e){if(e.downloadUrl)return!0;const s=e.taskId;return s?g.value[s]?.task?.status==="SUCCESS":{success:!1,retryable:!1}}function Ke(e){const s=e?.task?.id;if(!s)return!1;const t=i.value.find(r=>r.taskId===s);if(!t)return{success:!1,retryable:!1};const n=e?.items||[];let a=!1;const o=t.items.map(r=>{const u=n.find(ce=>M(ce,r));if(!u)return r;const c=Array.isArray(u.countries)?u.countries:void 0,_=Array.isArray(r.countries)?r.countries:void 0,k=c&&c.length>0?c:_&&_.length>0?_:c??_,m=Array.isArray(u.previewRows)?u.previewRows:void 0,w=Array.isArray(r.previewRows)?r.previewRows:void 0,I=m&&m.length>0?m:w&&w.length>0?w:m??w,C={...r,...u,countries:k,previewRows:I,_pushed:r._pushed,_completed:r._completed};return JSON.stringify(C)!==JSON.stringify(r)&&(a=!0),C});for(const r of n)o.some(u=>M(u,r))||(o.push({...r,_pushed:!1,_completed:!1}),a=!0);return JSON.stringify(o)!==JSON.stringify(t.items)&&(t.items=o,a=!0),a&&ne(),a}async function re(e){const s=e||U();if(s.length)try{const t=await ns(s);let n=!1;for(const a of t.items||[]){const o=a?.task?.id;typeof o=="number"&&o>0&&(g.value[o]=a,Ke(a)&&(n=!0),pe(o)&&(n=!0))}n&&N()}catch(t){const n=(t instanceof Error?t.message:String(t||"")).toLowerCase();["无法连接到后端服务","bad gateway","gateway timeout","network error","timeout","502","503","504"].some(o=>n.includes(o.toLowerCase()))&&P.value&&(b.value="当前任务运行中,正在等待后端服务恢复...")}}function He(){return document.visibilityState==="visible"?6e3:2e4}function oe(e=!1){if(T.value){if(!e)return;clearTimeout(T.value),T.value=null}const s=async()=>{if(T.value=null,ee.value){oe();return}const t=U();if(!t.length)return;ee.value=!0;let n=!1,a=!1;try{await re(t),await $e();for(const o of t)we(o)&&(ve(o),n=!0,a=!0)}finally{ee.value=!1}a?await O():n&&N(),U().length>0&&oe()};e?s():T.value=window.setTimeout(s,He())}function ue(e=!1){T.value&&!e||oe(e)}function Z(e){const s=e.taskId?g.value[e.taskId]?.task?.status:null;return s==="SUCCESS"?{className:"success",text:"已完成"}:s==="FAILED"?{className:"failed",text:"失败"}:s==="RUNNING"?{className:"running",text:"执行中"}:e.taskStatus==="SUCCESS"?{className:"success",text:"已完成"}:e.taskStatus==="FAILED"?{className:"failed",text:"失败"}:e.taskStatus==="RUNNING"?{className:"running",text:"执行中"}:e.success?{className:"success",text:"已完成"}:{className:"failed",text:"失败"}}function be(){T.value&&(window.clearTimeout(T.value),T.value=null),ie()}async function Se(e){const s=G();if(!s?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const n of e){const a=typeof n=="string"?n:n.absolutePath,o=typeof n=="string"?void 0:n.relativePath,r=await s.upload_file_to_java(a,o);if(!r?.success||!r.data)throw new Error(r?.error||r?.message||`上传失败:${a}`);t.push(r.data)}return t}async function Je(){const e=G();if(!e?.select_brand_xlsx_files){p.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const s=await e.select_brand_xlsx_files();if(!s?.length)return;A.value=s,j.value=await Se(s),p.success(`已选择 ${s.length} 个删除品牌文件`)}catch(s){p.error(s instanceof Error?s.message:"选择失败")}}async function Ge(){const e=G();if(!e?.select_brand_folder){p.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const s=await e.select_brand_folder();if(!s)return;const t=await ds(s);if(!t.success||!t.items?.length){p.warning(t.error||"该文件夹下没有 xlsx 文件");return}A.value=t.items.map(n=>n.relativePath||n.absolutePath),j.value=await Se(t.items),p.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`)}catch(s){p.error(s instanceof Error?s.message:"选择失败")}}async function je(){if(!j.value.length){p.warning("请先选择待处理文件");return}try{B.value=!0;const e=await as({files:j.value.map(a=>({fileKey:a.fileKey,originalFilename:a.originalFilename,relativePath:a.relativePath}))}),s=ge(e.items||[]),t=s.some(a=>a.matchStatus==="MATCHED"),n=s.some(a=>a.matchStatus&&a.matchStatus!=="MATCHED");b.value=t?"解析完成,可在左侧点击“推送到 Python 队列”开始串行处理":"解析完成,当前没有可推送的已匹配文件",S.value="",s.length&&s[0].taskId&&te(s[0].taskId,s,b.value,S.value),N(),n?p.warning("部分文件尚未命中可用店铺索引,已保留状态信息,请等待后台刷新后重试。"):t?p.success("删除品牌解析完成,请在左侧推送到 Python 队列"):p.warning("当前没有可推送的已匹配文件,请先等待店铺索引刷新。"),await O()}catch(e){p.error(e instanceof Error?e.message:"执行失败")}finally{B.value=!1}}async function Te(e){const s=e.taskId;if(!s){p.warning("未找到任务ID");return}const t=G(),n=ls(s),a=e.outputFilename||e.sourceFilename||`delete-brand_${s}.xlsx`;if(t?.save_file_from_url_new){const o=await t.save_file_from_url_new(n,a);o.success?p.success(`已保存:${o.path||a}`):o.error&&o.error!=="用户取消"&&p.error(o.error);return}}async function O(){try{const e=await rs();q.value=ge(e.items||[]),Be(q.value),N(),await re(),ue(!0)}catch(e){const s=(e instanceof Error?e.message:String(e||"")).toLowerCase();["无法连接到后端服务","bad gateway","gateway timeout","network error","timeout","502","503","504"].some(n=>s.includes(n.toLowerCase()))||console.error("loadHistory error:",e)}}function Ce(e){i.value.forEach(t=>{t.items=t.items.filter(n=>n.resultId!==e)});const s=i.value.length;i.value=i.value.filter(t=>t.items.length>0),i.value.length!==s&&typeof window<"u"&&window.localStorage.setItem($(),JSON.stringify(i.value)),q.value=q.value.filter(t=>t.resultId!==e),N(),U().length===0&&be()}async function Ee(e){try{await os(e),Ce(e),await O(),p.success("已删除")}catch(s){const t=s instanceof Error?s.message:"删除失败";if(typeof t=="string"&&t.includes("记录不存在")){Ce(e),p.success("已删除");return}p.error(t)}}function Fe(e){const s=e.slice(0,5).map(n=>`${n.country}:${n.asin}${n.status?`(${n.status})`:""}`);return e.length-s.length>0?`${s.join("、")} 等 ${e.length} 条`:s.join("、")}function kt(e,s){}function ie(){L.value&&(window.clearTimeout(L.value),L.value=null)}function ze(e=3e3){ie(),L.value=window.setTimeout(()=>{L.value=null,$e().catch(()=>{})},e)}function Ve(e){return e.includes("当前店铺正在执行中")}function Pe(){for(const e of i.value)for(const s of e.items){const t=F(s);if(t==="已入队"||t==="处理中")return!0}return!1}function K(e){return e.resultId!=null?`result:${e.resultId}`:`task:${e.taskId||0}:${e.sourceFilename||""}`}function Re(e){return!!(e.taskId&&(e.matchStatus==="MATCHED"||e.matched))}function xe(){return E.value.find(e=>!Re(e)||K(e)===D.value||x(e.taskId,e)?._completed?!1:F(e)==="未入队")}async function Ne(e,s){if(s?.auto&&Pe())return e.taskId,e.sourceFilename,{success:!1,retryable:!0};const t=G(),n=e.taskId;if(!n)return{success:!1,retryable:!1};const a=i.value.find(c=>c.taskId===n);if(!a)return{success:!1,retryable:!1};if(!t?.enqueue_json)return p.error("当前环境未启用 pywebview enqueue_json(浏览器环境不会推送)"),{success:!1,retryable:!1};const o={type:"delete-brand-run",ts:Date.now(),data:{taskId:n,items:[e]}};S.value=JSON.stringify(o,null,2);const r=await t.enqueue_json(o);let u="";if(r?.success){u=`已推送文件 ${e.sourceFilename},当前队列长度:${r.queue_size??"-"}`;const c=x(n,e);return c&&(c._pushed=!0,c._completed=!1),D.value=K(e),te(n,a.items,u,S.value),ue(!0),b.value=u,{success:!0,retryable:!1}}else{const c=r?.error||"未知错误";if(u=`推送到 Python 队列失败:${c}`,b.value=u,te(n,a.items,u,S.value),s?.auto&&Ve(c))return{success:!1,retryable:!0}}return b.value=u,{success:!1,retryable:!1}}async function Qe(){const e=xe();if(!e){p.warning("当前没有可推送的已匹配文件");return}V.value=!0,P.value=!0,ie();const s=await Ne(e);if(V.value=!1,!s.success){P.value=!1,D.value="";return}p.success("已开始按顺序推送到 Python 队列")}async function $e(){if(!P.value||Q.value){P.value,Q.value;return}if(Pe())return;const e=D.value;if(e){const t=E.value.find(n=>K(n)===e);if(t){const n=t.taskId,a=R(n);a&&(pe(n),F(t),void 0);const o=F(t);if(o==="已入队"||o==="处理中"){t.sourceFilename;return}if(a){const r=x(n,t);if(r&&!r._completed){t.sourceFilename,r._pushed,r._completed;return}}}}const s=xe();if(!s){D.value="",P.value=!1;return}Q.value=!0;try{s.taskId,s.sourceFilename,K(s);const t=await Ne(s,{auto:!0});if(!t.success){if(t.retryable){s.taskId,s.sourceFilename,ze();return}s.taskId,s.sourceFilename,P.value=!1}}finally{Q.value=!1}}return Ye(()=>{Me();const e=U();e.length>0?re(e).then(async()=>{let s=!1,t=!1;for(const n of e)we(n)&&(ve(n),s=!0,t=!0);t?await O():s&&N(),ue(!0)}):N(),O().catch(()=>{})}),Ze(()=>{be()}),(e,s)=>(d(),f("div",fs,[es(ss,{active:"delete-brand"}),l("div",vs,[l("aside",hs,[s[2]||(s[2]=l("div",{class:"section-title"},"选择文件",-1)),l("div",ps,[s[0]||(s[0]=l("div",{class:"hint"},"上传删除品牌 Excel;无论是直接上传文件还是上传文件夹批量导入,都会按每个 Excel 文件名作为店铺名。",-1)),l("div",{class:"btns"},[l("button",{type:"button",class:"opt-btn",onClick:Je},"选择 Excel 文件"),l("button",{type:"button",class:"opt-btn",onClick:Ge},"选择文件夹")]),l("div",_s,[A.value.length?(d(),f(H,{key:0},[(d(!0),f(H,null,de(se.value,t=>(d(),f("span",{key:t},v(t),1))),128)),A.value.length>se.value.length?(d(),f("span",ys," 还有 "+v(A.value.length-se.value.length)+" 个文件未展开显示 ",1)):h("",!0)],64)):(d(),f("span",gs,"暂未选择删除品牌文件"))])]),s[3]||(s[3]=ts('
执行说明
',2)),l("div",ms,[l("button",{type:"button",class:"btn-run",disabled:B.value,onClick:je},v(B.value?"解析中...":"开始解析"),9,ks),l("button",{type:"button",class:"btn-run btn-queue",disabled:V.value||!Le.value.length,onClick:Qe},v(V.value?"推送中...":"推送到 Python 队列"),9,ws),l("span",Is,v(B.value?"正在读取删除品牌数据,请稍候…":"解析后会在右侧展示按国家分组的去重结果"),1)]),b.value||S.value?(d(),f("div",bs,[s[1]||(s[1]=l("div",{class:"section-title queue-debug-title"},"Python 队列推送结果",-1)),b.value?(d(),f("div",Ss,v(b.value),1)):h("",!0),S.value?(d(),f("pre",Ts,v(S.value),1)):h("",!0)])):h("",!0)]),l("section",Cs,[s[12]||(s[12]=l("div",{class:"panel-header"},"删除品牌结果",-1)),l("div",Es,[l("div",Fs,[l("div",Ps,[s[4]||(s[4]=l("span",{class:"summary-label"},"已处理文件",-1)),l("strong",null,v(z.value.total),1)]),l("div",Rs,[s[5]||(s[5]=l("span",{class:"summary-label"},"成功结果",-1)),l("strong",null,v(z.value.successCount),1)]),l("div",xs,[s[6]||(s[6]=l("span",{class:"summary-label"},"失败文件",-1)),l("strong",null,v(z.value.failedCount),1)])]),l("div",Ns,[s[11]||(s[11]=l("div",{class:"result-list-header"},[l("span",null,"删除品牌结果列表")],-1)),qe.value?(d(),f(H,{key:1},[E.value.length?(d(),f("div",As,[s[8]||(s[8]=l("div",{class:"result-subsection-title"},"当前任务",-1)),l("ul",Ds,[(d(!0),f(H,null,de(E.value,t=>(d(),f("li",{key:`current-${t.taskId||t.resultId||t.sourceFilename}`,class:"task-item split-result-item"},[l("div",Ms,[l("span",{class:"id",title:t.sourceFilename},v(t.sourceFilename||"-"),9,Bs),l("div",qs,"店铺名:"+v(t.shopName||"-"),1),l("div",Ls,"匹配结果:"+v(me(t)),1),t.platform?(d(),f("div",Us,"平台:"+v(t.platform),1)):h("",!0),F(t)?(d(),f("div",Os,"队列状态:"+v(F(t)),1)):h("",!0),t.countryCount!==void 0&&t.matched?(d(),f("div",Ks,"国家数:"+v(t.countryCount),1)):h("",!0),t.totalRows!==void 0&&t.matched?(d(),f("div",Hs,"去重后 "+v(t.totalRows)+" 条 ",1)):h("",!0),ye(t)?(d(),f("div",Js,[l("div",Gs,[s[7]||(s[7]=l("span",null,"任务进度",-1)),l("span",null,v(Y(t))+"%",1)]),l("div",js,[l("div",{class:"delete-brand-progress-bar-fill",style:Ae({width:`${Y(t)}%`})},null,4)]),l("div",zs,v(ke(t)),1)])):h("",!0),t.previewRows?.length?(d(),f("div",Vs,v(Fe(t.previewRows)),1)):h("",!0),W(t)?(d(),f("div",Qs,"错误信息:"+v(W(t)),1)):h("",!0)]),l("div",Xs,[l("span",{class:De(["status",Z(t).className])},v(Z(t).text),3),Ie(t)?(d(),f("button",{key:0,type:"button",class:"download",onClick:n=>Te(t)}," 下载 ",8,Ws)):h("",!0),t.resultId?(d(),f("button",{key:1,type:"button",class:"btn-delete",onClick:n=>Ee(t.resultId)}," 删除 ",8,Ys)):h("",!0)])]))),128))])])):h("",!0),X.value.length?(d(),f("div",Zs,[s[10]||(s[10]=l("div",{class:"result-subsection-title"},"历史记录",-1)),l("ul",et,[(d(!0),f(H,null,de(X.value,t=>(d(),f("li",{key:`history-${t.taskId||t.resultId||t.sourceFilename}`,class:"task-item split-result-item"},[l("div",st,[l("span",{class:"id",title:t.sourceFilename},v(t.sourceFilename||"-"),9,tt),l("div",nt,"店铺名:"+v(t.shopName||"-"),1),l("div",at,"匹配结果:"+v(me(t)),1),t.platform?(d(),f("div",lt,"平台:"+v(t.platform),1)):h("",!0),t.countryCount!==void 0&&t.matched?(d(),f("div",rt,"国家数:"+v(t.countryCount),1)):h("",!0),t.totalRows!==void 0&&t.matched?(d(),f("div",ot,"去重后 "+v(t.totalRows)+" 条 ",1)):h("",!0),ye(t)?(d(),f("div",ut,[l("div",it,[s[9]||(s[9]=l("span",null,"任务进度",-1)),l("span",null,v(Y(t))+"%",1)]),l("div",ct,[l("div",{class:"delete-brand-progress-bar-fill",style:Ae({width:`${Y(t)}%`})},null,4)]),l("div",dt,v(ke(t)),1)])):h("",!0),t.previewRows?.length?(d(),f("div",ft,v(Fe(t.previewRows)),1)):h("",!0),W(t)?(d(),f("div",vt,"错误信息:"+v(W(t)),1)):h("",!0)]),l("div",ht,[l("span",{class:De(["status",Z(t).className])},v(Z(t).text),3),Ie(t)?(d(),f("button",{key:0,type:"button",class:"download",onClick:n=>Te(t)}," 下载 ",8,pt)):h("",!0),t.resultId?(d(),f("button",{key:1,type:"button",class:"btn-delete",onClick:n=>Ee(t.resultId)}," 删除 ",8,_t)):h("",!0)])]))),128))])])):h("",!0)],64)):(d(),f("div",$s," 暂无删除品牌结果,完成解析后会在这里展示按国家分组的去重结果 "))])])])])]))}}),gt=us(yt,[["__scopeId","data-v-b6259516"]]);is(gt).use(cs).mount("#app"); +import{d as Ye,o as Ze,I as es,a as d,c as f,b as ss,B as ts,e as l,F as H,r as fe,t as v,f as h,J as ns,K as De,n as Me,g as y,h as J,L as as,i as G,E as p,M as ls,N as rs,O as os,P as us,Q as is,_ as cs,p as ds,q as fs}from"./pywebview-DuyK2jB1.js";import{e as vs}from"./brand-CKgrwtni.js";const hs={class:"page-shell module-page"},ps={class:"main-content"},_s={class:"left-panel"},ys={class:"upload-zone"},gs={class:"selected-files clean-placeholder split-selected-files"},ms={key:0,class:"more-line"},ks={key:1},ws={class:"run-row"},Is=["disabled"],bs=["disabled"],Ss={class:"loading-msg"},Ts={key:0,class:"queue-debug-card"},Cs={key:0,class:"queue-debug-line"},Es={key:1,class:"queue-debug-payload"},Fs={class:"right-panel"},Ps={class:"task-list-wrap"},Rs={class:"clean-result-summary"},xs={class:"summary-card"},Ns={class:"summary-card"},$s={class:"summary-card"},As={class:"result-list-wrap"},Ds={key:0,class:"empty-tasks"},Ms={key:0,class:"result-subsection"},Bs={class:"task-list clean-result-list"},qs={class:"left split-result-main"},Ls=["title"],Us={class:"files"},Os={class:"files"},Ks={key:0,class:"files"},Hs={key:1,class:"files"},Js={key:2,class:"files"},Gs={key:3,class:"time"},js={key:4,class:"delete-brand-progress-block"},zs={class:"delete-brand-progress-header"},Vs={class:"delete-brand-progress-bar"},Qs={class:"files delete-brand-progress"},Xs={key:5,class:"files split-entry-list delete-brand-preview"},Ws={key:6,class:"files"},Ys={class:"task-right split-result-actions"},Zs=["onClick"],et=["onClick"],st={key:1,class:"result-subsection"},tt={class:"task-list clean-result-list"},nt={class:"left split-result-main"},at=["title"],lt={class:"files"},rt={class:"files"},ot={key:0,class:"files"},ut={key:1,class:"files"},it={key:2,class:"time"},ct={key:3,class:"delete-brand-progress-block"},dt={class:"delete-brand-progress-header"},ft={class:"delete-brand-progress-bar"},vt={class:"files delete-brand-progress"},ht={key:4,class:"files split-entry-list delete-brand-preview"},pt={key:5,class:"files"},_t={class:"task-right split-result-actions"},yt=["onClick"],gt=["onClick"],mt=Ye({__name:"BrandDeleteBrandTab",setup(wt){function $(){return`delete-brand:current-tasks:${typeof window<"u"&&window.localStorage.getItem("uid")||"0"}`}const A=y([]),j=y([]),B=y(!1),ve=y([]),i=y([]),q=y([]),z=y({total:0,successCount:0,failedCount:0,items:[]}),b=y(""),S=y(""),g=y({}),T=y(null),ee=y(!1),V=y(!1),E=y(!1),D=y(""),Q=y(!1),L=y(null),se=y(!1),te=J(()=>A.value.slice(0,10)),F=J(()=>{const e=[];return i.value.forEach(s=>{e.push(...s.items)}),e});function Be(){try{const e=typeof window<"u"?window.localStorage.getItem($()):null;if(e){if(i.value=JSON.parse(e),i.value.length>0){const s=i.value[i.value.length-1];b.value=s.queuePushResult||"",S.value=s.queuePayloadText||""}}else i.value=[]}catch{i.value=[]}}function ne(e,s,t,n){if(!s?.length)return;const a=i.value.findIndex(c=>c.taskId===e),o=a>=0?i.value[a]:null,r=s.map(c=>{const _=o?.items.find(We=>M(We,c)),k=Array.isArray(c.countries)?c.countries:void 0,m=Array.isArray(_?.countries)?_?.countries:void 0,w=k&&k.length>0?k:m&&m.length>0?m:k??m,I=Array.isArray(c.previewRows)?c.previewRows:void 0,C=Array.isArray(_?.previewRows)?_?.previewRows:void 0,de=I&&I.length>0?I:C&&C.length>0?C:I??C;return{...c,countries:w,previewRows:de,_pushed:_?._pushed??c._pushed??!1,_completed:_?._completed??c._completed??!1}}),u={taskId:e,items:r,createdAt:o?.createdAt||Date.now(),queuePushResult:t??o?.queuePushResult,queuePayloadText:n??o?.queuePayloadText};a>=0?i.value[a]=u:i.value.push(u),typeof window<"u"&&window.localStorage.setItem($(),JSON.stringify(i.value))}function he(e){const s=i.value.length;i.value=i.value.filter(t=>t.taskId!==e),i.value.length!==s&&typeof window<"u"&&window.localStorage.setItem($(),JSON.stringify(i.value))}function pe(e){return e==="SUCCESS"||e==="FAILED"||e==="COMPLETED"}function qe(e){if(!e?.length)return!1;const s=new Set;if(e.forEach(n=>{n.taskId&&pe(n.taskStatus)&&s.add(n.taskId)}),!s.size)return!1;const t=i.value.length;return i.value=i.value.filter(n=>!s.has(n.taskId)),i.value.length!==t?(ae(),!0):!1}const X=J(()=>q.value.filter(e=>!F.value.some(s=>M(s,e)))),Le=J(()=>F.value.length>0||X.value.length>0),Ue=J(()=>F.value.filter(e=>xe(e)&&P(e)==="未入队"));function ae(){typeof window<"u"&&(i.value.length===0?window.localStorage.removeItem($()):window.localStorage.setItem($(),JSON.stringify(i.value)))}function R(e){return e?(i.value.find(t=>t.taskId===e)?.items.length||0)>1:!1}function le(e){return e&&i.value.find(s=>s.taskId===e)||null}function M(e,s){return!e||!s?!1:e.resultId!=null&&s.resultId!=null?e.resultId===s.resultId:e.fileKey&&s.fileKey?e.fileKey===s.fileKey:e.sourceFilename===s.sourceFilename}function x(e,s){const t=le(e);return t&&t.items.find(n=>M(n,s))||null}function _e(e){if(!e)return!1;const s=le(e);if(!s)return!1;let t=!1;const n=g.value[e]?.line_progress?.info,a=R(e);if(n?.file_name&&n.current_line!=null&&n.total_lines!=null&&n.total_lines>0&&n.current_line>=n.total_lines){const o=s.items.find(r=>r.sourceFilename===n.file_name);o&&!o._completed&&(o._completed=!0,t=!0)}if(!a){const o=g.value[e]?.task?.status;if((o==="SUCCESS"||o==="FAILED")&&s.items.length===1){const r=s.items[0];r&&!r._completed&&(r._completed=!0,t=!0)}}return t&&ae(),t}function ye(e){return e&&g.value[e]?.task?.status||""}function W(e){if(e.error)return e.error;if(e.matchStatus&&e.matchStatus!=="MATCHED")return e.matchMessage||"";const s=e.taskId;return s&&g.value[s]?.task?.errorMessage||""}function ge(e){const s=e.taskId;if(!s)return!1;const t=g.value[s]?.line_progress,n=ye(s),a=R(s),o=P(e);if(a){const r=x(s,e),u=K(e)===D.value,c=!!(t?.has_progress&&t?.info?.file_name===e.sourceFilename);return u||c||r?._pushed||(n==="RUNNING"||n==="PENDING"||!n)&&r?._completed?!0:{success:!1,retryable:!0}}return o==="本文件解析完毕"||o==="已被全局判定为终态"?!1:t?.has_progress||t?.info?!0:o==="已入队"||o==="处理中"}function me(e){return e.map(s=>s.matchStatus==="MATCHED"?{...s,_pushed:!1,_completed:!1}:{...s,matched:!1,openStoreUrl:void 0,error:s.error||void 0,_pushed:!1,_completed:!1})}function ke(e){return e.matchStatus==="MATCHED"?`已匹配 ${e.shopId||""}`.trim():e.matchMessage?e.matchMessage:e.matchStatus==="CONFLICT"?"存在多个同名店铺,请人工确认":e.matchStatus==="PENDING"||e.matchStatus==="INDEX_STALE"?"店铺索引暂未就绪":e.matchStatus?e.matchStatus:e.matched?`已匹配 ${e.shopId||""}`.trim():"待匹配"}function Oe(){ve.value=[...F.value,...X.value]}function N(){Oe(),Ke(ve.value)}function Ke(e){z.value={total:e.length,successCount:e.filter(s=>s.success).length,failedCount:e.filter(s=>!s.success).length,items:e}}function re(e){const s=e.taskId;return s&&(g.value[s]?.fileProgress||[]).find(a=>a.fileKey&&e.fileKey?a.fileKey===e.fileKey:a.sourceFilename&&e.sourceFilename?a.sourceFilename===e.sourceFilename:!1)||null}function we(e){const s=e.taskId;if(!s)return"";const t=g.value[s],n=t?.line_progress?.info,a=re(e);if(R(s)){const u=x(s,e),c=t?.task?.status,_=!!(n?.file_name&&n.file_name===e.sourceFilename);let k=u?._completed?"COMPLETED":(u?._pushed,"PENDING"),m=0,w=Math.max(0,e.totalRows??a?.totalRows??0);c==="SUCCESS"||c==="FAILED"&&u?._completed?(k="COMPLETED",m=w):_?(k="RUNNING",m=Math.max(0,Math.min(w||Number.MAX_SAFE_INTEGER,n?.current_line??0)),!w&&n?.total_lines&&n.total_lines>0&&(w=n.total_lines)):u?._completed?(k="COMPLETED",m=w):u?._pushed&&(k="PENDING",m=0);const I=[];if(I.push(`文件状态:${k}`),I.push(`文件进度:${m}/${w}`),n?.finished_files!=null){const C=n.file_total&&n.file_total>0?`/${n.file_total}`:"";I.push(`已完成文件:${n.finished_files}${C}`)}return n?.phase&&I.push(`阶段:${n.phase}`),I.join("|")}if(a){const u=[],c=a.processedRows??0,_=a.totalRows??0;if(a.status&&u.push(`文件状态:${a.status}`),u.push(`文件进度:${c}/${_}`),n?.finished_files!=null){const k=n.file_total&&n.file_total>0?`/${n.file_total}`:"";u.push(`已完成文件:${n.finished_files}${k}`)}return n?.phase&&u.push(`阶段:${n.phase}`),u.join("|")}if(!t?.line_progress?.has_progress||!n){const u=t?.task?.status;return u?`任务状态:${u}`:""}const r=[];if(n.phase&&r.push(`阶段:${n.phase}`),n.file_index&&n.file_total&&r.push(`文件:${n.file_index}/${n.file_total}`),n.file_name&&r.push(`当前文件:${n.file_name}`),n.current_line!=null&&n.total_lines!=null&&r.push(`${R(s)?"当前文件进度":"进度"}:${n.current_line}/${n.total_lines}`),n.current_country&&r.push(`国家:${n.current_country}`),n.current_asin&&r.push(`ASIN:${n.current_asin}`),n.finished_files!=null){const u=n.file_total&&n.file_total>0?`/${n.file_total}`:"";r.push(`已完成文件:${n.finished_files}${u}`)}return r.join("|")}function Y(e){const s=e.taskId;if(!s)return 0;const t=g.value[s]?.line_progress?.info;if(R(s)){const r=x(s,e);if(g.value[s]?.task?.status==="SUCCESS"||r?._completed)return 100;const c=Math.max(0,e.totalRows??re(e)?.totalRows??0);if(!!(t?.file_name&&t.file_name===e.sourceFilename)){if(t?.current_line!=null&&t?.total_lines&&t.total_lines>0)return Math.max(0,Math.min(100,Math.round(t.current_line/t.total_lines*100)));if(t?.current_line!=null&&c>0)return Math.max(0,Math.min(100,Math.round(t.current_line/c*100)))}return 0}const a=re(e);return a?Math.max(0,Math.min(100,a.percent??0)):t?t.current_line!=null&&t.total_lines&&t.total_lines>0?Math.max(0,Math.min(100,Math.round(t.current_line/t.total_lines*100))):t.finished_files!=null&&t.file_total&&t.file_total>0?Math.max(0,Math.min(100,Math.round(t.finished_files/t.file_total*100))):g.value[s]?.task?.status==="SUCCESS"?100:0:0}function Ie(e){const s=g.value[e]?.task?.status;return pe(s)}function U(){const e=new Set;return i.value.forEach(s=>{s.items.some(t=>t._pushed)&&e.add(s.taskId)}),Array.from(e)}function P(e){const s=e.taskId;if(!s||!le(s))return"";const n=x(s,e),a=ye(s),o=g.value[s]?.line_progress,r=R(s);if(!r&&(a==="SUCCESS"||a==="FAILED"||e.taskStatus==="SUCCESS"||e.taskStatus==="FAILED"))return"已被全局判定为终态";if(n?._completed)return"本文件解析完毕";if(a==="RUNNING"&&o?.has_progress&&o?.info?.file_name===e.sourceFilename)return"处理中";if(r&&!n?._pushed&&!n?._completed)return"未入队";if(r&&a==="SUCCESS")return"本文件解析完毕";if(!r){const u=g.value[s];if(u?.items&&u.items.find(_=>M(_,e))&&a==="SUCCESS")return"本文件解析完毕"}return n?._pushed?"已入队":"未入队"}function be(e){if(e.downloadUrl)return!0;const s=e.taskId;return s?g.value[s]?.task?.status==="SUCCESS":{success:!1,retryable:!1}}function He(e){const s=e?.task?.id;if(!s)return!1;const t=i.value.find(r=>r.taskId===s);if(!t)return{success:!1,retryable:!1};const n=e?.items||[];let a=!1;const o=t.items.map(r=>{const u=n.find(de=>M(de,r));if(!u)return r;const c=Array.isArray(u.countries)?u.countries:void 0,_=Array.isArray(r.countries)?r.countries:void 0,k=c&&c.length>0?c:_&&_.length>0?_:c??_,m=Array.isArray(u.previewRows)?u.previewRows:void 0,w=Array.isArray(r.previewRows)?r.previewRows:void 0,I=m&&m.length>0?m:w&&w.length>0?w:m??w,C={...r,...u,countries:k,previewRows:I,_pushed:r._pushed,_completed:r._completed};return JSON.stringify(C)!==JSON.stringify(r)&&(a=!0),C});for(const r of n)o.some(u=>M(u,r))||(o.push({...r,_pushed:!1,_completed:!1}),a=!0);return JSON.stringify(o)!==JSON.stringify(t.items)&&(t.items=o,a=!0),a&&ae(),a}async function oe(e){const s=e||U();if(s.length)try{const t=await as(s);se.value&&E.value&&(b.value="后端已恢复,继续执行当前任务并自动衔接后续任务..."),se.value=!1;let n=!1;for(const a of t.items||[]){const o=a?.task?.id;typeof o=="number"&&o>0&&(g.value[o]=a,He(a)&&(n=!0),_e(o)&&(n=!0))}n&&N()}catch(t){const n=(t instanceof Error?t.message:String(t||"")).toLowerCase();["无法连接到后端服务","bad gateway","gateway timeout","network error","timeout","502","503","504"].some(o=>n.includes(o.toLowerCase()))&&E.value&&(se.value=!0,b.value="当前任务运行中,正在等待后端服务恢复...")}}function Je(){return is()}function ue(e=!1){if(T.value){if(!e)return;clearTimeout(T.value),T.value=null}const s=async()=>{if(T.value=null,ee.value){ue();return}const t=U();if(!t.length)return;ee.value=!0;let n=!1,a=!1;try{await oe(t),await Ae();for(const o of t)Ie(o)&&(he(o),n=!0,a=!0)}finally{ee.value=!1}a?await O():n&&N(),U().length>0&&ue()};e?s():T.value=window.setTimeout(s,Je())}function ie(e=!1){T.value&&!e||ue(e)}function Z(e){const s=e.taskId?g.value[e.taskId]?.task?.status:null;return s==="SUCCESS"?{className:"success",text:"已完成"}:s==="FAILED"?{className:"failed",text:"失败"}:s==="RUNNING"?{className:"running",text:"执行中"}:e.taskStatus==="SUCCESS"?{className:"success",text:"已完成"}:e.taskStatus==="FAILED"?{className:"failed",text:"失败"}:e.taskStatus==="RUNNING"?{className:"running",text:"执行中"}:e.success?{className:"success",text:"已完成"}:{className:"failed",text:"失败"}}function Se(){T.value&&(window.clearTimeout(T.value),T.value=null),ce()}async function Te(e){const s=G();if(!s?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const n of e){const a=typeof n=="string"?n:n.absolutePath,o=typeof n=="string"?void 0:n.relativePath,r=await s.upload_file_to_java(a,o);if(!r?.success||!r.data)throw new Error(r?.error||r?.message||`上传失败:${a}`);t.push(r.data)}return t}async function Ge(){const e=G();if(!e?.select_brand_xlsx_files){p.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const s=await e.select_brand_xlsx_files();if(!s?.length)return;A.value=s,j.value=await Te(s),p.success(`已选择 ${s.length} 个删除品牌文件`)}catch(s){p.error(s instanceof Error?s.message:"选择失败")}}async function je(){const e=G();if(!e?.select_brand_folder){p.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const s=await e.select_brand_folder();if(!s)return;const t=await vs(s);if(!t.success||!t.items?.length){p.warning(t.error||"该文件夹下没有 xlsx 文件");return}A.value=t.items.map(n=>n.relativePath||n.absolutePath),j.value=await Te(t.items),p.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`)}catch(s){p.error(s instanceof Error?s.message:"选择失败")}}async function ze(){if(!j.value.length){p.warning("请先选择待处理文件");return}try{B.value=!0;const e=await ls({files:j.value.map(a=>({fileKey:a.fileKey,originalFilename:a.originalFilename,relativePath:a.relativePath}))}),s=me(e.items||[]),t=s.some(a=>a.matchStatus==="MATCHED"),n=s.some(a=>a.matchStatus&&a.matchStatus!=="MATCHED");b.value=t?"解析完成,可在左侧点击“推送到 Python 队列”开始串行处理":"解析完成,当前没有可推送的已匹配文件",S.value="",s.length&&s[0].taskId&&ne(s[0].taskId,s,b.value,S.value),N(),n?p.warning("部分文件尚未命中可用店铺索引,已保留状态信息,请等待后台刷新后重试。"):t?p.success("删除品牌解析完成,请在左侧推送到 Python 队列"):p.warning("当前没有可推送的已匹配文件,请先等待店铺索引刷新。"),await O()}catch(e){p.error(e instanceof Error?e.message:"执行失败")}finally{B.value=!1}}async function Ce(e){const s=e.taskId;if(!s){p.warning("未找到任务ID");return}const t=G(),n=rs(s),a=e.outputFilename||e.sourceFilename||`delete-brand_${s}.xlsx`;if(t?.save_file_from_url_new){const o=await t.save_file_from_url_new(n,a);o.success?p.success(`已保存:${o.path||a}`):o.error&&o.error!=="用户取消"&&p.error(o.error);return}}async function O(){try{const e=await os();q.value=me(e.items||[]),qe(q.value),N(),await oe(),ie(!0)}catch(e){const s=(e instanceof Error?e.message:String(e||"")).toLowerCase();["无法连接到后端服务","bad gateway","gateway timeout","network error","timeout","502","503","504"].some(n=>s.includes(n.toLowerCase()))||console.error("loadHistory error:",e)}}function Ee(e){i.value.forEach(t=>{t.items=t.items.filter(n=>n.resultId!==e)});const s=i.value.length;i.value=i.value.filter(t=>t.items.length>0),i.value.length!==s&&typeof window<"u"&&window.localStorage.setItem($(),JSON.stringify(i.value)),q.value=q.value.filter(t=>t.resultId!==e),N(),U().length===0&&Se()}async function Fe(e){try{await us(e),Ee(e),await O(),p.success("已删除")}catch(s){const t=s instanceof Error?s.message:"删除失败";if(typeof t=="string"&&t.includes("记录不存在")){Ee(e),p.success("已删除");return}p.error(t)}}function Pe(e){const s=e.slice(0,5).map(n=>`${n.country}:${n.asin}${n.status?`(${n.status})`:""}`);return e.length-s.length>0?`${s.join("、")} 等 ${e.length} 条`:s.join("、")}function It(e,s){}function ce(){L.value&&(window.clearTimeout(L.value),L.value=null)}function Ve(e=3e3){ce(),L.value=window.setTimeout(()=>{L.value=null,Ae().catch(()=>{})},e)}function Qe(e){return e.includes("当前店铺正在执行中")}function Re(){for(const e of i.value)for(const s of e.items){const t=P(s);if(t==="已入队"||t==="处理中")return!0}return!1}function K(e){return e.resultId!=null?`result:${e.resultId}`:`task:${e.taskId||0}:${e.sourceFilename||""}`}function xe(e){return!!(e.taskId&&(e.matchStatus==="MATCHED"||e.matched))}function Ne(){return F.value.find(e=>!xe(e)||K(e)===D.value||x(e.taskId,e)?._completed?!1:P(e)==="未入队")}async function $e(e,s){if(s?.auto&&Re())return e.taskId,e.sourceFilename,{success:!1,retryable:!0};const t=G(),n=e.taskId;if(!n)return{success:!1,retryable:!1};const a=i.value.find(c=>c.taskId===n);if(!a)return{success:!1,retryable:!1};if(!t?.enqueue_json)return p.error("当前环境未启用 pywebview enqueue_json(浏览器环境不会推送)"),{success:!1,retryable:!1};const o={type:"delete-brand-run",ts:Date.now(),data:{taskId:n,items:[e]}};S.value=JSON.stringify(o,null,2);const r=await t.enqueue_json(o);let u="";if(r?.success){u=`已推送文件 ${e.sourceFilename},当前队列长度:${r.queue_size??"-"}`;const c=x(n,e);return c&&(c._pushed=!0,c._completed=!1),D.value=K(e),ne(n,a.items,u,S.value),ie(!0),b.value=u,{success:!0,retryable:!1}}else{const c=r?.error||"未知错误";if(u=`推送到 Python 队列失败:${c}`,b.value=u,ne(n,a.items,u,S.value),s?.auto&&Qe(c))return{success:!1,retryable:!0}}return b.value=u,{success:!1,retryable:!1}}async function Xe(){const e=Ne();if(!e){p.warning("当前没有可推送的已匹配文件");return}V.value=!0,E.value=!0,ce();const s=await $e(e);if(V.value=!1,!s.success){E.value=!1,D.value="";return}p.success("已开始按顺序推送到 Python 队列")}async function Ae(){if(!E.value||Q.value){E.value,Q.value;return}if(Re())return;const e=D.value;if(e){const t=F.value.find(n=>K(n)===e);if(t){const n=t.taskId,a=R(n);a&&(_e(n),P(t),void 0);const o=P(t);if(o==="已入队"||o==="处理中"){t.sourceFilename;return}if(a){const r=x(n,t);if(r&&!r._completed){t.sourceFilename,r._pushed,r._completed;return}}}}const s=Ne();if(!s){D.value="",E.value=!1;return}Q.value=!0;try{s.taskId,s.sourceFilename,K(s);const t=await $e(s,{auto:!0});if(!t.success){if(t.retryable){s.taskId,s.sourceFilename,Ve();return}s.taskId,s.sourceFilename,E.value=!1}}finally{Q.value=!1}}return Ze(()=>{Be();const e=U();e.length>0?oe(e).then(async()=>{let s=!1,t=!1;for(const n of e)Ie(n)&&(he(n),s=!0,t=!0);t?await O():s&&N(),ie(!0)}):N(),O().catch(()=>{})}),es(()=>{Se()}),(e,s)=>(d(),f("div",hs,[ss(ts,{active:"delete-brand"}),l("div",ps,[l("aside",_s,[s[2]||(s[2]=l("div",{class:"section-title"},"选择文件",-1)),l("div",ys,[s[0]||(s[0]=l("div",{class:"hint"},"上传删除品牌 Excel;无论是直接上传文件还是上传文件夹批量导入,都会按每个 Excel 文件名作为店铺名。",-1)),l("div",{class:"btns"},[l("button",{type:"button",class:"opt-btn",onClick:Ge},"选择 Excel 文件"),l("button",{type:"button",class:"opt-btn",onClick:je},"选择文件夹")]),l("div",gs,[A.value.length?(d(),f(H,{key:0},[(d(!0),f(H,null,fe(te.value,t=>(d(),f("span",{key:t},v(t),1))),128)),A.value.length>te.value.length?(d(),f("span",ms," 还有 "+v(A.value.length-te.value.length)+" 个文件未展开显示 ",1)):h("",!0)],64)):(d(),f("span",ks,"暂未选择删除品牌文件"))])]),s[3]||(s[3]=ns('
执行说明
',2)),l("div",ws,[l("button",{type:"button",class:"btn-run",disabled:B.value,onClick:ze},v(B.value?"解析中...":"开始解析"),9,Is),l("button",{type:"button",class:"btn-run btn-queue",disabled:V.value||!Ue.value.length,onClick:Xe},v(V.value?"推送中...":"推送到 Python 队列"),9,bs),l("span",Ss,v(B.value?"正在读取删除品牌数据,请稍候…":"解析后会在右侧展示按国家分组的去重结果"),1)]),b.value||S.value?(d(),f("div",Ts,[s[1]||(s[1]=l("div",{class:"section-title queue-debug-title"},"Python 队列推送结果",-1)),b.value?(d(),f("div",Cs,v(b.value),1)):h("",!0),S.value?(d(),f("pre",Es,v(S.value),1)):h("",!0)])):h("",!0)]),l("section",Fs,[s[12]||(s[12]=l("div",{class:"panel-header"},"删除品牌结果",-1)),l("div",Ps,[l("div",Rs,[l("div",xs,[s[4]||(s[4]=l("span",{class:"summary-label"},"已处理文件",-1)),l("strong",null,v(z.value.total),1)]),l("div",Ns,[s[5]||(s[5]=l("span",{class:"summary-label"},"成功结果",-1)),l("strong",null,v(z.value.successCount),1)]),l("div",$s,[s[6]||(s[6]=l("span",{class:"summary-label"},"失败文件",-1)),l("strong",null,v(z.value.failedCount),1)])]),l("div",As,[s[11]||(s[11]=l("div",{class:"result-list-header"},[l("span",null,"删除品牌结果列表")],-1)),Le.value?(d(),f(H,{key:1},[F.value.length?(d(),f("div",Ms,[s[8]||(s[8]=l("div",{class:"result-subsection-title"},"当前任务",-1)),l("ul",Bs,[(d(!0),f(H,null,fe(F.value,t=>(d(),f("li",{key:`current-${t.taskId||t.resultId||t.sourceFilename}`,class:"task-item split-result-item"},[l("div",qs,[l("span",{class:"id",title:t.sourceFilename},v(t.sourceFilename||"-"),9,Ls),l("div",Us,"店铺名:"+v(t.shopName||"-"),1),l("div",Os,"匹配结果:"+v(ke(t)),1),t.platform?(d(),f("div",Ks,"平台:"+v(t.platform),1)):h("",!0),P(t)?(d(),f("div",Hs,"队列状态:"+v(P(t)),1)):h("",!0),t.countryCount!==void 0&&t.matched?(d(),f("div",Js,"国家数:"+v(t.countryCount),1)):h("",!0),t.totalRows!==void 0&&t.matched?(d(),f("div",Gs,"去重后 "+v(t.totalRows)+" 条 ",1)):h("",!0),ge(t)?(d(),f("div",js,[l("div",zs,[s[7]||(s[7]=l("span",null,"任务进度",-1)),l("span",null,v(Y(t))+"%",1)]),l("div",Vs,[l("div",{class:"delete-brand-progress-bar-fill",style:De({width:`${Y(t)}%`})},null,4)]),l("div",Qs,v(we(t)),1)])):h("",!0),t.previewRows?.length?(d(),f("div",Xs,v(Pe(t.previewRows)),1)):h("",!0),W(t)?(d(),f("div",Ws,"错误信息:"+v(W(t)),1)):h("",!0)]),l("div",Ys,[l("span",{class:Me(["status",Z(t).className])},v(Z(t).text),3),be(t)?(d(),f("button",{key:0,type:"button",class:"download",onClick:n=>Ce(t)}," 下载 ",8,Zs)):h("",!0),t.resultId?(d(),f("button",{key:1,type:"button",class:"btn-delete",onClick:n=>Fe(t.resultId)}," 删除 ",8,et)):h("",!0)])]))),128))])])):h("",!0),X.value.length?(d(),f("div",st,[s[10]||(s[10]=l("div",{class:"result-subsection-title"},"历史记录",-1)),l("ul",tt,[(d(!0),f(H,null,fe(X.value,t=>(d(),f("li",{key:`history-${t.taskId||t.resultId||t.sourceFilename}`,class:"task-item split-result-item"},[l("div",nt,[l("span",{class:"id",title:t.sourceFilename},v(t.sourceFilename||"-"),9,at),l("div",lt,"店铺名:"+v(t.shopName||"-"),1),l("div",rt,"匹配结果:"+v(ke(t)),1),t.platform?(d(),f("div",ot,"平台:"+v(t.platform),1)):h("",!0),t.countryCount!==void 0&&t.matched?(d(),f("div",ut,"国家数:"+v(t.countryCount),1)):h("",!0),t.totalRows!==void 0&&t.matched?(d(),f("div",it,"去重后 "+v(t.totalRows)+" 条 ",1)):h("",!0),ge(t)?(d(),f("div",ct,[l("div",dt,[s[9]||(s[9]=l("span",null,"任务进度",-1)),l("span",null,v(Y(t))+"%",1)]),l("div",ft,[l("div",{class:"delete-brand-progress-bar-fill",style:De({width:`${Y(t)}%`})},null,4)]),l("div",vt,v(we(t)),1)])):h("",!0),t.previewRows?.length?(d(),f("div",ht,v(Pe(t.previewRows)),1)):h("",!0),W(t)?(d(),f("div",pt,"错误信息:"+v(W(t)),1)):h("",!0)]),l("div",_t,[l("span",{class:Me(["status",Z(t).className])},v(Z(t).text),3),be(t)?(d(),f("button",{key:0,type:"button",class:"download",onClick:n=>Ce(t)}," 下载 ",8,yt)):h("",!0),t.resultId?(d(),f("button",{key:1,type:"button",class:"btn-delete",onClick:n=>Fe(t.resultId)}," 删除 ",8,gt)):h("",!0)])]))),128))])])):h("",!0)],64)):(d(),f("div",Ds," 暂无删除品牌结果,完成解析后会在这里展示按国家分组的去重结果 "))])])])])]))}}),kt=cs(mt,[["__scopeId","data-v-64ee024e"]]);ds(kt).use(fs).mount("#app"); diff --git a/app/assets/split.js b/app/assets/split.js index a554f52..a5c8e4d 100644 --- a/app/assets/split.js +++ b/app/assets/split.js @@ -1 +1 @@ -import{d as G,o as J,a,c as n,b as L,B as O,e,F as S,r as $,t as i,f as v,n as x,w as k,s as V,C as M,g as c,h as Q,i as F,E as r,D as W,G as X,m as Y,v as Z,H as ss,_ as es,p as ts,q as ls}from"./pywebview-ClWy2SbE.js";import{e as as}from"./brand-DBpLCgAF.js";const ns={class:"page-shell module-page"},os={class:"main-content"},is={class:"left-panel"},rs={class:"upload-zone"},us={class:"selected-files clean-placeholder split-selected-files"},cs={key:0,class:"more-line"},ds={key:1},ps={class:"option-group column-option-group"},vs={class:"column-toolbar"},fs={class:"column-count"},_s={class:"column-actions"},ms={key:0,class:"column-grid"},ys=["value"],gs={key:1,class:"empty-column-state"},hs={class:"option-group"},bs={class:"radio-item active"},ws={class:"desc"},ks={class:"option-group"},Cs={class:"option-group"},Ss={key:0,class:"split-input-card"},xs={key:1,class:"split-input-card"},Fs={class:"run-row"},Es=["disabled"],Ps={class:"loading-msg"},$s={class:"right-panel"},Bs={class:"task-list-wrap"},Rs={class:"clean-result-summary"},Is={class:"summary-card"},Us={class:"summary-card"},Vs={class:"summary-card"},Ms={class:"result-list-wrap"},Ts={key:0,class:"empty-tasks"},Ds={key:1,class:"task-list clean-result-list"},Hs={class:"left split-result-main"},Ns=["title"],js={key:0,class:"files"},As={key:1,class:"time"},Ks={key:2,class:"files"},zs={key:3,class:"files split-entry-list"},qs={key:4,class:"files"},Gs={class:"task-right split-result-actions"},Js=["onClick"],Ls=["onClick"],Os=G({__name:"BrandSplitTab",setup(Ws){const f=c([]),C=c(""),d=c([]),_=c([]),p=c([]),B=c(0),u=c("rows_per_file"),m=c(2e3),y=c(5),g=c(!1),h=c([]),b=c({total:0,successCount:0,failedCount:0,items:[]}),E=Q(()=>f.value.slice(0,8));function T(l){b.value={total:l.length,successCount:l.filter(s=>s.success).length,failedCount:l.filter(s=>!s.success).length,items:l}}async function R(l){const s=F();if(!s?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const o of l){const U=typeof o=="string"?o:o.absolutePath,q=typeof o=="string"?void 0:o.relativePath,w=await s.upload_file_to_java(U,q);if(!w?.success||!w.data)throw new Error(w?.error||w?.message||`上传失败:${U}`);t.push(w.data)}return t}async function I(l){const s=await Y(l);_.value=s.headers||[],p.value=[...s.headers||[]],B.value=s.totalRows||0}async function D(l,s){f.value=l,C.value="",d.value=await R(l),d.value.length>0&&await I(d.value[0].fileKey),r.success(s)}async function H(){const l=F();if(!l?.select_brand_xlsx_files){r.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const s=await l.select_brand_xlsx_files();if(!s?.length)return;await D(s,`已选择 ${s.length} 个待拆分文件`)}catch(s){r.error(s instanceof Error?s.message:"选择失败")}}async function N(){const l=F();if(!l?.select_brand_folder){r.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const s=await l.select_brand_folder();if(!s)return;C.value=s.split(/[/\\]/).filter(Boolean).pop()||"";const t=await as(s);if(t.success&&t.items?.length){f.value=t.items.map(o=>o.relativePath),d.value=await R(t.items),d.value.length>0&&await I(d.value[0].fileKey),r.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`);return}r.warning(t.error||"该文件夹下没有 xlsx 文件")}catch(s){r.error(s instanceof Error?s.message:"选择失败")}}async function j(){if(!d.value.length){r.warning("请先选择待拆分 Excel 文件或文件夹");return}if(!p.value.length){r.warning("请至少选择一列保留列");return}if(u.value==="rows_per_file"&&(!m.value||m.value<=0)){r.warning("请输入有效的每份条数");return}if(u.value==="parts"&&(!y.value||y.value<=0)){r.warning("请输入有效的拆分份数");return}try{g.value=!0;const l=await W({files:d.value.map(s=>({fileKey:s.fileKey,originalFilename:s.originalFilename,relativePath:s.relativePath})),selectedColumns:p.value,splitMode:u.value,rowsPerFile:u.value==="rows_per_file"&&m.value||void 0,parts:u.value==="parts"&&y.value||void 0,archiveName:d.value.some(s=>!!s.relativePath)&&C.value?C.value:void 0});b.value=l,h.value=l.items||[],await P(),r.success("数据拆分完成")}catch(l){r.error(l instanceof Error?l.message:"数据拆分失败")}finally{g.value=!1}}async function P(){try{const l=await X();h.value=l.items||[],T(h.value)}catch{}}async function A(l){try{await ss(l),await P(),r.success("已删除")}catch(s){r.error(s instanceof Error?s.message:"删除失败")}}function K(l){const s=l.slice(0,4).map(o=>o.rowCount!==void 0?`${o.filename}(${o.rowCount}条)`:o.filename);return l.length-s.length>0?`${s.join("、")} 等 ${l.length} 个文件`:s.join("、")}async function z(l){const s=F();if(!l.downloadUrl){r.warning("当前结果没有下载地址");return}const t=l.outputFilename||`${new Date().toISOString().slice(0,10).replace(/-/g,"")}.zip`;if(s?.save_file_from_url_new){const o=await s.save_file_from_url_new(l.downloadUrl,t);o.success?r.success(`已保存:${o.path||t}`):o.error&&o.error!=="用户取消"&&r.error(o.error);return}}return J(()=>{P().catch(()=>{})}),(l,s)=>(a(),n("div",ns,[L(O,{active:"split"}),e("div",os,[e("aside",is,[s[14]||(s[14]=e("div",{class:"section-title"},"选择文件",-1)),e("div",rs,[s[7]||(s[7]=e("div",{class:"hint"},"选择需要拆分的 Excel 文件或文件夹,按规则拆成多个 Excel 文件。",-1)),e("div",{class:"btns"},[e("button",{type:"button",class:"opt-btn",onClick:H},"选择 Excel 文件"),e("button",{type:"button",class:"opt-btn",onClick:N},"选择文件夹")]),e("div",us,[f.value.length?(a(),n(S,{key:0},[(a(!0),n(S,null,$(E.value,t=>(a(),n("span",{key:t},i(t),1))),128)),f.value.length>E.value.length?(a(),n("span",cs," 还有 "+i(f.value.length-E.value.length)+" 个文件未展开显示 ",1)):v("",!0)],64)):(a(),n("span",ds,"暂未选择待拆分文件"))])]),s[15]||(s[15]=e("div",{class:"section-title"},"保留列设置",-1)),e("div",ps,[e("div",vs,[e("span",fs,"已选择 "+i(p.value.length)+" / "+i(_.value.length)+" 列",1),e("div",_s,[e("button",{type:"button",class:"opt-btn small",onClick:s[0]||(s[0]=t=>p.value=[..._.value])},"全选"),e("button",{type:"button",class:"opt-btn small",onClick:s[1]||(s[1]=t=>p.value=[])},"清空")])]),_.value.length?(a(),n("div",ms,[(a(!0),n(S,null,$(_.value,t=>(a(),n("label",{key:t,class:x(["column-item",{active:p.value.includes(t)}])},[k(e("input",{"onUpdate:modelValue":s[2]||(s[2]=o=>p.value=o),type:"checkbox",value:t},null,8,ys),[[Z,p.value]]),e("span",null,i(t),1)],2))),128))])):(a(),n("div",gs,"请选择 Excel 文件后读取表头并填充可保留列"))]),s[16]||(s[16]=e("div",{class:"section-title"},"总条数",-1)),e("div",hs,[e("label",bs,[s[9]||(s[9]=e("input",{type:"checkbox",checked:"",disabled:""},null,-1)),e("div",null,[s[8]||(s[8]=e("span",{class:"label"},"当前数据总条数",-1)),e("div",ws,i(B.value)+" 条(不含表头)",1)])])]),s[17]||(s[17]=e("div",{class:"section-title"},"拆分模式",-1)),e("div",ks,[e("label",{class:x(["radio-item",{active:u.value==="rows_per_file"}])},[k(e("input",{"onUpdate:modelValue":s[3]||(s[3]=t=>u.value=t),type:"radio",value:"rows_per_file",name:"split-mode"},null,512),[[V,u.value]]),s[10]||(s[10]=e("div",null,[e("span",{class:"label"},"按每份条数"),e("div",{class:"desc"},"例如每份 2000 条,最后一份放剩余数据")],-1))],2),e("label",{class:x(["radio-item",{active:u.value==="parts"}])},[k(e("input",{"onUpdate:modelValue":s[4]||(s[4]=t=>u.value=t),type:"radio",value:"parts",name:"split-mode"},null,512),[[V,u.value]]),s[11]||(s[11]=e("div",null,[e("span",{class:"label"},"按份数平均拆分"),e("div",{class:"desc"},"例如拆 5 份,按总条数尽量平均分配")],-1))],2)]),s[18]||(s[18]=e("div",{class:"section-title"},"拆分参数",-1)),e("div",Cs,[u.value==="rows_per_file"?(a(),n("label",Ss,[s[12]||(s[12]=e("span",{class:"label"},"每份条数",-1)),k(e("input",{"onUpdate:modelValue":s[5]||(s[5]=t=>m.value=t),class:"split-number-input",type:"number",min:"1",placeholder:"例如 2000"},null,512),[[M,m.value,void 0,{number:!0}]])])):(a(),n("label",xs,[s[13]||(s[13]=e("span",{class:"label"},"拆分份数",-1)),k(e("input",{"onUpdate:modelValue":s[6]||(s[6]=t=>y.value=t),class:"split-number-input",type:"number",min:"1",placeholder:"例如 5"},null,512),[[M,y.value,void 0,{number:!0}]])]))]),e("div",Fs,[e("button",{type:"button",class:"btn-run",disabled:g.value,onClick:j},i(g.value?"拆分中...":"开始拆分"),9,Es),e("span",Ps,i(g.value?"正在拆分并上传结果,请稍候…":"选择文件、保留列和拆分规则后即可执行拆分,结果会显示在右侧供下载"),1)])]),e("section",$s,[s[23]||(s[23]=e("div",{class:"panel-header"},"拆分结果",-1)),e("div",Bs,[e("div",Rs,[e("div",Is,[s[19]||(s[19]=e("span",{class:"summary-label"},"已处理文件",-1)),e("strong",null,i(b.value.total),1)]),e("div",Us,[s[20]||(s[20]=e("span",{class:"summary-label"},"成功结果",-1)),e("strong",null,i(b.value.successCount),1)]),e("div",Vs,[s[21]||(s[21]=e("span",{class:"summary-label"},"失败文件",-1)),e("strong",null,i(b.value.failedCount),1)])]),e("div",Ms,[s[22]||(s[22]=e("div",{class:"result-list-header"},[e("span",null,"拆分结果压缩包列表")],-1)),h.value.length===0?(a(),n("div",Ts," 暂无拆分结果,完成数据拆分后会在这里展示压缩包文件 ")):(a(),n("ul",Ds,[(a(!0),n(S,null,$(h.value,t=>(a(),n("li",{key:`${t.resultId||t.outputFilename||t.sourceFilename}-${t.rowCount||0}`,class:"task-item split-result-item"},[e("div",Hs,[e("span",{class:"id",title:t.sourceFilename},i(t.sourceFilename||"-"),9,Ns),t.outputFilename?(a(),n("div",js,"压缩包文件:"+i(t.outputFilename),1)):v("",!0),t.rowCount!==void 0?(a(),n("div",As,"压缩包共包含 "+i(t.rowCount)+" 条数据",1)):v("",!0),t.entryCount&&t.entryCount>0?(a(),n("div",Ks,"压缩包内共 "+i(t.entryCount)+" 个拆分文件 ",1)):v("",!0),t.entries&&t.entries.length?(a(),n("div",zs," 压缩包内容:"+i(K(t.entries)),1)):v("",!0),t.error?(a(),n("div",qs,"错误信息:"+i(t.error),1)):v("",!0)]),e("div",Gs,[e("span",{class:x(["status",t.success?"success":"failed"])},i(t.success?"已完成":"失败"),3),t.success&&t.downloadUrl?(a(),n("button",{key:0,type:"button",class:"download",onClick:o=>z(t)}," 下载压缩包 ",8,Js)):v("",!0),t.resultId?(a(),n("button",{key:1,type:"button",class:"btn-delete",onClick:o=>A(t.resultId)}," 删除 ",8,Ls)):v("",!0)])]))),128))]))])])])])]))}}),Qs=es(Os,[["__scopeId","data-v-c5a98d6b"]]);ts(Qs).use(ls).mount("#app"); +import{d as G,o as J,a,c as n,b as L,B as O,e,F as S,r as $,t as i,f as v,n as x,w as k,s as V,C as M,g as c,h as Q,i as F,E as r,D as W,G as X,m as Y,v as Z,H as ss,_ as es,p as ts,q as ls}from"./pywebview-DuyK2jB1.js";import{e as as}from"./brand-CKgrwtni.js";const ns={class:"page-shell module-page"},os={class:"main-content"},is={class:"left-panel"},rs={class:"upload-zone"},us={class:"selected-files clean-placeholder split-selected-files"},cs={key:0,class:"more-line"},ds={key:1},ps={class:"option-group column-option-group"},vs={class:"column-toolbar"},fs={class:"column-count"},_s={class:"column-actions"},ms={key:0,class:"column-grid"},ys=["value"],gs={key:1,class:"empty-column-state"},hs={class:"option-group"},bs={class:"radio-item active"},ws={class:"desc"},ks={class:"option-group"},Cs={class:"option-group"},Ss={key:0,class:"split-input-card"},xs={key:1,class:"split-input-card"},Fs={class:"run-row"},Es=["disabled"],Ps={class:"loading-msg"},$s={class:"right-panel"},Bs={class:"task-list-wrap"},Rs={class:"clean-result-summary"},Is={class:"summary-card"},Us={class:"summary-card"},Vs={class:"summary-card"},Ms={class:"result-list-wrap"},Ts={key:0,class:"empty-tasks"},Ds={key:1,class:"task-list clean-result-list"},Hs={class:"left split-result-main"},Ns=["title"],js={key:0,class:"files"},As={key:1,class:"time"},Ks={key:2,class:"files"},zs={key:3,class:"files split-entry-list"},qs={key:4,class:"files"},Gs={class:"task-right split-result-actions"},Js=["onClick"],Ls=["onClick"],Os=G({__name:"BrandSplitTab",setup(Ws){const f=c([]),C=c(""),d=c([]),_=c([]),p=c([]),B=c(0),u=c("rows_per_file"),m=c(2e3),y=c(5),g=c(!1),h=c([]),b=c({total:0,successCount:0,failedCount:0,items:[]}),E=Q(()=>f.value.slice(0,8));function T(l){b.value={total:l.length,successCount:l.filter(s=>s.success).length,failedCount:l.filter(s=>!s.success).length,items:l}}async function R(l){const s=F();if(!s?.upload_file_to_java)throw new Error("当前桌面端未提供文件上传桥接能力");const t=[];for(const o of l){const U=typeof o=="string"?o:o.absolutePath,q=typeof o=="string"?void 0:o.relativePath,w=await s.upload_file_to_java(U,q);if(!w?.success||!w.data)throw new Error(w?.error||w?.message||`上传失败:${U}`);t.push(w.data)}return t}async function I(l){const s=await Y(l);_.value=s.headers||[],p.value=[...s.headers||[]],B.value=s.totalRows||0}async function D(l,s){f.value=l,C.value="",d.value=await R(l),d.value.length>0&&await I(d.value[0].fileKey),r.success(s)}async function H(){const l=F();if(!l?.select_brand_xlsx_files){r.warning("当前环境不支持文件选择,请在本机客户端中打开");return}try{const s=await l.select_brand_xlsx_files();if(!s?.length)return;await D(s,`已选择 ${s.length} 个待拆分文件`)}catch(s){r.error(s instanceof Error?s.message:"选择失败")}}async function N(){const l=F();if(!l?.select_brand_folder){r.warning("当前环境不支持文件夹选择,请在本机客户端中打开");return}try{const s=await l.select_brand_folder();if(!s)return;C.value=s.split(/[/\\]/).filter(Boolean).pop()||"";const t=await as(s);if(t.success&&t.items?.length){f.value=t.items.map(o=>o.relativePath),d.value=await R(t.items),d.value.length>0&&await I(d.value[0].fileKey),r.success(`已选择文件夹内 ${t.items.length} 个 xlsx 文件`);return}r.warning(t.error||"该文件夹下没有 xlsx 文件")}catch(s){r.error(s instanceof Error?s.message:"选择失败")}}async function j(){if(!d.value.length){r.warning("请先选择待拆分 Excel 文件或文件夹");return}if(!p.value.length){r.warning("请至少选择一列保留列");return}if(u.value==="rows_per_file"&&(!m.value||m.value<=0)){r.warning("请输入有效的每份条数");return}if(u.value==="parts"&&(!y.value||y.value<=0)){r.warning("请输入有效的拆分份数");return}try{g.value=!0;const l=await W({files:d.value.map(s=>({fileKey:s.fileKey,originalFilename:s.originalFilename,relativePath:s.relativePath})),selectedColumns:p.value,splitMode:u.value,rowsPerFile:u.value==="rows_per_file"&&m.value||void 0,parts:u.value==="parts"&&y.value||void 0,archiveName:d.value.some(s=>!!s.relativePath)&&C.value?C.value:void 0});b.value=l,h.value=l.items||[],await P(),r.success("数据拆分完成")}catch(l){r.error(l instanceof Error?l.message:"数据拆分失败")}finally{g.value=!1}}async function P(){try{const l=await X();h.value=l.items||[],T(h.value)}catch{}}async function A(l){try{await ss(l),await P(),r.success("已删除")}catch(s){r.error(s instanceof Error?s.message:"删除失败")}}function K(l){const s=l.slice(0,4).map(o=>o.rowCount!==void 0?`${o.filename}(${o.rowCount}条)`:o.filename);return l.length-s.length>0?`${s.join("、")} 等 ${l.length} 个文件`:s.join("、")}async function z(l){const s=F();if(!l.downloadUrl){r.warning("当前结果没有下载地址");return}const t=l.outputFilename||`${new Date().toISOString().slice(0,10).replace(/-/g,"")}.zip`;if(s?.save_file_from_url_new){const o=await s.save_file_from_url_new(l.downloadUrl,t);o.success?r.success(`已保存:${o.path||t}`):o.error&&o.error!=="用户取消"&&r.error(o.error);return}}return J(()=>{P().catch(()=>{})}),(l,s)=>(a(),n("div",ns,[L(O,{active:"split"}),e("div",os,[e("aside",is,[s[14]||(s[14]=e("div",{class:"section-title"},"选择文件",-1)),e("div",rs,[s[7]||(s[7]=e("div",{class:"hint"},"选择需要拆分的 Excel 文件或文件夹,按规则拆成多个 Excel 文件。",-1)),e("div",{class:"btns"},[e("button",{type:"button",class:"opt-btn",onClick:H},"选择 Excel 文件"),e("button",{type:"button",class:"opt-btn",onClick:N},"选择文件夹")]),e("div",us,[f.value.length?(a(),n(S,{key:0},[(a(!0),n(S,null,$(E.value,t=>(a(),n("span",{key:t},i(t),1))),128)),f.value.length>E.value.length?(a(),n("span",cs," 还有 "+i(f.value.length-E.value.length)+" 个文件未展开显示 ",1)):v("",!0)],64)):(a(),n("span",ds,"暂未选择待拆分文件"))])]),s[15]||(s[15]=e("div",{class:"section-title"},"保留列设置",-1)),e("div",ps,[e("div",vs,[e("span",fs,"已选择 "+i(p.value.length)+" / "+i(_.value.length)+" 列",1),e("div",_s,[e("button",{type:"button",class:"opt-btn small",onClick:s[0]||(s[0]=t=>p.value=[..._.value])},"全选"),e("button",{type:"button",class:"opt-btn small",onClick:s[1]||(s[1]=t=>p.value=[])},"清空")])]),_.value.length?(a(),n("div",ms,[(a(!0),n(S,null,$(_.value,t=>(a(),n("label",{key:t,class:x(["column-item",{active:p.value.includes(t)}])},[k(e("input",{"onUpdate:modelValue":s[2]||(s[2]=o=>p.value=o),type:"checkbox",value:t},null,8,ys),[[Z,p.value]]),e("span",null,i(t),1)],2))),128))])):(a(),n("div",gs,"请选择 Excel 文件后读取表头并填充可保留列"))]),s[16]||(s[16]=e("div",{class:"section-title"},"总条数",-1)),e("div",hs,[e("label",bs,[s[9]||(s[9]=e("input",{type:"checkbox",checked:"",disabled:""},null,-1)),e("div",null,[s[8]||(s[8]=e("span",{class:"label"},"当前数据总条数",-1)),e("div",ws,i(B.value)+" 条(不含表头)",1)])])]),s[17]||(s[17]=e("div",{class:"section-title"},"拆分模式",-1)),e("div",ks,[e("label",{class:x(["radio-item",{active:u.value==="rows_per_file"}])},[k(e("input",{"onUpdate:modelValue":s[3]||(s[3]=t=>u.value=t),type:"radio",value:"rows_per_file",name:"split-mode"},null,512),[[V,u.value]]),s[10]||(s[10]=e("div",null,[e("span",{class:"label"},"按每份条数"),e("div",{class:"desc"},"例如每份 2000 条,最后一份放剩余数据")],-1))],2),e("label",{class:x(["radio-item",{active:u.value==="parts"}])},[k(e("input",{"onUpdate:modelValue":s[4]||(s[4]=t=>u.value=t),type:"radio",value:"parts",name:"split-mode"},null,512),[[V,u.value]]),s[11]||(s[11]=e("div",null,[e("span",{class:"label"},"按份数平均拆分"),e("div",{class:"desc"},"例如拆 5 份,按总条数尽量平均分配")],-1))],2)]),s[18]||(s[18]=e("div",{class:"section-title"},"拆分参数",-1)),e("div",Cs,[u.value==="rows_per_file"?(a(),n("label",Ss,[s[12]||(s[12]=e("span",{class:"label"},"每份条数",-1)),k(e("input",{"onUpdate:modelValue":s[5]||(s[5]=t=>m.value=t),class:"split-number-input",type:"number",min:"1",placeholder:"例如 2000"},null,512),[[M,m.value,void 0,{number:!0}]])])):(a(),n("label",xs,[s[13]||(s[13]=e("span",{class:"label"},"拆分份数",-1)),k(e("input",{"onUpdate:modelValue":s[6]||(s[6]=t=>y.value=t),class:"split-number-input",type:"number",min:"1",placeholder:"例如 5"},null,512),[[M,y.value,void 0,{number:!0}]])]))]),e("div",Fs,[e("button",{type:"button",class:"btn-run",disabled:g.value,onClick:j},i(g.value?"拆分中...":"开始拆分"),9,Es),e("span",Ps,i(g.value?"正在拆分并上传结果,请稍候…":"选择文件、保留列和拆分规则后即可执行拆分,结果会显示在右侧供下载"),1)])]),e("section",$s,[s[23]||(s[23]=e("div",{class:"panel-header"},"拆分结果",-1)),e("div",Bs,[e("div",Rs,[e("div",Is,[s[19]||(s[19]=e("span",{class:"summary-label"},"已处理文件",-1)),e("strong",null,i(b.value.total),1)]),e("div",Us,[s[20]||(s[20]=e("span",{class:"summary-label"},"成功结果",-1)),e("strong",null,i(b.value.successCount),1)]),e("div",Vs,[s[21]||(s[21]=e("span",{class:"summary-label"},"失败文件",-1)),e("strong",null,i(b.value.failedCount),1)])]),e("div",Ms,[s[22]||(s[22]=e("div",{class:"result-list-header"},[e("span",null,"拆分结果压缩包列表")],-1)),h.value.length===0?(a(),n("div",Ts," 暂无拆分结果,完成数据拆分后会在这里展示压缩包文件 ")):(a(),n("ul",Ds,[(a(!0),n(S,null,$(h.value,t=>(a(),n("li",{key:`${t.resultId||t.outputFilename||t.sourceFilename}-${t.rowCount||0}`,class:"task-item split-result-item"},[e("div",Hs,[e("span",{class:"id",title:t.sourceFilename},i(t.sourceFilename||"-"),9,Ns),t.outputFilename?(a(),n("div",js,"压缩包文件:"+i(t.outputFilename),1)):v("",!0),t.rowCount!==void 0?(a(),n("div",As,"压缩包共包含 "+i(t.rowCount)+" 条数据",1)):v("",!0),t.entryCount&&t.entryCount>0?(a(),n("div",Ks,"压缩包内共 "+i(t.entryCount)+" 个拆分文件 ",1)):v("",!0),t.entries&&t.entries.length?(a(),n("div",zs," 压缩包内容:"+i(K(t.entries)),1)):v("",!0),t.error?(a(),n("div",qs,"错误信息:"+i(t.error),1)):v("",!0)]),e("div",Gs,[e("span",{class:x(["status",t.success?"success":"failed"])},i(t.success?"已完成":"失败"),3),t.success&&t.downloadUrl?(a(),n("button",{key:0,type:"button",class:"download",onClick:o=>z(t)}," 下载压缩包 ",8,Js)):v("",!0),t.resultId?(a(),n("button",{key:1,type:"button",class:"btn-delete",onClick:o=>A(t.resultId)}," 删除 ",8,Ls)):v("",!0)])]))),128))]))])])])])]))}}),Qs=es(Os,[["__scopeId","data-v-c5a98d6b"]]);ts(Qs).use(ls).mount("#app"); diff --git a/app/blueprints/__pycache__/brand_备份.cpython-312.pyc b/app/blueprints/__pycache__/brand_备份.cpython-312.pyc new file mode 100644 index 0000000..8401a0d Binary files /dev/null and b/app/blueprints/__pycache__/brand_备份.cpython-312.pyc differ diff --git a/app/main.py b/app/main.py index 0286722..be4fba7 100644 --- a/app/main.py +++ b/app/main.py @@ -21,9 +21,6 @@ import requests import subprocess from amazon.del_brand import kill_process - - - with open("version.txt","w",encoding="utf-8") as file: file.write(json.dumps({"version":version})) diff --git a/app/new_web_source/convert.html b/app/new_web_source/convert.html index 8b2fe68..bfb015f 100644 --- a/app/new_web_source/convert.html +++ b/app/new_web_source/convert.html @@ -5,9 +5,9 @@ 格式转换 - 数富AI - - - + + + diff --git a/app/new_web_source/dedupe.html b/app/new_web_source/dedupe.html index 5ff728a..faae410 100644 --- a/app/new_web_source/dedupe.html +++ b/app/new_web_source/dedupe.html @@ -5,9 +5,9 @@ 数据去重 - 数富AI - - - + + + diff --git a/app/new_web_source/delete-brand.html b/app/new_web_source/delete-brand.html index 1ed24f6..8f767f0 100644 --- a/app/new_web_source/delete-brand.html +++ b/app/new_web_source/delete-brand.html @@ -5,10 +5,10 @@ 删除品牌 - 数富AI - - - - + + + +
diff --git a/app/new_web_source/split.html b/app/new_web_source/split.html index b096dd8..e099223 100644 --- a/app/new_web_source/split.html +++ b/app/new_web_source/split.html @@ -5,9 +5,9 @@ 数据拆分 - 数富AI - - - + + + diff --git a/backend-java/aiimage-backend.log.2026-04-16.0.gz b/backend-java/aiimage-backend.log.2026-04-16.0.gz deleted file mode 100644 index 029fbc5..0000000 Binary files a/backend-java/aiimage-backend.log.2026-04-16.0.gz and /dev/null differ diff --git a/backend-java/aiimage-backend.log.2026-04-17.0.gz b/backend-java/aiimage-backend.log.2026-04-17.0.gz deleted file mode 100644 index d6ed03c..0000000 Binary files a/backend-java/aiimage-backend.log.2026-04-17.0.gz and /dev/null differ diff --git a/backend-java/aiimage-backend.log.2026-04-19.0.gz b/backend-java/aiimage-backend.log.2026-04-19.0.gz deleted file mode 100644 index bb38944..0000000 Binary files a/backend-java/aiimage-backend.log.2026-04-19.0.gz and /dev/null differ diff --git a/backend-java/docs/patrol-delete-python-api.md b/backend-java/docs/patrol-delete-python-api.md new file mode 100644 index 0000000..f57a3ed --- /dev/null +++ b/backend-java/docs/patrol-delete-python-api.md @@ -0,0 +1,126 @@ +# 巡店删除 Python 提交接口 + +本文档给 Python 端同学说明 `patrol-delete` 的结果回传方式。前端不会再提供“手动归档”按钮,任务收尾、XLSX 组装、OSS 上传与历史清理均由服务端自动完成。 + +## 1. 提交入口 + +- 方法:`POST` +- 路径:`/api/patrol-delete/tasks/{taskId}/result` +- Content-Type:`application/json` + +示例: + +```http +POST /api/patrol-delete/tasks/123/result +Content-Type: application/json +``` + +```json +{ + "shops": [ + { + "shopName": "示例店铺", + "submissionId": "patrol-delete:123:示例店铺:1711111111111", + "chunkIndex": 1, + "chunkTotal": 3, + "shopDone": false, + "countrySections": [ + { + "country": "德国", + "rows": [ + { + "status": "正常", + "quantity": "12", + "deleteQuantity": "3", + "processStatus": "处理中" + } + ] + } + ], + "cartRatios": [ + { + "country": "德国", + "ratio": "25%" + } + ] + } + ] +} +``` + +## 2. 字段说明 + +- `shops`: 本次提交的店铺结果分片列表,至少 1 条。 +- `shopName`: 店铺名。服务端会按店铺名归并分片。 +- `submissionId`: 建议每个店铺任务周期内保持同一个 submissionId,方便排查日志。 +- `chunkIndex`: 当前分片序号,建议从 1 开始。 +- `chunkTotal`: 当前店铺总分片数。 +- `shopDone`: 当前店铺是否已全部提交完成。最后一片必须传 `true`。 +- `error`: 当前店铺执行失败时传错误信息。传了 `error` 后该店铺会直接记为失败并结束。 +- `countrySections`: 店铺各国家的状态数据。 +- `cartRatios`: 店铺各国家的购物车比例数据。 + +## 3. 分片合并规则 + +- 服务端按 `taskId + shopName` 聚合缓存。 +- 同一国家的 `countrySections[].rows` 会按提交顺序追加合并。 +- `cartRatios` 按国家覆盖,后到数据覆盖先到数据。 +- 当 `shopDone=true` 时,该店铺会被标记为完成。 +- 如果中途提交了 `error`,该店铺会被标记为失败,不再等待后续分片。 + +## 4. Excel 组装规则 + +最终结果文件使用 `xlsx/日常删除格式.xlsx` 对应的数据结构: + +- 第 1 列:`店铺名` +- 接着是 5 个国家块,每个国家 4 列: + - 国家状态 + - 数量 + - 删除数量 + - 删除结果 +- 最后是 5 组购物车比例列: + - 国家 + - 购物车比例 + +每个店铺输出规则: + +- 一个店铺占用的行数 = 5 个国家中数据行数的最大值 +- 每个店铺处理完成后会额外空一行 +- 然后再写下一个店铺 + +任务整体完成后: + +1. 服务端汇总所有已完成店铺的数据 +2. 组装成 XLSX +3. 上传 OSS +4. 前端历史任务中出现下载按钮,用户可直接下载 + +## 5. 自动收尾与超时补偿 + +前端不会手动触发归档,服务端自动处理: + +- Python 正常把所有店铺都提交完成后,服务端立即尝试收尾。 +- 如果最后一次提交后未来得及触发收尾,定时补偿任务会再次尝试 finalize。 +- 如果某店铺已有部分有效数据,但 Python 异常退出,补偿任务会尽量把已有数据收尾成成功店铺。 +- 如果某店铺完全没有完整结果且已超时,服务端会将该店铺标记为失败。 + +当前超时配置来自后端配置项: + +- `aiimage.delete-brand-progress.patrol-delete-stale-timeout-minutes` +- `aiimage.delete-brand-progress.patrol-delete-initial-timeout-minutes` + +## 6. 历史清理 + +服务端会定时清理历史数据,但会跳过仍在执行中的任务。 + +- `RUNNING` 任务不会被历史清理删除 +- 只有已完成或已失败的历史记录会进入清理范围 +- 临时目录缓存与结果目录也会定期清理 + +## 7. Python 端建议 + +- 一个店铺处理结束的最后一片一定要传 `shopDone=true` +- 如果店铺失败,请直接回传 `error` +- 大店铺建议按国家或按分页做分片,避免一次提交过大 +- 如果同一店铺会多次回传,`shopName` 必须保持一致 +- 如果最后一片提交成功,Python 端无需再调用额外“归档”接口 diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/DeleteBrandProgressProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/DeleteBrandProgressProperties.java index b7e4a78..3e17430 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/DeleteBrandProgressProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/DeleteBrandProgressProperties.java @@ -32,4 +32,10 @@ public class DeleteBrandProgressProperties { */ private long shopMatchStaleTimeoutMinutes = 20; private long shopMatchInitialTimeoutMinutes = 20; + + /** + * Patrol delete RUNNING timeout auto-finalize/fail threshold in minutes. + */ + private long patrolDeleteStaleTimeoutMinutes = 20; + private long patrolDeleteInitialTimeoutMinutes = 20; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/ModuleCleanupProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/ModuleCleanupProperties.java index f5ae7e8..f814efb 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/ModuleCleanupProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/ModuleCleanupProperties.java @@ -11,5 +11,5 @@ import java.util.List; public class ModuleCleanupProperties { private boolean enabled = true; private String cron = "0 0 0 * * *"; - private List moduleTypes = new ArrayList<>(List.of("DEDUPE", "SPLIT", "CONVERT", "DELETE_BRAND", "PRODUCT_RISK_RESOLVE", "PRICE_TRACK")); + private List moduleTypes = new ArrayList<>(List.of("DEDUPE", "SPLIT", "CONVERT", "DELETE_BRAND", "PRODUCT_RISK_RESOLVE", "PRICE_TRACK", "SHOP_MATCH", "PATROL_DELETE")); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java b/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java index 24b5973..6a48a49 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java @@ -4,6 +4,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Configuration; @Configuration -@EnableConfigurationProperties({OssProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class}) +@EnableConfigurationProperties({OssProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class}) public class PropertiesConfig { } diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/StorageProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/StorageProperties.java index 7eb4830..2fa7509 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/StorageProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/StorageProperties.java @@ -8,7 +8,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; public class StorageProperties { private String localTempDir; private boolean cleanupEnabled = true; - private String cleanupCron = "0 0 */6 * * *"; - private long sourceRetentionHours = 48; + private String cleanupCron = "0 0 * * * *"; + private long sourceRetentionHours = 24; private long resultRetentionHours = 24; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/TaskPressureProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/TaskPressureProperties.java new file mode 100644 index 0000000..5c7fdf8 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/config/TaskPressureProperties.java @@ -0,0 +1,11 @@ +package com.nanri.aiimage.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Data +@ConfigurationProperties(prefix = "aiimage.task-pressure") +public class TaskPressureProperties { + private long localTaskEntityCacheMillis = 3000; + private int dbSelectBatchSize = 200; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/controller/DeleteBrandRunController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/controller/DeleteBrandRunController.java index 45b895c..94a7ade 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/controller/DeleteBrandRunController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/controller/DeleteBrandRunController.java @@ -106,6 +106,8 @@ public class DeleteBrandRunController { @PathVariable Long taskId, @Valid @RequestBody DeleteBrandSubmitResultRequest request, jakarta.servlet.http.HttpServletResponse response) { + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setContentType("application/json;charset=UTF-8"); try { deleteBrandRunService.submitResult(taskId, request); return ApiResponse.success(null); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandRunService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandRunService.java index 1e357ae..4a0805c 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandRunService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandRunService.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.config.DeleteBrandProgressProperties; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.deletebrand.model.cache.DeleteBrandParsedFileCacheDto; import com.nanri.aiimage.modules.deletebrand.model.dto.DeleteBrandCountryResultItemDto; import com.nanri.aiimage.modules.deletebrand.model.dto.DeleteBrandProcessedCountryDto; @@ -77,6 +78,7 @@ public class DeleteBrandRunService { private final OssStorageService ossStorageService; private final ObjectMapper objectMapper; private final DeleteBrandProgressProperties deleteBrandProgressProperties; + private final TaskPressureProperties taskPressureProperties; public DeleteBrandRunVo run(DeleteBrandRunRequest request) { if (request.getUserId() == null || request.getUserId() <= 0) { @@ -248,7 +250,7 @@ public class DeleteBrandRunService { .distinct() .toList(); if (!taskIds.isEmpty()) { - List tasks = fileTaskMapper.selectBatchIds(taskIds); + List tasks = selectTasksByIdsInBatches(taskIds); for (FileTaskEntity task : tasks) { try { statusByTaskId.put(task.getId(), task.getStatus()); @@ -320,7 +322,7 @@ public class DeleteBrandRunService { } public DeleteBrandTaskDeletionStatusVo getTaskDeletionStatus(Long taskId, Long userId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { throw new BusinessException("任务不存在"); } @@ -487,7 +489,7 @@ public class DeleteBrandRunService { } public DeleteBrandTaskDetailVo getTaskDetail(Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { throw new BusinessException("任务不存在"); } @@ -519,7 +521,7 @@ public class DeleteBrandRunService { java.util.Map taskById = new java.util.LinkedHashMap<>(cachedTasks); if (!missingTaskIds.isEmpty()) { - java.util.List dbTasks = fileTaskMapper.selectBatchIds(missingTaskIds).stream() + java.util.List dbTasks = selectTasksByIdsInBatches(missingTaskIds).stream() .filter(task -> task != null && MODULE_TYPE.equals(task.getModuleType())) .toList(); for (FileTaskEntity dbTask : dbTasks) { @@ -569,7 +571,7 @@ public class DeleteBrandRunService { java.util.Map taskById = new java.util.LinkedHashMap<>(cachedTasks); if (!missingTaskIds.isEmpty()) { - java.util.List dbTasks = fileTaskMapper.selectBatchIds(missingTaskIds).stream() + java.util.List dbTasks = selectTasksByIdsInBatches(missingTaskIds).stream() .filter(task -> task != null && MODULE_TYPE.equals(task.getModuleType())) .toList(); for (FileTaskEntity dbTask : dbTasks) { @@ -949,6 +951,32 @@ public class DeleteBrandRunService { } } + private FileTaskEntity loadTaskForExecution(Long taskId) { + Map cachedTasks = deleteBrandTaskCacheService.getTaskCacheBatch(List.of(taskId)); + FileTaskEntity cached = cachedTasks.get(taskId); + if (cached != null) { + return cached; + } + FileTaskEntity dbTask = fileTaskMapper.selectById(taskId); + if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) { + deleteBrandTaskCacheService.saveTaskCache(dbTask); + } + return dbTask; + } + + private List selectTasksByIdsInBatches(List taskIds) { + List tasks = new ArrayList<>(); + if (taskIds == null || taskIds.isEmpty()) { + return tasks; + } + int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize()); + for (int start = 0; start < taskIds.size(); start += batchSize) { + int end = Math.min(start + batchSize, taskIds.size()); + tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end))); + } + return tasks; + } + @Transactional public void submitResult(Long taskId, DeleteBrandSubmitResultRequest request) { log.info("[DeleteBrand] Received submitResult for taskId: {}, files count: {}", taskId, request == null || request.getFiles() == null ? 0 : request.getFiles().size()); @@ -960,7 +988,7 @@ public class DeleteBrandRunService { throw new BusinessException("files 不能为空"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { throw new BusinessException("任务不存在"); } @@ -981,8 +1009,10 @@ public class DeleteBrandRunService { task.setUpdatedAt(LocalDateTime.now()); task.setFinishedAt(null); fileTaskMapper.updateById(task); + deleteBrandTaskCacheService.saveTaskCache(task); } + boolean shouldTryFinalize = false; for (DeleteBrandResultFileDto fileDto : request.getFiles()) { log.info("[DeleteBrand] submitResult processing chunk -> taskId: {}, file: {}, chunkIndex: {}/{}, processedRows: {}/{}", @@ -1000,15 +1030,11 @@ public class DeleteBrandRunService { } // 更早的 duplicate fast-path:尽量在较重的校验/进度写入之前直接短路 - if (deleteBrandTaskCacheService.hasSameResultChunk(taskId, fileIdentity, fileDto.getChunkIndex(), fileDto)) { - log.info("[DeleteBrand] Duplicate chunk ignored for taskId: {}, file: {}, chunkIndex: {}", taskId, fileIdentity, fileDto.getChunkIndex()); - continue; - } - validateChunk(fileDto, parsedFile); validateResultFileAgainstParsed(fileDto, parsedFile); - boolean stored = deleteBrandTaskCacheService.mergeResultChunk(taskId, fileIdentity, fileDto.getChunkIndex(), fileDto); + boolean stored = deleteBrandTaskCacheService.storeResultChunkIfChanged(taskId, fileIdentity, fileDto.getChunkIndex(), fileDto); if (!stored) { + log.info("[DeleteBrand] Duplicate chunk ignored for taskId: {}, file: {}, chunkIndex: {}", taskId, fileIdentity, fileDto.getChunkIndex()); continue; } @@ -1023,20 +1049,21 @@ public class DeleteBrandRunService { progress.put("current_asin", blankToEmpty(fileDto.getCurrentAsin())); progress.put("updated_at", String.valueOf(System.currentTimeMillis())); progress.put("last_heartbeat_at", String.valueOf(System.currentTimeMillis())); - deleteBrandTaskCacheService.saveProgress(taskId, progress); + deleteBrandTaskCacheService.saveProgress(taskId, progress, true); // 如果该文件已完成,立即更新成品计数的 Redis 缓存「提示」,让后续 tryFinalizeTask 能更精确 if (isFileCompleted(fileDto)) { log.info("[DeleteBrand] File chunk transmission fully reached via chunkTotal for taskId: {}, file: {}", taskId, fileDto.getSourceFilename()); - java.util.Map currentProgress = deleteBrandTaskCacheService.getProgress(taskId); - int finishedCount = parseInt(currentProgress.get("finished_files")) == null ? 0 : parseInt(currentProgress.get("finished_files")); + shouldTryFinalize = true; // 这里我们暂不直接 increment,而是标记需要重算或在 tryFinalizeTask 中重新计算 // 为了保险,我们直接在 tryFinalizeTask 里重新遍历分片状态 } } - tryFinalizeTask(taskId, false); + if (shouldTryFinalize) { + tryFinalizeTask(taskId, false); + } } public void tryFinalizeTask(Long taskId, boolean fromCompensation) { @@ -1044,7 +1071,7 @@ public class DeleteBrandRunService { return; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { return; } @@ -1082,6 +1109,7 @@ public class DeleteBrandRunService { task.setUpdatedAt(LocalDateTime.now()); } fileTaskMapper.updateById(task); + deleteBrandTaskCacheService.saveTaskCache(task); log.info("[DeleteBrand] tryFinalizeTask skipping -> finishedFiles ({}) < expectedFiles ({})", finishedFiles, expectedFiles); return; @@ -1093,11 +1121,12 @@ public class DeleteBrandRunService { if (fromCompensation) { progress.put("last_heartbeat_at", String.valueOf(System.currentTimeMillis())); } - deleteBrandTaskCacheService.saveProgress(taskId, progress); + deleteBrandTaskCacheService.saveProgress(taskId, progress, true); task.setSuccessFileCount(finishedFiles); task.setUpdatedAt(LocalDateTime.now()); fileTaskMapper.updateById(task); + deleteBrandTaskCacheService.saveTaskCache(task); log.info("[DeleteBrand] tryFinalizeTask calling finalizeTask -> finishedFiles: {}, expectedFiles: {}", finishedFiles, expectedFiles); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandStaleTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandStaleTaskService.java index 5b45afd..e80de33 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandStaleTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandStaleTaskService.java @@ -6,6 +6,8 @@ import com.nanri.aiimage.common.service.DistributedJobLockService; import com.nanri.aiimage.config.DeleteBrandProgressProperties; import com.nanri.aiimage.modules.productrisk.service.ProductRiskTaskCacheService; import com.nanri.aiimage.modules.productrisk.service.ProductRiskTaskService; +import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteTaskCacheService; +import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteTaskService; import com.nanri.aiimage.modules.pricetrack.service.PriceTrackTaskCacheService; import com.nanri.aiimage.modules.pricetrack.service.PriceTrackTaskService; import com.nanri.aiimage.modules.shopmatch.service.ShopMatchTaskCacheService; @@ -14,6 +16,7 @@ import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -37,10 +40,10 @@ public class DeleteBrandStaleTaskService { private static final String MODULE_TYPE_PRODUCT_RISK = "PRODUCT_RISK_RESOLVE"; private static final String MODULE_TYPE_PRICE_TRACK = "PRICE_TRACK"; private static final String MODULE_TYPE_SHOP_MATCH = "SHOP_MATCH"; + private static final String MODULE_TYPE_PATROL_DELETE = "PATROL_DELETE"; private static final Duration STALE_CHECK_LOCK_TTL = Duration.ofMinutes(10); private static final Duration FINALIZE_CHECK_LOCK_TTL = Duration.ofMinutes(10); private static final Duration TEMP_DIR_CLEANUP_LOCK_TTL = Duration.ofMinutes(10); - private static final Duration TEMP_DIR_RETENTION = Duration.ofHours(48); private final FileTaskMapper fileTaskMapper; private final DeleteBrandTaskCacheService deleteBrandTaskCacheService; @@ -51,9 +54,14 @@ public class DeleteBrandStaleTaskService { private final PriceTrackTaskCacheService priceTrackTaskCacheService; private final ShopMatchTaskService shopMatchTaskService; private final ShopMatchTaskCacheService shopMatchTaskCacheService; + private final PatrolDeleteTaskService patrolDeleteTaskService; + private final PatrolDeleteTaskCacheService patrolDeleteTaskCacheService; private final DeleteBrandProgressProperties deleteBrandProgressProperties; private final DistributedJobLockService distributedJobLockService; + @Value("${aiimage.temp-dir.retention-hours:24}") + private long tempDirRetentionHours; + @Scheduled(cron = "${aiimage.delete-brand-progress.stale-check-cron:0 */2 * * * *}") public void failStaleRunningTasks() { DistributedJobLockService.LockHandle lockHandle = @@ -69,6 +77,7 @@ public class DeleteBrandStaleTaskService { ProductRiskStaleCheckStats stats = failStaleProductRiskResolveTasks(); PriceTrackStaleCheckStats priceTrackStats = failStalePriceTrackTasks(); ShopMatchStaleCheckStats shopMatchStats = failStaleShopMatchTasks(); + ShopMatchStaleCheckStats patrolDeleteStats = failStalePatrolDeleteTasks(); log.info("[stale-check] product-risk summary scanned={} finalized={} failed={} skipped={} elapsedMs={} thread={}", stats.scannedTaskCount, stats.finalizedTaskCount, @@ -90,6 +99,13 @@ public class DeleteBrandStaleTaskService { shopMatchStats.skippedTaskCount, System.currentTimeMillis() - startedAt, Thread.currentThread().getName()); + log.info("[stale-check] patrol-delete summary scanned={} finalized={} failed={} skipped={} elapsedMs={} thread={}", + patrolDeleteStats.scannedTaskCount, + patrolDeleteStats.finalizedTaskCount, + patrolDeleteStats.failedTaskCount, + patrolDeleteStats.skippedTaskCount, + System.currentTimeMillis() - startedAt, + Thread.currentThread().getName()); } } @@ -335,6 +351,65 @@ public class DeleteBrandStaleTaskService { return stats; } + private ShopMatchStaleCheckStats failStalePatrolDeleteTasks() { + ShopMatchStaleCheckStats stats = new ShopMatchStaleCheckStats(); + long minutes = Math.max(1L, deleteBrandProgressProperties.getPatrolDeleteStaleTimeoutMinutes()); + long initialMinutes = Math.max(minutes, deleteBrandProgressProperties.getPatrolDeleteInitialTimeoutMinutes()); + long staleTimeoutMillis = Duration.ofMinutes(minutes).toMillis(); + long nowMillis = System.currentTimeMillis(); + LocalDateTime now = LocalDateTime.now(); + LocalDateTime threshold = now.minusMinutes(minutes); + LocalDateTime initialThreshold = now.minusMinutes(initialMinutes); + java.util.List runningTasks = fileTaskMapper.selectList(new LambdaQueryWrapper() + .eq(FileTaskEntity::getModuleType, MODULE_TYPE_PATROL_DELETE) + .eq(FileTaskEntity::getStatus, "RUNNING") + .lt(FileTaskEntity::getUpdatedAt, threshold) + .last("limit 200")); + stats.scannedTaskCount = runningTasks.size(); + if (runningTasks.isEmpty()) { + return stats; + } + for (FileTaskEntity task : runningTasks) { + long lastHeartbeatMillis = patrolDeleteTaskCacheService.getTaskHeartbeatMillis(task.getId()); + boolean hasUploadedPayload = patrolDeleteTaskCacheService.hasAnyShopMergedPayload(task.getId()); + boolean hasFinishedRows = (task.getSuccessFileCount() != null && task.getSuccessFileCount() > 0) + || (task.getFailedFileCount() != null && task.getFailedFileCount() > 0); + boolean hasStartedProgress = lastHeartbeatMillis > 0L || hasUploadedPayload || hasFinishedRows; + if (lastHeartbeatMillis > 0L && nowMillis - lastHeartbeatMillis < staleTimeoutMillis) { + stats.skippedTaskCount++; + continue; + } + if (!hasStartedProgress && task.getCreatedAt() != null && task.getCreatedAt().isAfter(initialThreshold)) { + stats.skippedTaskCount++; + continue; + } + try { + if (patrolDeleteTaskService.tryFinalizeTask(task.getId(), true)) { + stats.finalizedTaskCount++; + log.info("[stale-check] patrol-delete finalized taskId={}", task.getId()); + continue; + } + } catch (Exception ex) { + log.warn("[stale-check] patrol-delete finalize threw taskId={} msg={}", task.getId(), ex.getMessage()); + } + int updated = fileTaskMapper.update(null, new LambdaUpdateWrapper() + .eq(FileTaskEntity::getId, task.getId()) + .eq(FileTaskEntity::getModuleType, MODULE_TYPE_PATROL_DELETE) + .eq(FileTaskEntity::getStatus, "RUNNING") + .set(FileTaskEntity::getStatus, "FAILED") + .set(FileTaskEntity::getErrorMessage, "长时间未收到 Python 结果回传,任务已自动失败") + .set(FileTaskEntity::getUpdatedAt, LocalDateTime.now()) + .set(FileTaskEntity::getFinishedAt, LocalDateTime.now())); + if (updated > 0) { + stats.failedTaskCount++; + patrolDeleteTaskCacheService.deleteTaskCache(task.getId()); + } else { + stats.skippedTaskCount++; + } + } + return stats; + } + @Scheduled(cron = "${aiimage.delete-brand-progress.finalize-check-cron:30 */2 * * * *}") public void finalizeCompletedRunningTasks() { DistributedJobLockService.LockHandle lockHandle = @@ -369,12 +444,14 @@ public class DeleteBrandStaleTaskService { return; } try (lockHandle) { - Instant cutoff = Instant.now().minus(TEMP_DIR_RETENTION); + Instant cutoff = Instant.now().minus(Duration.ofHours(Math.max(1L, tempDirRetentionHours))); List dirNames = List.of( "shop-match-cache", + "patrol-delete-cache", "brand-task-cache", "delete-brand-task-cache", "shop-match-result", + "patrol-delete-result", "delete-brand-result", "product-risk-result", "price-track-result" diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandTaskCacheService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandTaskCacheService.java index 75e38ae..e13a50d 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandTaskCacheService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/deletebrand/service/DeleteBrandTaskCacheService.java @@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.deletebrand.service; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; @@ -24,10 +25,16 @@ public class DeleteBrandTaskCacheService { private static final long PAYLOAD_TTL_HOURS = 24; private static final long LOCAL_PARSED_PAYLOAD_CACHE_MILLIS = 3000; + private static final long LOCAL_PROGRESS_CACHE_MILLIS = 1500; + private static final long MIN_PROGRESS_REDIS_FLUSH_MILLIS = 1200; private final StringRedisTemplate stringRedisTemplate; private final ObjectMapper objectMapper; + private final TaskPressureProperties taskPressureProperties; private final ConcurrentHashMap parsedPayloadLocalCache = new ConcurrentHashMap<>(); + private final ConcurrentHashMap progressLocalCache = new ConcurrentHashMap<>(); + private final ConcurrentHashMap progressRedisFlushAt = new ConcurrentHashMap<>(); + private final ConcurrentHashMap taskEntityLocalCache = new ConcurrentHashMap<>(); public void saveParsedPayload(Long taskId, Object payload) { try { @@ -75,13 +82,44 @@ public class DeleteBrandTaskCacheService { } public void saveProgress(Long taskId, java.util.Map values) { + saveProgress(taskId, values, false); + } + + public void saveProgress(Long taskId, java.util.Map values, boolean forceWrite) { + if (taskId == null || taskId <= 0 || values == null || values.isEmpty()) { + return; + } + long now = System.currentTimeMillis(); + java.util.Map merged = new java.util.LinkedHashMap<>(); + LocalProgressCacheEntry cached = progressLocalCache.get(taskId); + if (cached != null && cached.isFresh(now)) { + merged.putAll(cached.values()); + } + merged.putAll(values); + progressLocalCache.put(taskId, new LocalProgressCacheEntry(now, merged)); + + Long lastFlushAt = progressRedisFlushAt.get(taskId); + if (!forceWrite && lastFlushAt != null && now - lastFlushAt < MIN_PROGRESS_REDIS_FLUSH_MILLIS) { + return; + } + String key = buildProgressKey(taskId); - stringRedisTemplate.opsForHash().putAll(key, values); + stringRedisTemplate.opsForHash().putAll(key, merged); stringRedisTemplate.expire(key, Duration.ofHours(PAYLOAD_TTL_HOURS)); + progressRedisFlushAt.put(taskId, now); } public java.util.Map getProgress(Long taskId) { - return stringRedisTemplate.opsForHash().entries(buildProgressKey(taskId)); + long now = System.currentTimeMillis(); + LocalProgressCacheEntry cached = progressLocalCache.get(taskId); + if (cached != null && cached.isFresh(now)) { + return new java.util.LinkedHashMap<>(cached.values()); + } + java.util.Map values = stringRedisTemplate.opsForHash().entries(buildProgressKey(taskId)); + if (!values.isEmpty()) { + progressLocalCache.put(taskId, new LocalProgressCacheEntry(now, toStringMap(values))); + } + return values; } public java.util.Map> getProgressBatch(java.util.List taskIds) { @@ -98,16 +136,30 @@ public class DeleteBrandTaskCacheService { return result; } + long now = System.currentTimeMillis(); + java.util.List missingTaskIds = new java.util.ArrayList<>(); + for (Long taskId : normalizedTaskIds) { + LocalProgressCacheEntry cached = progressLocalCache.get(taskId); + if (cached != null && cached.isFresh(now)) { + result.put(taskId, new java.util.LinkedHashMap<>(cached.values())); + } else { + missingTaskIds.add(taskId); + } + } + if (missingTaskIds.isEmpty()) { + return result; + } + java.util.List pipelineResults = stringRedisTemplate.executePipelined((org.springframework.data.redis.core.RedisCallback) connection -> { org.springframework.data.redis.serializer.RedisSerializer serializer = stringRedisTemplate.getStringSerializer(); - for (Long taskId : normalizedTaskIds) { + for (Long taskId : missingTaskIds) { connection.hGetAll(serializer.serialize(buildProgressKey(taskId))); } return null; }); - for (int i = 0; i < normalizedTaskIds.size(); i++) { - Long taskId = normalizedTaskIds.get(i); + for (int i = 0; i < missingTaskIds.size(); i++) { + Long taskId = missingTaskIds.get(i); Object raw = pipelineResults != null && i < pipelineResults.size() ? pipelineResults.get(i) : null; if (raw instanceof java.util.Map rawMap) { java.util.Map converted = new java.util.LinkedHashMap<>(); @@ -115,6 +167,7 @@ public class DeleteBrandTaskCacheService { converted.put(entry.getKey(), entry.getValue()); } result.put(taskId, converted); + progressLocalCache.put(taskId, new LocalProgressCacheEntry(now, toStringMap(converted))); } else { result.put(taskId, java.util.Collections.emptyMap()); } @@ -169,6 +222,10 @@ public class DeleteBrandTaskCacheService { return true; } + public boolean storeResultChunkIfChanged(Long taskId, String fileIdentity, Integer chunkIndex, Object chunk) { + return mergeResultChunk(taskId, fileIdentity, chunkIndex, chunk); + } + public java.util.Map> groupResultChunkJsonByFile(Long taskId) { java.util.Map stored = stringRedisTemplate.opsForHash().entries(buildResultChunksKey(taskId)); java.util.Map> grouped = new java.util.LinkedHashMap<>(); @@ -188,6 +245,9 @@ public class DeleteBrandTaskCacheService { public void delete(Long taskId) { parsedPayloadLocalCache.remove(taskId); + progressLocalCache.remove(taskId); + progressRedisFlushAt.remove(taskId); + taskEntityLocalCache.remove(taskId); stringRedisTemplate.delete(buildProgressKey(taskId)); stringRedisTemplate.delete(buildResultChunksKey(taskId)); stringRedisTemplate.delete(buildTaskEntityKey(taskId)); @@ -198,6 +258,11 @@ public class DeleteBrandTaskCacheService { if (task == null || task.getId() == null) { return; } + long now = System.currentTimeMillis(); + taskEntityLocalCache.put(task.getId(), new LocalTaskEntityCacheEntry( + now, + objectMapper.convertValue(task, com.nanri.aiimage.modules.task.model.entity.FileTaskEntity.class) + )); try { stringRedisTemplate.opsForValue().set(buildTaskEntityKey(task.getId()), objectMapper.writeValueAsString(task), Duration.ofHours(PAYLOAD_TTL_HOURS)); } catch (Exception ignored) { @@ -215,15 +280,32 @@ public class DeleteBrandTaskCacheService { return result; } - java.util.List keys = normalized.stream().map(this::buildTaskEntityKey).toList(); + long now = System.currentTimeMillis(); + java.util.List missingIds = new java.util.ArrayList<>(); + for (Long taskId : normalized) { + LocalTaskEntityCacheEntry cached = taskEntityLocalCache.get(taskId); + if (isLocalTaskEntityCacheFresh(cached, now)) { + result.put(taskId, objectMapper.convertValue(cached.task(), com.nanri.aiimage.modules.task.model.entity.FileTaskEntity.class)); + } else { + missingIds.add(taskId); + } + } + if (missingIds.isEmpty()) { + return result; + } + + java.util.List keys = missingIds.stream().map(this::buildTaskEntityKey).toList(); java.util.List values = stringRedisTemplate.opsForValue().multiGet(keys); - for (int i = 0; i < normalized.size(); i++) { - Long taskId = normalized.get(i); + for (int i = 0; i < missingIds.size(); i++) { + Long taskId = missingIds.get(i); String val = values != null && i < values.size() ? values.get(i) : null; if (val != null && !val.isBlank()) { try { - result.put(taskId, objectMapper.readValue(val, com.nanri.aiimage.modules.task.model.entity.FileTaskEntity.class)); + com.nanri.aiimage.modules.task.model.entity.FileTaskEntity task = + objectMapper.readValue(val, com.nanri.aiimage.modules.task.model.entity.FileTaskEntity.class); + result.put(taskId, task); + taskEntityLocalCache.put(taskId, new LocalTaskEntityCacheEntry(now, task)); } catch (Exception ignored) { } } @@ -265,6 +347,33 @@ public class DeleteBrandTaskCacheService { } } + private record LocalProgressCacheEntry(long cachedAtMillis, java.util.Map values) { + private boolean isFresh(long now) { + return now - cachedAtMillis <= LOCAL_PROGRESS_CACHE_MILLIS; + } + } + + private record LocalTaskEntityCacheEntry( + long cachedAtMillis, + com.nanri.aiimage.modules.task.model.entity.FileTaskEntity task + ) {} + + private boolean isLocalTaskEntityCacheFresh(LocalTaskEntityCacheEntry cached, long now) { + return cached != null + && now - cached.cachedAtMillis() <= Math.max(0L, taskPressureProperties.getLocalTaskEntityCacheMillis()); + } + + private java.util.Map toStringMap(java.util.Map values) { + java.util.Map converted = new java.util.LinkedHashMap<>(); + for (java.util.Map.Entry entry : values.entrySet()) { + if (entry.getKey() == null || entry.getValue() == null) { + continue; + } + converted.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())); + } + return converted; + } + private String buildProgressKey(Long taskId) { return "delete-brand:task:progress:" + taskId; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/controller/PatrolDeleteController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/controller/PatrolDeleteController.java new file mode 100644 index 0000000..13365e8 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/controller/PatrolDeleteController.java @@ -0,0 +1,371 @@ +package com.nanri.aiimage.modules.patroldelete.controller; + +import com.nanri.aiimage.common.api.ApiResponse; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCreateTaskRequest; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteSubmitResultRequest; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteTaskBatchRequest; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteCreateTaskVo; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteHistoryVo; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteTaskBatchVo; +import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteResolveService; +import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteTaskService; +import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; +import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.ExampleObject; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import java.io.InputStream; +import java.net.URI; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/patrol-delete") +@Tag( + name = "巡店删除", + description = "巡店删除模块:管理备选店铺、批量匹配紫鸟店铺、创建任务、接收 Python 分片结果、自动收尾生成 Excel 并提供下载。涉及查询和删除的接口需要携带 user_id。") +public class PatrolDeleteController { + + private final PatrolDeleteResolveService patrolDeleteResolveService; + private final PatrolDeleteTaskService patrolDeleteTaskService; + + @GetMapping("/candidates") + @Operation(summary = "查询备选店铺列表", description = "返回当前用户在巡店删除模块中已保存的备选店铺。") + public ApiResponse> listCandidates( + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId) { + return ApiResponse.success(patrolDeleteResolveService.listCandidates(userId)); + } + + @PostMapping("/candidates") + @Operation(summary = "新增备选店铺", description = "向巡店删除备选区新增一条店铺记录,供后续匹配和创建任务使用。") + public ApiResponse addCandidate( + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "新增备选店铺请求,需传入用户 ID 和店铺名。", + required = true, + content = @Content( + mediaType = "application/json", + schema = @Schema(implementation = ProductRiskCandidateAddRequest.class), + examples = @ExampleObject( + name = "新增店铺示例", + value = """ + { + "user_id": 1, + "shop_name": "郭亚芳" + } + """))) + @Valid @RequestBody ProductRiskCandidateAddRequest request) { + return ApiResponse.success(patrolDeleteResolveService.addCandidate(request)); + } + + @DeleteMapping("/candidates/{id}") + @Operation(summary = "删除备选店铺", description = "删除当前用户名下的一条备选店铺记录。") + public ApiResponse deleteCandidate( + @Parameter(description = "备选店铺记录主键", example = "10") + @PathVariable Long id, + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId) { + patrolDeleteResolveService.deleteCandidate(userId, id); + return ApiResponse.success(null); + } + + @PostMapping("/match-shops") + @Operation(summary = "批量匹配店铺", description = "根据店铺名批量匹配紫鸟店铺索引,返回是否命中、店铺 ID、平台、公司和匹配状态。") + public ApiResponse matchShops( + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "批量匹配请求,传入 user_id 和待匹配的店铺名称列表。", + required = true, + content = @Content( + mediaType = "application/json", + schema = @Schema(implementation = ProductRiskMatchShopsRequest.class), + examples = @ExampleObject( + name = "匹配店铺示例", + value = """ + { + "user_id": 1, + "shop_names": [ + "郭亚芳", + "示例店铺A" + ] + } + """))) + @Valid @RequestBody ProductRiskMatchShopsRequest request) { + return ApiResponse.success(patrolDeleteResolveService.matchShops(request)); + } + + @GetMapping("/dashboard") + @Operation(summary = "查询统计看板", description = "返回备选店铺数、已处理任务数、成功任务数和失败任务数。") + public ApiResponse dashboard( + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId) { + return ApiResponse.success(patrolDeleteTaskService.dashboard(userId)); + } + + @GetMapping("/history") + @Operation(summary = "查询任务记录", description = "返回当前用户在巡店删除模块中的当前任务和历史任务记录。") + public ApiResponse history( + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId) { + return ApiResponse.success(patrolDeleteTaskService.listHistory(userId)); + } + + @PostMapping("/tasks/progress/batch") + @Operation(summary = "批量查询巡店删除任务进度", description = "仅返回任务状态和店铺结果摘要,用于前端轮询降载。") + public ApiResponse taskProgressBatch(@Valid @RequestBody PatrolDeleteTaskBatchRequest request) { + return ApiResponse.success(patrolDeleteTaskService.getTaskProgressBatch(request.getTaskIds())); + } + + @PostMapping("/tasks") + @Operation(summary = "创建巡店删除任务", description = "根据已匹配的店铺创建任务和占位结果记录,后续由 Python 端处理并回传结果。") + public ApiResponse createTask( + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "创建任务请求。items 中每一项代表一个待处理店铺,需携带默认模板结构。", + required = true, + content = @Content( + mediaType = "application/json", + schema = @Schema(implementation = PatrolDeleteCreateTaskRequest.class), + examples = @ExampleObject( + name = "创建任务示例", + value = """ + { + "user_id": 1, + "items": [ + { + "shopName": "郭亚芳", + "matched": true, + "shopId": "27730548558377", + "platform": "亚马逊", + "companyName": "示例公司", + "matchStatus": "MATCHED", + "matchMessage": "索引已命中", + "countrySections": [ + { + "country": "德国", + "rows": [ + { + "status": "全部", + "quantity": "", + "deleteQuantity": "", + "processStatus": "" + } + ] + } + ], + "cartRatios": [ + { + "country": "德国", + "ratio": "" + } + ] + } + ] + } + """))) + @Valid @RequestBody PatrolDeleteCreateTaskRequest request) { + return ApiResponse.success(patrolDeleteTaskService.createTask(request)); + } + + @PostMapping("/tasks/{taskId}/result") + @Operation( + summary = "提交巡店删除结果", + description = "供 Python 端回传处理结果。支持按店铺分片多次提交,服务端会合并分片、按店铺完成状态自动收尾,并在任务结束后生成 Excel。") + public ApiResponse submitResult( + @Parameter(description = "任务主键,必须是运行中的任务", example = "3089") + @PathVariable Long taskId, + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "巡店删除结果回传请求。shops 表示本次提交的店铺结果分片列表。shopDone=true 表示该店铺已全部提交完成。", + required = true, + content = @Content( + mediaType = "application/json", + schema = @Schema(implementation = PatrolDeleteSubmitResultRequest.class), + examples = { + @ExampleObject( + name = "成功分片示例", + summary = "一个店铺分片回传,包含状态数据和购物车比例数据", + value = """ + { + "shops": [ + { + "shopName": "郭亚芳", + "submissionId": "patrol-delete:3089:郭亚芳:1711111111111", + "chunkIndex": 1, + "chunkTotal": 2, + "shopDone": false, + "countrySections": [ + { + "country": "德国", + "rows": [ + { + "status": "正常", + "quantity": "12", + "deleteQuantity": "3", + "processStatus": "处理中" + }, + { + "status": "下架", + "quantity": "2", + "deleteQuantity": "2", + "processStatus": "已完成" + } + ] + }, + { + "country": "英国", + "rows": [ + { + "status": "正常", + "quantity": "5", + "deleteQuantity": "1", + "processStatus": "处理中" + } + ] + } + ], + "cartRatios": [ + { + "country": "德国", + "ratio": "25%" + }, + { + "country": "英国", + "ratio": "18%" + } + ] + } + ] + } + """), + @ExampleObject( + name = "店铺完成示例", + summary = "最后一片提交完成后,将 shopDone 置为 true", + value = """ + { + "shops": [ + { + "shopName": "郭亚芳", + "submissionId": "patrol-delete:3089:郭亚芳:1711111111111", + "chunkIndex": 2, + "chunkTotal": 2, + "shopDone": true, + "countrySections": [ + { + "country": "法国", + "rows": [ + { + "status": "正常", + "quantity": "4", + "deleteQuantity": "0", + "processStatus": "已完成" + } + ] + } + ], + "cartRatios": [ + { + "country": "法国", + "ratio": "12%" + } + ] + } + ] + } + """), + @ExampleObject( + name = "失败示例", + summary = "店铺处理失败时直接回传错误信息", + value = """ + { + "shops": [ + { + "shopName": "郭亚芳", + "shopDone": true, + "error": "紫鸟页面加载超时,未能完成删除数据采集" + } + ] + } + """) + })) + @Valid @RequestBody PatrolDeleteSubmitResultRequest request, + jakarta.servlet.http.HttpServletResponse response) { + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setContentType("application/json;charset=UTF-8"); + patrolDeleteTaskService.submitResult(taskId, request); + return ApiResponse.success(null); + } + + @GetMapping("/results/{resultId}/download") + @Operation(summary = "下载结果文件", description = "按结果记录下载服务端已生成并上传到 OSS 的巡店删除 Excel 文件。") + public void downloadResult( + @Parameter(description = "结果记录主键", example = "4599") + @PathVariable Long resultId, + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId, + jakarta.servlet.http.HttpServletResponse response) { + String url = patrolDeleteTaskService.resolveResultDownloadUrl(resultId, userId); + String filename = patrolDeleteTaskService.resolveResultDownloadFilename(resultId, userId); + if (url == null || url.isBlank()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "暂无可下载结果"); + } + try { + String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8).replace("+", "%20"); + response.setContentType("application/octet-stream"); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + filename + "\"; filename*=UTF-8''" + encodedFilename); + try (InputStream in = URI.create(url).toURL().openStream()) { + byte[] buffer = new byte[65536]; + int read; + while ((read = in.read(buffer)) != -1) { + response.getOutputStream().write(buffer, 0, read); + } + response.getOutputStream().flush(); + } + } catch (Exception ex) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "下载失败"); + } + } + + @DeleteMapping("/tasks/{taskId}") + @Operation(summary = "删除任务", description = "删除整条巡店删除任务以及该任务下关联的所有结果记录。") + public ApiResponse deleteTask( + @Parameter(description = "任务主键", example = "3089") + @PathVariable Long taskId, + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId) { + patrolDeleteTaskService.deleteTask(taskId, userId); + return ApiResponse.success(null); + } + + @DeleteMapping("/history/{resultId}") + @Operation(summary = "删除单条历史记录", description = "删除一条巡店删除结果记录,并同步重算其所属任务状态。") + public ApiResponse deleteHistory( + @Parameter(description = "结果记录主键", example = "4599") + @PathVariable Long resultId, + @Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1") + @RequestParam("user_id") Long userId) { + patrolDeleteTaskService.deleteHistory(resultId, userId); + return ApiResponse.success(null); + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/mapper/PatrolDeleteShopCandidateMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/mapper/PatrolDeleteShopCandidateMapper.java new file mode 100644 index 0000000..e919a0c --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/mapper/PatrolDeleteShopCandidateMapper.java @@ -0,0 +1,9 @@ +package com.nanri.aiimage.modules.patroldelete.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.nanri.aiimage.modules.patroldelete.model.entity.PatrolDeleteShopCandidateEntity; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface PatrolDeleteShopCandidateMapper extends BaseMapper { +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCartRatioDto.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCartRatioDto.java new file mode 100644 index 0000000..fdc6ab9 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCartRatioDto.java @@ -0,0 +1,15 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +@Schema(description = "购物车比例数据") +public class PatrolDeleteCartRatioDto { + + @Schema(description = "国家名称", example = "德国") + private String country; + + @Schema(description = "购物车比例", example = "25%") + private String ratio; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCountryMetricRowDto.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCountryMetricRowDto.java new file mode 100644 index 0000000..07a65d9 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCountryMetricRowDto.java @@ -0,0 +1,24 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +@Schema(description = "国家状态数据中的单行记录") +public class PatrolDeleteCountryMetricRowDto { + + @Schema(description = "商品状态", example = "正常") + private String status; + + @Schema(description = "该状态下的商品数量", example = "12") + private String quantity; + + @JsonProperty("deleteQuantity") + @Schema(description = "已删除数量", example = "3") + private String deleteQuantity; + + @JsonProperty("processStatus") + @Schema(description = "处理结果或处理状态", example = "处理中") + private String processStatus; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCountrySectionDto.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCountrySectionDto.java new file mode 100644 index 0000000..c9ccc16 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCountrySectionDto.java @@ -0,0 +1,18 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +@Schema(description = "单个国家的状态数据分组") +public class PatrolDeleteCountrySectionDto { + + @Schema(description = "国家名称", example = "德国") + private String country; + + @Schema(description = "该国家下的多行状态数据") + private List rows = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCreateTaskRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCreateTaskRequest.java new file mode 100644 index 0000000..94a67dd --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteCreateTaskRequest.java @@ -0,0 +1,26 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +@Schema(description = "创建巡店删除任务请求") +public class PatrolDeleteCreateTaskRequest { + + @NotNull + @JsonProperty("user_id") + @Schema(description = "当前用户 ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED) + private Long userId; + + @Valid + @NotEmpty + @Schema(description = "待创建的店铺任务列表,通常由已匹配成功的店铺组成", requiredMode = Schema.RequiredMode.REQUIRED) + private List items = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteShopPayloadDto.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteShopPayloadDto.java new file mode 100644 index 0000000..da55853 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteShopPayloadDto.java @@ -0,0 +1,44 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +@Schema(description = "单个店铺的巡店删除结果分片") +public class PatrolDeleteShopPayloadDto { + + @JsonProperty("shopName") + @Schema(description = "店铺名称,服务端按店铺名称聚合分片", example = "郭亚芳") + private String shopName; + + @Schema(description = "店铺处理失败时的错误信息;有值时该店铺会直接标记失败", example = "紫鸟页面加载超时,未能完成删除数据采集") + private String error; + + @JsonProperty("countrySections") + @Schema(description = "各国家的状态数据列表。同一国家可分多片提交,服务端会按顺序合并") + private List countrySections = new ArrayList<>(); + + @JsonProperty("cartRatios") + @Schema(description = "购物车比例数据列表,通常只在店铺首行展示") + private List cartRatios = new ArrayList<>(); + + @JsonProperty("shopDone") + @Schema(description = "该店铺是否已全部提交完成。最后一片应传 true", example = "true") + private Boolean shopDone; + + @JsonProperty("submissionId") + @Schema(description = "本次店铺提交批次标识,便于问题排查与日志跟踪", example = "patrol-delete:3089:郭亚芳:1711111111111") + private String submissionId; + + @JsonProperty("chunkIndex") + @Schema(description = "当前分片序号,建议从 1 开始", example = "1") + private Integer chunkIndex; + + @JsonProperty("chunkTotal") + @Schema(description = "当前店铺总分片数", example = "2") + private Integer chunkTotal; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteSubmitResultRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteSubmitResultRequest.java new file mode 100644 index 0000000..06d184e --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteSubmitResultRequest.java @@ -0,0 +1,19 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +@Schema(description = "巡店删除结果回传请求") +public class PatrolDeleteSubmitResultRequest { + + @Valid + @NotEmpty + @Schema(description = "本次提交的店铺结果分片列表,支持一次提交多个店铺", requiredMode = Schema.RequiredMode.REQUIRED) + private List shops = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteTaskBatchRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteTaskBatchRequest.java new file mode 100644 index 0000000..dd775b8 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteTaskBatchRequest.java @@ -0,0 +1,14 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class PatrolDeleteTaskBatchRequest { + + @NotEmpty(message = "taskIds 不能为空") + private List taskIds = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteTaskItemDto.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteTaskItemDto.java new file mode 100644 index 0000000..01c3606 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/dto/PatrolDeleteTaskItemDto.java @@ -0,0 +1,50 @@ +package com.nanri.aiimage.modules.patroldelete.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.Valid; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +@Schema(description = "巡店删除任务中的单个店铺项") +public class PatrolDeleteTaskItemDto { + + @JsonProperty("shopName") + @Schema(description = "店铺名称", example = "郭亚芳") + private String shopName; + + @Schema(description = "该店铺是否已完成紫鸟匹配,创建任务时必须为 true", example = "true") + private boolean matched; + + @JsonProperty("shopId") + @Schema(description = "匹配到的紫鸟店铺 ID", example = "27730548558377") + private String shopId; + + @Schema(description = "店铺所属平台", example = "亚马逊") + private String platform; + + @JsonProperty("companyName") + @Schema(description = "店铺所属公司名称", example = "示例公司") + private String companyName; + + @JsonProperty("matchStatus") + @Schema(description = "匹配状态,如 MATCHED、PENDING、CONFLICT、INDEX_STALE", example = "MATCHED") + private String matchStatus; + + @JsonProperty("matchMessage") + @Schema(description = "匹配状态说明", example = "索引已命中") + private String matchMessage; + + @Valid + @JsonProperty("countrySections") + @Schema(description = "默认模板中的国家状态数据结构") + private List countrySections = new ArrayList<>(); + + @Valid + @JsonProperty("cartRatios") + @Schema(description = "默认模板中的购物车比例结构") + private List cartRatios = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/entity/PatrolDeleteShopCandidateEntity.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/entity/PatrolDeleteShopCandidateEntity.java new file mode 100644 index 0000000..e4595f1 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/entity/PatrolDeleteShopCandidateEntity.java @@ -0,0 +1,19 @@ +package com.nanri.aiimage.modules.patroldelete.model.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("biz_patrol_delete_shop_candidate") +public class PatrolDeleteShopCandidateEntity { + + @TableId(type = IdType.AUTO) + private Long id; + private Long userId; + private String shopName; + private LocalDateTime createdAt; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteCreateTaskVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteCreateTaskVo.java new file mode 100644 index 0000000..957dff9 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteCreateTaskVo.java @@ -0,0 +1,13 @@ +package com.nanri.aiimage.modules.patroldelete.model.vo; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class PatrolDeleteCreateTaskVo { + + private Long taskId; + private List items = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteHistoryVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteHistoryVo.java new file mode 100644 index 0000000..3c7b6d6 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteHistoryVo.java @@ -0,0 +1,12 @@ +package com.nanri.aiimage.modules.patroldelete.model.vo; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class PatrolDeleteHistoryVo { + + private List items = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteResultItemVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteResultItemVo.java new file mode 100644 index 0000000..9b827e2 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteResultItemVo.java @@ -0,0 +1,52 @@ +package com.nanri.aiimage.modules.patroldelete.model.vo; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCartRatioDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountrySectionDto; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@Data +public class PatrolDeleteResultItemVo { + + private Long resultId; + private Long taskId; + + @JsonProperty("shopName") + private String shopName; + + @JsonProperty("shopId") + private String shopId; + + private String platform; + + @JsonProperty("companyName") + private String companyName; + + private boolean matched; + + @JsonProperty("matchStatus") + private String matchStatus; + + @JsonProperty("matchMessage") + private String matchMessage; + + @JsonProperty("taskStatus") + private String taskStatus; + + private Boolean success; + private String error; + private LocalDateTime createdAt; + private LocalDateTime finishedAt; + private String outputFilename; + private String downloadUrl; + + @JsonProperty("countrySections") + private List countrySections = new ArrayList<>(); + + @JsonProperty("cartRatios") + private List cartRatios = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteTaskBatchVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteTaskBatchVo.java new file mode 100644 index 0000000..6fd1802 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/model/vo/PatrolDeleteTaskBatchVo.java @@ -0,0 +1,14 @@ +package com.nanri.aiimage.modules.patroldelete.model.vo; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class PatrolDeleteTaskBatchVo { + + private List items = new ArrayList<>(); + + private List missingTaskIds = new ArrayList<>(); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteExcelAssemblyService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteExcelAssemblyService.java new file mode 100644 index 0000000..90313e6 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteExcelAssemblyService.java @@ -0,0 +1,144 @@ +package com.nanri.aiimage.modules.patroldelete.service; + +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCartRatioDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountryMetricRowDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountrySectionDto; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteResultItemVo; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.List; + +@Service +@Slf4j +public class PatrolDeleteExcelAssemblyService { + + private static final String COUNTRY_DE = "德国"; + private static final String COUNTRY_UK = "英国"; + private static final String COUNTRY_FR = "法国"; + private static final String COUNTRY_IT = "意大利"; + private static final String COUNTRY_ES = "西班牙"; + + private static final String[] HEADER = { + "店铺名", + COUNTRY_DE, "数量", "删除数量", "删除结果", + COUNTRY_UK, "数量", "删除数量", "删除结果", + COUNTRY_FR, "数量", "删除数量", "删除结果", + COUNTRY_IT, "数量", "删除数量", "删除结果", + COUNTRY_ES, "数量", "删除数量", "删除结果", + "国家", "购物车比例", + "国家", "购物车比例", + "国家", "购物车比例", + "国家", "购物车比例", + "国家", "购物车比例" + }; + + public void writeWorkbook(File outputXlsx, List items) { + try (XSSFWorkbook workbook = new XSSFWorkbook(); + FileOutputStream outputStream = new FileOutputStream(outputXlsx)) { + Sheet sheet = workbook.createSheet("巡店删除结果"); + Row headerRow = sheet.createRow(0); + for (int columnIndex = 0; columnIndex < HEADER.length; columnIndex++) { + headerRow.createCell(columnIndex).setCellValue(HEADER[columnIndex]); + } + + int rowIndex = 1; + for (PatrolDeleteResultItemVo item : items) { + if (item == null || Boolean.FALSE.equals(item.getSuccess())) { + continue; + } + int shopRowCount = maxCountryRowCount(item.getCountrySections()); + for (int shopRowIndex = 0; shopRowIndex < shopRowCount; shopRowIndex++) { + Row row = sheet.createRow(rowIndex++); + row.createCell(0).setCellValue(shopRowIndex == 0 ? safe(item.getShopName()) : ""); + writeCountryColumns(row, 1, findSection(item.getCountrySections(), COUNTRY_DE), shopRowIndex); + writeCountryColumns(row, 5, findSection(item.getCountrySections(), COUNTRY_UK), shopRowIndex); + writeCountryColumns(row, 9, findSection(item.getCountrySections(), COUNTRY_FR), shopRowIndex); + writeCountryColumns(row, 13, findSection(item.getCountrySections(), COUNTRY_IT), shopRowIndex); + writeCountryColumns(row, 17, findSection(item.getCountrySections(), COUNTRY_ES), shopRowIndex); + if (shopRowIndex == 0) { + writeCartRatioColumns(row, 21, item.getCartRatios(), 0); + writeCartRatioColumns(row, 23, item.getCartRatios(), 1); + writeCartRatioColumns(row, 25, item.getCartRatios(), 2); + writeCartRatioColumns(row, 27, item.getCartRatios(), 3); + writeCartRatioColumns(row, 29, item.getCartRatios(), 4); + } + } + rowIndex++; + } + + for (int columnIndex = 0; columnIndex < HEADER.length; columnIndex++) { + sheet.autoSizeColumn(columnIndex); + } + workbook.write(outputStream); + } catch (Exception ex) { + log.warn("[patrol-delete] write workbook failed: {}", ex.getMessage()); + throw new BusinessException("生成巡店删除结果 Excel 失败: " + ex.getMessage()); + } + } + + public int countRows(List items) { + int count = 0; + if (items == null) { + return 0; + } + for (PatrolDeleteResultItemVo item : items) { + if (item != null && !Boolean.FALSE.equals(item.getSuccess())) { + count += maxCountryRowCount(item.getCountrySections()); + } + } + return count; + } + + private int maxCountryRowCount(List sections) { + int max = 1; + if (sections == null) { + return max; + } + for (PatrolDeleteCountrySectionDto section : sections) { + if (section != null && section.getRows() != null && section.getRows().size() > max) { + max = section.getRows().size(); + } + } + return max; + } + + private PatrolDeleteCountrySectionDto findSection(List sections, String country) { + if (sections == null) { + return null; + } + for (PatrolDeleteCountrySectionDto section : sections) { + if (section != null && country.equals(section.getCountry())) { + return section; + } + } + return null; + } + + private void writeCountryColumns(Row row, int startColumn, PatrolDeleteCountrySectionDto section, int rowIndex) { + PatrolDeleteCountryMetricRowDto metric = null; + if (section != null && section.getRows() != null && rowIndex < section.getRows().size()) { + metric = section.getRows().get(rowIndex); + } + row.createCell(startColumn).setCellValue(metric == null ? "" : safe(metric.getStatus())); + row.createCell(startColumn + 1).setCellValue(metric == null ? "" : safe(metric.getQuantity())); + row.createCell(startColumn + 2).setCellValue(metric == null ? "" : safe(metric.getDeleteQuantity())); + row.createCell(startColumn + 3).setCellValue(metric == null ? "" : safe(metric.getProcessStatus())); + } + + private void writeCartRatioColumns(Row row, int startColumn, List ratios, int ratioIndex) { + PatrolDeleteCartRatioDto ratio = ratios != null && ratioIndex < ratios.size() ? ratios.get(ratioIndex) : null; + row.createCell(startColumn).setCellValue(ratio == null ? "" : safe(ratio.getCountry())); + row.createCell(startColumn + 1).setCellValue(ratio == null ? "" : safe(ratio.getRatio())); + } + + private String safe(String value) { + return value == null ? "" : value; + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteResolveService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteResolveService.java new file mode 100644 index 0000000..4a2138f --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteResolveService.java @@ -0,0 +1,166 @@ +package com.nanri.aiimage.modules.patroldelete.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.modules.patroldelete.mapper.PatrolDeleteShopCandidateMapper; +import com.nanri.aiimage.modules.patroldelete.model.entity.PatrolDeleteShopCandidateEntity; +import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; +import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo; +import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Objects; + +@Service +@RequiredArgsConstructor +public class PatrolDeleteResolveService { + + private final PatrolDeleteShopCandidateMapper candidateMapper; + private final ZiniaoShopSwitchService ziniaoShopSwitchService; + + public List listCandidates(Long userId) { + validateUserId(userId); + List rows = candidateMapper.selectList( + new LambdaQueryWrapper() + .eq(PatrolDeleteShopCandidateEntity::getUserId, userId) + .orderByDesc(PatrolDeleteShopCandidateEntity::getId)); + List list = new ArrayList<>(); + for (PatrolDeleteShopCandidateEntity row : rows) { + ProductRiskCandidateVo vo = new ProductRiskCandidateVo(); + vo.setId(row.getId()); + vo.setShopName(row.getShopName()); + vo.setCreatedAt(row.getCreatedAt()); + list.add(vo); + } + return list; + } + + @Transactional + public ProductRiskCandidateVo addCandidate(ProductRiskCandidateAddRequest request) { + validateUserId(request.getUserId()); + String normalized = ziniaoShopSwitchService.normalizeShopName(request.getShopName()); + if (normalized.isBlank()) { + throw new BusinessException("店铺名不能为空"); + } + ZiniaoShopMatchResultVo indexHit = ziniaoShopSwitchService.findIndexedStoreByName(normalized, false); + if (indexHit == null || !indexHit.isMatched()) { + String hint = indexHit != null && indexHit.getMatchMessage() != null && !indexHit.getMatchMessage().isBlank() + ? indexHit.getMatchMessage() + : "店铺索引未命中,无法加入备选"; + throw new BusinessException(hint); + } + if (ZiniaoShopIndexService.MATCH_STATUS_CONFLICT.equals(indexHit.getMatchStatus())) { + throw new BusinessException(indexHit.getMatchMessage() != null ? indexHit.getMatchMessage() : "存在多个同名店铺,请人工确认"); + } + PatrolDeleteShopCandidateEntity existing = candidateMapper.selectOne( + new LambdaQueryWrapper() + .eq(PatrolDeleteShopCandidateEntity::getUserId, request.getUserId()) + .eq(PatrolDeleteShopCandidateEntity::getShopName, normalized) + .last("limit 1")); + if (existing != null) { + ProductRiskCandidateVo vo = new ProductRiskCandidateVo(); + vo.setId(existing.getId()); + vo.setShopName(existing.getShopName()); + vo.setCreatedAt(existing.getCreatedAt()); + return vo; + } + PatrolDeleteShopCandidateEntity entity = new PatrolDeleteShopCandidateEntity(); + entity.setUserId(request.getUserId()); + entity.setShopName(normalized); + entity.setCreatedAt(LocalDateTime.now()); + candidateMapper.insert(entity); + + ProductRiskCandidateVo vo = new ProductRiskCandidateVo(); + vo.setId(entity.getId()); + vo.setShopName(entity.getShopName()); + vo.setCreatedAt(entity.getCreatedAt()); + return vo; + } + + @Transactional + public void deleteCandidate(Long userId, Long id) { + validateUserId(userId); + if (id == null || id <= 0) { + throw new BusinessException("id 不合法"); + } + PatrolDeleteShopCandidateEntity row = candidateMapper.selectById(id); + if (row == null || !userId.equals(row.getUserId())) { + throw new BusinessException("记录不存在"); + } + candidateMapper.deleteById(id); + } + + public ProductRiskMatchShopsVo matchShops(ProductRiskMatchShopsRequest request) { + validateUserId(request.getUserId()); + LinkedHashSet ordered = new LinkedHashSet<>(); + for (String raw : request.getShopNames()) { + String normalized = ziniaoShopSwitchService.normalizeShopName(raw); + if (!normalized.isBlank()) { + ordered.add(normalized); + } + } + if (ordered.isEmpty()) { + throw new BusinessException("shop_names 无有效店铺名"); + } + ProductRiskMatchShopsVo vo = new ProductRiskMatchShopsVo(); + for (String shopName : ordered) { + vo.getItems().add(matchOneShop(shopName)); + } + return vo; + } + + public long countCandidates(Long userId) { + validateUserId(userId); + Long count = candidateMapper.selectCount(new LambdaQueryWrapper() + .eq(PatrolDeleteShopCandidateEntity::getUserId, userId)); + return count == null ? 0L : count; + } + + private ProductRiskShopQueueItemVo matchOneShop(String shopName) { + ProductRiskShopQueueItemVo item = new ProductRiskShopQueueItemVo(); + item.setShopName(shopName); + try { + ZiniaoShopMatchResultVo matched = ziniaoShopSwitchService.findIndexedStoreByName(shopName, false); + if (matched == null) { + item.setMatched(false); + item.setMatchStatus("PENDING"); + item.setMatchMessage("匹配结果为空"); + return item; + } + item.setMatched(matched.isMatched()); + item.setShopId(matched.getShopId()); + item.setPlatform(matched.getPlatform()); + item.setCompanyName(matched.getCompanyName()); + item.setMatchedUserId(matched.getMatchedUserId()); + item.setMatchStatus(matched.getMatchStatus()); + item.setMatchMessage(matched.getMatchMessage()); + item.setOpenStoreUrl(matched.getOpenStoreUrl()); + } catch (BusinessException ex) { + item.setMatched(false); + item.setMatchStatus("PENDING"); + item.setMatchMessage(ex.getMessage()); + } catch (Exception ex) { + item.setMatched(false); + item.setMatchStatus("PENDING"); + item.setMatchMessage(Objects.toString(ex.getMessage(), "匹配异常")); + } + return item; + } + + private void validateUserId(Long userId) { + if (userId == null || userId <= 0) { + throw new BusinessException("user_id 不合法"); + } + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteTaskCacheService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteTaskCacheService.java new file mode 100644 index 0000000..c48d0ee --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteTaskCacheService.java @@ -0,0 +1,211 @@ +package com.nanri.aiimage.modules.patroldelete.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteShopPayloadDto; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@Service +@RequiredArgsConstructor +public class PatrolDeleteTaskCacheService { + + private static final long PAYLOAD_TTL_HOURS = 24; + private final StringRedisTemplate stringRedisTemplate; + private final ObjectMapper objectMapper; + private final TaskPressureProperties taskPressureProperties; + private final ConcurrentHashMap taskEntityLocalCache = new ConcurrentHashMap<>(); + + public PatrolDeleteShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) { + if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank()) { + return null; + } + Object raw = stringRedisTemplate.opsForHash().get(buildShopPayloadKey(taskId), shopKey); + if (!(raw instanceof String json) || json.isBlank()) { + return null; + } + try { + return objectMapper.readValue(json, PatrolDeleteShopPayloadDto.class); + } catch (Exception ex) { + throw new BusinessException("读取巡店删除店铺缓存失败"); + } + } + + public void saveShopMergedPayload(Long taskId, String shopKey, PatrolDeleteShopPayloadDto payload) { + if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) { + return; + } + try { + stringRedisTemplate.opsForHash().put(buildShopPayloadKey(taskId), shopKey, objectMapper.writeValueAsString(payload)); + stringRedisTemplate.expire(buildShopPayloadKey(taskId), Duration.ofHours(PAYLOAD_TTL_HOURS)); + touchTaskHeartbeat(taskId); + } catch (Exception ex) { + throw new BusinessException("暂存巡店删除店铺缓存失败"); + } + } + + public void removeShopMergedPayload(Long taskId, String shopKey) { + if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank()) { + return; + } + stringRedisTemplate.opsForHash().delete(buildShopPayloadKey(taskId), shopKey); + } + + public Map getAllShopMergedPayload(Long taskId) { + try { + Map raw = stringRedisTemplate.opsForHash().entries(buildShopPayloadKey(taskId)); + if (raw == null || raw.isEmpty()) { + return Map.of(); + } + Map out = new LinkedHashMap<>(); + for (Map.Entry entry : raw.entrySet()) { + if (!(entry.getKey() instanceof String key) || !(entry.getValue() instanceof String val) || val.isBlank()) { + continue; + } + out.put(key, objectMapper.readValue(val, PatrolDeleteShopPayloadDto.class)); + } + return out; + } catch (Exception ex) { + throw new BusinessException("读取巡店删除缓存失败"); + } + } + + public boolean hasAnyShopMergedPayload(Long taskId) { + if (taskId == null || taskId <= 0) { + return false; + } + Long size = stringRedisTemplate.opsForHash().size(buildShopPayloadKey(taskId)); + return size != null && size > 0; + } + + public long countShopMergedPayload(Long taskId) { + if (taskId == null || taskId <= 0) { + return 0L; + } + Long size = stringRedisTemplate.opsForHash().size(buildShopPayloadKey(taskId)); + return size == null ? 0L : size; + } + + public long getTaskHeartbeatMillis(Long taskId) { + if (taskId == null || taskId <= 0) { + return 0L; + } + String raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId)); + if (raw == null || raw.isBlank()) { + return 0L; + } + try { + return Long.parseLong(raw); + } catch (NumberFormatException ignored) { + return 0L; + } + } + + public void touchTaskHeartbeat(Long taskId) { + stringRedisTemplate.opsForValue().set( + buildTaskHeartbeatKey(taskId), + String.valueOf(Instant.now().toEpochMilli()), + Duration.ofHours(PAYLOAD_TTL_HOURS)); + } + + public void deleteTaskCache(Long taskId) { + if (taskId == null || taskId <= 0) { + return; + } + taskEntityLocalCache.remove(taskId); + stringRedisTemplate.delete(buildShopPayloadKey(taskId)); + stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId)); + stringRedisTemplate.delete(buildTaskEntityKey(taskId)); + } + + public void saveTaskCache(FileTaskEntity task) { + if (task == null || task.getId() == null) { + return; + } + long now = System.currentTimeMillis(); + taskEntityLocalCache.put(task.getId(), new LocalTaskEntityCacheEntry( + now, + objectMapper.convertValue(task, FileTaskEntity.class) + )); + try { + stringRedisTemplate.opsForValue().set( + buildTaskEntityKey(task.getId()), + objectMapper.writeValueAsString(task), + Duration.ofHours(PAYLOAD_TTL_HOURS) + ); + } catch (Exception ignored) { + } + } + + public Map getTaskCacheBatch(java.util.List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + java.util.List normalized = taskIds.stream() + .filter(id -> id != null && id > 0) + .distinct() + .toList(); + if (normalized.isEmpty()) { + return result; + } + long now = System.currentTimeMillis(); + java.util.List missingIds = new ArrayList<>(); + for (Long taskId : normalized) { + LocalTaskEntityCacheEntry cached = taskEntityLocalCache.get(taskId); + if (isLocalCacheFresh(cached, now)) { + result.put(taskId, objectMapper.convertValue(cached.task(), FileTaskEntity.class)); + } else { + missingIds.add(taskId); + } + } + if (missingIds.isEmpty()) { + return result; + } + java.util.List keys = missingIds.stream().map(this::buildTaskEntityKey).toList(); + java.util.List values = stringRedisTemplate.opsForValue().multiGet(keys); + for (int i = 0; i < missingIds.size(); i++) { + Long taskId = missingIds.get(i); + String val = values != null && i < values.size() ? values.get(i) : null; + if (val == null || val.isBlank()) { + continue; + } + try { + FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class); + result.put(taskId, task); + taskEntityLocalCache.put(taskId, new LocalTaskEntityCacheEntry(now, task)); + } catch (Exception ignored) { + } + } + return result; + } + + private String buildShopPayloadKey(Long taskId) { + return "patrol-delete:task:shop-payload:" + taskId; + } + + private String buildTaskHeartbeatKey(Long taskId) { + return "patrol-delete:task:heartbeat:" + taskId; + } + + private String buildTaskEntityKey(Long taskId) { + return "patrol-delete:task:entity:" + taskId; + } + + private boolean isLocalCacheFresh(LocalTaskEntityCacheEntry cached, long now) { + return cached != null + && now - cached.cachedAtMillis() <= Math.max(0L, taskPressureProperties.getLocalTaskEntityCacheMillis()); + } + + private record LocalTaskEntityCacheEntry(long cachedAtMillis, FileTaskEntity task) {} +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteTaskService.java new file mode 100644 index 0000000..ffd31a8 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/patroldelete/service/PatrolDeleteTaskService.java @@ -0,0 +1,883 @@ +package com.nanri.aiimage.modules.patroldelete.service; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.IdUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCartRatioDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountryMetricRowDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountrySectionDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCreateTaskRequest; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteShopPayloadDto; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteSubmitResultRequest; +import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteTaskItemDto; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteTaskBatchVo; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteCreateTaskVo; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteHistoryVo; +import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteResultItemVo; +import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo; +import com.nanri.aiimage.modules.task.mapper.FileResultMapper; +import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; +import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.File; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +@Service +@RequiredArgsConstructor +@Slf4j +public class PatrolDeleteTaskService { + + private static final String MODULE_TYPE = "PATROL_DELETE"; + private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + private static final String INTERRUPTED_MESSAGE = "Python 在该店铺结果提交完成前中断"; + + private final FileTaskMapper fileTaskMapper; + private final FileResultMapper fileResultMapper; + private final PatrolDeleteResolveService patrolDeleteResolveService; + private final PatrolDeleteExcelAssemblyService excelAssemblyService; + private final PatrolDeleteTaskCacheService taskCacheService; + private final OssStorageService ossStorageService; + private final ZiniaoShopSwitchService ziniaoShopSwitchService; + private final ObjectMapper objectMapper; + private final TaskPressureProperties taskPressureProperties; + + private FileTaskEntity loadTaskForExecution(Long taskId) { + Map cachedTasks = taskCacheService.getTaskCacheBatch(List.of(taskId)); + FileTaskEntity cached = cachedTasks.get(taskId); + if (cached != null) { + return cached; + } + FileTaskEntity dbTask = fileTaskMapper.selectById(taskId); + if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) { + taskCacheService.saveTaskCache(dbTask); + } + return dbTask; + } + + private Map loadTaskMapByIds(List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + List normalizedTaskIds = taskIds.stream() + .filter(taskId -> taskId != null && taskId > 0) + .distinct() + .toList(); + if (normalizedTaskIds.isEmpty()) { + return result; + } + Map cachedTasks = taskCacheService.getTaskCacheBatch(normalizedTaskIds); + result.putAll(cachedTasks); + List missingTaskIds = normalizedTaskIds.stream() + .filter(taskId -> !cachedTasks.containsKey(taskId)) + .toList(); + if (!missingTaskIds.isEmpty()) { + for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) { + if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) { + continue; + } + result.put(dbTask.getId(), dbTask); + if ("RUNNING".equals(dbTask.getStatus())) { + taskCacheService.saveTaskCache(dbTask); + } + } + } + return result; + } + + private List selectTasksByIdsInBatches(List taskIds) { + List tasks = new ArrayList<>(); + if (taskIds == null || taskIds.isEmpty()) { + return tasks; + } + int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize()); + for (int start = 0; start < taskIds.size(); start += batchSize) { + int end = Math.min(start + batchSize, taskIds.size()); + tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end))); + } + return tasks; + } + + public ProductRiskDashboardVo dashboard(Long userId) { + validateUserId(userId); + ProductRiskDashboardVo vo = new ProductRiskDashboardVo(); + vo.setCandidateCount(patrolDeleteResolveService.countCandidates(userId)); + vo.setProcessedTaskCount(countTasks(userId, List.of("SUCCESS", "FAILED"))); + vo.setSuccessTaskCount(countTasks(userId, List.of("SUCCESS"))); + vo.setFailedTaskCount(countTasks(userId, List.of("FAILED"))); + return vo; + } + + public PatrolDeleteHistoryVo listHistory(Long userId) { + validateUserId(userId); + PatrolDeleteHistoryVo vo = new PatrolDeleteHistoryVo(); + List entities = fileResultMapper.selectList(new LambdaQueryWrapper() + .eq(FileResultEntity::getModuleType, MODULE_TYPE) + .eq(FileResultEntity::getUserId, userId) + .orderByDesc(FileResultEntity::getCreatedAt) + .last("limit 100")); + if (entities.isEmpty()) { + vo.setItems(List.of()); + return vo; + } + + Map taskMap = loadTaskMap(entities); + Map> snapshotMap = buildSnapshotMap(taskMap); + List items = new ArrayList<>(); + for (FileResultEntity entity : entities) { + FileTaskEntity task = taskMap.get(entity.getTaskId()); + PatrolDeleteResultItemVo snapshot = snapshotMap.getOrDefault(entity.getTaskId(), Map.of()).get(entity.getId()); + items.add(toHistoryItem(entity, task, snapshot)); + } + vo.setItems(items); + return vo; + } + + public PatrolDeleteTaskBatchVo getTaskProgressBatch(List taskIds) { + PatrolDeleteTaskBatchVo batch = new PatrolDeleteTaskBatchVo(); + if (taskIds == null || taskIds.isEmpty()) { + return batch; + } + + List normalizedTaskIds = taskIds.stream() + .filter(taskId -> taskId != null && taskId > 0) + .distinct() + .limit(50) + .toList(); + if (normalizedTaskIds.isEmpty()) { + return batch; + } + + Map taskMap = loadTaskMapByIds(normalizedTaskIds); + List rows = fileResultMapper.selectList(new LambdaQueryWrapper() + .eq(FileResultEntity::getModuleType, MODULE_TYPE) + .in(FileResultEntity::getTaskId, normalizedTaskIds) + .orderByAsc(FileResultEntity::getId)); + Map> rowsByTaskId = new LinkedHashMap<>(); + for (FileResultEntity row : rows) { + rowsByTaskId.computeIfAbsent(row.getTaskId(), ignored -> new ArrayList<>()).add(row); + } + + for (Long taskId : normalizedTaskIds) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { + batch.getMissingTaskIds().add(taskId); + continue; + } + List taskRows = rowsByTaskId.get(taskId); + if (taskRows == null || taskRows.isEmpty()) { + batch.getMissingTaskIds().add(taskId); + continue; + } + List snapshots = buildSnapshotFromDb(task, taskRows); + batch.getItems().addAll(snapshots); + } + return batch; + } + + public String resolveResultDownloadUrl(Long resultId, Long userId) { + FileResultEntity entity = fileResultMapper.selectById(resultId); + if (entity == null || !MODULE_TYPE.equals(entity.getModuleType()) || !userId.equals(entity.getUserId())) { + throw new BusinessException("记录不存在"); + } + if (blank(entity.getResultFileUrl())) { + throw new BusinessException("暂无可下载文件"); + } + return ossStorageService.generateFreshDownloadUrl(entity.getResultFileUrl()); + } + + public String resolveResultDownloadFilename(Long resultId, Long userId) { + FileResultEntity entity = fileResultMapper.selectById(resultId); + if (entity == null || !MODULE_TYPE.equals(entity.getModuleType()) || !userId.equals(entity.getUserId())) { + throw new BusinessException("记录不存在"); + } + return !blank(entity.getResultFilename()) + ? entity.getResultFilename() + : safeFileStem(entity.getSourceFilename()) + ".xlsx"; + } + + @Transactional + public PatrolDeleteCreateTaskVo createTask(PatrolDeleteCreateTaskRequest request) { + validateUserId(request.getUserId()); + if (request.getItems() == null || request.getItems().isEmpty()) { + throw new BusinessException("items 不能为空"); + } + + List uniqueItems = dedupeItems(request.getItems()); + if (uniqueItems.isEmpty()) { + throw new BusinessException("items 不能为空"); + } + for (PatrolDeleteTaskItemDto item : uniqueItems) { + if (item == null || !item.isMatched()) { + throw new BusinessException("存在未匹配店铺,无法创建任务"); + } + } + + LocalDateTime now = LocalDateTime.now(); + FileTaskEntity task = new FileTaskEntity(); + task.setTaskNo(MODULE_TYPE + "-" + IdUtil.getSnowflakeNextIdStr()); + task.setModuleType(MODULE_TYPE); + task.setTaskMode("PYTHON_QUEUE"); + task.setStatus("RUNNING"); + task.setSourceFileCount(uniqueItems.size()); + task.setSuccessFileCount(0); + task.setFailedFileCount(0); + task.setCreatedBy("user:" + request.getUserId()); + task.setUserId(request.getUserId()); + task.setCreatedAt(now); + task.setUpdatedAt(now); + fileTaskMapper.insert(task); + taskCacheService.saveTaskCache(task); + taskCacheService.touchTaskHeartbeat(task.getId()); + + List snapshots = new ArrayList<>(); + for (PatrolDeleteTaskItemDto item : uniqueItems) { + String normalizedShopName = ziniaoShopSwitchService.normalizeShopName(item.getShopName()); + if (blank(normalizedShopName)) { + throw new BusinessException("任务数据已失效,请刷新后重试"); + } + FileResultEntity result = new FileResultEntity(); + result.setTaskId(task.getId()); + result.setModuleType(MODULE_TYPE); + result.setSourceFilename(normalizedShopName); + result.setSourceFileUrl(item.getShopId()); + result.setUserId(request.getUserId()); + result.setSuccess(null); + result.setCreatedAt(now); + fileResultMapper.insert(result); + snapshots.add(toSnapshotVo(result, item, task.getStatus(), null)); + } + persistTaskJson(task, uniqueItems, snapshots); + + PatrolDeleteCreateTaskVo vo = new PatrolDeleteCreateTaskVo(); + vo.setTaskId(task.getId()); + vo.setItems(snapshots); + return vo; + } + + @Transactional + public void submitResult(Long taskId, PatrolDeleteSubmitResultRequest request) { + if (taskId == null || taskId <= 0) { + throw new BusinessException("taskId 不合法"); + } + if (request == null || request.getShops() == null || request.getShops().isEmpty()) { + throw new BusinessException("shops 不能为空"); + } + + FileTaskEntity task = loadTaskForExecution(taskId); + if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + throw new BusinessException("任务不存在"); + } + if ("SUCCESS".equals(task.getStatus()) || "FAILED".equals(task.getStatus())) { + throw new BusinessException("任务已结束,拒绝重复提交"); + } + + taskCacheService.touchTaskHeartbeat(taskId); + List resultRows = listTaskRows(taskId); + List snapshots = buildSnapshotFromDb(task, resultRows); + Map snapshotByResultId = indexSnapshotByResultId(snapshots); + Map payloadByShop = normalizePayloadByShop(request.getShops()); + + for (FileResultEntity row : resultRows) { + String shopKey = row.getSourceFilename(); + PatrolDeleteShopPayloadDto incoming = payloadByShop.get(shopKey); + if (incoming == null) { + continue; + } + PatrolDeleteShopPayloadDto mergedPayload = mergeShopPayload(taskId, shopKey, incoming); + PatrolDeleteResultItemVo snapshot = snapshotByResultId.get(row.getId()); + mergePayloadIntoSnapshot(snapshot, mergedPayload); + + if (!blank(mergedPayload.getError())) { + markResultFailed(row, mergedPayload.getError()); + applyFailureToSnapshot(snapshot, mergedPayload.getError()); + taskCacheService.removeShopMergedPayload(taskId, shopKey); + continue; + } + + if (Boolean.TRUE.equals(mergedPayload.getShopDone())) { + markResultSuccess(row); + applySuccessToSnapshot(snapshot); + taskCacheService.removeShopMergedPayload(taskId, shopKey); + } + } + + persistSnapshotJson(task, snapshots); + fileTaskMapper.updateById(task); + tryFinalizeTask(taskId, false); + } + + @Transactional + public boolean tryFinalizeTask(Long taskId, boolean fromCompensation) { + if (taskId == null || taskId <= 0) { + return false; + } + FileTaskEntity task = loadTaskForExecution(taskId); + if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + return false; + } + if ("SUCCESS".equals(task.getStatus()) || "FAILED".equals(task.getStatus())) { + return true; + } + + List rows = listTaskRows(taskId); + if (rows.isEmpty()) { + return false; + } + + Map cachedPayloads = taskCacheService.getAllShopMergedPayload(taskId); + List snapshots = buildSnapshotFromDb(task, rows); + Map snapshotByResultId = indexSnapshotByResultId(snapshots); + boolean changed = false; + + for (FileResultEntity row : rows) { + if (isResultFinished(row)) { + continue; + } + PatrolDeleteResultItemVo snapshot = snapshotByResultId.get(row.getId()); + PatrolDeleteShopPayloadDto cached = cachedPayloads.get(row.getSourceFilename()); + if (cached == null) { + if (fromCompensation) { + markResultFailed(row, INTERRUPTED_MESSAGE); + applyFailureToSnapshot(snapshot, INTERRUPTED_MESSAGE); + changed = true; + } + continue; + } + + mergePayloadIntoSnapshot(snapshot, cached); + + if (!blank(cached.getError())) { + markResultFailed(row, cached.getError()); + applyFailureToSnapshot(snapshot, cached.getError()); + taskCacheService.removeShopMergedPayload(taskId, row.getSourceFilename()); + changed = true; + continue; + } + + if (Boolean.TRUE.equals(cached.getShopDone()) || (fromCompensation && hasAnyPayloadData(cached))) { + markResultSuccess(row); + applySuccessToSnapshot(snapshot); + taskCacheService.removeShopMergedPayload(taskId, row.getSourceFilename()); + changed = true; + continue; + } + + if (fromCompensation) { + markResultFailed(row, INTERRUPTED_MESSAGE); + applyFailureToSnapshot(snapshot, INTERRUPTED_MESSAGE); + taskCacheService.removeShopMergedPayload(taskId, row.getSourceFilename()); + changed = true; + } + } + + List latestRows = listTaskRows(taskId); + updateTaskStatusFromRows(task, latestRows); + if (latestRows.stream().allMatch(this::isResultFinished)) { + finalizeTaskWorkbook(task, latestRows, snapshots); + return true; + } + + persistSnapshotJson(task, snapshots); + fileTaskMapper.updateById(task); + return changed; + } + + @Transactional + public void deleteTask(Long taskId, Long userId) { + validateUserId(userId); + FileTaskEntity task = loadTaskForExecution(taskId); + if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { + throw new BusinessException("任务不存在"); + } + fileResultMapper.delete(new LambdaQueryWrapper() + .eq(FileResultEntity::getTaskId, taskId) + .eq(FileResultEntity::getModuleType, MODULE_TYPE)); + fileTaskMapper.deleteById(taskId); + taskCacheService.deleteTaskCache(taskId); + } + + @Transactional + public void deleteHistory(Long resultId, Long userId) { + validateUserId(userId); + FileResultEntity entity = fileResultMapper.selectById(resultId); + if (entity == null || !MODULE_TYPE.equals(entity.getModuleType()) || !userId.equals(entity.getUserId())) { + throw new BusinessException("记录不存在"); + } + Long taskId = entity.getTaskId(); + fileResultMapper.deleteById(resultId); + reconcileTaskAfterResultRemoval(taskId); + } + + private void reconcileTaskAfterResultRemoval(Long taskId) { + if (taskId == null || taskId <= 0) { + return; + } + FileTaskEntity task = loadTaskForExecution(taskId); + if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + return; + } + List rows = listTaskRows(taskId); + if (rows.isEmpty()) { + fileTaskMapper.deleteById(taskId); + taskCacheService.deleteTaskCache(taskId); + return; + } + updateTaskStatusFromRows(task, rows); + persistSnapshotJson(task, buildSnapshotFromDb(task, rows)); + fileTaskMapper.updateById(task); + } + + private long countTasks(Long userId, List statuses) { + Long count = fileTaskMapper.selectCount(new LambdaQueryWrapper() + .eq(FileTaskEntity::getModuleType, MODULE_TYPE) + .eq(FileTaskEntity::getUserId, userId) + .in(FileTaskEntity::getStatus, statuses)); + return count == null ? 0L : count; + } + + private List listTaskRows(Long taskId) { + return fileResultMapper.selectList(new LambdaQueryWrapper() + .eq(FileResultEntity::getTaskId, taskId) + .eq(FileResultEntity::getModuleType, MODULE_TYPE) + .orderByAsc(FileResultEntity::getId)); + } + + private Map normalizePayloadByShop(List shops) { + Map payloadByShop = new LinkedHashMap<>(); + for (PatrolDeleteShopPayloadDto item : shops) { + if (item == null) { + continue; + } + String shopKey = ziniaoShopSwitchService.normalizeShopName(item.getShopName()); + if (blank(shopKey)) { + continue; + } + item.setShopName(shopKey); + payloadByShop.put(shopKey, item); + } + return payloadByShop; + } + + private Map loadTaskMap(List entities) { + List taskIds = entities.stream() + .map(FileResultEntity::getTaskId) + .filter(id -> id != null && id > 0) + .distinct() + .toList(); + Map taskMap = new LinkedHashMap<>(); + if (taskIds.isEmpty()) { + return taskMap; + } + for (FileTaskEntity task : selectTasksByIdsInBatches(taskIds)) { + if (task != null && MODULE_TYPE.equals(task.getModuleType())) { + taskMap.put(task.getId(), task); + } + } + return taskMap; + } + + private Map> buildSnapshotMap(Map taskMap) { + Map> out = new LinkedHashMap<>(); + for (Map.Entry entry : taskMap.entrySet()) { + out.put(entry.getKey(), indexSnapshotByResultId(parseTaskSnapshots(entry.getValue().getResultJson()))); + } + return out; + } + + private PatrolDeleteResultItemVo toHistoryItem(FileResultEntity entity, FileTaskEntity task, PatrolDeleteResultItemVo snapshot) { + PatrolDeleteResultItemVo item = snapshot != null ? snapshot : new PatrolDeleteResultItemVo(); + item.setResultId(entity.getId()); + item.setTaskId(entity.getTaskId()); + item.setShopName(firstNonBlank(item.getShopName(), entity.getSourceFilename())); + item.setShopId(firstNonBlank(item.getShopId(), entity.getSourceFileUrl())); + item.setTaskStatus(task != null ? task.getStatus() : item.getTaskStatus()); + item.setSuccess(entity.getSuccess() == null ? item.getSuccess() : entity.getSuccess() == 1); + item.setError(!blank(entity.getErrorMessage()) ? entity.getErrorMessage() : item.getError()); + item.setCreatedAt(entity.getCreatedAt()); + item.setFinishedAt(task != null ? task.getFinishedAt() : item.getFinishedAt()); + item.setOutputFilename(firstNonBlank(item.getOutputFilename(), entity.getResultFilename())); + item.setDownloadUrl(blank(entity.getResultFileUrl()) + ? item.getDownloadUrl() + : ossStorageService.generateFreshDownloadUrl(entity.getResultFileUrl())); + if (item.getCountrySections() == null) { + item.setCountrySections(new ArrayList<>()); + } + if (item.getCartRatios() == null) { + item.setCartRatios(new ArrayList<>()); + } + return item; + } + + private List dedupeItems(List items) { + LinkedHashMap map = new LinkedHashMap<>(); + for (PatrolDeleteTaskItemDto item : items) { + if (item == null) { + continue; + } + String normalizedShopName = ziniaoShopSwitchService.normalizeShopName(item.getShopName()); + if (blank(normalizedShopName)) { + continue; + } + item.setShopName(normalizedShopName); + String key = normalizedShopName + "::" + Objects.toString(item.getShopId(), ""); + map.put(key, item); + } + return new ArrayList<>(map.values()); + } + + private PatrolDeleteResultItemVo toSnapshotVo(FileResultEntity result, PatrolDeleteTaskItemDto item, String taskStatus, LocalDateTime finishedAt) { + PatrolDeleteResultItemVo vo = new PatrolDeleteResultItemVo(); + vo.setResultId(result.getId()); + vo.setTaskId(result.getTaskId()); + vo.setShopName(item.getShopName()); + vo.setShopId(item.getShopId()); + vo.setPlatform(item.getPlatform()); + vo.setCompanyName(item.getCompanyName()); + vo.setMatched(item.isMatched()); + vo.setMatchStatus(item.getMatchStatus()); + vo.setMatchMessage(item.getMatchMessage()); + vo.setTaskStatus(taskStatus); + vo.setSuccess(result.getSuccess() == null ? null : result.getSuccess() == 1); + vo.setError(result.getErrorMessage()); + vo.setCreatedAt(result.getCreatedAt()); + vo.setFinishedAt(finishedAt); + vo.setCountrySections(copyCountrySections(item.getCountrySections())); + vo.setCartRatios(copyCartRatios(item.getCartRatios())); + vo.setOutputFilename(result.getResultFilename()); + vo.setDownloadUrl(null); + return vo; + } + + private void persistTaskJson(FileTaskEntity task, List requestItems, List snapshots) { + try { + task.setRequestJson(objectMapper.writeValueAsString(requestItems)); + task.setResultJson(objectMapper.writeValueAsString(snapshots)); + fileTaskMapper.updateById(task); + } catch (Exception ex) { + throw new BusinessException("巡店删除任务快照保存失败"); + } + } + + private List buildSnapshotFromDb(FileTaskEntity task, List rows) { + List existing = parseTaskSnapshots(task.getResultJson()); + Map snapshotByResultId = indexSnapshotByResultId(existing); + List list = new ArrayList<>(); + for (FileResultEntity row : rows) { + list.add(toHistoryItem(row, task, snapshotByResultId.get(row.getId()))); + } + return list; + } + + private Map indexSnapshotByResultId(List snapshots) { + Map map = new LinkedHashMap<>(); + if (snapshots == null) { + return map; + } + for (PatrolDeleteResultItemVo snapshot : snapshots) { + if (snapshot != null && snapshot.getResultId() != null) { + map.put(snapshot.getResultId(), snapshot); + } + } + return map; + } + + private void updateTaskStatusFromRows(FileTaskEntity task, List rows) { + long successCount = rows.stream().filter(row -> Integer.valueOf(1).equals(row.getSuccess())).count(); + long failedCount = rows.stream().filter(row -> Integer.valueOf(0).equals(row.getSuccess())).count(); + long pendingCount = rows.stream().filter(row -> !isResultFinished(row)).count(); + task.setSuccessFileCount((int) successCount); + task.setFailedFileCount((int) failedCount); + task.setUpdatedAt(LocalDateTime.now()); + if (pendingCount > 0) { + task.setStatus("RUNNING"); + task.setFinishedAt(null); + task.setErrorMessage(null); + return; + } + task.setStatus(failedCount > 0 ? "FAILED" : "SUCCESS"); + task.setFinishedAt(LocalDateTime.now()); + if (failedCount > 0) { + List errors = new ArrayList<>(); + for (FileResultEntity row : rows) { + if (!blank(row.getErrorMessage())) { + errors.add(row.getSourceFilename() + ": " + row.getErrorMessage()); + } + } + task.setErrorMessage(String.join("; ", errors)); + } else { + task.setErrorMessage(null); + } + } + + private PatrolDeleteShopPayloadDto mergeShopPayload(Long taskId, String shopKey, PatrolDeleteShopPayloadDto incoming) { + PatrolDeleteShopPayloadDto merged = taskCacheService.getShopMergedPayload(taskId, shopKey); + if (merged == null) { + merged = new PatrolDeleteShopPayloadDto(); + merged.setShopName(shopKey); + merged.setCountrySections(new ArrayList<>()); + merged.setCartRatios(new ArrayList<>()); + } + + merged.setShopName(firstNonBlank(incoming.getShopName(), merged.getShopName())); + merged.setSubmissionId(firstNonBlank(incoming.getSubmissionId(), merged.getSubmissionId())); + merged.setChunkIndex(incoming.getChunkIndex()); + merged.setChunkTotal(incoming.getChunkTotal()); + + if (!blank(incoming.getError())) { + merged.setError(incoming.getError().trim()); + merged.setShopDone(Boolean.TRUE); + taskCacheService.saveShopMergedPayload(taskId, shopKey, merged); + return merged; + } + + merged.setCountrySections(mergeCountrySections(merged.getCountrySections(), incoming.getCountrySections())); + merged.setCartRatios(mergeCartRatios(merged.getCartRatios(), incoming.getCartRatios())); + if (Boolean.TRUE.equals(incoming.getShopDone())) { + merged.setShopDone(Boolean.TRUE); + } + taskCacheService.saveShopMergedPayload(taskId, shopKey, merged); + return merged; + } + + private void mergePayloadIntoSnapshot(PatrolDeleteResultItemVo snapshot, PatrolDeleteShopPayloadDto payload) { + if (snapshot == null || payload == null) { + return; + } + snapshot.setShopName(firstNonBlank(payload.getShopName(), snapshot.getShopName())); + snapshot.setCountrySections(copyCountrySections(payload.getCountrySections())); + snapshot.setCartRatios(copyCartRatios(payload.getCartRatios())); + if (!blank(payload.getError())) { + snapshot.setError(payload.getError().trim()); + } + } + + private void applySuccessToSnapshot(PatrolDeleteResultItemVo snapshot) { + if (snapshot == null) { + return; + } + snapshot.setSuccess(Boolean.TRUE); + snapshot.setError(null); + } + + private void applyFailureToSnapshot(PatrolDeleteResultItemVo snapshot, String error) { + if (snapshot == null) { + return; + } + snapshot.setSuccess(Boolean.FALSE); + snapshot.setError(blankToNull(error)); + } + + private List mergeCountrySections(List base, List incoming) { + Map map = new LinkedHashMap<>(); + for (PatrolDeleteCountrySectionDto section : copyCountrySections(base)) { + map.put(section.getCountry(), section); + } + for (PatrolDeleteCountrySectionDto section : copyCountrySections(incoming)) { + PatrolDeleteCountrySectionDto existing = map.get(section.getCountry()); + if (existing == null) { + map.put(section.getCountry(), section); + continue; + } + List mergedRows = existing.getRows() == null + ? new ArrayList<>() + : new ArrayList<>(existing.getRows()); + if (section.getRows() != null) { + mergedRows.addAll(section.getRows()); + } + existing.setRows(mergedRows); + } + return new ArrayList<>(map.values()); + } + + private List mergeCartRatios(List base, List incoming) { + Map map = new LinkedHashMap<>(); + for (PatrolDeleteCartRatioDto ratio : copyCartRatios(base)) { + map.put(ratio.getCountry(), ratio); + } + for (PatrolDeleteCartRatioDto ratio : copyCartRatios(incoming)) { + if (!blank(ratio.getCountry())) { + map.put(ratio.getCountry(), ratio); + } + } + return new ArrayList<>(map.values()); + } + + private boolean hasAnyPayloadData(PatrolDeleteShopPayloadDto payload) { + if (payload == null) { + return false; + } + if (payload.getCountrySections() != null) { + for (PatrolDeleteCountrySectionDto section : payload.getCountrySections()) { + if (section == null || section.getRows() == null) { + continue; + } + for (PatrolDeleteCountryMetricRowDto row : section.getRows()) { + if (row != null && (!blank(row.getStatus()) + || !blank(row.getQuantity()) + || !blank(row.getDeleteQuantity()) + || !blank(row.getProcessStatus()))) { + return true; + } + } + } + } + if (payload.getCartRatios() != null) { + for (PatrolDeleteCartRatioDto ratio : payload.getCartRatios()) { + if (ratio != null && (!blank(ratio.getCountry()) || !blank(ratio.getRatio()))) { + return true; + } + } + } + return false; + } + + private void finalizeTaskWorkbook(FileTaskEntity task, List rows, List snapshots) { + List successItems = snapshots.stream() + .filter(item -> Boolean.TRUE.equals(item.getSuccess())) + .toList(); + if (!successItems.isEmpty()) { + File workRoot = FileUtil.mkdir(FileUtil.file(System.getProperty("java.io.tmpdir"), "patrol-delete-result", String.valueOf(task.getId()))); + String filename = safeFileStem("巡店删除-" + task.getId()) + ".xlsx"; + File xlsx = FileUtil.file(workRoot, filename); + try { + excelAssemblyService.writeWorkbook(xlsx, successItems); + String objectKey = ossStorageService.uploadResultFile(xlsx, MODULE_TYPE); + long fileSize = xlsx.length(); + int rowCount = excelAssemblyService.countRows(successItems); + for (FileResultEntity row : rows) { + if (Integer.valueOf(1).equals(row.getSuccess())) { + row.setResultFilename(filename); + row.setResultFileUrl(objectKey); + row.setResultFileSize(fileSize); + row.setResultContentType(CONTENT_TYPE_XLSX); + row.setRowCount(rowCount); + fileResultMapper.updateById(row); + } + } + snapshots = buildSnapshotFromDb(task, rows); + } finally { + FileUtil.del(xlsx); + } + } + + persistSnapshotJson(task, snapshots); + fileTaskMapper.updateById(task); + taskCacheService.deleteTaskCache(task.getId()); + } + + private void markResultSuccess(FileResultEntity row) { + row.setSuccess(1); + row.setErrorMessage(null); + fileResultMapper.updateById(row); + } + + private void markResultFailed(FileResultEntity row, String message) { + row.setSuccess(0); + row.setErrorMessage(blankToNull(message)); + row.setResultFilename(null); + row.setResultFileUrl(null); + row.setResultFileSize(0L); + row.setResultContentType(null); + row.setRowCount(0); + fileResultMapper.updateById(row); + } + + private boolean isResultFinished(FileResultEntity row) { + return Integer.valueOf(1).equals(row.getSuccess()) || Integer.valueOf(0).equals(row.getSuccess()); + } + + private List parseTaskSnapshots(String json) { + if (blank(json)) { + return new ArrayList<>(); + } + try { + return objectMapper.readValue(json, new TypeReference>() {}); + } catch (Exception ex) { + log.warn("[patrol-delete] parse task snapshot failed: {}", ex.getMessage()); + return new ArrayList<>(); + } + } + + private void persistSnapshotJson(FileTaskEntity task, List snapshots) { + try { + task.setResultJson(objectMapper.writeValueAsString(snapshots == null ? List.of() : snapshots)); + } catch (Exception ex) { + throw new BusinessException("巡店删除任务快照保存失败"); + } + } + + private List copyCountrySections(List sections) { + List copy = new ArrayList<>(); + if (sections == null) { + return copy; + } + for (PatrolDeleteCountrySectionDto section : sections) { + if (section == null) { + continue; + } + PatrolDeleteCountrySectionDto item = new PatrolDeleteCountrySectionDto(); + item.setCountry(section.getCountry()); + item.setRows(section.getRows() == null ? new ArrayList<>() : new ArrayList<>(section.getRows())); + copy.add(item); + } + return copy; + } + + private List copyCartRatios(List ratios) { + List copy = new ArrayList<>(); + if (ratios == null) { + return copy; + } + for (PatrolDeleteCartRatioDto ratio : ratios) { + if (ratio == null) { + continue; + } + PatrolDeleteCartRatioDto item = new PatrolDeleteCartRatioDto(); + item.setCountry(ratio.getCountry()); + item.setRatio(ratio.getRatio()); + copy.add(item); + } + return copy; + } + + private String firstNonBlank(String first, String second) { + return !blank(first) ? first : second; + } + + private String safeFileStem(String value) { + String raw = value == null ? "result" : value.trim(); + String safe = raw.replaceAll("[\\\\/:*?\"<>|]+", "_"); + return safe.isBlank() ? "result" : safe; + } + + private boolean blank(String value) { + return value == null || value.isBlank(); + } + + private String blankToNull(String value) { + return blank(value) ? null : value.trim(); + } + + private void validateUserId(Long userId) { + if (userId == null || userId <= 0) { + throw new BusinessException("user_id 不合法"); + } + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/controller/PriceTrackController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/controller/PriceTrackController.java index 5482cd0..8a8a811 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/controller/PriceTrackController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/controller/PriceTrackController.java @@ -223,7 +223,10 @@ public class PriceTrackController { example = "200" ) @PathVariable Long taskId, - @Valid @RequestBody PriceTrackSubmitResultRequest request) { + @Valid @RequestBody PriceTrackSubmitResultRequest request, + jakarta.servlet.http.HttpServletResponse response) { + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setContentType("application/json;charset=UTF-8"); priceTrackTaskService.submitResult(taskId, request); return ApiResponse.success(null); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackCreateTaskVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackCreateTaskVo.java index 1e6b686..d9887ae 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackCreateTaskVo.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackCreateTaskVo.java @@ -20,4 +20,7 @@ public class PriceTrackCreateTaskVo { @Schema(description = "parsed asin rows by country") private Map>> asinRowsByCountry; + + @Schema(description = "minimum price grouped by country code and asin") + private Map> minimumPriceByCountryAndAsin; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackMatchShopsVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackMatchShopsVo.java index 6d220f0..426e06d 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackMatchShopsVo.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/model/vo/PriceTrackMatchShopsVo.java @@ -20,6 +20,9 @@ public class PriceTrackMatchShopsVo { @Schema(description = "Parsed asin rows grouped by country code") private Map>> asinRowsByCountry; + @Schema(description = "Minimum price grouped by country code and asin") + private Map> minimumPriceByCountryAndAsin; + @Data @Schema(description = "Single matched shop item") public static class PriceTrackShopQueueItem { diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackService.java index 3792c0a..7caade6 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackService.java @@ -158,6 +158,7 @@ public class PriceTrackService { PriceTrackMatchShopsVo vo = new PriceTrackMatchShopsVo(); vo.setSkipAsinsByCountry(skipAsinsByCountry); vo.setAsinRowsByCountry(asinRowsByCountry); + vo.setMinimumPriceByCountryAndAsin(priceTrackTaskService.buildMinimumPriceLookupForMatch(asinRowsByCountry)); for (String shopName : ordered) { vo.getItems().add(matchOneShop(shopName, skipAsinsByCountry)); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskCacheService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskCacheService.java index 20a0ce9..432c18e 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskCacheService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskCacheService.java @@ -2,24 +2,29 @@ package com.nanri.aiimage.modules.pricetrack.service; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.pricetrack.model.dto.PriceTrackSubmitResultRequest; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; @Service @RequiredArgsConstructor public class PriceTrackTaskCacheService { private static final long HEARTBEAT_TTL_HOURS = 24; - private final StringRedisTemplate stringRedisTemplate; private final ObjectMapper objectMapper; + private final TaskPressureProperties taskPressureProperties; + private final ConcurrentHashMap taskEntityLocalCache = new ConcurrentHashMap<>(); public void touchTaskHeartbeat(Long taskId) { if (taskId == null || taskId <= 0) { @@ -112,8 +117,72 @@ public class PriceTrackTaskCacheService { if (taskId == null || taskId <= 0) { return; } + taskEntityLocalCache.remove(taskId); stringRedisTemplate.delete(buildShopPayloadKey(taskId)); stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId)); + stringRedisTemplate.delete(buildTaskEntityKey(taskId)); + } + + public void saveTaskCache(FileTaskEntity task) { + if (task == null || task.getId() == null) { + return; + } + long now = System.currentTimeMillis(); + taskEntityLocalCache.put(task.getId(), new LocalTaskEntityCacheEntry( + now, + objectMapper.convertValue(task, FileTaskEntity.class) + )); + try { + stringRedisTemplate.opsForValue().set( + buildTaskEntityKey(task.getId()), + objectMapper.writeValueAsString(task), + Duration.ofHours(HEARTBEAT_TTL_HOURS) + ); + } catch (Exception ignored) { + } + } + + public Map getTaskCacheBatch(java.util.List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + java.util.List normalized = taskIds.stream() + .filter(id -> id != null && id > 0) + .distinct() + .toList(); + if (normalized.isEmpty()) { + return result; + } + long now = System.currentTimeMillis(); + java.util.List missingIds = new ArrayList<>(); + for (Long taskId : normalized) { + LocalTaskEntityCacheEntry cached = taskEntityLocalCache.get(taskId); + if (isLocalCacheFresh(cached, now)) { + result.put(taskId, objectMapper.convertValue(cached.task(), FileTaskEntity.class)); + } else { + missingIds.add(taskId); + } + } + if (missingIds.isEmpty()) { + return result; + } + java.util.List keys = missingIds.stream().map(this::buildTaskEntityKey).toList(); + java.util.List values = stringRedisTemplate.opsForValue().multiGet(keys); + for (int i = 0; i < missingIds.size(); i++) { + Long taskId = missingIds.get(i); + String val = values != null && i < values.size() ? values.get(i) : null; + if (val == null || val.isBlank()) { + continue; + } + try { + FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class); + result.put(taskId, task); + taskEntityLocalCache.put(taskId, new LocalTaskEntityCacheEntry(now, task)); + } catch (Exception ignored) { + } + } + return result; } private String buildShopPayloadKey(Long taskId) { @@ -123,4 +192,15 @@ public class PriceTrackTaskCacheService { private String buildTaskHeartbeatKey(Long taskId) { return "price-track:task:heartbeat:" + taskId; } + + private String buildTaskEntityKey(Long taskId) { + return "price-track:task:entity:" + taskId; + } + + private boolean isLocalCacheFresh(LocalTaskEntityCacheEntry cached, long now) { + return cached != null + && now - cached.cachedAtMillis() <= Math.max(0L, taskPressureProperties.getLocalTaskEntityCacheMillis()); + } + + private record LocalTaskEntityCacheEntry(long cachedAtMillis, FileTaskEntity task) {} } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskService.java index b66815a..75c07d0 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/pricetrack/service/PriceTrackTaskService.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.util.ExcelStreamReader; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; import com.nanri.aiimage.modules.pricetrack.mapper.PriceTrackShopCandidateMapper; @@ -64,6 +65,62 @@ public class PriceTrackTaskService { private final PriceTrackExcelAssemblyService excelAssemblyService; private final PriceTrackTaskCacheService priceTrackTaskCacheService; private final PriceTrackLoopRunService priceTrackLoopRunService; + private final TaskPressureProperties taskPressureProperties; + + private FileTaskEntity loadTaskForExecution(Long taskId) { + Map cachedTasks = priceTrackTaskCacheService.getTaskCacheBatch(List.of(taskId)); + FileTaskEntity cached = cachedTasks.get(taskId); + if (cached != null) { + return cached; + } + FileTaskEntity dbTask = fileTaskMapper.selectById(taskId); + if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) { + priceTrackTaskCacheService.saveTaskCache(dbTask); + } + return dbTask; + } + + private Map loadTaskMapByIds(List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + List normalizedTaskIds = taskIds.stream() + .filter(taskId -> taskId != null && taskId > 0) + .distinct() + .toList(); + if (normalizedTaskIds.isEmpty()) { + return result; + } + Map cachedTasks = priceTrackTaskCacheService.getTaskCacheBatch(normalizedTaskIds); + result.putAll(cachedTasks); + List missingTaskIds = normalizedTaskIds.stream() + .filter(taskId -> !cachedTasks.containsKey(taskId)) + .toList(); + for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) { + if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) { + continue; + } + result.put(dbTask.getId(), dbTask); + if ("RUNNING".equals(dbTask.getStatus())) { + priceTrackTaskCacheService.saveTaskCache(dbTask); + } + } + return result; + } + + private List selectTasksByIdsInBatches(List taskIds) { + List tasks = new ArrayList<>(); + if (taskIds == null || taskIds.isEmpty()) { + return tasks; + } + int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize()); + for (int start = 0; start < taskIds.size(); start += batchSize) { + int end = Math.min(start + batchSize, taskIds.size()); + tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end))); + } + return tasks; + } public PriceTrackDashboardVo dashboard(Long userId) { if (userId == null || userId <= 0) throw new BusinessException("user_id 不合法"); @@ -104,7 +161,7 @@ public class PriceTrackTaskService { .distinct() .toList(); if (!taskIds.isEmpty()) { - List tasks = fileTaskMapper.selectBatchIds(taskIds); + List tasks = selectTasksByIdsInBatches(taskIds); for (FileTaskEntity t : tasks) { if (t != null) statusByTaskId.put(t.getId(), t.getStatus()); } @@ -134,7 +191,7 @@ public class PriceTrackTaskService { @Transactional public void deleteTask(Long taskId, Long userId) { if (userId == null || userId <= 0) throw new BusinessException("user_id 不合法"); - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { throw new BusinessException("任务不存在"); } @@ -164,7 +221,7 @@ public class PriceTrackTaskService { vo.setRemoved(false); for (FileResultEntity fr : candidates) { if (fr.getTaskId() == null) continue; - FileTaskEntity task = fileTaskMapper.selectById(fr.getTaskId()); + FileTaskEntity task = loadTaskForExecution(fr.getTaskId()); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) continue; if (!"RUNNING".equals(task.getStatus())) continue; fileResultMapper.deleteById(fr.getId()); @@ -178,10 +235,11 @@ public class PriceTrackTaskService { public PriceTrackTaskBatchVo getTaskDetailsBatch(List taskIds) { PriceTrackTaskBatchVo batch = new PriceTrackTaskBatchVo(); if (taskIds == null || taskIds.isEmpty()) return batch; + Map taskMap = loadTaskMapByIds(taskIds); for (Long taskId : taskIds) { if (taskId == null || taskId <= 0) continue; - FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { batch.getMissingTaskIds().add(taskId); continue; } @@ -195,12 +253,13 @@ public class PriceTrackTaskService { if (taskIds == null || taskIds.isEmpty()) { return batch; } + Map taskMap = loadTaskMapByIds(taskIds); for (Long taskId : taskIds) { if (taskId == null || taskId <= 0) { continue; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { batch.getMissingTaskIds().add(taskId); continue; } @@ -237,6 +296,8 @@ public class PriceTrackTaskService { Map>> asinRowsByCountry = request.isAsinMode() ? parseAsinRowsByCountry(request.getAsinFiles(), request.getCountryCodes()) : new LinkedHashMap<>(); + Map> minimumPriceByCountryAndAsin = + buildMinimumPriceByCountryAndAsin(asinRowsByCountry); log.info("[price-track] createTask skipAsins countries={} asinMode={}", skipAsinsByCountry.keySet(), request.isAsinMode()); @@ -254,6 +315,7 @@ public class PriceTrackTaskService { task.setCreatedAt(LocalDateTime.now()); task.setUpdatedAt(LocalDateTime.now()); fileTaskMapper.insert(task); + priceTrackTaskCacheService.saveTaskCache(task); priceTrackTaskCacheService.touchTaskHeartbeat(task.getId()); if (request.getLoopRunId() != null) { priceTrackLoopRunService.bindChildTask(request.getLoopRunId(), task.getId(), request.getRoundIndex(), request.getShopIndex()); @@ -287,6 +349,7 @@ public class PriceTrackTaskService { ctx.put("countryCodes", request.getCountryCodes()); ctx.put("skipAsinsByCountry", skipAsinsByCountry); ctx.put("asinRowsByCountry", asinRowsByCountry); + ctx.put("minimumPriceByCountryAndAsin", minimumPriceByCountryAndAsin); ctx.put("items", uniqueItems); ctx.put("loopRunId", request.getLoopRunId()); ctx.put("roundIndex", request.getRoundIndex()); @@ -306,6 +369,7 @@ public class PriceTrackTaskService { vo.setItems(snapshot); vo.setSkipAsinsByCountry(skipAsinsByCountry); vo.setAsinRowsByCountry(asinRowsByCountry); + vo.setMinimumPriceByCountryAndAsin(minimumPriceByCountryAndAsin); return vo; } @@ -315,7 +379,7 @@ public class PriceTrackTaskService { if (request == null || request.getShops() == null || request.getShops().isEmpty()) { throw new BusinessException("shops 不能为空"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { throw new BusinessException("任务不存在"); } @@ -386,7 +450,7 @@ public class PriceTrackTaskService { @Transactional public boolean tryFinalizeTask(Long taskId, boolean fromCompensation) { if (taskId == null || taskId <= 0) return false; - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) return false; if (!"RUNNING".equals(task.getStatus())) { log.warn("[price-track] stale finalize skipped taskId={} status={} fromCompensation={}", @@ -490,7 +554,7 @@ public class PriceTrackTaskService { // ---- private helpers ---- private void reconcileTaskAfterResultRemoval(Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) return; List latest = fileResultMapper.selectList( new LambdaQueryWrapper() @@ -552,6 +616,11 @@ public class PriceTrackTaskService { return parseAsinRowsByCountry(asinFiles, countryCodes); } + public Map> buildMinimumPriceLookupForMatch( + Map>> asinRowsByCountry) { + return buildMinimumPriceByCountryAndAsin(asinRowsByCountry); + } + private Map>> parseAsinRowsByCountry(List asinFiles, List countryCodes) { Map>> merged = new LinkedHashMap<>(); if (asinFiles == null || asinFiles.isEmpty()) { @@ -654,6 +723,37 @@ public class PriceTrackTaskService { } } + private Map> buildMinimumPriceByCountryAndAsin( + Map>> asinRowsByCountry) { + Map> out = new LinkedHashMap<>(); + if (asinRowsByCountry == null || asinRowsByCountry.isEmpty()) { + return out; + } + for (Map.Entry>> entry : asinRowsByCountry.entrySet()) { + String countryCode = entry.getKey(); + if (countryCode == null || countryCode.isBlank()) { + continue; + } + Map minimumPriceByAsin = new LinkedHashMap<>(); + List> rows = entry.getValue(); + if (rows != null) { + for (Map row : rows) { + if (row == null || row.isEmpty()) { + continue; + } + String asin = normalizeCellText(row.get("asin")).toUpperCase(Locale.ROOT); + if (asin.isBlank()) { + continue; + } + String minimumPrice = normalizeCellText(row.get("minimumPrice")); + minimumPriceByAsin.put(asin, minimumPrice); + } + } + out.put(countryCode, minimumPriceByAsin); + } + return out; + } + private String resolveCountryCode(String sheetName, List countryCodes, boolean singleSheetFallback) { String normalized = normalizeHeaderText(sheetName).toUpperCase(Locale.ROOT); for (ProductRiskCountryCode code : ProductRiskCountryCode.values()) { @@ -832,7 +932,7 @@ public class PriceTrackTaskService { } private void mergeQueueFieldsFromRequest(PriceTrackResultItemVo vo, Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || task.getRequestJson() == null || task.getRequestJson().isBlank()) return; try { Map payload = objectMapper.readValue(task.getRequestJson(), new TypeReference>() {}); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/controller/ProductRiskResolveController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/controller/ProductRiskResolveController.java index dad10af..3a9151d 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/controller/ProductRiskResolveController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/controller/ProductRiskResolveController.java @@ -229,7 +229,10 @@ public class ProductRiskResolveController { } """) })) - @Valid @RequestBody ProductRiskSubmitResultRequest request) { + @Valid @RequestBody ProductRiskSubmitResultRequest request, + jakarta.servlet.http.HttpServletResponse response) { + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setContentType("application/json;charset=UTF-8"); productRiskTaskService.submitResult(taskId, request); return ApiResponse.success(null); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskCacheService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskCacheService.java index c509033..aac342b 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskCacheService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskCacheService.java @@ -2,23 +2,29 @@ package com.nanri.aiimage.modules.productrisk.service; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskShopPayloadDto; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; @Service @RequiredArgsConstructor public class ProductRiskTaskCacheService { private static final long PAYLOAD_TTL_HOURS = 24; - private final StringRedisTemplate stringRedisTemplate; private final ObjectMapper objectMapper; + private final TaskPressureProperties taskPressureProperties; + private final ConcurrentHashMap taskEntityLocalCache = new ConcurrentHashMap<>(); public ProductRiskShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) { if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank()) { @@ -109,8 +115,72 @@ public class ProductRiskTaskCacheService { if (taskId == null || taskId <= 0) { return; } + taskEntityLocalCache.remove(taskId); stringRedisTemplate.delete(buildShopPayloadKey(taskId)); stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId)); + stringRedisTemplate.delete(buildTaskEntityKey(taskId)); + } + + public void saveTaskCache(FileTaskEntity task) { + if (task == null || task.getId() == null) { + return; + } + long now = System.currentTimeMillis(); + taskEntityLocalCache.put(task.getId(), new LocalTaskEntityCacheEntry( + now, + objectMapper.convertValue(task, FileTaskEntity.class) + )); + try { + stringRedisTemplate.opsForValue().set( + buildTaskEntityKey(task.getId()), + objectMapper.writeValueAsString(task), + Duration.ofHours(PAYLOAD_TTL_HOURS) + ); + } catch (Exception ignored) { + } + } + + public Map getTaskCacheBatch(java.util.List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + java.util.List normalized = taskIds.stream() + .filter(id -> id != null && id > 0) + .distinct() + .toList(); + if (normalized.isEmpty()) { + return result; + } + long now = System.currentTimeMillis(); + java.util.List missingIds = new ArrayList<>(); + for (Long taskId : normalized) { + LocalTaskEntityCacheEntry cached = taskEntityLocalCache.get(taskId); + if (isLocalCacheFresh(cached, now)) { + result.put(taskId, objectMapper.convertValue(cached.task(), FileTaskEntity.class)); + } else { + missingIds.add(taskId); + } + } + if (missingIds.isEmpty()) { + return result; + } + java.util.List keys = missingIds.stream().map(this::buildTaskEntityKey).toList(); + java.util.List values = stringRedisTemplate.opsForValue().multiGet(keys); + for (int i = 0; i < missingIds.size(); i++) { + Long taskId = missingIds.get(i); + String val = values != null && i < values.size() ? values.get(i) : null; + if (val == null || val.isBlank()) { + continue; + } + try { + FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class); + result.put(taskId, task); + taskEntityLocalCache.put(taskId, new LocalTaskEntityCacheEntry(now, task)); + } catch (Exception ignored) { + } + } + return result; } private String buildShopPayloadKey(Long taskId) { @@ -127,4 +197,15 @@ public class ProductRiskTaskCacheService { private String buildTaskHeartbeatKey(Long taskId) { return "product-risk:task:heartbeat:" + taskId; } + + private String buildTaskEntityKey(Long taskId) { + return "product-risk:task:entity:" + taskId; + } + + private boolean isLocalCacheFresh(LocalTaskEntityCacheEntry cached, long now) { + return cached != null + && now - cached.cachedAtMillis() <= Math.max(0L, taskPressureProperties.getLocalTaskEntityCacheMillis()); + } + + private record LocalTaskEntityCacheEntry(long cachedAtMillis, FileTaskEntity task) {} } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskService.java index 837fd9f..a7cf6d5 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/productrisk/service/ProductRiskTaskService.java @@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.productrisk.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.productrisk.mapper.ProductRiskShopCandidateMapper; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCreateTaskRequest; @@ -57,6 +58,62 @@ public class ProductRiskTaskService { private final OssStorageService ossStorageService; private final ObjectMapper objectMapper; private final ProductRiskTaskCacheService productRiskTaskCacheService; + private final TaskPressureProperties taskPressureProperties; + + private FileTaskEntity loadTaskForExecution(Long taskId) { + Map cachedTasks = productRiskTaskCacheService.getTaskCacheBatch(List.of(taskId)); + FileTaskEntity cached = cachedTasks.get(taskId); + if (cached != null) { + return cached; + } + FileTaskEntity dbTask = fileTaskMapper.selectById(taskId); + if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) { + productRiskTaskCacheService.saveTaskCache(dbTask); + } + return dbTask; + } + + private Map loadTaskMapByIds(List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + List normalizedTaskIds = taskIds.stream() + .filter(taskId -> taskId != null && taskId > 0) + .distinct() + .toList(); + if (normalizedTaskIds.isEmpty()) { + return result; + } + Map cachedTasks = productRiskTaskCacheService.getTaskCacheBatch(normalizedTaskIds); + result.putAll(cachedTasks); + List missingTaskIds = normalizedTaskIds.stream() + .filter(taskId -> !cachedTasks.containsKey(taskId)) + .toList(); + for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) { + if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) { + continue; + } + result.put(dbTask.getId(), dbTask); + if ("RUNNING".equals(dbTask.getStatus())) { + productRiskTaskCacheService.saveTaskCache(dbTask); + } + } + return result; + } + + private List selectTasksByIdsInBatches(List taskIds) { + List tasks = new ArrayList<>(); + if (taskIds == null || taskIds.isEmpty()) { + return tasks; + } + int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize()); + for (int start = 0; start < taskIds.size(); start += batchSize) { + int end = Math.min(start + batchSize, taskIds.size()); + tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end))); + } + return tasks; + } public ProductRiskDashboardVo dashboard(Long userId) { if (userId == null || userId <= 0) { @@ -100,7 +157,7 @@ public class ProductRiskTaskService { .distinct() .toList(); if (!taskIds.isEmpty()) { - List tasks = fileTaskMapper.selectBatchIds(taskIds); + List tasks = selectTasksByIdsInBatches(taskIds); for (FileTaskEntity t : tasks) { if (t != null) { statusByTaskId.put(t.getId(), t.getStatus()); @@ -139,7 +196,7 @@ public class ProductRiskTaskService { if (userId == null || userId <= 0) { throw new BusinessException("user_id 不合法"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { throw new BusinessException("任务不存在"); } @@ -178,7 +235,7 @@ public class ProductRiskTaskService { if (fr.getTaskId() == null) { continue; } - FileTaskEntity t = fileTaskMapper.selectById(fr.getTaskId()); + FileTaskEntity t = loadTaskForExecution(fr.getTaskId()); if (t == null || !MODULE_TYPE.equals(t.getModuleType()) || !userId.equals(t.getUserId())) { continue; } @@ -198,7 +255,7 @@ public class ProductRiskTaskService { * 删除一条 file_result 后重算父任务状态;若无剩余结果则删除任务。 */ private void reconcileTaskAfterResultRemoval(Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { return; } @@ -270,12 +327,13 @@ public class ProductRiskTaskService { if (taskIds == null || taskIds.isEmpty()) { return batch; } + Map taskMap = loadTaskMapByIds(taskIds); for (Long taskId : taskIds) { if (taskId == null || taskId <= 0) { continue; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { batch.getMissingTaskIds().add(taskId); continue; } @@ -289,12 +347,13 @@ public class ProductRiskTaskService { if (taskIds == null || taskIds.isEmpty()) { return batch; } + Map taskMap = loadTaskMapByIds(taskIds); for (Long taskId : taskIds) { if (taskId == null || taskId <= 0) { continue; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { batch.getMissingTaskIds().add(taskId); continue; } @@ -359,6 +418,7 @@ public class ProductRiskTaskService { task.setCreatedAt(LocalDateTime.now()); task.setUpdatedAt(LocalDateTime.now()); fileTaskMapper.insert(task); + productRiskTaskCacheService.saveTaskCache(task); productRiskTaskCacheService.touchTaskHeartbeat(task.getId()); List snapshot = new ArrayList<>(); @@ -405,7 +465,7 @@ public class ProductRiskTaskService { throw new BusinessException("shops 不能为空"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { throw new BusinessException("任务不存在"); } @@ -566,7 +626,7 @@ public class ProductRiskTaskService { if (taskId == null || taskId <= 0) { return false; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { return false; } @@ -861,7 +921,7 @@ public class ProductRiskTaskService { } private void mergeQueueFieldsFromRequest(ProductRiskResultItemVo vo, Long taskId) { - FileTaskEntity t = fileTaskMapper.selectById(taskId); + FileTaskEntity t = loadTaskForExecution(taskId); if (t == null || t.getRequestJson() == null || t.getRequestJson().isBlank()) { return; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/mapper/ShopManageGroupMemberMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/mapper/ShopManageGroupMemberMapper.java new file mode 100644 index 0000000..5802616 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/mapper/ShopManageGroupMemberMapper.java @@ -0,0 +1,9 @@ +package com.nanri.aiimage.modules.shopkey.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageGroupMemberEntity; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface ShopManageGroupMemberMapper extends BaseMapper { +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupCreateRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupCreateRequest.java index 236cba7..3db5c4d 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupCreateRequest.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupCreateRequest.java @@ -5,6 +5,8 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; +import java.util.List; + @Data public class ShopManageGroupCreateRequest { @@ -12,9 +14,8 @@ public class ShopManageGroupCreateRequest { @NotBlank(message = "分组名称不能为空") private String groupName; - @Schema(description = "关联用户ID", requiredMode = Schema.RequiredMode.REQUIRED) - @NotNull(message = "关联用户不能为空") - private Long userId; + @Schema(description = "组员用户ID列表") + private List memberUserIds; @Schema(description = "创建人用户ID,由后台代理透传", requiredMode = Schema.RequiredMode.REQUIRED) @NotNull(message = "创建人不能为空") diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupUpdateRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupUpdateRequest.java index cfa0f92..4dcf6c9 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupUpdateRequest.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/dto/ShopManageGroupUpdateRequest.java @@ -2,9 +2,10 @@ package com.nanri.aiimage.modules.shopkey.model.dto; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; import lombok.Data; +import java.util.List; + @Data public class ShopManageGroupUpdateRequest { @@ -12,7 +13,6 @@ public class ShopManageGroupUpdateRequest { @NotBlank(message = "分组名称不能为空") private String groupName; - @Schema(description = "关联用户ID", requiredMode = Schema.RequiredMode.REQUIRED) - @NotNull(message = "关联用户不能为空") - private Long userId; + @Schema(description = "组员用户ID列表") + private List memberUserIds; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/entity/ShopManageGroupMemberEntity.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/entity/ShopManageGroupMemberEntity.java new file mode 100644 index 0000000..c22fccb --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/entity/ShopManageGroupMemberEntity.java @@ -0,0 +1,26 @@ +package com.nanri.aiimage.modules.shopkey.model.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("biz_shop_manage_group_member") +public class ShopManageGroupMemberEntity { + + @TableId(type = IdType.AUTO) + private Long id; + + @TableField("group_id") + private Long groupId; + + @TableField("user_id") + private Long userId; + + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/vo/ShopManageGroupItemVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/vo/ShopManageGroupItemVo.java index 3f6de7f..0d2aa0e 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/vo/ShopManageGroupItemVo.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/model/vo/ShopManageGroupItemVo.java @@ -3,11 +3,17 @@ package com.nanri.aiimage.modules.shopkey.model.vo; import lombok.Data; import java.time.LocalDateTime; +import java.util.List; @Data public class ShopManageGroupItemVo { private Long id; private String groupName; + private Long leaderUserId; + private String leaderUsername; + private List memberUserIds; + private List memberUsernames; + private Integer memberCount; private Long userId; private String username; private LocalDateTime createdAt; diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java index b18f70d..7aae45f 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java @@ -5,26 +5,37 @@ import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.modules.permission.mapper.AdminUserMapper; import com.nanri.aiimage.modules.permission.model.entity.AdminUserEntity; import com.nanri.aiimage.modules.shopkey.mapper.ShopManageGroupMapper; +import com.nanri.aiimage.modules.shopkey.mapper.ShopManageGroupMemberMapper; import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper; +import com.nanri.aiimage.modules.shopkey.mapper.SkipPriceAsinMapper; import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageGroupCreateRequest; import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageGroupUpdateRequest; import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageEntity; import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageGroupEntity; +import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageGroupMemberEntity; +import com.nanri.aiimage.modules.shopkey.model.entity.SkipPriceAsinEntity; import com.nanri.aiimage.modules.shopkey.model.vo.ShopManageGroupItemVo; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class ShopManageGroupService { private final ShopManageGroupMapper groupMapper; + private final ShopManageGroupMemberMapper groupMemberMapper; private final ShopManageMapper shopManageMapper; + private final SkipPriceAsinMapper skipPriceAsinMapper; private final AdminUserMapper adminUserMapper; public List list() { @@ -32,28 +43,22 @@ public class ShopManageGroupService { } public List list(Long createdById) { - return groupMapper.selectList(new LambdaQueryWrapper() - .eq(createdById != null && createdById > 0, ShopManageGroupEntity::getCreatedById, createdById) - .orderByAsc(ShopManageGroupEntity::getId)) - .stream() - .map(this::toVo) - .toList(); + return toVoList(groupMapper.selectList(new LambdaQueryWrapper() + .eq(createdById != null && createdById > 0, ShopManageGroupEntity::getCreatedById, createdById) + .orderByAsc(ShopManageGroupEntity::getId))); } public List listAccessible(Long operatorId, boolean superAdmin) { if (superAdmin) { return list(); } - Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空"); - return groupMapper.selectList(new LambdaQueryWrapper() - .and(wrapper -> wrapper - .eq(ShopManageGroupEntity::getCreatedById, normalizedOperatorId) - .or() - .eq(ShopManageGroupEntity::getUserId, normalizedOperatorId)) - .orderByAsc(ShopManageGroupEntity::getId)) - .stream() - .map(this::toVo) - .toList(); + Set groupIds = listAccessibleGroupIds(operatorId, false); + if (groupIds.isEmpty()) { + return List.of(); + } + return toVoList(groupMapper.selectList(new LambdaQueryWrapper() + .in(ShopManageGroupEntity::getId, groupIds) + .orderByAsc(ShopManageGroupEntity::getId))); } public Set listAccessibleGroupIds(Long operatorId, boolean superAdmin) { @@ -63,69 +68,80 @@ public class ShopManageGroupService { .stream() .map(ShopManageGroupEntity::getId) .filter(id -> id != null && id > 0) - .collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new)); + .collect(Collectors.toCollection(LinkedHashSet::new)); } Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空"); - return groupMapper.selectList(new LambdaQueryWrapper() + LinkedHashSet groupIds = groupMapper.selectList(new LambdaQueryWrapper() .select(ShopManageGroupEntity::getId) - .and(wrapper -> wrapper - .eq(ShopManageGroupEntity::getCreatedById, normalizedOperatorId) - .or() - .eq(ShopManageGroupEntity::getUserId, normalizedOperatorId))) + .eq(ShopManageGroupEntity::getCreatedById, normalizedOperatorId)) .stream() .map(ShopManageGroupEntity::getId) .filter(id -> id != null && id > 0) - .collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new)); + .collect(Collectors.toCollection(LinkedHashSet::new)); + groupIds.addAll(groupMemberMapper.selectList(new LambdaQueryWrapper() + .select(ShopManageGroupMemberEntity::getGroupId) + .eq(ShopManageGroupMemberEntity::getUserId, normalizedOperatorId)) + .stream() + .map(ShopManageGroupMemberEntity::getGroupId) + .filter(id -> id != null && id > 0) + .collect(Collectors.toCollection(LinkedHashSet::new))); + return groupIds; } @Transactional public ShopManageGroupItemVo create(ShopManageGroupCreateRequest request, Long operatorId, boolean superAdmin) { Long createdById = normalizePositiveId(request.getCreatedById(), "创建人不能为空"); - AdminUserEntity bindUser = getBindableUser(request.getUserId(), operatorId, superAdmin); + validateLeaderOperator(createdById, operatorId, superAdmin); String groupName = normalizeRequired(request.getGroupName(), "分组名称不能为空"); - ensureGroupNameMatchesUser(groupName, bindUser); ensureGroupNameUnique(groupName, null); - ensureUserUnique(request.getUserId(), null); ShopManageGroupEntity entity = new ShopManageGroupEntity(); entity.setGroupName(groupName); - entity.setUserId(bindUser.getId()); entity.setCreatedById(createdById); groupMapper.insert(entity); + replaceMembers(entity.getId(), request.getMemberUserIds(), createdById, operatorId, superAdmin); return toVo(getById(entity.getId())); } @Transactional public ShopManageGroupItemVo update(Long id, Long operatorId, boolean superAdmin, ShopManageGroupUpdateRequest request) { - ShopManageGroupEntity entity = getById(id); - validateOwnership(entity, operatorId, superAdmin); - AdminUserEntity bindUser = getBindableUser(request.getUserId(), operatorId, superAdmin); + ShopManageGroupEntity entity = getManageableById(id, operatorId, superAdmin); String groupName = normalizeRequired(request.getGroupName(), "分组名称不能为空"); - ensureGroupNameMatchesUser(groupName, bindUser); ensureGroupNameUnique(groupName, id); - ensureUserUnique(bindUser.getId(), id); entity.setGroupName(groupName); - entity.setUserId(bindUser.getId()); groupMapper.updateById(entity); + replaceMembers(entity.getId(), request.getMemberUserIds(), entity.getCreatedById(), operatorId, superAdmin); return toVo(getById(id)); } @Transactional public void delete(Long id, Long operatorId, boolean superAdmin) { - ShopManageGroupEntity entity = getById(id); - validateOwnership(entity, operatorId, superAdmin); + ShopManageGroupEntity entity = getManageableById(id, operatorId, superAdmin); Long useCount = shopManageMapper.selectCount(new LambdaQueryWrapper() .eq(ShopManageEntity::getGroupId, entity.getId())); if (useCount != null && useCount > 0) { throw new BusinessException("该分组下存在店铺,无法删除"); } + Long skipPriceAsinCount = skipPriceAsinMapper.selectCount(new LambdaQueryWrapper() + .eq(SkipPriceAsinEntity::getGroupId, entity.getId())); + if (skipPriceAsinCount != null && skipPriceAsinCount > 0) { + throw new BusinessException("该分组下存在跳过跟价 ASIN,无法删除"); + } + groupMemberMapper.delete(new LambdaQueryWrapper() + .eq(ShopManageGroupMemberEntity::getGroupId, entity.getId())); groupMapper.deleteById(entity.getId()); } public ShopManageGroupEntity getAccessibleById(Long id, Long operatorId, boolean superAdmin) { ShopManageGroupEntity entity = getById(id); - validateOwnership(entity, operatorId, superAdmin); + validateAccess(entity, operatorId, superAdmin); + return entity; + } + + public ShopManageGroupEntity getManageableById(Long id, Long operatorId, boolean superAdmin) { + ShopManageGroupEntity entity = getById(id); + validateManageAccess(entity, operatorId, superAdmin); return entity; } @@ -149,56 +165,105 @@ public class ShopManageGroupService { } } - private void ensureUserUnique(Long userId, Long excludeId) { - LambdaQueryWrapper query = new LambdaQueryWrapper() - .eq(ShopManageGroupEntity::getUserId, userId); - if (excludeId != null) { - query.ne(ShopManageGroupEntity::getId, excludeId); - } - Long count = groupMapper.selectCount(query); - if (count != null && count > 0) { - throw new BusinessException("该用户已绑定分组"); - } - } - - private void ensureGroupNameMatchesUser(String groupName, AdminUserEntity bindUser) { - if (bindUser == null || bindUser.getUsername() == null || !groupName.equals(bindUser.getUsername().trim())) { - throw new BusinessException("分组名称必须与关联用户名一致"); - } - } - - private void validateOwnership(ShopManageGroupEntity entity, Long operatorId, boolean superAdmin) { + private void validateAccess(ShopManageGroupEntity entity, Long operatorId, boolean superAdmin) { if (superAdmin) { return; } Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空"); boolean createdBySelf = entity.getCreatedById() != null && entity.getCreatedById().equals(normalizedOperatorId); - boolean boundToSelf = entity.getUserId() != null && entity.getUserId().equals(normalizedOperatorId); - if (!createdBySelf && !boundToSelf) { + boolean memberOfGroup = isGroupMember(entity.getId(), normalizedOperatorId); + if (!createdBySelf && !memberOfGroup) { throw new BusinessException("只能操作自己有权限的分组数据"); } } - private AdminUserEntity getBindableUser(Long userId, Long operatorId, boolean superAdmin) { - Long normalizedUserId = normalizePositiveId(userId, "关联用户不能为空"); - AdminUserEntity user = adminUserMapper.selectById(normalizedUserId); - if (user == null) { - throw new BusinessException("关联用户不存在"); - } + private void validateManageAccess(ShopManageGroupEntity entity, Long operatorId, boolean superAdmin) { if (superAdmin) { - return user; + return; } Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空"); - boolean self = user.getId() != null && user.getId().equals(normalizedOperatorId); - boolean createdBySelf = user.getCreatedById() != null && user.getCreatedById().equals(normalizedOperatorId); - if (!self && !createdBySelf) { - throw new BusinessException("只能关联自己可见的用户"); + boolean createdBySelf = entity.getCreatedById() != null && entity.getCreatedById().equals(normalizedOperatorId); + if (!createdBySelf) { + throw new BusinessException("只有组长才能维护分组"); } - return user; + } + + private void validateLeaderOperator(Long leaderId, Long operatorId, boolean superAdmin) { + if (superAdmin) { + return; + } + Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空"); + if (!normalizedOperatorId.equals(leaderId)) { + throw new BusinessException("普通管理员只能创建自己作为组长的分组"); + } + } + + private boolean isGroupMember(Long groupId, Long userId) { + if (groupId == null || userId == null || groupId <= 0 || userId <= 0) { + return false; + } + Long count = groupMemberMapper.selectCount(new LambdaQueryWrapper() + .eq(ShopManageGroupMemberEntity::getGroupId, groupId) + .eq(ShopManageGroupMemberEntity::getUserId, userId)); + return count != null && count > 0; + } + + private void replaceMembers(Long groupId, List memberUserIds, Long leaderId, Long operatorId, boolean superAdmin) { + List members = resolveMembers(memberUserIds, leaderId, operatorId, superAdmin, groupId); + groupMemberMapper.delete(new LambdaQueryWrapper() + .eq(ShopManageGroupMemberEntity::getGroupId, groupId)); + for (AdminUserEntity member : members) { + ShopManageGroupMemberEntity entity = new ShopManageGroupMemberEntity(); + entity.setGroupId(groupId); + entity.setUserId(member.getId()); + groupMemberMapper.insert(entity); + } + } + + private List resolveMembers(List memberUserIds, Long leaderId, Long operatorId, + boolean superAdmin, Long currentGroupId) { + validateLeaderOperator(leaderId, operatorId, superAdmin); + if (memberUserIds == null || memberUserIds.isEmpty()) { + return List.of(); + } + AdminUserEntity operatorUser = operatorId == null ? null : adminUserMapper.selectById(operatorId); + boolean operatorCanBindAllNormals = superAdmin + || (operatorUser != null && "admin".equalsIgnoreCase(normalizeBlank(operatorUser.getUsername()))); + LinkedHashSet uniqueIds = new LinkedHashSet<>(); + for (Long memberUserId : memberUserIds) { + uniqueIds.add(normalizePositiveId(memberUserId, "组员用户不能为空")); + } + List members = new ArrayList<>(); + for (Long memberUserId : uniqueIds) { + AdminUserEntity user = adminUserMapper.selectById(memberUserId); + if (user == null) { + throw new BusinessException("组员用户不存在"); + } + if (leaderId != null && leaderId.equals(user.getId())) { + throw new BusinessException("组长不能作为组员重复加入分组"); + } + if (!superAdmin && !"normal".equals(normalizeBlank(user.getRole()))) { + throw new BusinessException("只能绑定普通员工账号"); + } + if (!operatorCanBindAllNormals && (user.getCreatedById() == null || !user.getCreatedById().equals(leaderId))) { + throw new BusinessException("只能添加组长自己创建的普通员工账号"); + } + LambdaQueryWrapper query = new LambdaQueryWrapper() + .eq(ShopManageGroupMemberEntity::getUserId, user.getId()); + if (currentGroupId != null) { + query.ne(ShopManageGroupMemberEntity::getGroupId, currentGroupId); + } + Long count = groupMemberMapper.selectCount(query); + if (count != null && count > 0) { + throw new BusinessException("该普通员工账号已加入其他分组"); + } + members.add(user); + } + return members; } private String normalizeRequired(String value, String message) { - String normalized = value == null ? "" : value.trim(); + String normalized = normalizeBlank(value); if (normalized.isEmpty()) { throw new BusinessException(message); } @@ -212,15 +277,82 @@ public class ShopManageGroupService { return value; } + private String normalizeBlank(String value) { + return value == null ? "" : value.trim(); + } + private ShopManageGroupItemVo toVo(ShopManageGroupEntity entity) { - ShopManageGroupItemVo vo = new ShopManageGroupItemVo(); - vo.setId(entity.getId()); - vo.setGroupName(entity.getGroupName()); - vo.setUserId(entity.getUserId()); - AdminUserEntity bindUser = entity.getUserId() == null ? null : adminUserMapper.selectById(entity.getUserId()); - vo.setUsername(bindUser == null ? "" : (bindUser.getUsername() == null ? "" : bindUser.getUsername())); - vo.setCreatedAt(entity.getCreatedAt()); - vo.setUpdatedAt(entity.getUpdatedAt()); - return vo; + return toVoList(List.of(entity)).stream().findFirst().orElseThrow(); + } + + private List toVoList(List groups) { + if (groups == null || groups.isEmpty()) { + return List.of(); + } + + List groupIds = groups.stream() + .map(ShopManageGroupEntity::getId) + .filter(id -> id != null && id > 0) + .toList(); + Map> membersByGroupId = groupIds.isEmpty() + ? Map.of() + : groupMemberMapper.selectList(new LambdaQueryWrapper() + .in(ShopManageGroupMemberEntity::getGroupId, groupIds) + .orderByAsc(ShopManageGroupMemberEntity::getId)) + .stream() + .collect(Collectors.groupingBy( + ShopManageGroupMemberEntity::getGroupId, + LinkedHashMap::new, + Collectors.toList())); + + LinkedHashSet userIds = new LinkedHashSet<>(); + for (ShopManageGroupEntity group : groups) { + if (group.getCreatedById() != null && group.getCreatedById() > 0) { + userIds.add(group.getCreatedById()); + } + } + for (List members : membersByGroupId.values()) { + for (ShopManageGroupMemberEntity member : members) { + if (member.getUserId() != null && member.getUserId() > 0) { + userIds.add(member.getUserId()); + } + } + } + + Map userById = userIds.isEmpty() + ? Map.of() + : adminUserMapper.selectBatchIds(userIds).stream() + .filter(user -> user.getId() != null) + .collect(Collectors.toMap(AdminUserEntity::getId, user -> user, (left, right) -> left, LinkedHashMap::new)); + + List result = new ArrayList<>(); + for (ShopManageGroupEntity entity : groups) { + ShopManageGroupItemVo vo = new ShopManageGroupItemVo(); + vo.setId(entity.getId()); + vo.setGroupName(entity.getGroupName()); + vo.setLeaderUserId(entity.getCreatedById()); + AdminUserEntity leaderUser = entity.getCreatedById() == null ? null : userById.get(entity.getCreatedById()); + vo.setLeaderUsername(leaderUser == null ? "" : normalizeBlank(leaderUser.getUsername())); + + List memberEntities = membersByGroupId.getOrDefault(entity.getId(), Collections.emptyList()); + List memberUserIds = memberEntities.stream() + .map(ShopManageGroupMemberEntity::getUserId) + .filter(id -> id != null && id > 0) + .toList(); + List memberUsernames = memberUserIds.stream() + .map(userById::get) + .map(user -> user == null ? "" : normalizeBlank(user.getUsername())) + .toList(); + + vo.setMemberUserIds(memberUserIds); + vo.setMemberUsernames(memberUsernames); + vo.setMemberCount(memberUserIds.size()); + vo.setUserId(memberUserIds.isEmpty() ? null : memberUserIds.get(0)); + vo.setUsername(memberUsernames.isEmpty() ? "" : memberUsernames.get(0)); + vo.setCreatedAt(entity.getCreatedAt()); + vo.setUpdatedAt(entity.getUpdatedAt()); + result.add(vo); + } + return result; } } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/controller/ShopMatchController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/controller/ShopMatchController.java index afd9300..3d43e77 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/controller/ShopMatchController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/controller/ShopMatchController.java @@ -173,7 +173,10 @@ public class ShopMatchController { @Operation(summary = "提交匹配结果", description = "Python 可多次分批回传结果;后端会组装文件并在超时场景自动补偿收尾。") public ApiResponse submitResult( @Parameter(description = "任务主键", example = "200") @PathVariable Long taskId, - @Valid @RequestBody ShopMatchSubmitResultRequest request) { + @Valid @RequestBody ShopMatchSubmitResultRequest request, + jakarta.servlet.http.HttpServletResponse response) { + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setContentType("application/json;charset=UTF-8"); shopMatchTaskService.submitResult(taskId, request); return ApiResponse.success(null); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskCacheService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskCacheService.java index 1f98db7..9925eae 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskCacheService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskCacheService.java @@ -2,7 +2,9 @@ package com.nanri.aiimage.modules.shopmatch.service; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchShopPayloadDto; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -14,10 +16,12 @@ import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.security.MessageDigest; import java.time.Duration; +import java.util.ArrayList; import java.util.Comparator; import java.util.HexFormat; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; @Service @RequiredArgsConstructor @@ -25,8 +29,9 @@ import java.util.Map; public class ShopMatchTaskCacheService { private static final long PAYLOAD_TTL_HOURS = 24; - private final ObjectMapper objectMapper; + private final TaskPressureProperties taskPressureProperties; + private final ConcurrentHashMap taskEntityLocalCache = new ConcurrentHashMap<>(); public ShopMatchShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) { if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank()) { @@ -127,9 +132,11 @@ public class ShopMatchTaskCacheService { if (taskId == null || taskId <= 0) { return; } + taskEntityLocalCache.remove(taskId); try { Path taskDir = buildTaskDir(taskId); if (!Files.exists(taskDir)) { + Files.deleteIfExists(buildTaskEntityFile(taskId)); return; } try (var walk = Files.walk(taskDir)) { @@ -145,6 +152,69 @@ public class ShopMatchTaskCacheService { } catch (Exception ex) { log.warn("[shop-match] delete task cache failed taskId={} msg={}", taskId, ex.getMessage()); } + try { + Files.deleteIfExists(buildTaskEntityFile(taskId)); + } catch (IOException ignored) { + } + } + + public void saveTaskCache(FileTaskEntity task) { + if (task == null || task.getId() == null) { + return; + } + long now = System.currentTimeMillis(); + taskEntityLocalCache.put(task.getId(), new LocalTaskEntityCacheEntry( + now, + objectMapper.convertValue(task, FileTaskEntity.class) + )); + try { + Files.createDirectories(buildTaskDir(task.getId())); + Files.writeString( + buildTaskEntityFile(task.getId()), + objectMapper.writeValueAsString(task), + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING, + StandardOpenOption.WRITE + ); + } catch (Exception ignored) { + } + } + + public Map getTaskCacheBatch(java.util.List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + java.util.List normalized = taskIds.stream() + .filter(id -> id != null && id > 0) + .distinct() + .toList(); + if (normalized.isEmpty()) { + return result; + } + long now = System.currentTimeMillis(); + java.util.List missingIds = new ArrayList<>(); + for (Long taskId : normalized) { + LocalTaskEntityCacheEntry cached = taskEntityLocalCache.get(taskId); + if (isLocalCacheFresh(cached, now)) { + result.put(taskId, objectMapper.convertValue(cached.task(), FileTaskEntity.class)); + } else { + missingIds.add(taskId); + } + } + for (Long taskId : missingIds) { + Path file = buildTaskEntityFile(taskId); + if (!Files.isRegularFile(file)) { + continue; + } + try { + FileTaskEntity task = objectMapper.readValue(Files.readString(file), FileTaskEntity.class); + result.put(taskId, task); + taskEntityLocalCache.put(taskId, new LocalTaskEntityCacheEntry(now, task)); + } catch (Exception ignored) { + } + } + return result; } public Duration getPayloadTtl() { @@ -159,6 +229,10 @@ public class ShopMatchTaskCacheService { return buildTaskDir(taskId).resolve(hashShopKey(shopKey) + ".json"); } + private Path buildTaskEntityFile(Long taskId) { + return buildTaskDir(taskId).resolve("_task-entity.json"); + } + private String hashShopKey(String shopKey) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); @@ -181,4 +255,11 @@ public class ShopMatchTaskCacheService { this.payload = payload; } } + + private boolean isLocalCacheFresh(LocalTaskEntityCacheEntry cached, long now) { + return cached != null + && now - cached.cachedAtMillis() <= Math.max(0L, taskPressureProperties.getLocalTaskEntityCacheMillis()); + } + + private record LocalTaskEntityCacheEntry(long cachedAtMillis, FileTaskEntity task) {} } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskService.java index c308662..1b213c9 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopmatch/service/ShopMatchTaskService.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.IdUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCreateTaskVo; @@ -60,6 +61,71 @@ public class ShopMatchTaskService { private final OssStorageService ossStorageService; private final ObjectMapper objectMapper; private final ShopMatchTaskCacheService shopMatchTaskCacheService; + private final TaskPressureProperties taskPressureProperties; + + private FileTaskEntity loadTaskForExecution(Long taskId) { + Map cachedTasks = shopMatchTaskCacheService.getTaskCacheBatch(List.of(taskId)); + FileTaskEntity cached = cachedTasks.get(taskId); + if (cached != null) { + if (!MODULE_TYPE.equals(cached.getModuleType()) + || (cached.getRequestJson() != null && !cached.getRequestJson().isBlank())) { + return cached; + } + FileTaskEntity dbTask = fileTaskMapper.selectById(taskId); + if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) { + shopMatchTaskCacheService.saveTaskCache(dbTask); + return dbTask; + } + return cached; + } + FileTaskEntity dbTask = fileTaskMapper.selectById(taskId); + if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) { + shopMatchTaskCacheService.saveTaskCache(dbTask); + } + return dbTask; + } + + private Map loadTaskMapByIds(List taskIds) { + Map result = new LinkedHashMap<>(); + if (taskIds == null || taskIds.isEmpty()) { + return result; + } + List normalizedTaskIds = taskIds.stream() + .filter(taskId -> taskId != null && taskId > 0) + .distinct() + .toList(); + if (normalizedTaskIds.isEmpty()) { + return result; + } + Map cachedTasks = shopMatchTaskCacheService.getTaskCacheBatch(normalizedTaskIds); + result.putAll(cachedTasks); + List missingTaskIds = normalizedTaskIds.stream() + .filter(taskId -> !cachedTasks.containsKey(taskId)) + .toList(); + for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) { + if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) { + continue; + } + result.put(dbTask.getId(), dbTask); + if ("RUNNING".equals(dbTask.getStatus()) || "SCHEDULED".equals(dbTask.getStatus())) { + shopMatchTaskCacheService.saveTaskCache(dbTask); + } + } + return result; + } + + private List selectTasksByIdsInBatches(List taskIds) { + List tasks = new ArrayList<>(); + if (taskIds == null || taskIds.isEmpty()) { + return tasks; + } + int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize()); + for (int start = 0; start < taskIds.size(); start += batchSize) { + int end = Math.min(start + batchSize, taskIds.size()); + tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end))); + } + return tasks; + } public ProductRiskDashboardVo dashboard(Long userId) { if (userId == null || userId <= 0) { @@ -103,7 +169,7 @@ public class ShopMatchTaskService { .distinct() .toList(); if (!taskIds.isEmpty()) { - for (FileTaskEntity task : fileTaskMapper.selectBatchIds(taskIds)) { + for (FileTaskEntity task : selectTasksByIdsInBatches(taskIds)) { if (task != null) { statusByTaskId.put(task.getId(), task.getStatus()); } @@ -138,7 +204,7 @@ public class ShopMatchTaskService { if (userId == null || userId <= 0) { throw new BusinessException("user_id 不合法"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { throw new BusinessException("任务不存在"); } @@ -150,7 +216,7 @@ public class ShopMatchTaskService { } private void reconcileTaskAfterResultRemoval(Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { return; } @@ -171,12 +237,13 @@ public class ShopMatchTaskService { if (taskIds == null || taskIds.isEmpty()) { return batch; } + Map taskMap = loadTaskMapByIds(taskIds); for (Long taskId : taskIds) { if (taskId == null || taskId <= 0) { continue; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { batch.getMissingTaskIds().add(taskId); continue; } @@ -190,12 +257,13 @@ public class ShopMatchTaskService { if (taskIds == null || taskIds.isEmpty()) { return batch; } + Map taskMap = loadTaskMapByIds(taskIds); for (Long taskId : taskIds) { if (taskId == null || taskId <= 0) { continue; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + FileTaskEntity task = taskMap.get(taskId); + if (task == null) { batch.getMissingTaskIds().add(taskId); continue; } @@ -270,6 +338,7 @@ public class ShopMatchTaskService { task.setUpdatedAt(now); task.setScheduledAt(scheduleTimes.isEmpty() ? null : scheduleTimes.get(0)); fileTaskMapper.insert(task); + shopMatchTaskCacheService.saveTaskCache(task); List snapshot = new ArrayList<>(); for (ProductRiskShopQueueItemVo item : uniqueItems) { @@ -294,7 +363,7 @@ public class ShopMatchTaskService { } catch (Exception ex) { throw new BusinessException("task 不存在"); } - fileTaskMapper.updateById(task); + updateTaskAndRefreshCache(task); ProductRiskCreateTaskVo vo = new ProductRiskCreateTaskVo(); vo.setTaskId(task.getId()); @@ -313,7 +382,7 @@ public class ShopMatchTaskService { if (stageIndex == null || stageIndex < 0) { throw new BusinessException("stage_index 不合法"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { throw new BusinessException("任务不存在"); } @@ -341,7 +410,7 @@ public class ShopMatchTaskService { task.setStatus("RUNNING"); task.setUpdatedAt(now); persistTaskRequest(task, state); - fileTaskMapper.updateById(task); + updateTaskAndRefreshCache(task); } @Transactional @@ -355,7 +424,7 @@ public class ShopMatchTaskService { if (request == null || request.getStageIndex() == null || request.getStageIndex() < 0) { throw new BusinessException("stage_index 不合法"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType()) || !userId.equals(task.getUserId())) { throw new BusinessException("任务不存在"); } @@ -377,7 +446,7 @@ public class ShopMatchTaskService { task.setUpdatedAt(now()); task.setFinishedAt(null); persistTaskRequest(task, state); - fileTaskMapper.updateById(task); + updateTaskAndRefreshCache(task); } @@ -390,7 +459,7 @@ public class ShopMatchTaskService { throw new BusinessException("shops 不能为空"); } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { throw new BusinessException("任务不存在"); } @@ -460,7 +529,7 @@ public class ShopMatchTaskService { if (taskId == null || taskId <= 0) { return false; } - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { return false; } @@ -640,7 +709,7 @@ public class ShopMatchTaskService { } catch (Exception ex) { log.warn("[shop-match] compact result json failed: {}", ex.getMessage()); } - fileTaskMapper.updateById(task); + updateTaskAndRefreshCache(task); if ("SUCCESS".equals(task.getStatus()) || "FAILED".equals(task.getStatus())) { shopMatchTaskCacheService.deleteTaskCache(task.getId()); } @@ -799,12 +868,12 @@ public class ShopMatchTaskService { } private LocalDateTime loadScheduledAt(Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); return task == null ? null : task.getScheduledAt(); } private void mergeQueueFieldsFromRequest(ProductRiskResultItemVo vo, Long taskId) { - FileTaskEntity task = fileTaskMapper.selectById(taskId); + FileTaskEntity task = loadTaskForExecution(taskId); ShopMatchCreateTaskRequest request = parseTaskRequestSilently(task); if (request == null) { return; @@ -849,7 +918,8 @@ public class ShopMatchTaskService { } try { return objectMapper.readValue(task.getRequestJson(), ShopMatchCreateTaskRequest.class); - } catch (Exception ignored) { + } catch (Exception ex) { + log.warn("[shop-match] parse task request failed taskId={} msg={}", task.getId(), ex.getMessage()); return null; } } @@ -862,6 +932,11 @@ public class ShopMatchTaskService { } } + private void updateTaskAndRefreshCache(FileTaskEntity task) { + fileTaskMapper.updateById(task); + shopMatchTaskCacheService.saveTaskCache(task); + } + private static String fmt(LocalDateTime time) { return time == null ? null : time.toString(); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoAuthService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoAuthService.java index 6b137e3..bc206da 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoAuthService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoAuthService.java @@ -142,6 +142,26 @@ public class ZiniaoAuthService { return getOrLoadUserStores(apiKey, companyId, userId); } +public boolean isIpWhitelistError(BusinessException ex) { + String message = ex == null ? null : ex.getMessage(); + if (message == null || message.isBlank()) { + return false; + } + String lowerMessage = message.toLowerCase(); + return message.contains("白名单") + || message.contains("IP白名单") + || message.contains("ip白名单") + || message.contains("当前ip") + || message.contains("当前IP") + || message.contains("禁止当前ip") + || message.contains("禁止当前IP") + || message.contains("禁止访问") + || lowerMessage.contains("whitelist") + || lowerMessage.contains("current ip") + || lowerMessage.contains("forbid") + || lowerMessage.contains("forbidden"); + } + public boolean isInvalidUserStoresError(BusinessException ex) { String message = ex == null ? null : ex.getMessage(); if (message == null || message.isBlank()) { diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoShopIndexService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoShopIndexService.java index dad6137..40339bb 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoShopIndexService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/ziniao/service/ZiniaoShopIndexService.java @@ -190,12 +190,19 @@ public class ZiniaoShopIndexService { try { companyId = ziniaoAuthService.resolveCompanyIdForIndex(apiKey); } catch (BusinessException ex) { + rethrowIfIpWhitelistError(ex); log.warn("[ziniao-index] skip apiKey while resolving companyId, msg={}", ex.getMessage()); continue; } Set invalidUserIds = new LinkedHashSet<>(); - List staff = ziniaoAuthService.getOrLoadStaffForIndex(apiKey, companyId); + List staff; + try { + staff = ziniaoAuthService.getOrLoadStaffForIndex(apiKey, companyId); + } catch (BusinessException ex) { + rethrowIfIpWhitelistError(ex); + throw ex; + } for (Long userId : buildUserIds(staff)) { if (invalidUserIds.contains(userId)) { continue; @@ -204,6 +211,7 @@ public class ZiniaoShopIndexService { try { stores = ziniaoAuthService.getOrLoadUserStoresForIndex(apiKey, companyId, userId); } catch (BusinessException ex) { + rethrowIfIpWhitelistError(ex); if (ziniaoAuthService.isInvalidUserStoresError(ex)) { invalidUserIds.add(userId); allInvalidUserIds.add(userId); @@ -662,4 +670,10 @@ public class ZiniaoShopIndexService { throw new BusinessException("生成紫鸟 apiKey 缓存键失败"); } } + + private void rethrowIfIpWhitelistError(BusinessException ex) { + if (ziniaoAuthService.isIpWhitelistError(ex)) { + throw new BusinessException("紫鸟刷新店铺索引失败:当前服务器 IP 未加入紫鸟白名单,已停止本轮刷新且不会更新索引数据"); + } + } } diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index 26247eb..faf0ee3 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -70,9 +70,11 @@ aiimage: storage: local-temp-dir: ${AIIMAGE_STORAGE_LOCAL_TEMP_DIR:./data/tmp} cleanup-enabled: ${AIIMAGE_STORAGE_CLEANUP_ENABLED:true} - cleanup-cron: ${AIIMAGE_STORAGE_CLEANUP_CRON:0 0 */6 * * *} - source-retention-hours: ${AIIMAGE_STORAGE_SOURCE_RETENTION_HOURS:48} + cleanup-cron: ${AIIMAGE_STORAGE_CLEANUP_CRON:0 0 * * * *} + source-retention-hours: ${AIIMAGE_STORAGE_SOURCE_RETENTION_HOURS:24} result-retention-hours: ${AIIMAGE_STORAGE_RESULT_RETENTION_HOURS:24} + temp-dir: + retention-hours: ${AIIMAGE_TEMP_DIR_RETENTION_HOURS:24} brand-progress: ttl-hours: ${AIIMAGE_BRAND_PROGRESS_TTL_HOURS:24} failed-ttl-hours: ${AIIMAGE_BRAND_PROGRESS_FAILED_TTL_HOURS:2} @@ -88,10 +90,15 @@ aiimage: price-track-initial-timeout-minutes: ${AIIMAGE_PRICE_TRACK_INITIAL_TIMEOUT_MINUTES:20} shop-match-stale-timeout-minutes: ${AIIMAGE_SHOP_MATCH_STALE_TIMEOUT_MINUTES:20} shop-match-initial-timeout-minutes: ${AIIMAGE_SHOP_MATCH_INITIAL_TIMEOUT_MINUTES:20} + patrol-delete-stale-timeout-minutes: ${AIIMAGE_PATROL_DELETE_STALE_TIMEOUT_MINUTES:20} + patrol-delete-initial-timeout-minutes: ${AIIMAGE_PATROL_DELETE_INITIAL_TIMEOUT_MINUTES:20} module-cleanup: enabled: ${AIIMAGE_MODULE_CLEANUP_ENABLED:true} cron: ${AIIMAGE_MODULE_CLEANUP_CRON:0 0 0 * * *} - module-types: ${AIIMAGE_MODULE_CLEANUP_MODULE_TYPES:DEDUPE,SPLIT,CONVERT,DELETE_BRAND,PRODUCT_RISK_RESOLVE,PRICE_TRACK,SHOP_MATCH} + module-types: ${AIIMAGE_MODULE_CLEANUP_MODULE_TYPES:DEDUPE,SPLIT,CONVERT,DELETE_BRAND,PRODUCT_RISK_RESOLVE,PRICE_TRACK,SHOP_MATCH,PATROL_DELETE} + task-pressure: + local-task-entity-cache-millis: ${AIIMAGE_TASK_LOCAL_ENTITY_CACHE_MILLIS:3000} + db-select-batch-size: ${AIIMAGE_TASK_DB_SELECT_BATCH_SIZE:200} security: shop-credential-key: ${AIIMAGE_SHOP_CREDENTIAL_KEY:change-me-shop-credential-key} internal-token: ${AIIMAGE_INTERNAL_TOKEN:} diff --git a/backend-java/src/main/resources/db/V27__patrol_delete_shop_candidate.sql b/backend-java/src/main/resources/db/V27__patrol_delete_shop_candidate.sql new file mode 100644 index 0000000..460cc0c --- /dev/null +++ b/backend-java/src/main/resources/db/V27__patrol_delete_shop_candidate.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS biz_patrol_delete_shop_candidate ( + id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT 'primary key', + user_id BIGINT NOT NULL COMMENT 'user id', + shop_name VARCHAR(255) NOT NULL COMMENT 'normalized shop name', + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'created time', + UNIQUE KEY uk_user_shop (user_id, shop_name), + KEY idx_user_id (user_id) +) COMMENT='patrol delete candidate shops'; diff --git a/backend-java/src/main/resources/db/V28__shop_manage_group_member.sql b/backend-java/src/main/resources/db/V28__shop_manage_group_member.sql new file mode 100644 index 0000000..f10b817 --- /dev/null +++ b/backend-java/src/main/resources/db/V28__shop_manage_group_member.sql @@ -0,0 +1,17 @@ +CREATE TABLE IF NOT EXISTS biz_shop_manage_group_member ( + id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键', + group_id BIGINT NOT NULL COMMENT '分组ID', + user_id BIGINT NOT NULL COMMENT '组员用户ID', + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + UNIQUE KEY uk_user_id (user_id), + KEY idx_group_id (group_id) +) COMMENT='店铺管理分组成员表'; + +INSERT INTO biz_shop_manage_group_member (group_id, user_id) +SELECT g.id, g.user_id +FROM biz_shop_manage_group g +LEFT JOIN biz_shop_manage_group_member m ON m.user_id = g.user_id +WHERE g.user_id IS NOT NULL + AND g.user_id > 0 + AND m.id IS NULL; diff --git a/backend-java/src/main/resources/logback-spring.xml b/backend-java/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..a36eabb --- /dev/null +++ b/backend-java/src/main/resources/logback-spring.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + ${CONSOLE_PATTERN} + UTF-8 + + + + + ${APP_LOG_FILE} + + ${FILE_PATTERN} + UTF-8 + + + ${APP_LOG_ARCHIVE} + 50MB + 1 + 500MB + true + + + + + + + + + + diff --git a/backend/blueprints/__pycache__/admin_api.cpython-312.pyc b/backend/blueprints/__pycache__/admin_api.cpython-312.pyc index 4b38d6b..da6ab9a 100644 Binary files a/backend/blueprints/__pycache__/admin_api.cpython-312.pyc and b/backend/blueprints/__pycache__/admin_api.cpython-312.pyc differ diff --git a/backend/blueprints/admin_api.py b/backend/blueprints/admin_api.py index c5a0e46..66b6633 100644 --- a/backend/blueprints/admin_api.py +++ b/backend/blueprints/admin_api.py @@ -406,12 +406,13 @@ def list_users(): search_username = (request.args.get('username') or request.args.get('search') or '').strip() created_by_id_arg = request.args.get('created_by_id') or request.args.get('admin_id') created_by_id = int(created_by_id_arg) if created_by_id_arg and str(created_by_id_arg).isdigit() else None - if role != 'super_admin': + can_view_all_users = role == 'super_admin' or (current_row and (current_row.get('username') or '').strip().lower() == 'admin') + if not can_view_all_users: created_by_id = None try: conn = get_db() with conn.cursor() as cur: - if role == 'super_admin': + if can_view_all_users: where_parts = ["1=1"] params = [] if search_username: @@ -1526,6 +1527,11 @@ def list_shop_manage_groups(): { 'id': item.get('id'), 'group_name': item.get('groupName') or '', + 'leader_user_id': item.get('leaderUserId'), + 'leader_username': item.get('leaderUsername') or '', + 'member_user_ids': item.get('memberUserIds') or [], + 'member_usernames': item.get('memberUsernames') or [], + 'member_count': item.get('memberCount') or 0, 'user_id': item.get('userId'), 'username': item.get('username') or '', 'created_at': (item.get('createdAt') or '').replace('T', ' ')[:16], @@ -1545,7 +1551,7 @@ def create_shop_manage_group(): data = request.get_json() or {} payload = { 'groupName': (data.get('group_name') or '').strip(), - 'userId': data.get('user_id'), + 'memberUserIds': data.get('member_user_ids') or [], 'createdById': current_row.get('id') if current_row else None, } result, error_response, status = _proxy_backend_java( @@ -1566,6 +1572,11 @@ def create_shop_manage_group(): 'item': { 'id': item.get('id'), 'group_name': item.get('groupName') or '', + 'leader_user_id': item.get('leaderUserId'), + 'leader_username': item.get('leaderUsername') or '', + 'member_user_ids': item.get('memberUserIds') or [], + 'member_usernames': item.get('memberUsernames') or [], + 'member_count': item.get('memberCount') or 0, 'user_id': item.get('userId'), 'username': item.get('username') or '', 'created_at': (item.get('createdAt') or '').replace('T', ' ')[:16], @@ -1583,7 +1594,7 @@ def update_shop_manage_group(item_id): data = request.get_json() or {} payload = { 'groupName': (data.get('group_name') or '').strip(), - 'userId': data.get('user_id'), + 'memberUserIds': data.get('member_user_ids') or [], } result, error_response, status = _proxy_backend_java( 'PUT', @@ -1603,6 +1614,11 @@ def update_shop_manage_group(item_id): 'item': { 'id': item.get('id'), 'group_name': item.get('groupName') or '', + 'leader_user_id': item.get('leaderUserId'), + 'leader_username': item.get('leaderUsername') or '', + 'member_user_ids': item.get('memberUserIds') or [], + 'member_usernames': item.get('memberUsernames') or [], + 'member_count': item.get('memberCount') or 0, 'user_id': item.get('userId'), 'username': item.get('username') or '', 'created_at': (item.get('createdAt') or '').replace('T', ' ')[:16], diff --git a/backend/web_source/admin.html b/backend/web_source/admin.html index 82837a5..2aa0b12 100644 --- a/backend/web_source/admin.html +++ b/backend/web_source/admin.html @@ -314,6 +314,10 @@ display: flex; } + #shopManageGroupModal { + z-index: 1010; + } + .modal { background: #fff; border-radius: 8px; @@ -948,17 +952,24 @@

分组管理

+
- - + +
- +
+
+ + +
可添加当前组长创建的普通员工账号,按住 Ctrl 或 Command 可多选。
+
+
+
@@ -968,7 +979,9 @@ 序号 分组名称 - 关联用户 + 组长 + 组员数量 + 组员 创建时间 修改时间 操作 @@ -1658,43 +1671,61 @@ sel.value = cur || ''; }); } - var shopManageGroupUsers = []; - function refreshShopManageGroupUserSelect(selectedUserId) { - var sel = document.getElementById('shopManageGroupUserSelect'); - if (!sel) return; - var opts = ['']; - shopManageGroupUsers.forEach(function (u) { - opts.push(''); + var shopManageAllUsers = []; + function getEligibleShopManageGroupUsers(leaderUserId) { + var canViewAllMembers = currentUserRole === 'super_admin'; + return shopManageAllUsers.filter(function (u) { + if (String(u.id || '') === String(leaderUserId || '')) return false; + if (canViewAllMembers) return true; + return String(u.role || '') === 'normal' && + String(u.created_by_id || '') === String(leaderUserId || ''); }); - sel.innerHTML = opts.join(''); - if (selectedUserId != null && selectedUserId !== '') { - sel.value = String(selectedUserId); - } - syncShopManageGroupNameWithUser(); } - function syncShopManageGroupNameWithUser() { - var sel = document.getElementById('shopManageGroupUserSelect'); - var input = document.getElementById('shopManageGroupInput'); - if (!sel || !input) return; - var selectedUser = null; - shopManageGroupUsers.some(function (u) { - if (String(u.id) === String(sel.value || '')) { - selectedUser = u; - return true; - } - return false; + function refreshShopManageGroupMemberSelect(leaderUserId, selectedUserIds) { + var sel = document.getElementById('shopManageGroupMemberSelect'); + if (!sel) return; + var selectedMap = {}; + (selectedUserIds || []).forEach(function (id) { + selectedMap[String(id)] = true; }); - input.value = selectedUser ? (selectedUser.username || '') : ''; + var options = getEligibleShopManageGroupUsers(leaderUserId).map(function (u) { + return ''; + }); + sel.innerHTML = options.join(''); + } + function setShopManageGroupLeader(leaderUserId, leaderUsername, selectedUserIds) { + var leaderIdEl = document.getElementById('shopManageGroupLeaderUserId'); + var leaderNameEl = document.getElementById('shopManageGroupLeaderName'); + if (leaderIdEl) leaderIdEl.value = leaderUserId ? String(leaderUserId) : ''; + if (leaderNameEl) leaderNameEl.value = leaderUsername || ''; + refreshShopManageGroupMemberSelect(leaderUserId, selectedUserIds || []); + } + function getSelectedShopManageGroupMemberIds() { + var sel = document.getElementById('shopManageGroupMemberSelect'); + if (!sel) return []; + return Array.prototype.slice.call(sel.options || []) + .filter(function (opt) { return !!opt.selected; }) + .map(function (opt) { return Number(opt.value); }) + .filter(function (id) { return !!id; }); } function loadUserOptions() { return fetch('/api/admin/users?page=1&page_size=999') .then(function (r) { return r.json(); }) .then(function (res) { var items = res.items || []; - shopManageGroupUsers = items.map(function (u) { - return { id: u.id, username: u.username || '', role: u.role || 'normal' }; + shopManageAllUsers = items.map(function (u) { + return { + id: u.id, + username: u.username || '', + role: u.role || 'normal', + created_by_id: u.created_by_id || null + }; }); - refreshShopManageGroupUserSelect(document.getElementById('shopManageGroupUserSelect') ? document.getElementById('shopManageGroupUserSelect').value : ''); + setShopManageGroupLeader( + document.getElementById('shopManageGroupLeaderUserId') ? document.getElementById('shopManageGroupLeaderUserId').value : '', + document.getElementById('shopManageGroupLeaderName') ? document.getElementById('shopManageGroupLeaderName').value : '', + getSelectedShopManageGroupMemberIds() + ); var sel = document.getElementById('filterUser'); var cur = sel.value; sel.innerHTML = ''; @@ -2163,107 +2194,6 @@ return query; } - function refreshShopGroupSelects(selectedCreateId, selectedEditId) { - var createSel = document.getElementById('shopManageGroupSelect'); - var editSel = document.getElementById('editShopManageGroupSelect'); - var filterSel = document.getElementById('shopManageFilterGroupId'); - var skipCreateSel = document.getElementById('skipPriceAsinGroupSelect'); - var skipFilterSel = document.getElementById('skipPriceAsinFilterGroupId'); - var chooseSkipShopSel = document.getElementById('chooseSkipPriceAsinShopGroupId'); - var selectedFilterId = filterSel ? filterSel.value : ''; - var selectedSkipCreateId = skipCreateSel ? skipCreateSel.value : ''; - var selectedSkipFilterId = skipFilterSel ? skipFilterSel.value : ''; - var selectedChooseSkipShopId = chooseSkipShopSel ? chooseSkipShopSel.value : ''; - var createOpts = ['']; - var filterOpts = ['']; - shopManageGroups.forEach(function (g) { - var option = ''; - createOpts.push(option); - filterOpts.push(option); - }); - createSel.innerHTML = createOpts.join(''); - editSel.innerHTML = createOpts.join(''); - if (skipCreateSel) skipCreateSel.innerHTML = createOpts.join(''); - if (filterSel) filterSel.innerHTML = filterOpts.join(''); - if (skipFilterSel) skipFilterSel.innerHTML = filterOpts.join(''); - if (chooseSkipShopSel) chooseSkipShopSel.innerHTML = filterOpts.join(''); - if (selectedCreateId != null) createSel.value = String(selectedCreateId); - if (selectedEditId != null) editSel.value = String(selectedEditId); - if (filterSel && selectedFilterId) filterSel.value = selectedFilterId; - if (skipCreateSel && selectedSkipCreateId) skipCreateSel.value = selectedSkipCreateId; - if (skipFilterSel && selectedSkipFilterId) skipFilterSel.value = selectedSkipFilterId; - if (chooseSkipShopSel && selectedChooseSkipShopId) chooseSkipShopSel.value = selectedChooseSkipShopId; - } - - function loadShopManageGroups(selectedCreateId, selectedEditId) { - return fetch('/api/admin/shop-manage-groups') - .then(function (r) { return r.json(); }) - .then(function (res) { - if (!res.success) throw new Error(res.error || '加载分组失败'); - shopManageGroups = res.items || []; - refreshShopGroupSelects(selectedCreateId, selectedEditId); - return shopManageGroups; - }) - .catch(function () { - shopManageGroups = []; - refreshShopGroupSelects(); - return []; - }); - } - - function openShopManageGroupModal() { - document.getElementById('shopManageGroupEditId').value = ''; - document.getElementById('shopManageGroupInput').value = ''; - document.getElementById('msgShopManageGroup').textContent = ''; - document.getElementById('msgShopManageGroup').className = 'msg'; - loadShopManageGroups().then(function () { - renderShopManageGroupRows(); - document.getElementById('shopManageGroupModal').classList.add('show'); - }); - } - - function renderShopManageGroupRows() { - var tbody = document.getElementById('shopManageGroupListBody'); - if (!shopManageGroups.length) { - tbody.innerHTML = '暂无分组'; - return; - } - tbody.innerHTML = shopManageGroups.map(function (item, index) { - return '' + (index + 1) + '' + (item.group_name || '') + '' + (item.created_at || '') + '' + (item.updated_at || '') + '' + - ' ' + - '' + - ''; - }).join(''); - - document.querySelectorAll('[data-shop-group-edit]').forEach(function (btn) { - btn.onclick = function () { - document.getElementById('shopManageGroupEditId').value = btn.dataset.shopGroupEdit; - document.getElementById('shopManageGroupInput').value = (btn.dataset.shopGroupName || '').replace(/"/g, '"'); - }; - }); - - document.querySelectorAll('[data-shop-group-delete]').forEach(function (btn) { - btn.onclick = function () { - var gid = btn.dataset.shopGroupDelete; - var gname = (btn.dataset.shopGroupName || '').replace(/"/g, '"'); - if (!confirm('确定删除分组“' + gname + '”吗?')) return; - fetch('/api/admin/shop-manage-group/' + gid, { method: 'DELETE' }) - .then(function (r) { return r.json(); }) - .then(function (res) { - if (!res.success) { - alert(res.error || '删除失败'); - return; - } - loadShopManageGroups().then(function () { - renderShopManageGroupRows(); - loadShopManage(shopManagePage); - loadSkipPriceAsin(skipPriceAsinPage); - }); - }); - }; - }); - } - function loadShopManage(page) { shopManagePage = page || 1; fetch('/api/admin/shop-manages?' + buildShopManageQuery(shopManagePage)) @@ -2325,65 +2255,6 @@ }); } - document.getElementById('btnManageShopGroups').onclick = openShopManageGroupModal; - document.getElementById('btnManageShopGroupsFromEdit').onclick = openShopManageGroupModal; - document.getElementById('btnSearchShopManage').onclick = function () { - loadShopManage(1); - }; - document.getElementById('shopManageFilterShopName').addEventListener('keydown', function (e) { - if (e.key === 'Enter') { - e.preventDefault(); - loadShopManage(1); - } - }); - document.getElementById('btnCloseShopManageGroupModal').onclick = function () { - document.getElementById('shopManageGroupModal').classList.remove('show'); - }; - document.getElementById('btnCancelShopManageGroupEdit').onclick = function () { - document.getElementById('shopManageGroupEditId').value = ''; - document.getElementById('shopManageGroupInput').value = ''; - }; - document.getElementById('btnSaveShopManageGroup').onclick = function () { - var editId = (document.getElementById('shopManageGroupEditId').value || '').trim(); - var groupName = (document.getElementById('shopManageGroupInput').value || '').trim(); - var msgEl = document.getElementById('msgShopManageGroup'); - msgEl.textContent = ''; - msgEl.className = 'msg'; - if (!groupName) { - msgEl.textContent = '请输入分组名称'; - msgEl.className = 'msg err'; - return; - } - var method = editId ? 'PUT' : 'POST'; - var url = editId ? ('/api/admin/shop-manage-group/' + editId) : '/api/admin/shop-manage-group'; - fetch(url, { - method: method, - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ group_name: groupName }) - }) - .then(function (r) { return r.json(); }) - .then(function (res) { - if (!res.success) { - msgEl.textContent = res.error || '保存失败'; - msgEl.className = 'msg err'; - return; - } - document.getElementById('shopManageGroupEditId').value = ''; - document.getElementById('shopManageGroupInput').value = ''; - msgEl.textContent = res.msg || '保存成功'; - msgEl.className = 'msg ok'; - loadShopManageGroups().then(function () { - renderShopManageGroupRows(); - loadShopManage(shopManagePage); - loadSkipPriceAsin(skipPriceAsinPage); - }); - }) - .catch(function () { - msgEl.textContent = '请求失败'; - msgEl.className = 'msg err'; - }); - }; - function refreshShopGroupSelects(selectedCreateId, selectedEditId) { var createSel = document.getElementById('shopManageGroupSelect'); var editSel = document.getElementById('editShopManageGroupSelect'); @@ -2434,10 +2305,12 @@ function resetShopManageGroupForm() { document.getElementById('shopManageGroupEditId').value = ''; + document.getElementById('shopManageGroupLeaderUserId').value = currentUserId || ''; + document.getElementById('shopManageGroupLeaderName').value = currentUserUsername || ''; document.getElementById('shopManageGroupInput').value = ''; document.getElementById('msgShopManageGroup').textContent = ''; document.getElementById('msgShopManageGroup').className = 'msg'; - refreshShopManageGroupUserSelect(''); + refreshShopManageGroupMemberSelect(currentUserId, []); } function openShopManageGroupModal() { @@ -2453,20 +2326,33 @@ function renderShopManageGroupRows() { var tbody = document.getElementById('shopManageGroupListBody'); if (!shopManageGroups.length) { - tbody.innerHTML = '暂无分组'; + tbody.innerHTML = '暂无分组'; return; } tbody.innerHTML = shopManageGroups.map(function (item, index) { - return '' + (index + 1) + '' + (item.group_name || '') + '' + (item.username || '') + '' + (item.created_at || '') + '' + (item.updated_at || '') + '' + - ' ' + + var memberNames = Array.isArray(item.member_usernames) ? item.member_usernames.join('、') : ''; + return '' + (index + 1) + '' + (item.group_name || '') + '' + (item.leader_username || '') + '' + (item.member_count || 0) + '' + (memberNames || '-') + '' + (item.created_at || '') + '' + (item.updated_at || '') + '' + + ' ' + '' + ''; }).join(''); document.querySelectorAll('[data-shop-group-edit]').forEach(function (btn) { btn.onclick = function () { - document.getElementById('shopManageGroupEditId').value = btn.dataset.shopGroupEdit; - refreshShopManageGroupUserSelect(btn.dataset.shopGroupUserId || ''); + var item = null; + shopManageGroups.some(function (group) { + if (String(group.id || '') === String(btn.dataset.shopGroupEdit || '')) { + item = group; + return true; + } + return false; + }); + if (!item) return; + document.getElementById('shopManageGroupEditId').value = item.id || ''; + document.getElementById('shopManageGroupInput').value = item.group_name || ''; + setShopManageGroupLeader(item.leader_user_id || '', item.leader_username || '', item.member_user_ids || []); + document.getElementById('msgShopManageGroup').textContent = ''; + document.getElementById('msgShopManageGroup').className = 'msg'; }; }); @@ -2492,10 +2378,18 @@ }); } - document.getElementById('shopManageGroupUserSelect').onchange = syncShopManageGroupNameWithUser; document.getElementById('btnManageShopGroups').onclick = openShopManageGroupModal; document.getElementById('btnManageShopGroupsFromEdit').onclick = openShopManageGroupModal; document.getElementById('btnManageSkipPriceAsinGroups').onclick = openShopManageGroupModal; + document.getElementById('btnSearchShopManage').onclick = function () { + loadShopManage(1); + }; + document.getElementById('shopManageFilterShopName').addEventListener('keydown', function (e) { + if (e.key === 'Enter') { + e.preventDefault(); + loadShopManage(1); + } + }); document.getElementById('btnCloseShopManageGroupModal').onclick = function () { document.getElementById('shopManageGroupModal').classList.remove('show'); }; @@ -2503,12 +2397,12 @@ document.getElementById('btnSaveShopManageGroup').onclick = function () { var editId = (document.getElementById('shopManageGroupEditId').value || '').trim(); var groupName = (document.getElementById('shopManageGroupInput').value || '').trim(); - var userId = (document.getElementById('shopManageGroupUserSelect').value || '').trim(); + var memberUserIds = getSelectedShopManageGroupMemberIds(); var msgEl = document.getElementById('msgShopManageGroup'); msgEl.textContent = ''; msgEl.className = 'msg'; - if (!userId || !groupName) { - msgEl.textContent = '请选择关联用户'; + if (!groupName) { + msgEl.textContent = '请输入分组名称'; msgEl.className = 'msg err'; return; } @@ -2517,7 +2411,7 @@ fetch(url, { method: method, headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ group_name: groupName, user_id: Number(userId) }) + body: JSON.stringify({ group_name: groupName, member_user_ids: memberUserIds }) }) .then(function (r) { return r.json(); }) .then(function (res) { diff --git a/frontend-vue/patrol-delete.html b/frontend-vue/patrol-delete.html new file mode 100644 index 0000000..6cee3bf --- /dev/null +++ b/frontend-vue/patrol-delete.html @@ -0,0 +1,12 @@ + + + + + + 巡店删除 - 数富AI + + +
+ + + diff --git a/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue b/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue index 0817eee..ac2ba96 100644 --- a/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue +++ b/frontend-vue/src/pages/brand/components/BrandDeleteBrandTab.vue @@ -1,4 +1,4 @@ -