package com.steering.pss.qcm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;

/**
 * @author whb 2014-10-30
 * @JDK Version <1.6.0_10-rc2-b32>
 */

public class JGCodeManage extends DBCommon {

    public CoreReturnObject GetJGSpec() {
        return this.getDataBySqlID("JGCodeManage.InitJGSpec");    //规格描述
    }

    public CoreReturnObject GetJGProduct() {
        return this.getDataBySqlID("JGCodeManage.InitJGProduct"); // 品种描述
    }

    public CoreReturnObject GetCPProduct() {
        return this.getDataBySqlID("JGCodeManage.InitCPProduct"); // 品名描述
    }

    public CoreReturnObject GetJGAlpha() {
        return this.getDataBySqlID("JGCodeManage.InitJGALPHA"); // Alpha描述
    }

    public CoreReturnObject GetJGType() {
        return this.getDataBySqlID("JGCodeManage.InitJGType"); // 类别
    }

    public CoreReturnObject GetJGSteel() {
        return this.getDataBySqlID("JGCodeManage.InitJGSteel"); // 钢级名称
    }

    public CoreReturnObject GetJGModel() {
        return this.getDataBySqlID("JGCodeManage.InitJGModel"); // 扣型描述
    }
    
    public CoreReturnObject getStdName() {
        return this.getDataBySqlID("JGCodeManage.InitJGStd"); // 扣型描述
    }
    
    public CoreReturnObject getProductName() {
        return this.getDataBySqlID("JGCodeManage.getProductName"); // 品名
    }
 
    /**
     * Title: doQuery
     * Description: 查询
     * @param includeInvalid  true 有效数据 false 有效+无效数据
     * @param parm  查询参数
     * @return   平台统一返回对象
     */
    public CoreReturnObject doQuery(Boolean includeInvalid,
            ArrayList<Object> parm) {
        String deletedFlag = includeInvalid ? "0" : "";
        parm.add(deletedFlag);
        Object[] params = parm.toArray();
        return this.getDataBySqlID("JGCodeManage.QUERY", params);
    }
    
    /**
     * 冶金规范界面根据冶金规范码查询接箍信息
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject QueryWithMsc(ArrayList<String> list) {
    	String sql = CoreXmlSqlParsersFactory.getSql("JGCodeManage.QueryWithMsc");
    	sql = StringTool.ReplaceAll(sql, "#MSC#", list.get(0));
    	sql = StringTool.ReplaceAll(sql, "#STEELCODE#", list.get(1));
        return this.getData(sql);
    }

    /**
     * 冶金规范界面根据冶金规范码查询接箍信息
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject QueryTorqueWithMsc(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory.getSql("JGCodeManage.QueryTorqueWithMsc");
        sql = StringTool.ReplaceAll(sql, "#MSC#", list.get(0));
        sql = StringTool.ReplaceAll(sql, "#STEELCODE#", list.get(1));
        return this.getData(sql);
    }

    /**
     * Title: doQuery1
     * Description:  JGCodeManage.cs界面的查询
     * @param msc  冶金规范码
     * @return   平台统一返回对象
     */
    public CoreReturnObject doQuery1() {
        return this.getDataBySqlID("JGCodeManage.QUERY1");
    }

