package com.steering.pss.sale.order.DAL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.model.CraftAutoPeopleEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 数据库表CRAFT_AUTO_PEOPLE所对应的Java类（生成工具：代码生成工具4.0   访问地址：http://172.16.2.128/tool/）
 * @author tgcx-test   date 2017-02-24
 */
public class CraftAutoPeopleDAL extends DBCommon{

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String customInfo){
        SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.Query");
    	b.Fill(new String[]{ customInfo });
        return this.getData(b.toString());
    }
    
    public CoreReturnObject QueryPopup(String ordLnPk){
        SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.QueryPopup");
    	b.Fill(new String[]{ ordLnPk });
    	
        return this.getData(b.toString());
    }
    
    /**
     * 确认人工审核
     * @param ordLnPk
     * @param auditCode
     * @param auditCode1
     * @param approveCode
     * @param approveCode1
     * @return
     */
    public CoreReturnObject Submit(String ordLnPk, String auditCode, String auditCode1,
    		String approveCode, String approveCode1,String userId,String userName){
    	Connection con = null;
    	CoreReturnObject core = new CoreReturnObject();
    	
    	
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			
			//删除原有审核数据
			ArrayList<String> sqllist = new ArrayList<String>();
	    	SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.DeleteDesignUnit");
	    	b.Fill(new String[]{ ordLnPk });
	    	sqllist.add(b.toString());
			
	    	//获取最大流水号顺序号
			String demandNo="";
			String sql = CoreXmlSqlParsersFactory
					.getSql("CraftAutoPeopleDAL.getMaxSeq");
			PreparedStatement ps = con.prepareStatement(sql);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
	        String a="C"+dateFormat.format(new Date());
	        ps.setObject(1, "C"+dateFormat.format(new Date()));
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				demandNo=rs.getString("MESSAGE_NO");
	            if(demandNo==null)
	            	demandNo="C"+dateFormat.format(new Date())+"00001";
	            else
	            	demandNo="C"+dateFormat.format(new Date())+String.format("%05d",(Integer.parseInt(demandNo.substring(10))+1));
	        }else{
	        	demandNo="C"+dateFormat.format(new Date())+"00001";
			}
			rs.close();
			ps.close();
			
			//删除消息从表数据
			String sqlDeleteList = CoreXmlSqlParsersFactory
					.getSql("CraftAutoPeopleDAL.DeleteMessageUserInfo");
			PreparedStatement patDeleteList = con
					.prepareStatement(sqlDeleteList);
			patDeleteList.setObject(1, ordLnPk);
			patDeleteList.addBatch();
			patDeleteList.executeBatch();
			
			//删除消息主表数据
			sqlDeleteList = CoreXmlSqlParsersFactory
					.getSql("CraftAutoPeopleDAL.DeleteMessageInfo");
			patDeleteList = con
					.prepareStatement(sqlDeleteList);
			patDeleteList.setObject(1, ordLnPk);
			patDeleteList.addBatch();
			patDeleteList.executeBatch();
			
			//新增消息主表数据
			String sqlAddBcOutList = CoreXmlSqlParsersFactory
					.getSql("CraftAutoPeopleDAL.InsertMessageInfo");
			PreparedStatement patAddBcOutList = con
					.prepareStatement(sqlAddBcOutList);
			patAddBcOutList.setObject(1, demandNo);
			patAddBcOutList.setObject(2, "工艺审批");
			patAddBcOutList.setObject(3, "90040101");
			patAddBcOutList.setObject(4, "*");
			patAddBcOutList.setObject(5, "90040202");
			patAddBcOutList.setObject(6, "审批流程");
			patAddBcOutList.setObject(7, "20");
			patAddBcOutList.setObject(8, ordLnPk);
			patAddBcOutList.setObject(9, "1020702");
			patAddBcOutList.setObject(10, "craft");
			patAddBcOutList.setObject(11, "1020702");
			patAddBcOutList.setObject(12, "1");
			patAddBcOutList.setObject(13, userId);
			patAddBcOutList.setObject(14, userName);
			patAddBcOutList.setObject(15, userId);
			patAddBcOutList.setObject(16, userName);
			patAddBcOutList.addBatch();
			patAddBcOutList.executeBatch();
			
			//新增消息子表表数据
			sqlAddBcOutList = CoreXmlSqlParsersFactory
					.getSql("CraftAutoPeopleDAL.InsertMessageUserInfo");
			patAddBcOutList = con
					.prepareStatement(sqlAddBcOutList);
			patAddBcOutList.setObject(1, demandNo);
			patAddBcOutList.setObject(4, "00");
			
			
			if(!"".equals(auditCode)){
				patAddBcOutList.setObject(2, auditCode);
				patAddBcOutList.setObject(3, auditCode);
				patAddBcOutList.setObject(5, "1020702");
				patAddBcOutList.addBatch();
				patAddBcOutList.executeBatch();
			}
			if(!"".equals(auditCode1)){
				patAddBcOutList.setObject(2, auditCode1);
				patAddBcOutList.setObject(3, auditCode1);
				patAddBcOutList.setObject(5, "1020711");
				patAddBcOutList.addBatch();
				patAddBcOutList.executeBatch();
			}
			if(!"".equals(approveCode)){
				patAddBcOutList.setObject(2, approveCode);
				patAddBcOutList.setObject(3, approveCode);
				patAddBcOutList.setObject(5, "1020710");
				patAddBcOutList.addBatch();
				patAddBcOutList.executeBatch();
			}
			if(!"".equals(approveCode1)){
				patAddBcOutList.setObject(2, approveCode1);
				patAddBcOutList.setObject(3, approveCode1);
				patAddBcOutList.setObject(5, "1020712");
				patAddBcOutList.addBatch();
				patAddBcOutList.executeBatch();
			}
			b = new SqlBuilder("CraftAutoPeopleDAL.InsertDesignUnit");
	    	b.Fill(new String[]{ ordLnPk, auditCode, auditCode1, approveCode, approveCode1 });
	    	sqllist.add(b.toString());
	    	b = new SqlBuilder("CraftAutoPeopleDAL.UpdateCraftOrdAudit");
	    	b.Fill(new String[]{ auditCode, auditCode1, approveCode, approveCode1, ordLnPk });
	    	sqllist.add(b.toString());
	    	con.commit();
	    	return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return core;
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String pk){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("CraftAutoPeopleDAL.QueryByPk");
        return this.getData(sqlQuery);
    }

    /**
     * 保存
     */
    public CoreReturnObject Save(ArrayList<String> jsons){
    TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                CraftAutoPeopleEntity craftAutoPeopleEntity = EntityTool.getEntityByGson(json, CraftAutoPeopleEntity.class);
                if(craftAutoPeopleEntity.getAutoFlag().equals("true")){
                	craftAutoPeopleEntity.setAutoFlag("1");
                }else{
                	craftAutoPeopleEntity.setAutoFlag("0");
                }
                String sqlSave = "";
                if(craftAutoPeopleEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftAutoPeopleDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftAutoPeopleDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftAutoPeopleEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftAutoPeopleEntity.setPk(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftAutoPeopleDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftAutoPeopleEntity);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    /**
     * 删除
     * @param jsons
     * @return
     */
    public CoreReturnObject Delete(ArrayList<String> jsons){
    	ArrayList<String> sqllist = new ArrayList<String>();
    	for(String json : jsons){
	    	CraftAutoPeopleEntity craftAutoPeopleEntity = EntityTool.getEntityByGson(json, CraftAutoPeopleEntity.class);
	        String sqlDelete = CoreXmlSqlParsersFactory.getSql("CraftAutoPeopleDAL.Delete");
	        sqlDelete = EntityTool.fillSqlByEntity(sqlDelete, craftAutoPeopleEntity);
	        sqllist.add(sqlDelete);
    	}
    	return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
    }
    
    /**
     * 获取管理科室
     * @return
     */
    public CoreReturnObject GetUnit(){
    	SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.GetUnit");
    	return this.getData(b.toString());
    }
    
    /**
     * 获取审核人
     * @return
     */
    public CoreReturnObject GetAudit(){
    	SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.GetAudit");
    	return this.getData(b.toString());
    }
    
    /**
     * 是否存在科室
     * @param unitCode
     * @param pk
     * @return
     */
    public CoreReturnObject IsExisUnit(String unitCode, String pk){
    	SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.IsExistUnit");
    	b.Fill(new String[]{unitCode, pk});
    	return this.getData(b.toString());
    }
    
    /**
     * 是否存在科室 人工审核
     * @param unitCode
     * @param pk
     * @return
     */
    public CoreReturnObject IsExisUnit2(String unitCode, String auditCode, String auditCode1,
    		String approveCode, String approveCode1, String pk){
    	SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.IsExistUnit2");
    	b.Fill(new String[]{unitCode, auditCode, auditCode1, approveCode, approveCode1, pk});
    	return this.getData(b.toString());
    }
    
    /**
     * 获取工艺评审制定的审核人
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getCraftOrdUnit(String ordLnPk){
    	SqlBuilder b = new SqlBuilder("CraftAutoPeopleDAL.getCraftOrdUnit");
    	b.Fill(new String[]{ ordLnPk });
    	return this.getData(b.toString());
    }
}
