//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nccloud.web.arap.arappub.action;

import com.alibaba.fastjson.JSON;
import com.hazelcast.internal.json.Json;
import com.hazelcast.internal.json.JsonValue;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.MapListProcessor;
import nccloud.framework.core.json.IJson;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.web.arap.arappub.info.SaveInfo;
import nccloud.web.arap.arappub.util.BillActionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BillSaveCheckAgainAction implements ICommonAction {
    public BillSaveCheckAgainAction() {
    }

    public Object doAction(IRequest request) {
        String json = request.read();
        Map<String, Object> maps = JSON.parseObject(json, Map.class);


        List<String> amounts= (List<String>) maps.get("amounts");
        List<String> accounts= (List<String>) maps.get("accounts");
//        String sql = "SELECT pb.PK_ORG " +
//                "FROM ap_payitem pi " +
//                "LEFT JOIN ap_paybill pb " +
//                "ON pb.PK_PAYBILL = pi.PK_PAYBILL  " +
//                "WHERE pb.dr = 0 " +
//                "  AND pi.recaccount = '"+accounts.get(0)+"' AND pi.local_money_de = "+amounts.get(0)+"  AND pb.PK_ORG ='"+maps.get("pkorg")+"'" +
//                "  AND TO_DATE(pb.BILLDATE, 'YYYY-MM-DD HH24:MI:SS') > TO_DATE('"+maps.get("billdata")+"', 'YYYY-MM-DD HH24:MI:SS') - INTERVAL '10' DAY \n" +
//                "                                                         AND TO_DATE(pb.BILLDATE, 'YYYY-MM-DD HH24:MI:SS') < TO_DATE('"+maps.get("billdata")+"', 'YYYY-MM-DD HH24:MI:SS');";
        String sql = buildQuery(maps, accounts, amounts);
        BaseDAO dao = new BaseDAO();
        try {
            ArrayList<HashMap<String, Object>> al = (ArrayList<HashMap<String, Object>>)dao.executeQuery(sql, new MapListProcessor());
            Map<String, Object> map = new HashMap<>();
            if(al.size() == 0) {
                map.put("num",0);
                return map;
            }else{
                map.put("num",1);
                map.put("list",al);
                return map;
            }
        } catch (DAOException e) {
            throw new RuntimeException(e);
        }

    }
    private String buildQuery(Map<String, Object> maps, List<String> accounts, List<String> amounts) {
        StringBuilder conditions = new StringBuilder();

        // 生成账号和金额的对应条件
        for (int i = 0; i < accounts.size(); i++) {
            if (i > 0) {
                conditions.append(" OR ");
            }
            conditions.append("(pi.recaccount = '").append(accounts.get(i)).append("' AND pi.local_money_de = '").append(amounts.get(i)).append("')");
        }

        return "SELECT pi.recaccount,pi.local_money_de " +
                "FROM ap_payitem pi " +
                "LEFT JOIN ap_paybill pb " +
                "ON pb.PK_PAYBILL = pi.PK_PAYBILL  " +
                "WHERE pb.dr = 0 " +
                "  AND (" + conditions.toString() + ") " +
                "  AND pb.PK_ORG = '" + maps.get("pkorg") + "' " +
                "  AND TO_DATE(pb.BILLDATE, 'YYYY-MM-DD HH24:MI:SS') > TO_DATE('" + maps.get("billdata") + "', 'YYYY-MM-DD HH24:MI:SS') - INTERVAL '10' DAY " +
                "  AND TO_DATE(pb.BILLDATE, 'YYYY-MM-DD HH24:MI:SS') < TO_DATE('" + maps.get("billdata") + "', 'YYYY-MM-DD HH24:MI:SS')";
    }

}
