Kaynağa Gözat

优化经销商目标完成情况查询

shenjingwei 1 hafta önce
ebeveyn
işleme
0cf516c146

+ 2 - 2
src/custom/restcontroller/sale/salestarget/SQL/企业-业绩目标完成列表.sql

@@ -1,6 +1,6 @@
 with sa_salestarget as (
     SELECT point, sum(target_l) target_l, sum(target_h) target_h, year,sys_enterpriseid from sa_salestarget
-    WHERE  type = '季' and targettype='企业目标' and siteid=$siteid$ and $where$
+    WHERE  type = '季' and targettype='企业目标' and siteid=$siteid$ and sys_enterpriseid in $sys_enterpriseids$ and $where$
     GROUP  BY point ,year,sys_enterpriseid
     )
 SELECT DISTINCT m.year,
@@ -27,4 +27,4 @@ from sa_salestarget m
          LEFT JOIN sa_agents t2 on t1.sys_enterpriseid=t2.sys_enterpriseid and t1.siteid=t2.siteid
          LEFT JOIN sys_enterprise_tradefield t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t1.siteid=t3.siteid
          LEFT JOIN sa_salearea t4 on t3.sa_saleareaid=t4.sa_saleareaid and t3.siteid=t4.siteid
-ORDER BY m.year desc
+ORDER BY m.year desc, m.sys_enterpriseid

+ 97 - 98
src/custom/restcontroller/sale/salestarget/enterprise.java

