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.commons.lang.StringUtils;
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.*;

/**
 * 备料计划-借料入库弹窗-数据校验
 *
 * @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<>();
            Set<String> codeSet = new HashSet<>();
            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 + "'";
                    String countSql = "SELECT h.vbillcode" +
                            " FROM ic_generalin_b b" +
                            " LEFT JOIN ic_generalin_h h ON b.cgeneralhid = h.cgeneralhid" +
                            " WHERE b.dr = 0" +
                            " AND b.csourcebillbid = '" + cpickmbid + "'";
                    String vbillcode = (String) new BaseDAO().executeQuery(countSql, new ColumnProcessor());
                    if (StringUtils.isNotEmpty(vbillcode) &&
                            !"~".equals(vbillcode) && !"null".equals(vbillcode)) {
                        codeSet.add(vbillcode);
                    } else {
                        bids.add(cpickmbid);
                    }

                }
                if (MMValueCheck.isEmpty(bids)) {
                    String codeStr = String.join(",", codeSet);
                    ExceptionUtils.wrapBusinessException("存在有关联的其它入库，单号：" + codeStr);
                }

                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;
        }
    }
}
