package com.awspaas.user.apps.bnbm.datalinkup.job;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: NineGridCountJob
 * @Description: 九宫格展示计算汇总
 * @date: 2025/9/25 10:30
 * @Blog: https://
 */
public class NineGridCountJob implements IJob {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LocalDate now = LocalDate.now();
        String nowStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String nowYearMonthStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM"));
        LocalDate nowYearFirstDay = now.withDayOfYear(1);
        String nowYearFirstDayStr = nowYearFirstDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        BO bo = new BO();
        bo.set("YEARMONTH",nowYearMonthStr);
        //查询销售数据
        RowMap map = DBSql.getMap("SELECT (SUM(SSJERMB)/10000) AS total_sales_rmb," +
                "    CASE" +
                "        WHEN SUM(CASE WHEN LB_1 = '石膏板' THEN XSSL ELSE 0 END) > 0" +
                "            THEN SUM(CASE WHEN LB_1 = '石膏板' THEN SSJERMB ELSE 0 END) /" +
                "                 SUM(CASE WHEN LB_1 = '石膏板' THEN XSSL ELSE 0 END)" +
                "        ELSE 0" +
                "        END AS gypsum_board_avg_price," +
                "    CASE" +
                "        WHEN SUM(CASE WHEN LB_1 = '轻钢龙骨' THEN ZSSL ELSE 0 END) > 0" +
                "            THEN SUM(CASE WHEN LB_1 = '轻钢龙骨' THEN SSJERMB ELSE 0 END) /" +
                "                 SUM(CASE WHEN LB_1 = '轻钢龙骨' THEN ZSSL ELSE 0 END)" +
                "        ELSE 0" +
                "        END AS steel_keel_avg_price" +
                " FROM BO_EU_BNBM_DATALINKUP_XS_XSL_HZ" +
                " WHERE LB_1 IN ('石膏板', '轻钢龙骨')  AND DZRQ >= '"+nowYearFirstDayStr+" 00:00:00'");
        bo.set("ZB1",map.getString("total_sales_rmb"));// 销售总额
        //查询采购数据
        RowMap cgsj = DBSql.getMap("SELECT (SUM(DHJE)/10000) AS JSHJHYF FROM BO_EU_DWD_ORDER_RKD_HZ WHERE DJRQ >= '" + nowYearFirstDayStr + " 00:00:00'");
        bo.set("ZB2",cgsj.getString("JSHJHYF"));//采购总额
        //取财务数据
        int lastMonthValue = now.minusMonths(1).getMonthValue();
        int year = now.minusMonths(1).getYear();
        RowMap cwsj = DBSql.getMap("SELECT DNSJLJ FROM BO_EU_CWSJ_INTERFACE_LRZE " +
                "WHERE BK = '公司整体' AND DWMC = '合并' AND YEAR = '" + year + "' AND MONTH = '" + lastMonthValue + "'");
        bo.set("ZB3",cwsj==null?"":cwsj.getString("DNSJLJ"));//利润总额
        //预算完成率
        RowMap yswcl = DBSql.getMap("select hz.ysze AS ysze,BUDGETTARGET,(hz.ysze / mb.BUDGETTARGET)*100 AS yewcl " +
                " from " +
                "    (select sum( `BHSYYSR`) / 10000 ysze " +
                "     from BO_EU_XS_YESR " +
                "     where `YEARMONTH` >= concat(substring('" + nowYearMonthStr + "', 1, 4), '-01') " +
                "       and `YEARMONTH` <= '" + nowYearMonthStr + "') hz left join " +
                "    (select SUM(`BUDGETTARGET`) AS BUDGETTARGET from BO_EU_ANNUAL_BUDGET WHERE YEAR = '" + year + "') mb on 1=1");
        bo.set("ZB4",yswcl.getString("yewcl"));//预算完成率
        String lastMonth = now.minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        RowMap cl = DBSql.getMap("SELECT YEARS_MONTH," +
                "    MAX(CASE WHEN PRODEUCT = '石膏板' THEN YTD END) AS SGBYTD," +
                "    MAX(CASE WHEN PRODEUCT = '轻钢龙骨' THEN YTD END) AS QGLGYTD" +
                " FROM BO_EU_CLMXB" +
                " WHERE YEARS_MONTH = '" + lastMonth + "' AND GONGCHANG IN ('小计（万㎡）','小计（吨）')" +
                "  AND PRODEUCT IN ('石膏板', '轻钢龙骨') GROUP BY YEARS_MONTH");
        bo.set("ZB5",cl.getString("SGBYTD"));//石膏板产量
        bo.set("ZB6",cl.getString("QGLGYTD"));//龙骨产量
        bo.set("ZB7",map.getString("gypsum_board_avg_price"));// 石膏板均价
        bo.set("ZB8",map.getString("steel_keel_avg_price"));// 龙骨均价
        bo.set("ZB9","");//总运费


        //商旅总额
        String querySql_SL ="SELECT SUM(PRICESERVICETOTALFEE) AS PRICESERVICETOTALFEE_SUM FROM BO_EU_TRAVELMODULE_XFMX WHERE TRAVELTIME>=CONCAT(YEAR (CURDATE()),'-01-01')";
        double priceservicetotalfeeSum = DBSql.getDouble(querySql_SL, "PRICESERVICETOTALFEE_SUM");
        bo.set("ZB11",priceservicetotalfeeSum);//商旅总额

        SDK.getBOAPI().createDataBO("BO_EU_OA_NINE_GRID",bo, UserContext.fromUID("admin"));
    }
}
