package com.steering.pss.sale.order.orderchange;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.judge.model.QcmGpJudgeApplyEntity;
import com.steering.pss.plnsaleord.stove.entity.QcmZgJugdeApplyEntity;
import com.steering.pss.sale.order.model.CrmOrderProduce;
import com.steering.pss.ydm.entity.ProcessOrderMEntity;

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

public class CrmOrderProduceService {

    /**
     * String转成double的值， 若无法转换，默认返回0.00
     */
    public static double strToDouble(String string) {
        return strToDouble(string, 0.00);
    }

    public static double strToDouble(String string, double defaultValue) {
        if ((string == null) || (string.equalsIgnoreCase(""))) {
            return defaultValue;
        }
        double ret;
        try {
            ret = Double.parseDouble(string);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return defaultValue;
        }

        return ret;
    }

    public CrmOrderProduce convertProcessOrderToCop(ProcessOrderMEntity processOrderMEntity, String userName) {
        if (processOrderMEntity == null) {
            return null;
        }
        CrmOrderProduce crmOrderProduce = new CrmOrderProduce();
        crmOrderProduce.setOrdPk(processOrderMEntity.getOrdPk());
        crmOrderProduce.setOrdLnPk(processOrderMEntity.getOrdLnPk());
        crmOrderProduce.setOrdLnDlyPk(processOrderMEntity.getOrdLnDlyPk());
        crmOrderProduce.setCreateName(userName);
        crmOrderProduce.setAppNum(processOrderMEntity.getAppNum());
        crmOrderProduce.setAppUnit(processOrderMEntity.getAppUnit());
        crmOrderProduce.setPssNum(processOrderMEntity.getPssNum());
        crmOrderProduce.setPssWgt(processOrderMEntity.getPssWgt());
        crmOrderProduce.setWgtEstmt(processOrderMEntity.getWgtEstmt());
//       crmOrderProduce.setCreateTime("");
//        crmOrderProduce.setDataStatus("");
//        crmOrderProduce.setIsFlag("0");
//        crmOrderProduce.setSynchroTime("");
//        crmOrderProduce.setLgNum("");
//        crmOrderProduce.setLgWgt("");
//        crmOrderProduce.setLgTime("");
//        crmOrderProduce.setDPlineCode(qcmGp.getPlineCode());
//        crmOrderProduce.setDPlineName(qcmGp.getPlineName());
//        crmOrderProduce.setPassCode("");
//        crmOrderProduce.setPassName("");
//        crmOrderProduce.setProductLen("");
//        crmOrderProduce.setPlanTimeB("");
//        crmOrderProduce.setExeStatus("");
//        crmOrderProduce.setGradecode(qcmGp.getGradecode());
//        crmOrderProduce.setGradename(qcmGp.getGradename());
//        crmOrderProduce.setDiameterGp("");
//        crmOrderProduce.setPlanTimeJgx("");
//        crmOrderProduce.setPlanTimeDbk("");
//        crmOrderProduce.setPlanTimeRcl("");
        return crmOrderProduce;
    }

