package nccloud.web.mmpac.pickm.query;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.util.mmf.framework.base.MMValueCheck;
import nccloud.dto.mmpac.pickm.pub.entity.PickmQueryInfoDTO;
import nccloud.framework.core.exception.ExceptionUtils;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 备料计划-借料入库弹窗查询
 *
 * @author mzr
 * @date 2025/7/8
 */
public class PickmItemsQuery implements ICommonAction {
    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();
        PickmQueryInfoDTO queryInfo = json.fromJson(read, PickmQueryInfoDTO.class);
        try {
            String[] cpickmbids = queryInfo.getCpickmbids();
            List<String> bids = new ArrayList<>();
            if (null != cpickmbids && cpickmbids.length != 0) {
                for (String cpickmbid : cpickmbids) {
                    String countSql = "SELECT count(1) FROM ic_generalin_b"
                            + " WHERE dr = 0 and csourcebillbid = '" + cpickmbid + "'";
                    Integer num = (Integer) new BaseDAO().executeQuery(countSql, new ColumnProcessor());
                    if (num <= 0) {
                        bids.add(cpickmbid);
                    }

                }
                if (MMValueCheck.isEmpty(bids)) {
                    ExceptionUtils.wrapBusinessException("不存在符合条件的备料计划明细");
                }

                Map<String, Object> returnMap = new HashMap<>();
                returnMap.put("data", bids);
                returnMap.put("success", true);
                return returnMap;
            } else {
                Map<String, Object> returnMap = new HashMap<>();
                returnMap.put("data", null);
                returnMap.put("success", false);
                return returnMap;
            }
        } catch (Exception ex) {
            ExceptionUtils.wrapBusinessException(ex.getMessage());
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("data", null);
            returnMap.put("success", false);
            return returnMap;
        }
    }
}
