package com.steering.pss.qcm;

import CoreFS.SA06.CoreReturnObject;
import com.steering.comm.db.DBCommon;
import com.steering.pss.qcm.model.ComBaseWellMonthPlineEntity;
import com.steering.pss.sale.order.model.ComBasePlineEntity;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * 产能井设置管理类
 * @author Administrator pdy 2014/10/31
 * @version v1.0
 */
public class ComBaseWellPline extends DBCommon {
   /**
    * 产能井 查询
    * @param validFlag 有效标识
    * @return 数据集
    */
    public CoreReturnObject getComBaseWellPlineData(String validFlag) {
        Object[] params = new Object[] { validFlag };
        return this.getDataBySqlID("ComBaseWellPline.QUERY", params);
    }

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

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

    public CoreReturnObject checkYear(String wellcode,String year){
        Object[] params = new Object[] { wellcode ,year};
        return this.getDataBySqlID("ComBaseWellPline.CHECKYear", params);
    }
  
    public CoreReturnObject checkWellDelete(String wellcode){
        Object[] params = new Object[] { wellcode };
        return this.getDataBySqlID("ComBaseWellPline.checkWellDelete", params);
    }
  
    public CoreReturnObject checkFromResume(String wellcode){
        Object[] params = new Object[] { wellcode };
        return this.getDataBySqlID("ComBaseWellPline.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>();
        for (int i = 0; i < baseWell.size(); i++) {
            JSONObject json = JSONObject.fromObject(baseWell.get(i)
                    .toString());
            ComBasePlineEntity com = (ComBasePlineEntity) JSONObject.toBean(json,
                    ComBasePlineEntity.class);
            Object[] param = new Object[] {com.getSpecName(),com.getPlineCode() };
            sqlIdList.add("ComBaseWellPline.UPDATE");
            params.add(param);
        }

        for (int i = 0; i < monthWell.size(); i++) {
            JSONObject json = JSONObject.fromObject(monthWell.get(i)
                    .toString());
            ComBaseWellMonthPlineEntity com = (ComBaseWellMonthPlineEntity) JSONObject.toBean(json,
                    ComBaseWellMonthPlineEntity.class);
            Object[] param = null;
            if (com.getValidflag().equals("")) {
                if (stdCodes.containsKey(com.getPlineCode())) {
                    com.setPlineCode(stdCodes.get(com.getPlineCode()));
                }
                param = new Object[] { com.getPlineCode(),
                       com.getPlineName(),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(),com.getHaltHour1(),com.getHaltHour2(),
                        com.getHaltHour3(),com.getHaltHour4(),
                        com.getHaltHour5(),com.getHaltHour6(),
                        com.getHaltHour7(),com.getHaltHour8(),
                        com.getHaltHour9(),com.getHaltHour10(),
                        com.getHaltHour11(),com.getHaltHour12()};
                sqlIdList.add("ComBaseWellPline.ADDMonth");
                params.add(param);
            }
            else {
                param = new Object[] {
                        com.getPlineName(),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.getHaltHour1(),com.getHaltHour2(),
                        com.getHaltHour3(),com.getHaltHour4(),
                        com.getHaltHour5(),com.getHaltHour6(),
                        com.getHaltHour7(),com.getHaltHour8(),
                        com.getHaltHour9(),com.getHaltHour10(),
                        com.getHaltHour11(),com.getHaltHour12(),
                        com.getPlineCode(),com.getYearOld()};
                sqlIdList.add("ComBaseWellPline.UpDateMonth");
                params.add(param);
            }
        }
        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());
            ComBasePlineEntity com = (ComBasePlineEntity) JSONObject.toBean(json,
                    ComBasePlineEntity.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] {  com.getPlineCode() };
                sqlIdList.add("CoreBaseWellPline.deleteWell");
            }
            else {
                param = new Object[] {  com.getPlineCode() };
                sqlIdList.add("CoreBaseWellPline.resumeWell");
            }
            params.add(param);
        }
        for (int i = 0; i < monthData.size(); i++) {
            JSONObject json = JSONObject.fromObject(monthData.get(i).toString());
            ComBaseWellMonthPlineEntity com = (ComBaseWellMonthPlineEntity) JSONObject
                    .toBean(json, ComBaseWellMonthPlineEntity.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] { com.getDeleteName(), com.getPlineCode(),com.getYear() };
                sqlIdList.add("CoreBaseWellPline.deleteMonthWell");
            }
            else {
                param = new Object[] { "",com.getPlineCode(),com.getYear()};
                sqlIdList.add("CoreBaseWellPline.resumeMonthWell");
            }
            params.add(param);
        }
        return this.setDataById(sqlIdList, params);
    }

}