    /**
     * Title: doAdd
     * Description: 新增JG码
     * @param params  新增接箍码的信息
     * @return   平台统一返回对象
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doAdd(ArrayList<Object> params,ArrayList<Object> steelList,
    		String steelcode, ArrayList<String[]> specs) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<Object> sqlIDList = new ArrayList<Object>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        String jgcode = "";
        String str = CoreXmlSqlParsersFactory.getSql("JGCodeManage.GetJGCode"); //取得最大编号的SQL语句
        try {//自动生成JG码
            ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(str);
            if (rs.next()) {//取得最大编号
                jgcode = rs.getString(1).toString().substring(2, 7); //Java的substring两个参数都是索引，且返回第二个参数-1的字符
                long code = Long.parseLong(jgcode);
                if (code == 99999) {
                    cro = cro.SetErrInfo("接箍编码已经达到最大，请清理数据库");
                    return cro;
                }
                else {//编号加1再加上JG前缀。
                    code += 1;
                    jgcode = "0000" + String.valueOf(code);
                    jgcode = jgcode.substring(jgcode.length()-5,jgcode.length() );
                    jgcode = "JG" + jgcode;
                    params.add(jgcode);
                }
            }
            else {//如果数据库没有值，则第一个添加的序号为JG00001
                jgcode = "JG00001";
                params.add(jgcode);
            }
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
        }
        sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.DeleteSteel"));
        parmlist.add(new Object[]{steelcode});
        for(int i=0;i<steelList.size();i++)
        {
            ArrayList<Object> tempList=( ArrayList<Object>)steelList.get(i);
            sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.LinkSteel"));
            parmlist.add(tempList.toArray());
        }
        Object[] obj = params.toArray();  //执行新增操作。
        sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.ADD"));
        parmlist.add(obj);
        
//        String sqlInsertJgBacklog = CoreXmlSqlParsersFactory.getSql("JGCodeManage.insertJgBacklog");
//        sqlInsertJgBacklog = StringTool.ReplaceAll(sqlInsertJgBacklog, "#CODE_JG#", jgcode);
//        sqlInsertJgBacklog = StringTool.ReplaceAll(sqlInsertJgBacklog, "#CREATE_NAME#", params.get(21).toString());
//        sqlIDList.add(sqlInsertJgBacklog);
//        parmlist.add(null);
        

    	String sqlDeleteJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.deleteJgRSpec");
    	sqlDeleteJgRSpec = StringTool.ReplaceAll(sqlDeleteJgRSpec, "#CODE_JG#", jgcode);
    	sqlIDList.add(sqlDeleteJgRSpec);
        parmlist.add(null);
        
        for(String[] strs : specs){
	        String sqlInsertJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.insertJgRSpec");
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CODE_JG#", jgcode);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_CODE#", strs[0]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_NAME#", strs[1]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CREATE_NAME#", strs[2]);
	        sqlIDList.add(sqlInsertJgRSpec);
	        parmlist.add(null);
        }
        cro = this.getDao("TgDao").ExcuteTransction(sqlIDList, parmlist);
        return cro;
    }

    /**
     * Title: doUpdate
     * Description: 修改
     * @param params   修改的信息
     * @return   平台统一返回对象
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doUpdate(ArrayList<Object> params,ArrayList<Object> steelList,
    		String code, ArrayList<String[]> specs) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<Object> sqlIDList = new ArrayList<Object>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.DeleteSteel"));
        parmlist.add(new Object[]{code});
        for(int i=0;i<steelList.size();i++)
        {
            ArrayList<Object> tempList=( ArrayList<Object>)steelList.get(i);
            sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.LinkSteel"));
            parmlist.add(tempList.toArray());
        }
        Object[] obj = params.toArray();  //执行修改操作。
        sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.UPDATE"));
        parmlist.add(obj);
        //如果MSC与接箍的关系表有记录并且字段为空就更新
        sqlIDList.add(CoreXmlSqlParsersFactory.getSql("JGCodeManage.updateMscRJg"));
        parmlist.add(new Object[]{ obj[37], obj[38], obj[39], obj[40], obj[41], obj[22], obj[42]});
        
    	String sqlDeleteJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.deleteJgRSpec");
    	sqlDeleteJgRSpec = StringTool.ReplaceAll(sqlDeleteJgRSpec, "#CODE_JG#", obj[obj.length - 1].toString());
    	sqlIDList.add(sqlDeleteJgRSpec);
    	parmlist.add(null);
    	
        for(String[] strs : specs){
	        String sqlInsertJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.insertJgRSpec");
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CODE_JG#", obj[obj.length - 1].toString());
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_CODE#", strs[0]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_NAME#", strs[1]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CREATE_NAME#", strs[2]);
	        sqlIDList.add(sqlInsertJgRSpec);
	        parmlist.add(null);
        }
        cro = this.getDao("TgDao").ExcuteTransction(sqlIDList, parmlist);
        return cro;
        
    }
    
    public CoreReturnObject isExistJGCode(ArrayList<Object> params) {
        Object[] obj = params.toArray();
        return this.getDataBySqlID("JGCodeManage.QueryReapt", obj);
    }
    
    public CoreReturnObject isExistJGDescUpdate(ArrayList<Object> params) {
        Object[] obj = params.toArray();
        return this.getDataBySqlID("JGCodeManage.QueryReaptUpdate", obj);
    }

    /**
     * Title: doDelete
     * Description: 删除或者恢复
     * @param code  接箍码
     * @param deletename  删除人
     * @param flag  true 删除 false 恢复
     * @return  平台统一返回对象
     */
    public CoreReturnObject doDelete(String code, String deletename,
            Boolean flag) {
        if (flag) {
            Object[] params = new Object[] { deletename, code };
            return this.setDataBySqlID("JGCodeManage.DELETE", params);
        }
        else {
            Object[] params = new Object[] { code };
            return this.setDataBySqlID("JGCodeManage.RESUME", params);
        }
    }
    
