package com.steering.pss.qcm;

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

import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
 
import com.steering.pss.qcm.model.ComBaseWellEntity;
import com.steering.pss.qcm.model.ComBaseWellMonthEntity;
 
 
/**
 * 产能井设置管理类
 * @author Administrator pdy 2014/10/31
 * @version v1.0
 */
public class ComBaseWell extends DBCommon {
   /**
    * 产能井 查询
    * @param validFlag 有效标识
    * @return 数据集
    */
    public CoreReturnObject getComBaseWellData(String validFlag) {
        Object[] params = new Object[] { validFlag };
        return this.getDataBySqlID("ComBaseWell.QUERY", params);
    }

    public CoreReturnObject getMonthWell(String wellcode) {
        Object[] params = new Object[] { wellcode };
        return this.getDataBySqlID("ComBaseWell.QUERYMonth", params);
    }

 
    /**
     * 产能井设置 验证产能井描述是否重复
     * @param wellName 产能井描述
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject checkComBaseWell(String wellName) {
        Object[] params = new Object[] { wellName };
        return this.getDataBySqlID("ComBaseWell.CHECK", params);
    }
    
    /**
     * 产能井设置 验证产能井描述是否重复
     * @param wellName 产能井描述
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject checkBaseWellUpdate(String wellName,String wellCode) {
        Object[] params = new Object[] { wellName ,wellCode};
        return this.getDataBySqlID("ComBaseWell.CHECKUpdate", params);
    }

    public CoreReturnObject checkYear(String wellcode,String year){
        Object[] params = new Object[] { wellcode ,year};
        return this.getDataBySqlID("ComBaseWell.CHECKYear", params);
    }
  
    public CoreReturnObject checkWellDelete(String wellcode){
        Object[] params = new Object[] { wellcode };
        return this.getDataBySqlID("CoreBaseWell.checkWellDelete", params);
    }
  
    public CoreReturnObject checkFromResume(String wellcode){
        Object[] params = new Object[] { wellcode };
        return this.getDataBySqlID("CoreBaseWell.checkFromResume", params);
    }

    
    public CoreReturnObject saveWell(ArrayList<Object> baseWell,ArrayList<Object> monthWell){
        ArrayList<String> sqlIdList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        HashMap<String, String> stdCodes = new HashMap<String, String>();
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreBaseWell.queryWellMax");
            ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(sql);
            int stdCodePart = 0;
            if (rs.next()) {
                stdCodePart = Integer
                        .parseInt(rs.getString(1).substring(1, 4));
            }
            rs.close();
            for (int i = 0; i < baseWell.size(); i++) {
                JSONObject json = JSONObject.fromObject(baseWell.get(i)
                        .toString());
                ComBaseWellEntity com = (ComBaseWellEntity) JSONObject.toBean(json,
                        ComBaseWellEntity.class);
                Object[] param = null;
                if (com.getValidflag().equals("")) {
                    DecimalFormat df = new DecimalFormat("000");
                    String stdCode = "J" + df.format(stdCodePart + 1);
                    param = new Object[] { stdCode, com.getWellName(),
                            com.getWeight(),com.getCreateName() ,
                            com.getMemo(), com.getProcessCdoe(), com.getProcessDesc()};
                    stdCodePart += 1;
                    stdCodes.put(com.getWellCode(), stdCode);
                    sqlIdList.add("ComBaseWell.ADD");
                    params.add(param);
                }
                else {
                    param = new Object[] {com.getWellName(),
                            com.getWeight(),  com.getMemo(), 
                            com.getUpdateName(),com.getProcessCdoe(), com.getProcessDesc(),
                            com.getWellCode() };
                    sqlIdList.add("ComBaseWell.UPDATE");
                    params.add(param);
                }
            }
            
            for (int i = 0; i < monthWell.size(); i++) {
                JSONObject json = JSONObject.fromObject(monthWell.get(i)
                        .toString());
                ComBaseWellMonthEntity com = (ComBaseWellMonthEntity) JSONObject.toBean(json,
                        ComBaseWellMonthEntity.class);
                Object[] param = null;
                if (com.getValidflag().equals("")) {
                    if (stdCodes.containsKey(com.getWellCode())) {
                        com.setWellCode(stdCodes.get(com.getWellCode()));
                    }
                    param = new Object[] { com.getWellCode(),
                           com.getWellName(),com.getYear(),
                           com.getMonth1(),com.getMonth2(),
                           com.getMonth3(),com.getMonth4(),
                           com.getMonth5(),com.getMonth6(),
                           com.getMonth7(),com.getMonth8(),
                           com.getMonth9(),com.getMonth10(),
                           com.getMonth11(),com.getMonth12(),
                           com.getCreateName(),com.getMemo()};
                    sqlIdList.add("ComBaseWell.ADDMonth");
                    params.add(param);
                }
                else {
                    param = new Object[] { 
                            com.getWellName(),com.getYear(),
                            com.getMonth1(),com.getMonth2(),
                            com.getMonth3(),com.getMonth4(),
                            com.getMonth5(),com.getMonth6(),
                            com.getMonth7(),com.getMonth8(),
                            com.getMonth9(),com.getMonth10(),
                            com.getMonth11(),com.getMonth12(),
                            com.getUpdateName(),com.getMemo(),
                            com.getWellCode(),com.getYearOld()};
                    sqlIdList.add("ComBaseWell.UpDateMonth");
                    params.add(param);
                }
            }
        }
        catch(SQLException e) {
        }
        return  this.setDataById(sqlIdList, params);
    }

    
    public CoreReturnObject deleteOrResume(ArrayList<Object> wellData,
            ArrayList<Object> monthData,   Boolean flag) {
        ArrayList<String> sqlIdList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        
        for (int i = 0; i < wellData.size(); i++) {
            JSONObject json = JSONObject.fromObject(wellData.get(i).toString());
            ComBaseWellEntity com = (ComBaseWellEntity) JSONObject.toBean(json,
                    ComBaseWellEntity.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] { com.getDeleteName(), com.getWellCode() };
                sqlIdList.add("CoreBaseWell.deleteWell");
            }
            else {
                param = new Object[] { "", com.getWellCode() };
                sqlIdList.add("CoreBaseWell.resumeWell");
            }
            params.add(param);
        }
        for (int i = 0; i < monthData.size(); i++) {
            JSONObject json = JSONObject.fromObject(monthData.get(i).toString());
            ComBaseWellMonthEntity com = (ComBaseWellMonthEntity) JSONObject
                    .toBean(json, ComBaseWellMonthEntity.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] { com.getDeleteName(), com.getWellCode(),com.getYear() };
                sqlIdList.add("CoreBaseWell.deleteMonthWell");
            }
            else {
                param = new Object[] { "",com.getWellCode(),com.getYear()};
                sqlIdList.add("CoreProductManage.resumeMonthWell");
            }
            params.add(param);
        }
        return this.setDataById(sqlIdList, params);
    }
        
        
}
