package com.steering.pss.ydm.base;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

/**
 * @author WHB 2015-1-23
 */
public class CoreLocationDefine extends DBCommon {
    /**
     * 查询仓库信息
     * @param unit 所属部门
     * @return 平台对象
     */
    public CoreReturnObject getStorage(String departMent,String[] arr) {
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreLocationDefine.GetStorage");
    	sql += CoreConverter.toSqlConditionString("MANAGEMENT_NO", arr);
    	sql += " ORDER BY STORAGE_NO";
    	return this.getData(sql,new Object[] { departMent, departMent, departMent } );
    }

    public CoreReturnObject getDorJ(String row,String storeNo,String areNo)
    {
    	return this.getDataBySqlID("CoreLocationDefine.getDorJ",new Object[]{row,storeNo,areNo});
    }
    
    /***
     * 查询此料位是否已放置实物
     * @param rackNo
     * @return
     */
	public CoreReturnObject getButessCount(String rackNo) {
		return this.getDataBySqlID("CoreLocationDefine.getButessCount", new Object[] { rackNo,rackNo,rackNo });
	}
    /**
     * 查询料位信息
     * @param isvalid 是否包含无效数据
     * @param storageno 仓库编号
     * @return 平台对象
     */
    public CoreReturnObject doQuery(Boolean isvalid, String storageno) {
        String deletedFlag = isvalid ? "0" : "";
        Object[] obj = new Object[] { deletedFlag, storageno };
        return this.getDataBySqlID("CoreLocationDefine.Query", obj);
    }

    /**
     * 新增料位信息
     * @param parm 新增的数据参数
     * @return 平台对象
     */
    public CoreReturnObject doAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String storageno = parm.get(0).toString();
        String area = parm.get(2).toString();
        String row = parm.get(3).toString();
        String col = parm.get(4).toString();
//        if (row.length() == 1) {
//            row = "0" + row;
//        }
//        if (col.length() == 1) {
//            col = "0" + col;
//        }
        if (parm.get(5).toString().equals("")) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreLocationDefine.IsExistCol");
            sql += "AND T.COL_NO=?";
            Object[] obj = new Object[] { storageno, area, row, col };
            try {
                ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(
                        sql, obj);
                if (rs.next()) {
                    cro.setV_errMsg("列号已经存在！");
                    cro.setV_errCode(-1);
                    rs.close();
                    return cro;
                }
                rs.close();
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }

            parm.set(3, row);
            parm.set(4, col);
            parm.remove(5);
            String location = storageno;
            parm.add(location);
            parm.add(location);
            return this
                    .setDataBySqlID("CoreLocationDefine.Add", parm.toArray());
        }
        else {
            ArrayList<Object> sqlIDList = new ArrayList<Object>(); // SQL语句集合
            ArrayList<Object[]> parmlist = new ArrayList<Object[]>(); // 参数集合
            String colstart = parm.get(4).toString();
            String colend = parm.get(5).toString();
            int start = Integer.parseInt(colstart);
            int end = Integer.parseInt(colend);
            int len = Integer.parseInt(parm.get(6).toString()); // 初始长度
            int wid = Integer.parseInt(parm.get(7).toString()); // 初始宽度
            parm.remove(5);
            parm.set(3, row);
            int j = 0;
            for (int i = start; i <= end; i++) {
                sqlIDList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreLocationDefine.Add"));
                DecimalFormat df = new DecimalFormat("00");
                String newcol = df.format(i);
                String loca = storageno;
                parm.set(4, newcol);
                parm.set(8, len + len * j); // 新增多条数据时，XY坐标根据初始坐标和长度宽度自动确定
                parm.set(9, wid + wid * j);
                parm.add(loca);
                parm.add(loca);
                parmlist.add(parm.toArray());
                parm.remove(parm.size() - 1);
                j++;
            }
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreLocationDefine.IsExistCol");
            sql += "AND T.COL_NO>=? AND T.COL_NO<=?";
            Object[] obj = new Object[] { storageno, area, row, colstart,
                    colend };
            try {
                ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(
                        sql, obj);
                if (rs.next()) {
                    cro.setV_errMsg("列号已经存在！");
                    cro.setV_errCode(-1);
                    rs.close();
                    return cro;
                }
                rs.close();
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }
            cro = this.getDao("TgDao").ExcuteTransction(sqlIDList, parmlist);
            return cro;
        }
    }

    /**
     * 更新料位的信息
     * @param parm 更新信息参数
     * @return 平台对象
     */
    public CoreReturnObject doUpdate(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String storageno = parm.get(0).toString();
        String area = parm.get(2).toString();
        String row = parm.get(3).toString();
        String col = parm.get(4).toString();
        String oldloca = parm.get(parm.size() - 1).toString();
//        if (row.length() == 1) {
//            row = "0" + row;
//        }
//        if (col.length() == 1) {
//            col = "0" + col;
//        }
      /*  if (parm.get(5).toString().equals("")) {*/
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreLocationDefine.IsExistCol_Update");
            sql += " AND T.COL_NO=?";
            Object[] obj = new Object[] { storageno, area, row, oldloca, col };
            try {
                ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(
                        sql, obj);
                if (rs.next()) {
                    cro.setV_errMsg("列号已经存在！");
                    cro.setV_errCode(-1);
                    rs.close();
                    return cro;
                }
                rs.close();
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }

            parm.set(3, row);
            parm.set(4, col);
            parm.remove(5);

           // parm.add(oldloca);
            return this.setDataBySqlID("CoreLocationDefine.Update", parm
                    .toArray());
       /* }
        else {
            ArrayList<Object> sqlIDList = new ArrayList<Object>(); // SQL语句集合
            ArrayList<Object[]> parmlist = new ArrayList<Object[]>(); // 参数
            String colstart = parm.get(4).toString();
            String colend = parm.get(5).toString();
            if (colstart.length() == 1) {
                colstart = "0" + colstart;
            }
            if (colend.length() == 1) {
                colend = "0" + colend;
            }
            int start = Integer.parseInt(colstart);
            int end = Integer.parseInt(colend);
            int len = Integer.parseInt(parm.get(6).toString()); // 料位长度
            int wid = Integer.parseInt(parm.get(7).toString()); // 料位宽度
            parm.remove(5);
            parm.set(3, row);
            int j = 0;
            for (int i = start; i <= end; i++) {
              //  String newloca = oldloca.substring(0, oldloca.length() - 2);
                sqlIDList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreLocationDefine.Update"));
                DecimalFormat df = new DecimalFormat("00");
                String newcol = df.format(i);
                String loca = storageno + area + row + newcol;
                parm.set(4, newcol);
                parm.set(8, len + len * j); // 批量修改了 初始值 XY坐标自动更新
                parm.set(9, wid + wid * j);
                parm.set(parm.size() - 1, loca);
               // newloca = newloca + newcol;
               // parm.add(newloca);
                parmlist.add(parm.toArray());
                parm.remove(parm.size() - 1);
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreLocationDefine.IsExistCol_Update");
                sql += " AND T.COL_NO=?";
                Object[] obj = new Object[] { storageno, area, row, newloca,
                        newcol };
                try {
                    ResultSet rs = this.getDao("TgDao")
                            .ExceuteQueryForResultSet(sql, obj);
                    if (rs.next()) {
                        cro.setV_errMsg("列号已经存在！");
                        cro.setV_errCode(-1);
                        rs.close();
                        return cro;
                    }
                    rs.close();
                    j++;
                }
                catch (SQLException e) {
                    CoreIComponent.logAdapter.filelogger.logError(e
                            .getMessage());// 将服务端的错误信息写入日志文件
                }
            }

            cro = this.getDao("TgDao").ExcuteTransction(sqlIDList, parmlist);
            return cro;
        }*/
    }

    /**
     * 删除或恢复数据
     * @param rackno 料位编码
     * @param name 操作人
     * @param flag 删除 恢复标志
     * @return 平台对象
     */
    public CoreReturnObject doDelete(String rackno, String name, Boolean flag) {
        if (flag) {
            return this.setDataBySqlID("CoreLocationDefine.Delete",
                    new Object[] {rackno });
        }
        else {
            return this.setDataBySqlID("CoreLocationDefine.Resume",
                    new Object[] { rackno });
        }
    }

    public CoreReturnObject getRack() {
        return this.getDataBySqlID("CoreLocationDefine.GetRack");
    }

    /**
     * 料架维护信息
     * @param parm 更新的料位参数
     * @param parmupdate 更新料架的编码
     * @return 平台对象
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject setRack(ArrayList<Object> parm,
            ArrayList<Object> parmupdate) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<Object> sqlIDList = new ArrayList<Object>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < parm.size(); i++) {
            ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
            String rackno = list.get(0).toString();
            Object[] obj = new Object[] { rackno };
            sqlIDList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreLocationDefine.UpdateRack_Use"));
            parmlist.add(obj);
            sqlIDList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreLocationDefine.SetRack"));
            parmlist.add(list.toArray());
        }
        for (int i = 0; i < parmupdate.size(); i++) {
            sqlIDList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreLocationDefine.UpdateRack_Notuse"));
            parmlist.add(new Object[] { parmupdate.get(i).toString() });
        }
        cro = this.getDao("TgDao").ExcuteTransction(sqlIDList, parmlist);
        return cro;
    }
}