@@ -29,6 +29,7 @@ public class enterprise extends Controller {
     public enterprise(JSONObject content) throws YosException {
         super(content);
     }
+
     @API(title = "经销商列表查询", apiversion = R.ID20231018162103.v1.class)
     @CACHEING
     public String query_agentList() throws YosException {
@@ -51,12 +52,12 @@ public class enterprise extends Controller {
 //        sqlFactory.addParameter("year", content.getLong("year"));
 //        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
         QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sys_enterprise", "sys_enterpriseid", "enterprisename",
-                "province","city","county","address","contact","phonenumber","taxno");
+                "province", "city", "county", "address", "contact", "phonenumber", "taxno");
         querySQL.setTableAlias("t1");
-        querySQL.addJoinTable(JOINTYPE.inner, "sa_agents", "t2", "t1.sys_enterpriseid=t2.sys_enterpriseid and t1.siteid=t2.siteid","agentnum");
+        querySQL.addJoinTable(JOINTYPE.inner, "sa_agents", "t2", "t1.sys_enterpriseid=t2.sys_enterpriseid and t1.siteid=t2.siteid", "agentnum");
         querySQL.setSiteid(siteid);
         querySQL.setWhere("t2.status='启用'");
-        querySQL.setWhere("t1.sys_enterpriseid not in (select t2.sys_enterpriseid from sa_salestargetbill t1 inner join sa_salestargethr t2 on t1.sa_salestargetbillid=t2.sa_salestargetbillid and t1.siteid=t2.siteid where  t1.year='"+content.getLong("year")+"' and t1.targettype='企业目标' )");
+        querySQL.setWhere("t1.sys_enterpriseid not in (select t2.sys_enterpriseid from sa_salestargetbill t1 inner join sa_salestargethr t2 on t1.sa_salestargetbillid=t2.sa_salestargetbillid and t1.siteid=t2.siteid where  t1.year='" + content.getLong("year") + "' and t1.targettype='企业目标' )");
         querySQL.setWhere(where.toString());
         querySQL.setPage(pageSize, pageNumber);
         Rows rows = querySQL.query();
@@ -66,55 +67,53 @@ public class enterprise extends Controller {
 
     @API(title = "企业业绩目标(经销商)", apiversion = R.ID20230110151902.v1.class)
     public String selectList() throws YosException {
+
+        ArrayList<Long> sys_enterpriseids = new ArrayList<>();
          /*
           过滤条件设置
          */
         StringBuffer where = new StringBuffer(" 1=1 ");
-         if(sys_enterpriseid!=0){
-             where.append(" and(");
-             where.append("sys_enterpriseid ='").append(sys_enterpriseid).append("' ");
-             where.append(")");
-         }
-
-
-        StringBuffer where2 = new StringBuffer(" 1=1 ");
-        long curryear=0;
-        if (content.containsKey("where")) {
-            JSONObject whereObject = content.getJSONObject("where");
-            if (whereObject.containsKey("year") && !"".equals(whereObject.getString("year"))) {
-                where.append(" and(");
-                where.append("year ='").append(whereObject.getString("year")).append("' ");
-                where.append(")");
-                curryear=whereObject.getLong("year");
-            }
-
-            if (whereObject.containsKey("sys_enterpriseid") && !"".equals(whereObject.getString("sys_enterpriseid"))) {
-                where.append(" and(");
-                where.append("sys_enterpriseid ='").append(whereObject.getString("sys_enterpriseid")).append("' ");
-                where.append(")");
-            }
-
+        long curryear = 0;
+        if (!content_where.getStringValue("year").isEmpty()) {
+            where.append(" and(");
+            where.append("year ='").append(content_where.getString("year")).append("' ");
+            where.append(")");
+            curryear = content_where.getLong("year");
+        }
+        if (sys_enterpriseid != 0) {
+            sys_enterpriseids.add(sys_enterpriseid);
+        } else if (!content_where.getStringValue("sys_enterpriseid").isEmpty()) {
+            sys_enterpriseids.add(content_where.getLong("sys_enterpriseid"));
         }
-
 
         //考核指标类型
-        String assessmentindicators="";
+        String assessmentindicators = "";
         //统计维度
-        String statisticaldimension="";
+        String statisticaldimension = "";
         //统计维度
-        String sa_accountclassids="";
-        if(curryear!=0){
-            Rows rows =dbConnect.runSqlQuery("SELECT assessmentindicators,statisticaldimension,sa_accountclassids FROM sa_salestargetbill WHERE `year` = " + curryear + " AND targettype ='企业目标' AND siteid = '" + siteid + "'");
-            if(rows.isNotEmpty()){
-                assessmentindicators=rows.get(0).getString("assessmentindicators");
-                statisticaldimension=rows.get(0).getString("statisticaldimension");
-                sa_accountclassids=rows.get(0).getString("sa_accountclassids");
+        String sa_accountclassids = "";
+        long sa_salestargetbillid = 0;
+        if (curryear != 0) {
+            Rows rows = dbConnect.runSqlQuery("SELECT assessmentindicators,statisticaldimension,sa_accountclassids,sa_salestargetbillid FROM sa_salestargetbill WHERE `year` = " + curryear + " AND targettype ='企业目标' AND siteid = '" + siteid + "'");
+            if (rows.isNotEmpty()) {
+                assessmentindicators = rows.get(0).getString("assessmentindicators");
+                statisticaldimension = rows.get(0).getString("statisticaldimension");
+                sa_accountclassids = rows.get(0).getString("sa_accountclassids");
+                sa_salestargetbillid = rows.get(0).getLong("sa_salestargetbillid");
             }
         }
 
+        if (sys_enterpriseids.isEmpty()) {
+            QuerySQL enterpriseQuery = SQLFactory.createQuerySQL(this, "sys_enterprise", "sys_enterpriseid");
+            enterpriseQuery.setWhere("exists (select * from sa_salestarget where sa_salestarget.sa_salestargetbillid=" + sa_salestargetbillid + " and sa_salestarget.sys_enterpriseid=sys_enterprise.sys_enterpriseid) ");
+            enterpriseQuery.setPage(pageSize, pageNumber);
+            enterpriseQuery.setOrderBy("sys_enterpriseid");
+            sys_enterpriseids = enterpriseQuery.query().toArrayList("sys_enterpriseid", new ArrayList<>());
+        }
 
         SQLFactory sqlFactory = new SQLFactory(this, "企业-业绩目标完成列表");
         sqlFactory.addParameter_SQL("where", where);
+        sqlFactory.addParameter_in("sys_enterpriseids", sys_enterpriseids);
         sqlFactory.addParameter("siteid", siteid);
         String sql = sqlFactory.getSQL();
         Rows rows = dbConnect.runSqlQuery(sql);
@@ -122,121 +121,121 @@ public class enterprise extends Controller {
         Rows actualRows = new Rows();
         //开票
         if (assessmentindicators.equals("开票")) {
-            String where1=" 1=1 ";
+            String where1 = " 1=1 ";
             sqlFactory = new SQLFactory(this, "企业-开票");
-            if(content.getLongValue("num")==0){
-                sqlFactory.addParameter("num",2);
-            }else{
-                sqlFactory.addParameter("num",content.getLongValue("num"));
+            if (content.getLongValue("num") == 0) {
+                sqlFactory.addParameter("num", 2);
+            } else {
+                sqlFactory.addParameter("num", content.getLongValue("num"));
             }
-            if(statisticaldimension.equals("订单审核")){
-                where1=where1+" and t2.status='审核' ";
-            }else{
-                where1=where1+" and t2.status='提交' ";
+            if (statisticaldimension.equals("订单审核")) {
+                where1 = where1 + " and t2.status='审核' ";
+            } else {
+                where1 = where1 + " and t2.status='提交' ";
             }
-            if(StringUtils.isNotBlank(sa_accountclassids)){
-                if(isJSONArray(sa_accountclassids)){
+            if (StringUtils.isNotBlank(sa_accountclassids)) {
+                if (isJSONArray(sa_accountclassids)) {
                     JSONArray jsonArrayResult = JSONArray.parseArray(sa_accountclassids);
-                    if(!jsonArrayResult.isEmpty()){
-                        where1=where1+ " and t2.sa_accountclassid in"+jsonArrayResult;
+                    if (!jsonArrayResult.isEmpty()) {
+                        where1 = where1 + " and t2.sa_accountclassid in" + jsonArrayResult;
                         where1 = where1.replace("[", "(").replace("]", ")");
                     }
 
                 }
             }
-            sqlFactory.addParameter_SQL("where1",where1);
+            sqlFactory.addParameter_SQL("where1", where1);
         }
         //订单
         if (assessmentindicators.equals("订单")) {
-            String where1=" 1=1 ";
+            String where1 = " 1=1 ";
             sqlFactory = new SQLFactory(this, "企业-订单");
-            if(content.getLongValue("num")==0){
-                sqlFactory.addParameter("num",2);
-            }else{
-                sqlFactory.addParameter("num",content.getLongValue("num"));
+            if (content.getLongValue("num") == 0) {
+                sqlFactory.addParameter("num", 2);
+            } else {
+                sqlFactory.addParameter("num", content.getLongValue("num"));
             }
-            if(statisticaldimension.equals("订单审核")){
-                where1=where1+" and t2.status='审核' ";
-            }else{
-                where1=where1+" and t2.status='提交' ";
+            if (statisticaldimension.equals("订单审核")) {
+                where1 = where1 + " and t2.status='审核' ";
+            } else {
+                where1 = where1 + " and t2.status='提交' ";
             }
-            if(StringUtils.isNotBlank(sa_accountclassids)){
-                if(isJSONArray(sa_accountclassids)){
+            if (StringUtils.isNotBlank(sa_accountclassids)) {
+                if (isJSONArray(sa_accountclassids)) {
                     JSONArray jsonArrayResult = JSONArray.parseArray(sa_accountclassids);
-                    if(!jsonArrayResult.isEmpty()){
-                        where1=where1+ " and t2.sa_accountclassid in"+jsonArrayResult;
+                    if (!jsonArrayResult.isEmpty()) {
+                        where1 = where1 + " and t2.sa_accountclassid in" + jsonArrayResult;
                         where1 = where1.replace("[", "(").replace("]", ")");
                     }
 
                 }
             }
-            sqlFactory.addParameter_SQL("where1",where1);
+            sqlFactory.addParameter_SQL("where1", where1);
         }
         //出货
         if (assessmentindicators.equals("出货")) {
-            String where1=" 1=1 ";
+            String where1 = " 1=1 ";
             sqlFactory = new SQLFactory(this, "企业-出货");
-            if(content.getLongValue("num")==0){
-                sqlFactory.addParameter("num",2);
-            }else{
-                sqlFactory.addParameter("num",content.getLongValue("num"));
+            if (content.getLongValue("num") == 0) {
+                sqlFactory.addParameter("num", 2);
+            } else {
+                sqlFactory.addParameter("num", content.getLongValue("num"));
             }
 
-            if(StringUtils.isNotBlank(sa_accountclassids)){
-                if(isJSONArray(sa_accountclassids)){
+            if (StringUtils.isNotBlank(sa_accountclassids)) {
+                if (isJSONArray(sa_accountclassids)) {
                     JSONArray jsonArrayResult = JSONArray.parseArray(sa_accountclassids);
-                    if(!jsonArrayResult.isEmpty()){
-                        where1=where1+ " and t2.sa_accountclassid in"+jsonArrayResult;
+                    if (!jsonArrayResult.isEmpty()) {
+                        where1 = where1 + " and t2.sa_accountclassid in" + jsonArrayResult;
                         where1 = where1.replace("[", "(").replace("]", ")");
                     }
 
                 }
             }
-            sqlFactory.addParameter_SQL("where1",where1);
+            sqlFactory.addParameter_SQL("where1", where1);
         }
         //收款
         if (assessmentindicators.equals("收款")) {
-            String where1=" 1=1 ";
+            String where1 = " 1=1 ";
             sqlFactory = new SQLFactory(this, "企业-收款");
-            if(content.getLongValue("num")==0){
-                sqlFactory.addParameter("num",2);
-            }else{
-                sqlFactory.addParameter("num",content.getLongValue("num"));
+            if (content.getLongValue("num") == 0) {
+                sqlFactory.addParameter("num", 2);
+            } else {
+                sqlFactory.addParameter("num", content.getLongValue("num"));
             }
-            if(StringUtils.isNotBlank(statisticaldimension)){
-                if(isJSONObject(statisticaldimension)){
+            if (StringUtils.isNotBlank(statisticaldimension)) {
+                if (isJSONObject(statisticaldimension)) {
                     JSONObject jsonObjectResult = JSONObject.parseObject(statisticaldimension);
-                    if(!jsonObjectResult.isEmpty()){
-                        StringBuffer stringBuffer= new StringBuffer();
-                        if(!((JSONArray)jsonObjectResult.get("type")).isEmpty()){
-                            stringBuffer.append(" or class in"+ jsonObjectResult.get("type"));
+                    if (!jsonObjectResult.isEmpty()) {
+                        StringBuffer stringBuffer = new StringBuffer();
+                        if (!((JSONArray) jsonObjectResult.get("type")).isEmpty()) {
+                            stringBuffer.append(" or class in" + jsonObjectResult.get("type"));
                         }
-                        if(!((JSONArray)jsonObjectResult.get("mx")).isEmpty()){
-                            stringBuffer.append(" or subclass in"+ jsonObjectResult.get("mx"));
+                        if (!((JSONArray) jsonObjectResult.get("mx")).isEmpty()) {
+                            stringBuffer.append(" or subclass in" + jsonObjectResult.get("mx"));
                         }
-                        if(((JSONArray)jsonObjectResult.get("mx")).isEmpty() && ((JSONArray)jsonObjectResult.get("type")).isEmpty()){
+                        if (((JSONArray) jsonObjectResult.get("mx")).isEmpty() && ((JSONArray) jsonObjectResult.get("type")).isEmpty()) {
                             stringBuffer.append(" 1=1 ");
                         }
-                        where1= where1+ " and ("+(stringBuffer.toString()).replaceFirst("or", "")+")";
+                        where1 = where1 + " and (" + (stringBuffer.toString()).replaceFirst("or", "") + ")";
                         where1 = where1.replace("[", "(").replace("]", ")");
                     }
 
                 }
             }
-            if(StringUtils.isNotBlank(sa_accountclassids)){
-                if(isJSONArray(sa_accountclassids)){
+            if (StringUtils.isNotBlank(sa_accountclassids)) {
+                if (isJSONArray(sa_accountclassids)) {
                     JSONArray jsonArrayResult = JSONArray.parseArray(sa_accountclassids);
-                    if(!jsonArrayResult.isEmpty()){
-                        where1=where1+ " and sa_accountclassid in"+jsonArrayResult;
+                    if (!jsonArrayResult.isEmpty()) {
+                        where1 = where1 + " and sa_accountclassid in" + jsonArrayResult;
                         where1 = where1.replace("[", "(").replace("]", ")");
                     }
 
                 }
             }
 
-            sqlFactory.addParameter_SQL("where1",where1);
-    }
-        sqlFactory.addParameter_in("sys_enterpriseid", rows.toArrayList("sys_enterpriseid",new ArrayList<Long>()));
+            sqlFactory.addParameter_SQL("where1", where1);
+        }
+        sqlFactory.addParameter_in("sys_enterpriseid", rows.toArrayList("sys_enterpriseid", new ArrayList<Long>()));
         sqlFactory.addParameter("siteid", siteid);
         sqlFactory.addParameter("year", curryear);
         actualRows = dbConnect.runSqlQuery(sqlFactory);
@@ -248,7 +247,7 @@ public class enterprise extends Controller {
             Long sys_enterpriseid = row.getLong("sys_enterpriseid");
             Row actualRow = new Row();
             for (Row tempActualRow : actualRows) {
-                if (tempActualRow.getLong("year") == year && tempActualRow.getLong("sys_enterpriseid")==sys_enterpriseid) {
+                if (tempActualRow.getLong("year") == year && tempActualRow.getLong("sys_enterpriseid") == sys_enterpriseid) {
                     actualRow.putAll(tempActualRow);
                 }
             }