package com.hzs.scm.general.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.domain.sale.classify.BdSpecs;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.scm.general.param.GeneralBParam;
import com.hzs.scm.general.param.GeneralParam;
import com.hzs.scm.pub.controller.ScmController;

import java.util.*;

/**
 * @description: 出入库公共的controller
 * @author: sui q
 * @time: 2023/11/29 11:05
 * @classname: IcGeneralController
 * @package_name: com.hzs.scm.general.controller
 * version 1.0.0
 */
public abstract class IcGeneralController extends ScmController {

    protected String validateGeneralParam(GeneralParam generalParam) {
        if (validateGeneralFieldParam(generalParam)) {
            return MemberMsgConstants.REQUIRED_NOT_EMPTY;
        }
        generalParam.setPkCountry(SecurityUtils.getPkCountry());
        Map<String, BdSpecs> specsMap = new HashMap<>();
        String message = validateProduct(generalParam.getGeneralbParamList(), specsMap);
        if(ObjectUtil.isNotEmpty(message)){
            return message;
        }
        // 验证供应商
        String msg = validateCuBasDoc(generalParam.getPkCubasdoc(), generalParam.getPkCountry());
        if(ObjectUtil.isNotEmpty(msg)){
            return msg;
        }
        // 验证客户
        msg = validateCustomer(generalParam.getPkCustomer(), generalParam.getPkCountry());
        if(ObjectUtil.isNotEmpty(msg)){
            return msg;
        }
        // 验证采购组织
        msg = validateSysCorp(generalParam.getPkCorp(), generalParam.getPkCountry());
        if(ObjectUtil.isNotEmpty(msg)){
            return msg;
        }
        // 验证批次号
        return validateProductBatchCode(generalParam.getGeneralbParamList(), specsMap);
    }

    protected Boolean validateGeneralFieldParam(GeneralParam generalParam) {
        if (generalParam == null || generalParam.getOrderDate() == null || generalParam.getPkWarehouse() == null ||
                generalParam.getPkCorp() == null || CollectionUtil.isEmpty(generalParam.getGeneralbParamList())) {
            return Boolean.TRUE;
        }
        // 验证 采购订单明细字段不能为空
        for (GeneralBParam generalbParam : generalParam.getGeneralbParamList()) {
            if (generalbParam.getPkUnit() == null || generalbParam.getPkProduct() == null ||
                    generalbParam.getBizDate() == null || generalbParam.getNnum() == null ||
                    generalbParam.getPkStorehouse() == null || CollectionUtil.isEmpty(generalbParam.getScmItemSpecsList())) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
}
