package com.awspaas.user.apps.donenow_ctt.at;

import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.AWSExpressionException;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class getPickSNListSql extends AbstExpression {
    public getPickSNListSql(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    /**
     * 合同产品拣货
     * @param s
     * @return
     * @throws AWSExpressionException
     */
    @Override
    public String execute(String s) throws AWSExpressionException {
        String WAREHOUSE_PRODUCT_ID = getParameter(s, 1).trim();// 仓库产品ID
        int QUANTITY = Integer.parseInt(getParameter(s, 2).trim());// 拣货数量
        int TYPE = Integer.parseInt(getParameter(s, 3).trim());//1 返回客户选择  2 返回已选择
        //库存数量
        RowMap stockMap = DBSql.getMap("SELECT * FROM BO_EU_DNIVT_WAREHOUSE_PRODUCT WHERE ID=?", new Object[]{WAREHOUSE_PRODUCT_ID});

        //库存产品
        List<RowMap> lotList = DBSql.getMaps("SELECT b.* FROM bo_eu_dnivt_warehouse_product a JOIN BO_EU_DNIVT_WAREHOUSE_PRODUCT_LOT b ON a.BINDID=b.BINDID WHERE b.QUANTITY>0 AND a.ID=? ORDER BY b.IN_DATE", new Object[]{WAREHOUSE_PRODUCT_ID});


        //已被拣货数量
        List<RowMap> pickList = DBSql.getMaps("SELECT a.* from BO_EU_DNCTT_CONTRACT_COST_PRODUCT a JOIN BO_EU_DNCTT_CONTRACT_COST b ON a.CONTRACT_COST_ID=b.ID WHERE a.WAREHOUSE_ID=? AND b.PRODUCT_ID=?", new Object[]{stockMap.getString("WAREHOUSE_ID"), stockMap.getString("PRODUCT_ID")});
        List<String> selectedSns = new ArrayList<>();//默认选择
        for (RowMap lotMap : lotList) {
            String lotSNs = lotMap.getString("SN");
            List<String> lotsns = new ArrayList<>();//本批次允许拣货数量
            for (String SN : lotSNs.split(",")) {
                boolean isPicked = false;
                for (RowMap pickMap : pickList) {
                    String pickSNs = pickMap.getString("SN");
                    for (String pickSN : pickSNs.split(",")) {
                        if (SN.equals(pickSN)) {
                            //已被拣货
                            isPicked = true;
                            break;
                        }
                    }
                    if (isPicked)
                        break;
                }
                if (!isPicked) {
                    lotsns.add(SN);
                }
            }
            if (lotsns.size() > QUANTITY)//允许挑选
            {
                if (TYPE == 1) {
                    //返回到客户选择
                    String snSql = null;
                    int i = 0;
                    for (String sn : lotsns) {
                        if (i == 0) {
                            snSql = "SELECT '" + sn + "' AS sn";
                        } else {
                            snSql += " UNION SELECT '" + sn + "' AS sn";
                        }
                        i++;
                    }
                    return "SELECT tt.* FROM (" + snSql + ") as tt WHERE tt.sn IS NOT NULL";
                } else if (TYPE == 2) {
                    return StringUtils.join(selectedSns, ",");
                }
            } else {
                //全部拣货，不允许挑选
                QUANTITY -= lotsns.size();
                selectedSns.addAll(lotsns);
            }
        }
        if (TYPE == 1)
            return "SELECT tt.* FROM (SELECT NULL AS sn) as tt WHERE tt.sn IS NOT NULL";
        if (TYPE == 2)
            return StringUtils.join(selectedSns, ",");

        return null;
    }
}
