package nc.bs.ic.m4d.sign.rule;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.ic.m4d.entity.MaterialOutBodyVO;
import nc.vo.ic.m4d.entity.MaterialOutVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;

public class CheckDbizdateProcess  implements IRule<MaterialOutVO> {

    public CheckDbizdateProcess() {
    }
    @Override
    public void process(MaterialOutVO[] vos) {
        for (MaterialOutVO vo : vos) {
            if (vo.getBodys().length > 0){
                for (MaterialOutBodyVO bodyVO : vo.getBodys()) {
//                    查询流程生产订单 完工时间
//                    String sql = "SELECT m.twillendtime from  mm_mo m LEFT join mm_pmo p ON p.cpmohid=m.cpmohid WHERE p.VBILLCODE = '"+bodyVO.getVproductbatch()+"' and m.VFIRSTBID='"+bodyVO.getCsrc2billbid()+"'";


                    String sql = "SELECT m.twillendtime from  mm_mo m  WHERE m.cmoid in (SELECT\n" +
                            "\tm.VSOURCEMOROWID\n" +
                            "FROM\n" +
                            "\tmm_pickm_b b\n" +
                            "LEFT JOIN mm_pickm m ON\n" +
                            "\tm.cpickmid = b.cpickmid\n" +
                            "WHERE\n" +
                            "\tb.CPICKM_BID = '"+bodyVO.getCpickmbid()+"' )";
                    BaseDAO dao = new BaseDAO();
                    try {
                        String data =(String) dao.executeQuery(sql, new ColumnProcessor());
                        if(bodyVO.getDbizdate().getDateAfter(7).compareTo(new UFDate(data)) >0){
                            throw new BusinessException("材料出库单审批的时候出库日期如果大于对应流程生产订单上的完工日期7天，不能审核。！");
                        };
                    } catch (DAOException e) {
                        throw new RuntimeException(e);
                    } catch (BusinessException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

        }
    }
}
