package com.steering.pss.plan.order;

import java.util.ArrayList;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plan.order.model.PlnSaleorderSetEntity;

/**
 * 导入excl设置合同状态
 * @author xiaohai
 */

public class CoreOrderStatusSet extends DBCommon {
    /**
     * 查询Baseinfor
     * @param list
     * @return
     */
    public CoreReturnObject getBaseInfSort(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderStatusSet.getBaseInfSort",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询导入设置记录
     * @param list
     * @return
     */
    public CoreReturnObject queryOrderSetLog(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderStatusSet.queryOrderSetLog", list
                .toArray());
        return cro;
    }

    /**
     * 保存导入日志文本
     * @param list
     * @return
     */
    public CoreReturnObject saveOrderSetlog(
            ArrayList<PlnSaleorderSetEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        // 更新合同状态
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderStatusSet.insertOrderSetlog");
        for (int i = 0; i < list.size(); i++) {
            String sqlUp = EntityTool.fillSqlByEntity(sql, list.get(i));
            sqllist.add(sqlUp);
            parmlist.add(new Object[] {});
        }
        cro = this.setData(sqllist, parmlist);
        return cro;
    }

    /**
     * 同步
     * @param list
     * @return
     */
    public CoreReturnObject updateOrderStatus(
            ArrayList<PlnSaleorderSetEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        // 更新合同状态
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrdLnDlyPk().equals("")) {
                String orderNoseq = list.get(i).getOrderNoSeq();
                String orderNo = orderNoseq.substring(0, orderNoseq
                        .lastIndexOf('/'));
                String orderSeq = orderNoseq.substring(orderNoseq
                        .lastIndexOf('/') + 1);
                //orderSeq = CorePlnComm.padding(orderSeq, '0', 3);
                Object[] param = new Object[] { list.get(i).getEndStatus(),
                        orderNo, orderSeq };
                sqllist.add("CoreOrderStatusSet.setOrderStatus");
                parmlist.add(param);
                
                if(list.get(i).getEndStatus().equals("4")){
                    Object[] Obj = new Object[] { "12020502",
                            orderNo, orderSeq };
                    sqllist.add("CoreOrderStatusSet.setDeliveryStatus");
                    parmlist.add(Obj);
                }
                else if(list.get(i).getEndStatus().equals("5")){
                    Object[] Obj = new Object[] { "12020602",
                            orderNo, orderSeq };
                    sqllist.add("CoreOrderStatusSet.setDeliveryStatus");
                    parmlist.add(Obj);
                }
                else{
                    Object[] Obj = new Object[] { "12020402",
                            orderNo, orderSeq };
                    sqllist.add("CoreOrderStatusSet.setDeliveryStatus");
                    parmlist.add(Obj);
                }
            }
            else {
                Object[] param = new Object[] { list.get(i).getEndStatus(),
                        list.get(i).getOrdLnDlyPk() };
                sqllist.add("CoreOrderStatusSet.setOrderStatusByPk");
                parmlist.add(param);
                
                if(list.get(i).getEndStatus().equals("4")){
                    Object[] Obj = new Object[] { "12020502",
                            list.get(i).getOrdLnDlyPk() };
                    sqllist.add("CoreOrderStatusSet.setDeliveryStatusByPk");
                    parmlist.add(Obj);
                }
                else if(list.get(i).getEndStatus().equals("5")){
                    Object[] Obj = new Object[] { "12020602",
                            list.get(i).getOrdLnDlyPk() };
                    sqllist.add("CoreOrderStatusSet.setDeliveryStatusByPk");
                    parmlist.add(Obj);
                }
                else{
                    Object[] Obj = new Object[] { "12020402",
                            list.get(i).getOrdLnDlyPk() };
                    sqllist.add("CoreOrderStatusSet.setDeliveryStatusByPk");
                    parmlist.add(Obj);
                }
            }

