package com.steering.pss.plnsaleord.processOrder.base;


import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.order.model.CrmOrderProduceEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderZgSEntity;
import com.steering.pss.plnsaleord.processOrder.entity.PlnOrderDbkSEntity;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ProducCrm  extends DBCommon{

    /***
     * 下发新增CRM
     * @return
     */
    public static String insertCrmD(Connection con,ProducEntity producEntity,String processCode,String userName,String strTime) throws SQLException {
        String insertCrmSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.insertCrmD");
        String sql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectCrmOrder");
        sql  = EntityTool.fillSqlByEntity(sql,producEntity);
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<CrmOrderProduceEntity> crmList = EntityTool.getEntitysByRs(rs,CrmOrderProduceEntity.class);
        rs.close();
        ps.close();
        if(crmList.size()>0)
        {
            if(processCode.equals("D"))
            {
                crmList.get(0).setDPlineCode(producEntity.getPlineCode());
                crmList.get(0).setDPlineName(producEntity.getPlineName());
                crmList.get(0).setPassCode(producEntity.getPassCode());
                crmList.get(0).setPassName(producEntity.getPassName());
                if(producEntity.getExeStatus().equals("0"))
                {
                    crmList.get(0).setExeStatus("计划");
                }else if(producEntity.getExeStatus().equals("1"))
                {
                    crmList.get(0).setExeStatus("生产中");
                }else if(producEntity.getExeStatus().equals("2"))
                {
                    crmList.get(0).setExeStatus("完成");
                }else if(producEntity.getExeStatus().equals("3"))
                {
                    crmList.get(0).setExeStatus("关闭");
                }
                if(producEntity.getOutsBilletFl().equals("1"))
                {
                    crmList.get(0).setOutsBilletFl("是");
                }else if(producEntity.getOutsBilletFl().equals("0"))
                {
                    crmList.get(0).setOutsBilletFl("否");
                }
                crmList.get(0).setGradecode(producEntity.getGradecode());
                crmList.get(0).setGradename(producEntity.getGradename());
                crmList.get(0).setDiameterGp(producEntity.getDiameterGp());
                crmList.get(0).setPlanTimeB(producEntity.getPlanTimeB());
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setCreateTime(strTime);
                crmList.get(0).setCreateName(userName);
            }else if(processCode.equals("F"))
            {
                crmList.get(0).setPlanTimeRcl(strTime);
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setCreateTime(strTime);
                crmList.get(0).setCreateName(userName);
            }else if(processCode.equals("G"))
            {
                crmList.get(0).setPlanTimeJgx(strTime);
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setCreateTime(strTime);
                crmList.get(0).setCreateName(userName);
            }
            insertCrmSql = EntityTool.fillSqlByEntity(insertCrmSql,crmList.get(0));
            ps = con.prepareStatement(insertCrmSql);
            ps.executeUpdate();
            ps.close();
        }

        return  "";
    }

    /***
     * 撤销下发新增CRM
     * @return
     */
    public static String insertCrmDcancel(Connection con,ProducEntity producEntity,String processCode,String userName,String userTime) throws SQLException {
        String insertCrmSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.insertCrmD");
        String sql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectCrmOrder");
        sql  = EntityTool.fillSqlByEntity(sql,producEntity);
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<CrmOrderProduceEntity> crmList = EntityTool.getEntitysByRs(rs,CrmOrderProduceEntity.class);
        rs.close();
        ps.close();
        if(crmList.size()>0)
        {
            if(processCode.equals("D"))
            {
                crmList.get(0).setDPlineCode("");
                crmList.get(0).setDPlineName("");
                crmList.get(0).setPassCode("");
                crmList.get(0).setPassName("");

                crmList.get(0).setExeStatus("");
                crmList.get(0).setGradecode("");
                crmList.get(0).setGradename("");
                crmList.get(0).setDiameterGp("");
                crmList.get(0).setPlanTimeB("");
                crmList.get(0).setOutsBilletFl("");
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setCreateTime(userTime);
                crmList.get(0).setCreateName(userName);
            }else if(processCode.equals("F"))
            {
                crmList.get(0).setPlanTimeRcl("");
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setCreateTime(userTime);
                crmList.get(0).setCreateName(userName);
            }else if(processCode.equals("G"))
            {
                crmList.get(0).setPlanTimeJgx("");
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setCreateTime(userTime);
                crmList.get(0).setCreateName(userName);
            }
            insertCrmSql = EntityTool.fillSqlByEntity(insertCrmSql,crmList.get(0));
            ps = con.prepareStatement(insertCrmSql);
            ps.executeUpdate();
            ps.close();
        }

        return  "";
    }

    /***
     * 镦拔扩排产下发
     * @param con
     * @param producEntity
     * @param strTime
     * @return
     */
    public  static String insertCrmE(Connection con, PlnOrderDbkSEntity producEntity, String userName,String strTime) throws SQLException {
        String insertCrmSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.insertCrmD");
        String sql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectCrmOrder");
        sql  = EntityTool.fillSqlByEntity(sql,producEntity);
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<CrmOrderProduceEntity> crmList = EntityTool.getEntitysByRs(rs,CrmOrderProduceEntity.class);
        rs.close();
        ps.close();
        if(crmList.size()>0) {
            crmList.get(0).setPlanTimeDbk(strTime);
            crmList.get(0).setDataStatus("1");
            crmList.get(0).setCreateName(userName);
            crmList.get(0).setCreateTime(strTime);
            insertCrmSql = EntityTool.fillSqlByEntity(insertCrmSql,crmList.get(0));
            ps = con.prepareStatement(insertCrmSql);
            ps.executeUpdate();
            ps.close();
        }

        return "";
    }
    /***
     * 镦拔扩取消排产下发
     * @param con
     * @param producEntity
     * @param
     * @return
     */
    public  static String insertCrmECancel(Connection con, PlnOrderDbkSEntity producEntity,String userName,String userTime) throws SQLException {
        String insertCrmSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.insertCrmD");
        String sql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectCrmOrder");
        sql  = EntityTool.fillSqlByEntity(sql,producEntity);
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<CrmOrderProduceEntity> crmList = EntityTool.getEntitysByRs(rs,CrmOrderProduceEntity.class);
        rs.close();
        ps.close();
        if(crmList.size()>0) {
            crmList.get(0).setPlanTimeDbk("");
            crmList.get(0).setDataStatus("1");
            crmList.get(0).setCreateName(userName);
            crmList.get(0).setCreateTime(userTime);

            insertCrmSql = EntityTool.fillSqlByEntity(insertCrmSql,crmList.get(0));
            ps = con.prepareStatement(insertCrmSql);
            ps.executeUpdate();
            ps.close();
        }

        return "";
    }

    /***
     * 状态调整
     * @param con
     * @param
     * @param
     * @param
     * @return
     */
    public static String insertCrmSetStaus(Connection con,String plineCode,int groSeq,String processCode) throws SQLException {
        String insertCrmSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.insertCrmStatus");

        String proOrderSql = "";//查询改变状态订单
        ResultSet rs = null;
        PreparedStatement ps = null;
        String exeStatus = "";
        if(processCode.equals("D"))
        {
            proOrderSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectZgs");
        }else if(processCode.equals("E"))
        {
            proOrderSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectDbks");
        }else if(processCode.equals("F"))
        {
            proOrderSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectRcls");
        }else if(processCode.equals("G"))
        {
            proOrderSql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectJgxs");
        }
        ps = con.prepareStatement(proOrderSql);
        ps.setObject(1,plineCode);
        ps.setObject(2,groSeq);
        rs = ps.executeQuery();
        List<PlnOrderZgSEntity> proEntitys = EntityTool.getEntitysByRs(rs,PlnOrderZgSEntity.class);
        rs.close();
        ps.close();

        for(PlnOrderZgSEntity proEntity : proEntitys) {

            if(Integer.parseInt(proEntity.getGxProSeq())<groSeq)
            {
               exeStatus = "已生产";
            }else if(Integer.parseInt(proEntity.getGxProSeq()) == groSeq && Integer.parseInt(proEntity.getGxProSeq()) != 1)
            {
                exeStatus = "生产中";
            }else if(Integer.parseInt(proEntity.getGxProSeq()) == groSeq&& proEntity.getExeStatus().equals("1"))
            {
                exeStatus = "计划";
            }else
            {
                exeStatus = "生产中";
            }



            String sql = CoreXmlSqlParsersFactory.getSql("ProducCrm.selectCrmOrder");
            sql = EntityTool.fillSqlByEntity(sql, proEntity);
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            List<CrmOrderProduceEntity> crmList = EntityTool.getEntitysByRs(rs, CrmOrderProduceEntity.class);
            rs.close();
            ps.close();



            if (crmList.size() > 0) {
                crmList.get(0).setDataStatus("1");
                crmList.get(0).setGxProSeq(groSeq);
                if (processCode.equals("D")) {
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS#", exeStatus);
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_DBK#", "");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_RCL#", "");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_JGX#", "");
                    insertCrmSql = EntityTool.fillSqlByEntity(insertCrmSql, crmList.get(0));
                } else if (processCode.equals("E")) {
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_DBK#", exeStatus);
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS#", "''");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_RCL#", "''");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_JGX#", "''");
                } else if (processCode.equals("F")) {
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_RCL#", exeStatus);
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS#", "''");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_DBK#", "''");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_JGX#", "''");
                } else if (processCode.equals("G")) {

                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_RCL#", exeStatus);
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS#", "''");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_DBK#", "''");
                    insertCrmSql = insertCrmSql.replace("#EXE_STATUS_JGX#", "''");
                }
                ps = con.prepareStatement(insertCrmSql);
                ps.executeUpdate();
                ps.close();
            }
        }


        return "";
    }
}