    public CrmOrderProduce convertJugdeZGToCop(CrmOrderProduce crmOrderProduce, QcmZgJugdeApplyEntity qcmGp, String processCode, boolean isJugde) {
        Double actCount = strToDouble(qcmGp.getActCount());
        Double actWeight = strToDouble(qcmGp.getActWeight());
        if (isJugde) {
            if (processCode.equals("D")) {//轧管工序判定
                crmOrderProduce.setZgNum(Double.toString(strToDouble(crmOrderProduce.getZgNum()) + actCount));
                crmOrderProduce.setZgWgt(Double.toString(strToDouble(crmOrderProduce.getZgWgt()) + actWeight));
            } else if (processCode.equals("E")) {//镦拔扩工序判定
                crmOrderProduce.setDbkNum(Double.toString(strToDouble(crmOrderProduce.getDbkNum()) + actCount));
                crmOrderProduce.setDbkWgt(Double.toString(strToDouble(crmOrderProduce.getDbkWgt()) + actWeight));
            } else if (processCode.equals("F")) {//热处理工序判定
                crmOrderProduce.setRclNum(Double.toString(strToDouble(crmOrderProduce.getRclNum()) + actCount));
                crmOrderProduce.setRclWgt(Double.toString(strToDouble(crmOrderProduce.getRclWgt()) + actWeight));
            } else if (processCode.equals("G")) {//加工判定
                crmOrderProduce.setJgxNum(Double.toString(strToDouble(crmOrderProduce.getJgxNum()) + actCount));
                crmOrderProduce.setJgxWgt(Double.toString(strToDouble(crmOrderProduce.getJgxWgt()) + actWeight));
            }
        } else {
            if (processCode.equals("D")) {//轧管工序判定
                crmOrderProduce.setZgNum(Double.toString(strToDouble(crmOrderProduce.getZgNum()) - actCount));
                crmOrderProduce.setZgWgt(Double.toString(strToDouble(crmOrderProduce.getZgWgt()) - actWeight));
            } else if (processCode.equals("E")) {//镦拔扩工序判定
                crmOrderProduce.setDbkNum(Double.toString(strToDouble(crmOrderProduce.getDbkNum()) - actCount));
                crmOrderProduce.setDbkWgt(Double.toString(strToDouble(crmOrderProduce.getDbkWgt()) - actWeight));
            } else if (processCode.equals("F")) {//热处理工序判定
                crmOrderProduce.setRclNum(Double.toString(strToDouble(crmOrderProduce.getRclNum()) - actCount));
                crmOrderProduce.setRclWgt(Double.toString(strToDouble(crmOrderProduce.getRclWgt()) - actWeight));
            } else if (processCode.equals("G")) {//加工判定
                crmOrderProduce.setJgxNum(Double.toString(strToDouble(crmOrderProduce.getJgxNum()) - actCount));
                crmOrderProduce.setJgxWgt(Double.toString(strToDouble(crmOrderProduce.getJgxWgt()) - actWeight));
            }
        }
        crmOrderProduce.setDataStatus("1");
        return crmOrderProduce;
    }

    public CrmOrderProduce convertJugdeLGToCop(CrmOrderProduce crmOrderProduce, QcmGpJudgeApplyEntity JudgeApplyEntity, boolean isJugde) {
        Double actCount = strToDouble(JudgeApplyEntity.getActCount());
        Double actWeight = strToDouble(JudgeApplyEntity.getActTheoryWeight());
        if (isJugde) {
            crmOrderProduce.setLgNum(Double.toString(strToDouble(crmOrderProduce.getLgNum()) + actCount));
            crmOrderProduce.setLgWgt(Double.toString(strToDouble(crmOrderProduce.getLgWgt()) + actWeight));
        } else {
            crmOrderProduce.setLgNum(Double.toString(strToDouble(crmOrderProduce.getLgNum()) - actCount));
            crmOrderProduce.setLgWgt(Double.toString(strToDouble(crmOrderProduce.getLgWgt()) - actWeight));
        }

        crmOrderProduce.setDataStatus("1");
        return crmOrderProduce;
    }

    public CrmOrderProduce convertJugdeCPToCop(CrmOrderProduce crmOrderProduce, com.steering.pss.judge.model.QcmZgJugdeApplyEntity crmJudgeApplyEntity, boolean isJugde) {
        Double actCount = strToDouble(crmJudgeApplyEntity.getActCount());
        Double actWeight = strToDouble(crmJudgeApplyEntity.getActWeight());
        if (isJugde) {
            crmOrderProduce.setProductNum(Double.toString(strToDouble(crmOrderProduce.getProductNum()) + actCount));
            crmOrderProduce.setProductWgt(Double.toString(strToDouble(crmOrderProduce.getProductWgt()) + actWeight));
        } else {
            crmOrderProduce.setProductNum(Double.toString(strToDouble(crmOrderProduce.getProductNum()) - actCount));
            crmOrderProduce.setProductWgt(Double.toString(strToDouble(crmOrderProduce.getProductWgt()) - actWeight));
        }

        crmOrderProduce.setDataStatus("1");
        return crmOrderProduce;
    }

