package restcontroller.sale.salestarget; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import common.Controller; import common.YosException; import common.annotation.API; import common.annotation.CACHEING; import common.data.*; import org.apache.commons.lang.StringUtils; import restcontroller.R; import java.math.BigDecimal; import java.util.ArrayList; /** * 企业目标(经销商目标) */ public class enterprise extends Controller { /** * 构造函数 * * @param content */ public enterprise(JSONObject content) throws YosException { super(content); } @API(title = "经销商列表查询", apiversion = R.ID20231018162103.v1.class) @CACHEING public String query_agentList() throws YosException { /* 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append(" t1.enterprisename like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.agentnum like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } } // SQLFactory sqlFactory = new SQLFactory(this, "查询经销商", pageSize, pageNumber, pageSorting); // sqlFactory.addParameter_SQL("where", where); // sqlFactory.addParameter("siteid", siteid); // 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"); querySQL.setTableAlias("t1"); 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(where.toString()); querySQL.setPage(pageSize, pageNumber); Rows rows = querySQL.query(); return getSucReturnObject().setData(rows).toString(); } @API(title = "企业业绩目标(经销商)", apiversion = R.ID20230110151902.v1.class) public String selectList() throws YosException { ArrayList sys_enterpriseids = new ArrayList<>(); /* 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); 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 statisticaldimension = ""; //统计维度 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"); } } long totalPage = 0; long totalRows = 0; 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"); Rows enterpriseRows = enterpriseQuery.query(); sys_enterpriseids = enterpriseRows.toArrayList("sys_enterpriseid", new ArrayList<>()); totalPage = enterpriseRows.getTotalPage(); totalRows = enterpriseRows.getTotalRows(); } 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); if (totalRows > 0) { rows.totalRows = totalRows; rows.totalPage = totalPage; rows.pageSize = pageSize; rows.pageNumber = pageNumber; } Rows actualRows = new Rows(); //开票 if (assessmentindicators.equals("开票")) { 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 (statisticaldimension.equals("订单审核")) { where1 = where1 + " and t2.status='审核' "; } else { where1 = where1 + " and t2.status='提交' "; } 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; where1 = where1.replace("[", "(").replace("]", ")"); } } } sqlFactory.addParameter_SQL("where1", where1); } //订单 if (assessmentindicators.equals("订单")) { 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 (statisticaldimension.equals("订单审核")) { where1 = where1 + " and t2.status='审核' "; } else { where1 = where1 + " and t2.status='提交' "; } 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; where1 = where1.replace("[", "(").replace("]", ")"); } } } sqlFactory.addParameter_SQL("where1", where1); } //出货 if (assessmentindicators.equals("出货")) { 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 (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; where1 = where1.replace("[", "(").replace("]", ")"); } } } sqlFactory.addParameter_SQL("where1", where1); } //收款 if (assessmentindicators.equals("收款")) { 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 (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 (!((JSONArray) jsonObjectResult.get("mx")).isEmpty()) { stringBuffer.append(" or subclass in" + jsonObjectResult.get("mx")); } if (((JSONArray) jsonObjectResult.get("mx")).isEmpty() && ((JSONArray) jsonObjectResult.get("type")).isEmpty()) { stringBuffer.append(" 1=1 "); } where1 = where1 + " and (" + (stringBuffer.toString()).replaceFirst("or", "") + ")"; where1 = where1.replace("[", "(").replace("]", ")"); } } } 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; where1 = where1.replace("[", "(").replace("]", ")"); } } } sqlFactory.addParameter_SQL("where1", where1); } sqlFactory.addParameter_in("sys_enterpriseid", rows.toArrayList("sys_enterpriseid", new ArrayList())); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("year", curryear); actualRows = dbConnect.runSqlQuery(sqlFactory); for (Row row : rows) { //初始化 row = addActualRow(row); Long year = row.getLong("year"); 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) { actualRow.putAll(tempActualRow); } } row.putAll(actualRow); row.putAll(calculate(row, actualRow)); // Rows monthRows = new Rows(); // for (int i = 1; i < 13; i++) { // Row monthRow = new Row(); // monthRow.put("l", row.getString("m" + i + "l")); // monthRow.put("h", row.getString("m" + i + "h")); // monthRow.put("a", row.getString("m" + i + "a")); // monthRow.put("pl", row.getString("m" + i + "pl")); // monthRow.put("ph", row.getString("m" + i + "ph")); // monthRow.put("month", i); // monthRows.add(monthRow); // } // row.put("month", monthRows); } return getSucReturnObject().setData(rows).toString(); } @API(title = "企业业绩目标(指定经销商)", apiversion = R.ID2025101413174203.v1.class) public String selectAgentList() throws YosException { ArrayList sys_enterpriseids = new ArrayList<>(); /* 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); 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")); } 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); sqlFactory = new SQLFactory(this, "企业-出货-经销商"); if (content.getLongValue("num") == 0) { sqlFactory.addParameter("num", 2); } else { sqlFactory.addParameter("num", content.getLongValue("num")); } sqlFactory.addParameter_SQL("where1", " 1=1 "); sqlFactory.addParameter_in("sys_enterpriseid", rows.toArrayList("sys_enterpriseid", new ArrayList())); sqlFactory.addParameter("siteid", siteid); Rows chuhuoRows = dbConnect.runSqlQuery(sqlFactory); for(Row row :rows){ Rows actualRows = new Rows(); row = addActualRow(row); String assessmentindicators = row.getString("assessmentindicators"); //统计维度 String statisticaldimension = row.getString("statisticaldimension"); //统计维度 String sa_accountclassids = row.getString("sa_accountclassids"); //开票 if (assessmentindicators.equals("开票")) { 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 (statisticaldimension.equals("订单审核")) { where1 = where1 + " and t2.status='审核' "; } else { where1 = where1 + " and t2.status='提交' "; } 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; where1 = where1.replace("[", "(").replace("]", ")"); } } } sqlFactory.addParameter_SQL("where1", where1); } //订单 if (assessmentindicators.equals("订单")) { 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 (statisticaldimension.equals("订单审核")) { where1 = where1 + " and t2.status='审核' "; } else { where1 = where1 + " and t2.status='提交' "; } sqlFactory.addParameter_SQL("where1", where1); } //出货 // if (assessmentindicators.equals("出货")) { // String where1 = " 1=1 "; // sqlFactory = new SQLFactory(this, "企业-出货-经销商"); // if (content.getLongValue("num") == 0) { // sqlFactory.addParameter("num", 2); // } else { // sqlFactory.addParameter("num", content.getLongValue("num")); // } // sqlFactory.addParameter_SQL("where1", where1); // } //收款 if (assessmentindicators.equals("收款")) { 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 (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 (!((JSONArray) jsonObjectResult.get("mx")).isEmpty()) { stringBuffer.append(" or subclass in" + jsonObjectResult.get("mx")); } if (((JSONArray) jsonObjectResult.get("mx")).isEmpty() && ((JSONArray) jsonObjectResult.get("type")).isEmpty()) { stringBuffer.append(" 1=1 "); } where1 = where1 + " and (" + (stringBuffer.toString()).replaceFirst("or", "") + ")"; where1 = where1.replace("[", "(").replace("]", ")"); } } } 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; where1 = where1.replace("[", "(").replace("]", ")"); } } } sqlFactory.addParameter_SQL("where1", where1); } sqlFactory.addParameter_in("sys_enterpriseid", rows.toArrayList("sys_enterpriseid", new ArrayList())); sqlFactory.addParameter("siteid", siteid); if(assessmentindicators.equals("出货")){ actualRows=chuhuoRows; }else{ actualRows = dbConnect.runSqlQuery(sqlFactory); } for (Row tempActualRow : actualRows) { Row actualRow = new Row(); if (tempActualRow.getLong("year")==row.getLong("year")) { actualRow.putAll(tempActualRow); } row.putAll(actualRow); row.putAll(calculate(row, actualRow)); } } return getSucReturnObject().setData(rows).toString(); } // @API(title = "企业业绩目标(业务员)", apiversion = R.ID20230111163102.v1.class) // public String selectSellerList() throws YosException { // /* // 过滤条件设置 // */ // StringBuffer where = new StringBuffer(" 1=1 "); // StringBuffer where2 = new StringBuffer(" 1=1 "); // if (content.containsKey("where")) { // JSONObject whereObject = content.getJSONObject("where"); // if (whereObject.containsKey("year") && !"".equals(whereObject.getString("year"))) { // where.append(" and("); // where.append("year like'%").append(whereObject.getString("year")).append("%' "); // where.append(")"); // // where2.append(" and("); // where2.append("YEAR(t2.billdate) like'%").append(whereObject.getString("year")).append("%' "); // where2.append(")"); // } // if (whereObject.containsKey("tradefield") && !"".equals(whereObject.getString("tradefield"))) { // where.append(" and("); // where.append("tradefield like'%").append(whereObject.getString("tradefield")).append("%' "); // where.append(")"); // // where2.append(" and("); // where2.append("tradefield like'%").append(whereObject.getString("tradefield")).append("%' "); // where2.append(")"); // } // // } // // //考核指标类型 // Long type = content.getLongValue("type"); // // if (content.containsKey("sys_enterpriseid")) { // sys_enterpriseid = content.getLongValue("sys_enterpriseid"); // } // // // SQLFactory sqlFactory = new SQLFactory(this, "企业-业绩目标完成列表-业务员"); // sqlFactory.addParameter_SQL("where", where); // sqlFactory.addParameter_in("hrid", hrid); // sqlFactory.addParameter_SQL("where", where); // sqlFactory.addParameter("siteid", siteid); // String sql = sqlFactory.getSQL(); // Rows rows = dbConnect.runSqlQuery(sql); // // Rows actualRows = new Rows(); // //开票 // if (type == 0 || type == 1) { // sqlFactory = new SQLFactory(this, "企业-开票-业务员"); // } // //订单 // if (type == 2) { // sqlFactory = new SQLFactory(this, "企业-订单-业务员"); // } // //出货 // if (type == 3) { // sqlFactory = new SQLFactory(this, "企业-出货-业务员"); // } // sqlFactory.addParameter_in("hrid", hrid); // sqlFactory.addParameter_SQL("where", where2); // sqlFactory.addParameter("siteid", siteid); // actualRows = dbConnect.runSqlQuery(sqlFactory); // // for (Row row : rows) { // //初始化 // row = addActualRow(row); // Long year = row.getLong("year"); // String tradefield = row.getString("tradefield"); // Row actualRow = new Row(); // for (Row tempActualRow : actualRows) { // if (tempActualRow.getLong("year") == year && tempActualRow.getString("tradefield").equals(tradefield)) { // actualRow.putAll(tempActualRow); // } // } // row.putAll(actualRow); // row.putAll(calculate(row, actualRow)); // Rows monthRows = new Rows(); // for (int i = 1; i < 13; i++) { // Row monthRow = new Row(); // monthRow.put("l", row.getString("m" + i + "l")); // monthRow.put("h", row.getString("m" + i + "h")); // monthRow.put("a", row.getString("m" + i + "a")); // monthRow.put("pl", row.getString("m" + i + "pl")); // monthRow.put("ph", row.getString("m" + i + "ph")); // monthRow.put("month", i); // monthRows.add(monthRow); // } // row.put("month", monthRows); // } // return getSucReturnObject().setData(rows).toString(); // } //添加实际数据 public Row addActualRow(Row targetRow) { String key = "y1a"; targetRow.put(key, 0); for (int i = 1; i < 5; i++) { key = "s" + i + "a"; targetRow.put(key, 0); } for (int i = 1; i < 13; i++) { key = "m" + i + "a"; targetRow.put(key, 0); } return targetRow; } public Row calculate(Row targetRow, Row actualRow) { Row row = new Row(); String key_l = "y1l"; String key_h = "y1h"; String key_a = "y1a"; String key_p_l = "y1pl"; String key_p_h = "y1ph"; if (targetRow.getDouble(key_l) == 0) { row.put(key_p_l, 0); } else { row.put(key_p_l, getPercent(actualRow.getDouble(key_a), targetRow.getDouble(key_l))); } if (targetRow.getDouble(key_h) == 0) { row.put(key_p_h, 0); } else { row.put(key_p_h, getPercent(actualRow.getDouble(key_a), targetRow.getDouble(key_h))); } for (int i = 1; i < 5; i++) { key_l = "s" + i + "l"; key_h = "s" + i + "h"; key_a = "s" + i + "a"; key_p_l = "s" + i + "pl"; key_p_h = "s" + i + "ph"; if (targetRow.getDouble(key_l) == 0) { row.put(key_p_l, 0); } else { row.put(key_p_l, getPercent(actualRow.getDouble(key_a), targetRow.getDouble(key_l))); } if (targetRow.getDouble(key_h) == 0) { row.put(key_p_h, 0); } else { row.put(key_p_h, getPercent(actualRow.getDouble(key_a), targetRow.getDouble(key_h))); } } // for (int i = 1; i < 13; i++) { // key_l = "m" + i + "l"; // key_h = "m" + i + "h"; // key_a = "m" + i + "a"; // key_p_l = "m" + i + "pl"; // key_p_h = "m" + i + "ph"; // if (targetRow.getDouble(key_l) == 0) { // row.put(key_p_l, 0); // } else { // row.put(key_p_l, getPercent(actualRow.getDouble(key_a), targetRow.getDouble(key_l))); // } // if (targetRow.getDouble(key_h) == 0) { // row.put(key_p_h, 0); // } else { // row.put(key_p_h, getPercent(actualRow.getDouble(key_a), targetRow.getDouble(key_h))); // } // } return row; } //(实际-目标)/目标 public double getPercent(Double a, Double b) { double c = a * 100 / b; BigDecimal bigDecimal = new BigDecimal(c); return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); } }