package com.steering.pss.qcm;

import java.util.ArrayList;

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.ResBaseResourceAllocationEntity;

public class CoreResBaseResourceAllocation extends DBCommon {

    public CoreReturnObject queryResourceData(Boolean flag) {
        String isValid = flag ? "1" : "0";
        Object[] obj = new Object[] { isValid };
        return this.getDataBySqlID("CoreResBaseResourceAllocation.QUERY", obj);
    }

    public CoreReturnObject getSaleOrg() {
        return this.getDataBySqlID("CoreResBaseResourceAllocation.GetSaleOrg");
    }

    public CoreReturnObject getProcess() {
        return this.getDataBySqlID("CoreResBaseResourceAllocation.GetProcess");
    }

    public CoreReturnObject save(ArrayList<Object> list) {

        ArrayList<String> sqlIdList = new ArrayList<String>();
        // 字段集合
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        CoreReturnObject cro = new CoreReturnObject();
        for (int i = 0; i < list.size(); i++) {
            JSONObject json = JSONObject.fromObject(list.get(i).toString());
            ResBaseResourceAllocationEntity res = (ResBaseResourceAllocationEntity) JSONObject
                    .toBean(json, ResBaseResourceAllocationEntity.class);
            if (res.getValidflag().equals("")) {
                Object[] obj = new Object[] { res.getSaleOrg(),
                        res.getSaleOrgDesc(), res.getProcessCode(),
                        res.getProcessDesc(), res.getYear(), res.getMonth1(),
                        res.getMonth2(), res.getMonth3(), res.getMonth4(),
                        res.getMonth5(), res.getMonth6(), res.getMonth7(),
                        res.getMonth8(), res.getMonth9(), res.getMonth10(),
                        res.getMonth11(), res.getMonth12(),
                        res.getCreateName(), res.getMemo()

                };
                sqlIdList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreResBaseResourceAllocation.INSERT"));
                params.add(obj);
            }
            else {
                Object[] obj = new Object[] { res.getSaleOrg(),
                        res.getSaleOrgDesc(), res.getProcessCode(),
                        res.getProcessDesc(), res.getYear(), res.getMonth1(),
                        res.getMonth2(), res.getMonth3(), res.getMonth4(),
                        res.getMonth5(), res.getMonth6(), res.getMonth7(),
                        res.getMonth8(), res.getMonth9(), res.getMonth10(),
                        res.getMonth11(), res.getMonth12(),
                        res.getUpdateName(), res.getMemo(),
                        res.getSaleOrgOld(), res.getProcessCodeOld(),
                        res.getYearOld()

                };
                sqlIdList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreResBaseResourceAllocation.UPDATE"));
                params.add(obj);
            }
        }
        cro = this.setData(sqlIdList, params);
        return cro;
    }

    public CoreReturnObject deleteOrResume(ArrayList<Object> parm, Boolean flag) {

        ArrayList<String> sqlIdList = new ArrayList<String>();
        // 字段集合
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        if (flag) {
            for (int i = 0; i < parm.size(); i++) {
                JSONObject json = JSONObject.fromObject(parm.get(i).toString());
                ResBaseResourceAllocationEntity res = (ResBaseResourceAllocationEntity) JSONObject
                        .toBean(json, ResBaseResourceAllocationEntity.class);
                Object[] obj = new Object[] { res.getDeleteName(),
                        res.getSaleOrg(), res.getProcessCode(), res.getYear() };
                sqlIdList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreResBaseResourceAllocation.DELETE"));
                params.add(obj);
            }
        }
        else {
            for (int i = 0; i < parm.size(); i++) {
                JSONObject json = JSONObject.fromObject(parm.get(i).toString());
                ResBaseResourceAllocationEntity res = (ResBaseResourceAllocationEntity) JSONObject
                        .toBean(json, ResBaseResourceAllocationEntity.class);
                Object[] obj = new Object[] { res.getSaleOrg(),
                        res.getProcessCode(), res.getYear() };
                sqlIdList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreResBaseResourceAllocation.RESUME"));
                params.add(obj);
            }
        }
        return this.setData(sqlIdList, params);
    }

    public CoreReturnObject isReapt(String sale, String process, String year) {

        Object[] obj = new Object[] { sale, process, year };
        return this
                .getDataBySqlID("CoreResBaseResourceAllocation.ISREAPT", obj);

    }

}