    //根据交货行查询
    public List<ProcessOrderMEntity> getOrderInfo(
            Connection con, String ordLnDlyPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmOrderProduce.queryProcessOrderM");
        if (!ordLnDlyPk.equals("")) {
            sql += "  AND T.ORD_LN_DLY_PK = '" + ordLnDlyPk + "'";
        }
        List<ProcessOrderMEntity> list = new ArrayList<ProcessOrderMEntity>();
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        list = EntityTool
                .getEntitysByRs(rs, ProcessOrderMEntity.class);

        rs.close();
        ps.close();
        return list;
    }

    //获取最新crm表数据
    public CrmOrderProduce getCrmOrderProduce(Connection con, String ordLnDlyPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmOrderProduce.queryCrmOrderProduce");
        List<CrmOrderProduce> list = new ArrayList<CrmOrderProduce>();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        list = EntityTool
                .getEntitysByRs(rs, CrmOrderProduce.class);
        rs.close();
        ps.close();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //合同提报
    public void insertCrmOrderProduce(Connection con, String userName, String ordLnDlyPk, String dataStatus) throws SQLException {
        List<ProcessOrderMEntity> list = this.getOrderInfo(con, ordLnDlyPk);
        if (list.size() != 0) {
            CrmOrderProduce crmOrderProduce = this.convertProcessOrderToCop(list.get(0), userName);
            crmOrderProduce.setDataStatus(dataStatus);
            //新增
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("CrmOrderProduce.insertCrmOrderProduce");
            sql2 = EntityTool.fillSqlByEntity(sql2, crmOrderProduce);
            PreparedStatement ps = con.prepareStatement(sql2);
            ps.executeUpdate();
            ps.close();
        }
    }

    //扎管，镦拔扩，热处理，加工工序判定
    //isJugde  true 判定    false 取消判定
    public void insertJudgeZGCrmOrderProduce(Connection con, QcmZgJugdeApplyEntity qcmGp, String processCode, boolean isJugde) throws SQLException {
        //获取最新crm表数据
        CrmOrderProduce crmOrderProduce = getCrmOrderProduce(con, qcmGp.getOrdLnDlyPk());
        if (crmOrderProduce == null)
            return;
        crmOrderProduce = convertJugdeZGToCop(crmOrderProduce, qcmGp, processCode, isJugde);
        //新增
        String sql2 = CoreXmlSqlParsersFactory
                .getSql("CrmOrderProduce.insertCrmOrderProduce");
        sql2 = EntityTool.fillSqlByEntity(sql2, crmOrderProduce);
        PreparedStatement ps = con.prepareStatement(sql2);
        ps.executeUpdate();
        ps.close();
    }

    //炼钢
    public void insertJudgeLGCrmOrderProduce(Connection con, QcmGpJudgeApplyEntity crmJudgeApplyEntity, boolean isJugde) throws SQLException {
        //获取最新crm表数据
        CrmOrderProduce crmOrderProduce = getCrmOrderProduce(con, crmJudgeApplyEntity.getOrdLnDlyPk());
        if (crmOrderProduce == null)
            return;
        crmOrderProduce = convertJugdeLGToCop(crmOrderProduce, crmJudgeApplyEntity, isJugde);
        //新增
        String sql2 = CoreXmlSqlParsersFactory
                .getSql("CrmOrderProduce.insertCrmOrderProduce");
        sql2 = EntityTool.fillSqlByEntity(sql2, crmOrderProduce);
        PreparedStatement ps = con.prepareStatement(sql2);
        ps.executeUpdate();
        ps.close();
    }

    //成品
    public void insertJudgeCPCrmOrderProduce(Connection con, com.steering.pss.judge.model.QcmZgJugdeApplyEntity crmJudgeApplyEntity, boolean isJugde) throws SQLException {
        //获取最新crm表数据
        CrmOrderProduce crmOrderProduce = getCrmOrderProduce(con, crmJudgeApplyEntity.getOrdLnDlyPk());
        if (crmOrderProduce == null)
            return;
        crmOrderProduce = convertJugdeCPToCop(crmOrderProduce, crmJudgeApplyEntity, isJugde);
        //新增
        String sql2 = CoreXmlSqlParsersFactory
                .getSql("CrmOrderProduce.insertCrmOrderProduce");
        sql2 = EntityTool.fillSqlByEntity(sql2, crmOrderProduce);
        PreparedStatement ps = con.prepareStatement(sql2);
        ps.executeUpdate();
        ps.close();
    }
}