            // 保存记录
            sqllist.add("CoreOrderStatusSet.updateOrderSetlog");
            Object[] param = new Object[] { list.get(i).getEndStatus(),
                    list.get(i).getOrderNoSeq(), list.get(i).getOrdLnDlyPk(),
                    list.get(i).getOrdLnDlyPk(), list.get(i).getOrdLnDlyPk() };
            parmlist.add(param);
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 同步月份
     * @param list
     * @return
     */
    public CoreReturnObject updateOrderStatusMonth(
            ArrayList<PlnSaleorderSetEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        // 更新合同状态
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrdLnDlyPk().equals("")) {
                String orderNoseq = list.get(i).getOrderNoSeq();
                String orderNo = orderNoseq.substring(0, orderNoseq
                        .lastIndexOf('/'));
                String orderSeq = orderNoseq.substring(orderNoseq
                        .lastIndexOf('/') + 1);
                //orderSeq = CorePlnComm.padding(orderSeq, '0', 3);
                Object[] param = new Object[] { list.get(i).getRollMonth(),
                        orderNo, orderSeq };
                sqllist.add("CoreOrderStatusSet.setOrderStatusMonth");
                parmlist.add(param);
            }
            else {
                Object[] param = new Object[] { list.get(i).getRollMonth(),
                        list.get(i).getOrdLnDlyPk() };
                sqllist.add("CoreOrderStatusSet.setOrderStatusMonthByPk");
                parmlist.add(param);
            }

            // 保存记录
            sqllist.add("CoreOrderStatusSet.updateOrderSetlogMonth");
            Object[] param = new Object[] { list.get(i).getIfSetMonth(),
                    list.get(i).getOrderNoSeq(), list.get(i).getOrdLnDlyPk(),
                    list.get(i).getOrdLnDlyPk(), list.get(i).getOrdLnDlyPk() };
            parmlist.add(param);
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 同步生产分类
     * @param list
     * @return
     */
    public CoreReturnObject updateProSort(ArrayList<PlnSaleorderSetEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrdLnDlyPk().equals("")) {
                String orderNoseq = list.get(i).getOrderNoSeq();
                String orderNo = orderNoseq.substring(0, orderNoseq
                        .lastIndexOf('/'));
                String orderSeq = orderNoseq.substring(orderNoseq
                        .lastIndexOf('/') + 1);
                //orderSeq = CorePlnComm.padding(orderSeq, '0', 3);
                Object[] param = new Object[] { list.get(i).getProduceSort(),
                        orderNo, orderSeq };
                sqllist.add("CoreOrderStatusSet.setProSort");
                parmlist.add(param);
            }
            else {
                Object[] param = new Object[] { list.get(i).getProduceSort(),
                        list.get(i).getOrdLnDlyPk() };
                sqllist.add("CoreOrderStatusSet.setProSortByPk");
                parmlist.add(param);
            }

            // 保存记录
            sqllist.add("CoreOrderStatusSet.updateOrderSetlogProSort");
            Object[] param = new Object[] { "1", list.get(i).getOrderNoSeq(),
                    list.get(i).getOrdLnDlyPk(), list.get(i).getOrdLnDlyPk(),
                    list.get(i).getOrdLnDlyPk() };
            parmlist.add(param);
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 同步来源
     * @param list
     * @return
     */
    public CoreReturnObject updateOrderfrom(
            ArrayList<PlnSaleorderSetEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrdLnDlyPk().equals("")) {
                String orderNoseq = list.get(i).getOrderNoSeq();
                String orderNo = orderNoseq.substring(0, orderNoseq
                        .lastIndexOf('/'));
                String orderSeq = orderNoseq.substring(orderNoseq
                        .lastIndexOf('/') + 1);
                //orderSeq = CorePlnComm.padding(orderSeq, '0', 3);
                Object[] param = new Object[] { list.get(i).getOrderfrom(),
                        orderNo, orderSeq };
                sqllist.add("CoreOrderStatusSet.setOrderfrom");
                parmlist.add(param);
            }
            else {
                Object[] param = new Object[] { list.get(i).getOrderfrom(),
                        list.get(i).getOrdLnDlyPk() };
                sqllist.add("CoreOrderStatusSet.setOrderfromByPk");
                parmlist.add(param);
            }

            // 保存记录
            sqllist.add("CoreOrderStatusSet.updateOrderSetlogOrderfrom");
            Object[] param = new Object[] { "1", list.get(i).getOrderNoSeq(),
                    list.get(i).getOrdLnDlyPk(), list.get(i).getOrdLnDlyPk(),
                    list.get(i).getOrdLnDlyPk() };
            parmlist.add(param);
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 删除
     * @param list
     * @return
     */
    public CoreReturnObject deleteOrderStatus(
            ArrayList<PlnSaleorderSetEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        // 更新合同状态
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CoreOrderStatusSet.deleteOrderStatus");
            Object[] param = new Object[] { list.get(i).getOrderNoSeq(),
                    list.get(i).getOrdLnDlyPk(), list.get(i).getOrdLnDlyPk(),
                    list.get(i).getOrdLnDlyPk() };
            parmlist.add(param);
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

}