# ReticleQueryDAOImpl.java

    package com.mycim.server.wip.dao.impl;

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.jdbc.mapper.RowMapper;
    import com.mycim.framework.utils.lang.BooleanUtils;
    import com.mycim.server.wip.dao.ReticleQueryDAO;
    import com.mycim.valueobject.bas.Relation;
    import com.mycim.valueobject.consts.EntityEnum;
    import com.mycim.valueobject.consts.LinkTypeList;
    import org.springframework.stereotype.Repository;

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/23
     **/
    @Repository
    public class ReticleQueryDAOImpl implements ReticleQueryDAO {

        private final JdbcTemplate jdbcTemplate;

        public ReticleQueryDAOImpl(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }

        @Override
        public List<Relation> getReticlesForMovein(Long reticleFamilyRrn, Long entityRrn, Boolean statusFLag) {
            List args = new ArrayList();
            StringBuilder sb = new StringBuilder();
            sb.append("select R.FROM_RRN,R.TO_RRN,R.LINK_TYPE,R.SEQUENCE_NUMBER,nb.instance_id, ");
            sb.append(" nb.instance_desc ");
            sb.append(" from relation r, named_object nb, entity e,entity_status es,entity_ext x ");
            sb.append(" where r.link_type = ? ");
            sb.append("  and nb.instance_rrn = r.from_rrn and nb.instance_rrn = e.entity_rrn");
            sb.append(" and e.entity_rrn=x.eqpt_rrn ");
            sb.append(" and e.entity_rrn=es.entity_rrn  ");
            sb.append(" and r.to_rrn = ? ");
            args.add(LinkTypeList.RETICLE_TO_RETICLEFAMILY_KEY);
            args.add(reticleFamilyRrn);
            if (entityRrn != null && entityRrn > 0) {
                sb.append(" and e.location_rrn=? ");
                args.add(entityRrn);
            }

            if (BooleanUtils.isTrue(statusFLag)) {
                sb.append(" and es.current_status in(?,?,?)");
                args.add(EntityEnum.IDLE.getValue());
                args.add(EntityEnum.RELEASE.getValue());
                args.add(EntityEnum.RUN.getValue());

            }
            return jdbcTemplate.query(sb.toString(), args.toArray(), new RowMapper<Relation>() {

                @Override
                public Relation mapRow(ResultSet rs, int rowNum) throws SQLException {

                    Relation relation = new Relation();
                    relation.setInstanceRrn(rs.getLong("FROM_RRN"));
                    relation.setFromRrn(rs.getLong("FROM_RRN"));
                    relation.setToRrn(rs.getLong("TO_RRN"));

                    relation.setInstanceId(rs.getString("instance_id"));
                    relation.setInstanceDesc(rs.getString("instance_desc"));

                    return relation;
                }

            });
        }

    }