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

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.CrmChangApply;
import com.steering.pss.sale.order.model.CrmSlmOrder;
import com.steering.pss.sale.order.orderchange.entity.ChgeSlmApplyMEntity;

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

public class CrmChangApplyService {

    public CrmChangApply convertSamToCca(ChgeSlmApplyMEntity csm) {
        if (csm == null) {
            return null;
        }
        CrmChangApply crmChangApply = new CrmChangApply();
        crmChangApply.setOrderChgeNo(csm.getOrderChgeNo());
        crmChangApply.setOrdPk(csm.getOrdPk());
        crmChangApply.setOrdLnPk(csm.getOrdLnPk());
        crmChangApply.setOrdLnDlyPk(csm.getOrdLnDlyPk());
        crmChangApply.setOrderNo(csm.getOrderNo());
        crmChangApply.setOrderSeq(csm.getOrderSeq());
        crmChangApply.setDeliveryNo(csm.getDeliveryNo());
        crmChangApply.setProduccode("");//品名代码
        crmChangApply.setProducname("");//品名描述
        crmChangApply.setStdCode("");//标准代码
        crmChangApply.setStdName("");//标准名称
        crmChangApply.setSteelcode("");//钢级(牌号)代码
        crmChangApply.setSteelname("");//钢级(牌号)名称
        crmChangApply.setStdStyle("");//标准类别代码
        crmChangApply.setStdStyleDesc("");//标准类别描述
        crmChangApply.setModelCode("");//扣型代码
        crmChangApply.setModelDesc("");//扣型描述
        crmChangApply.setDimater("0");//外径(mm)
        crmChangApply.setHeight("0");//壁厚(mm)
        crmChangApply.setChgeType(csm.getChgeType());
        crmChangApply.setChgeVal(csm.getChgeVal());
        crmChangApply.setChgeStatus(csm.getChgeStatus());//变更单状态(L封锁、W待审、S通过、F失败)
        crmChangApply.setChger(csm.getChger());
        crmChangApply.setChgeDate(csm.getChgeDate());
        crmChangApply.setAcceptor(csm.getUpdateName());
        crmChangApply.setDealEdate(csm.getDealEdate());
        crmChangApply.setDataStatus("");//数据类型(0 新增 1修改 2删除)
        crmChangApply.setIsFlag("");//CRM是否同步(0 未同步 1已同步)
        crmChangApply.setSynchroTime("");//CRM同步时间
        return crmChangApply;
    }

    ////新增变更数据
    public void insertCrmChangApply(
            Connection con, ChgeSlmApplyMEntity csm, Boolean isPass) throws SQLException {
        CrmChangApply cca = this.convertSamToCca(csm);
        if (isPass) {
            cca.setChgeStatus("S");
            cca.setDataStatus("0");
        } else {
            cca.setChgeStatus("F");
            cca.setDataStatus("0");
        }
        //查询合同信息
        CrmSlmOrderService crmSlmOrderService = new CrmSlmOrderService();
        List<CrmSlmOrder> orderInfo = crmSlmOrderService.getOrderInfo(con, cca.getOrdPk(), cca.getOrdLnPk());
        if (orderInfo.size() > 0) {
            CrmSlmOrder crmSlmOrder = orderInfo.get(0);
            if(csm.getChgeType().contains("交货行")){
                cca.setOrdLnDlyPk(crmSlmOrder.getOrdLnDlyPk());
                cca.setDeliveryNo(crmSlmOrder.getDeliveryNo());
            }
            cca.setProduccode(crmSlmOrder.getProduccode());//品名代码
            cca.setProducname(crmSlmOrder.getProducname());//品名描述
            cca.setStdCode(crmSlmOrder.getStdCode());//标准代码
            cca.setStdName(crmSlmOrder.getStdName());//标准名称
            cca.setSteelcode(crmSlmOrder.getSteelcode());//钢级(牌号)代码
            cca.setSteelname(crmSlmOrder.getSteelname());//钢级(牌号)名称
            cca.setStdStyle(crmSlmOrder.getStdStyle());//标准类别代码
            cca.setStdStyleDesc(crmSlmOrder.getStdStyleDesc());//标准类别描述
            cca.setModelCode(crmSlmOrder.getModelCode());//扣型代码
            cca.setModelDesc(crmSlmOrder.getModelDesc());//扣型描述
            cca.setDimater(crmSlmOrder.getDimater());//外径(mm)
            cca.setHeight(crmSlmOrder.getHeight());//壁厚(mm)
        }

        //新增crm变更单
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmChangApply.insertCrmChangApply");
        sql = EntityTool.fillSqlByEntity(sql, cca);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }
}