    public CoreReturnObject getSteelGrade() {
        return this.getDataBySqlID("JGCodeManage.QuerySteel");
    }
    
    public CoreReturnObject getDynamicSteel(String steelcode) {
        Object []obj=new Object[]{steelcode};
        return this.getDataBySqlID("JGCodeManage.DynamicSteel", obj);
    }
    
    public CoreReturnObject getStdStyleName() {
        return this.getDataBySqlID("JGCodeManage.InitStdStyle");
    }
    
    public CoreReturnObject QueryJgRMscByDH(String msc, String diameter,
    		String height, String specCode, String modelDesc, String steelCode) {
    	SqlBuilder b = new SqlBuilder("JGCodeManage.QueryJgRMscByDH");
    	b.Fill(new String[]{msc, diameter, height, specCode, modelDesc, steelCode});

        return this.getData(b.toString());
    }

    public CoreReturnObject QueryTorqueByDH(String msc, String diameter,
                                            String height, String specCode, String modelDesc, String steelCode) {
        SqlBuilder b = new SqlBuilder("JGCodeManage.QueryTorqueByDH");
        b.Fill(new String[]{msc, diameter, height, specCode, modelDesc, steelCode});

        return this.getData(b.toString());
    }
    
    /**
     * 修改接箍文件路径
     * @param codeJg
     * @param path
     * @param user
     * @return
     */
    public CoreReturnObject updateJgFile(String codeJg, String path, String user){
    	SqlBuilder b = new SqlBuilder("JGCodeManage.updateJgFile");
    	b.Fill(new String[]{codeJg, path, user});

    	return this.setData(b.toString());
    }

    /**
     * 新增扭矩
     * @param keyJg
     * @param torqueUnit
     * @param torqueMax
     * @param torqueMin
     * @param torqueBest
     * @param pointMax
     * @param pointMin
     * @return
     */
    public CoreReturnObject SaveTorque(String id, String keyJg, String torqueUnit, String torqueMax,
        String torqueMin, String torqueBest, String pointMax, String pointMin, String minD,
        String maxD, String minH, String maxH){
        SqlBuilder b;
        if(id.equals("")){
            b = new SqlBuilder("JGCodeManage.insertTorque", keyJg, torqueUnit, torqueMax,
                    torqueMin, torqueBest, pointMax, pointMin, minD, maxD, minH, maxH );
        }else{
            b = new SqlBuilder("JGCodeManage.updateTorque", id, torqueUnit, torqueMax,
                    torqueMin, torqueBest, pointMax, pointMin, minD, maxD, minH, maxH );
        }

        return this.setData(b.toString());
    }

    /**
     * 修改扭矩
     * @param id
     * @param torqueUnit
     * @param torqueMax
     * @param torqueMin
     * @param torqueBest
     * @param pointMax
     * @param pointMin
     * @return
     */
    public CoreReturnObject updateTorque(String id, String torqueUnit, String torqueMax,
         String torqueMin, String torqueBest, String pointMax, String pointMin) {
        SqlBuilder b = new SqlBuilder("JGCodeManage.updateTorque", id, torqueUnit, torqueMax,
                torqueMin, torqueBest, pointMax, pointMin );

        return this.setData(b.toString());
    }

    /**
     * 删除扭矩
     * @param id
     * @return
     */
    public CoreReturnObject deleteTorque(String id){
        SqlBuilder b = new SqlBuilder("JGCodeManage.deleteTorque", id);

        return this.setData(b.toString());
    }
}
