package market.Qms.qcm.mapper;
import java.util.HashMap;
import java.util.List;

import market.Qms.qcm.vo.QcmZbsDataOrd;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.hnshituo.orm.GenericMapper;

public interface QcmZbsDataOrdMapper extends GenericMapper<QcmZbsDataOrd,Object> {

	@Select("select ${item_code} from QCM_ZBS_DATA_ORD where CRET_NO=#{cret_no}")
	String findValue(@Param("item_code")String item_code, @Param("cret_no")String cret_no);

	List<QcmZbsDataOrd> findChem();

	List<QcmZbsDataOrd> findTitle();

	List<QcmZbsDataOrd> findOrg();

	List<QcmZbsDataOrd> findPic();

	List<QcmZbsDataOrd> getCic();
	
	public List<HashMap<String, String>> getRecord(@Param("cons") String cons);
	
	@Select("SELECT DISTINCT(mat.cret_no) FROM QCM_ZBS_DATA_MAT mat ,Qcm_Zbs_Data_Ord ord where mat.cret_no = ord.cret_no and mat.material_no like '%${material_no}%'")
	List<String> findCrets(@Param("material_no")String material_no);
	
	/*之前的sql：
	 * select * from (select temp.*, rownum row_num from (select cret_no, tmpl_no,train_no, deltail_no, contract_no,steel_code, steel_name,prod_code, prod_name,"
			+ "std_code, std_name,delivery_state_code,delivery_state_desc,order_unit_code,order_unit_desc,reciver_unit_code,reciver_unit_desc,sum_cnt,sum_wgt,load_num,"
			+ "view_num,print_cnt,print_id,print_dtime,is_send,validflag,cfnm_date,create_name,create_time,delete_name,delete_time,plan_no,license_no,org_code,org_name,"
			+ "is_batch,cret_type,print_flag,ce_no,memo,contract_line_seq,send_name,send_time from qcm_zbs_data_ord where ${condition} "
			+ "order by ${orderBy}) temp "
			+ "where rownum <= ${maxR}) where row_num > ${skipR} 
	 * */
	@Select("select * from (select temp.*, rownum row_num from (select t.cret_no, t\n" +
		      "    .tmpl_no, t.train_no, t.deltail_no, t.contract_no, t.steel_code,\n" +
		      "     t.steel_name, t.prod_code, t.prod_name,\n" +
		      "\tt.std_code, t.std_name, t.delivery_state_code, t.delivery_state_desc,\n" +
		      "    t.order_unit_code, t.order_unit_desc, t.reciver_unit_code,t.reciver_unit_desc,t.sum_cnt,t.sum_wgt,t.load_num,t.view_num,t.print_cnt,t.print_id,t.print_dtime,t.is_send,t.validflag,t.cfnm_date,t.create_name,t.create_time,t.delete_name,t.delete_time,t.plan_no,t.license_no,t.org_code,t.org_name,\n" +
		      "\t\t\tt.is_batch,t.cret_type,t.print_flag,t.ce_no,t.contract_line_seq,\n" +
		      "                                                          t.send_name,t\n" +
		      "                                                              .send_time,\n" +
		      "y.RECIVER_ADDRESS \n" +
		      "from qcm_zbs_data_ord t\n" +
		      "left join\n" +
		      "(select distinct t2.CRET_NO, ss.RECIVER_ADDRESS from QCM_ZBS_DATA_MAT t2\n" +
		      "left join YDM_PRODUCT_DETAIL ypd on t2.MATERIAL_NO = ypd.COILNO\n" +
		      "left join SLM_ORDER_INFO soi on ypd.ORDERNO = soi.CONTRACT_NO || soi\n" +
		      "    .CONTRACT_LINE_SEQ || soi.ORDER_SEQ and nvl(soi.CHANGE_FLAG, '0') != '1'\n" +
		      "left join SLM_CUSTOMER_TRANS_SOLUTION ss on soi.SOLUTIONS_NO = ss.SOLUTIONS_NO\n" +
		      " and ROWNUM = 1) y on t.CRET_NO = y.CRET_NO\n" +
		      "where ${condition}\n" +
		      "    order by ${orderBy}\n" +
		      "\t\t\t) temp\n" +
		      "\t\t\twhere rownum <= ${maxR}) where row_num > ${skipR}")
	public List<QcmZbsDataOrd> getBookInfo(@Param("condition")String condition, 
			@Param("orderBy")String orderBy, @Param("skipR")int skipR, @Param("maxR")int maxR);
	
	@Select("select count(1)\n" +
		      "from qcm_zbs_data_ord t\n" +
		      "left join\n" +
		      "(select distinct t2.CRET_NO, ss.RECIVER_ADDRESS from QCM_ZBS_DATA_MAT t2\n" +
		      "left join YDM_PRODUCT_DETAIL ypd on t2.MATERIAL_NO = ypd.COILNO\n" +
		      "left join SLM_ORDER_INFO soi on ypd.ORDERNO = soi.CONTRACT_NO || soi\n" +
		      "    .CONTRACT_LINE_SEQ || soi.ORDER_SEQ and nvl(soi.CHANGE_FLAG, '0') != '1'\n" +
		      "left join SLM_CUSTOMER_TRANS_SOLUTION ss on soi.SOLUTIONS_NO = ss.SOLUTIONS_NO\n" +
		      " and ROWNUM = 1) y on t.CRET_NO = y.CRET_NO\n" +
		      "where ${condition}")
	public int countOrds(@Param("condition")String condition);
}
