package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.Mcms.entity.CmmInputRecordEntity;
import com.steering.Mcms.entity.CmmWeightMatEntity;
import com.steering.Mcms.entity.CmmWeightRecordEntity;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import org.hsqldb.lib.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by 朱少波 on 2020/11/20.
 */
public class InputRecordServer extends DBCommon {


    public CoreReturnObject doQuery(HashMap<String,Object> para) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("InputRecordServer.doQuery");


        if(para.containsKey("recordType") && para.get("recordType")!=null && para.get("recordType")  instanceof java.util.ArrayList ){

            sql +=   CoreConverter.toSqlConditionStringList("RECORD_TYPE", (ArrayList<String>) para.get("recordType"));
        }

        sql += " Order by  RECORD_DATE   ";

        return this.getData(sql);
    }


    public static void Save(Connection connection, CmmWeightRecordEntity cmmWeightRecordEntity) throws SQLException {
        if(!StringUtil.isEmpty(cmmWeightRecordEntity.getCarNo())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("1");
            cmmInputRecordEntity.setRecordValue(cmmWeightRecordEntity.getCarNo().startsWith("厂内")
                    ?cmmWeightRecordEntity.getCarNo().replace("厂内 ","")
                    :cmmWeightRecordEntity.getCarNo().substring(1));

           DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightRecordEntity.getMatName())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("2");
            cmmInputRecordEntity.setRecordKey(cmmWeightRecordEntity.getRecordType());
            cmmInputRecordEntity.setRecordValue(cmmWeightRecordEntity.getMatName());

            DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightRecordEntity.getOrderName())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("3");
            cmmInputRecordEntity.setRecordValue(cmmWeightRecordEntity.getOrderName());
           DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightRecordEntity.getShippersName())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("4");
            cmmInputRecordEntity.setRecordValue(cmmWeightRecordEntity.getShippersName());

            DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightRecordEntity.getReceiveName())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("5");
            cmmInputRecordEntity.setRecordValue(cmmWeightRecordEntity.getReceiveName());

            DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightRecordEntity.getTransportName())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("6");
            cmmInputRecordEntity.setRecordValue(cmmWeightRecordEntity.getTransportName());

            DoSave(connection, cmmInputRecordEntity);
        }



    }



    public static void Save(Connection connection, CmmWeightMatEntity cmmWeightMatEntity) throws SQLException {

        if(!StringUtil.isEmpty(cmmWeightMatEntity.getProducname())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("7");
            cmmInputRecordEntity.setRecordValue(cmmWeightMatEntity.getProducname());

            DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightMatEntity.getSteelname())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("8");
            cmmInputRecordEntity.setRecordValue(cmmWeightMatEntity.getSteelname());

            DoSave(connection, cmmInputRecordEntity);
        }

        if(!StringUtil.isEmpty(cmmWeightMatEntity.getGradename())) {

            CmmInputRecordEntity cmmInputRecordEntity = new CmmInputRecordEntity();
            cmmInputRecordEntity.setRecordType("9");
            cmmInputRecordEntity.setRecordValue(cmmWeightMatEntity.getGradename());

            DoSave(connection, cmmInputRecordEntity);
        }


    }


    public static void DoSave(Connection connection, CmmInputRecordEntity cmmInputRecordEntity) throws SQLException {

        PreparedStatement pst;


        String sql = CoreXmlSqlParsersFactory
                .getSql("InputRecordServer.doDelete");
        pst = connection.prepareStatement(sql);
        pst.setObject(1,cmmInputRecordEntity.getRecordLocation());
        pst.setObject(2,cmmInputRecordEntity.getRecordKey());
        pst.setObject(3,cmmInputRecordEntity.getRecordType());
        pst.setObject(4,cmmInputRecordEntity.getRecordValue());
        pst.executeUpdate();
        pst.close();


        sql = CoreXmlSqlParsersFactory
                .getSql("InputRecordServer.doAdd");
        pst = connection.prepareStatement(sql);
        pst.setObject(1,cmmInputRecordEntity.getRecordLocation());
        pst.setObject(2,cmmInputRecordEntity.getRecordKey());
        pst.setObject(3,cmmInputRecordEntity.getRecordType());
        pst.setObject(4,cmmInputRecordEntity.getRecordValue());
        pst.executeUpdate();
        pst.close();


        sql = CoreXmlSqlParsersFactory
                .getSql("InputRecordServer.doDelete2");
        pst = connection.prepareStatement(sql);
        pst.setObject(1,cmmInputRecordEntity.getRecordLocation());
        pst.setObject(2,cmmInputRecordEntity.getRecordKey());
        pst.setObject(3,cmmInputRecordEntity.getRecordType());
        pst.executeUpdate();
        pst.close();




    }


}
