package com.steering.pss.sale.order;

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

import com.steering.pss.sale.order.orderchange.CrmBaseMaterialService;
import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.model.SlmBaseMaterialAPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialBPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialCPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialDPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialEPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialFPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialIPlEntity;
import com.steering.pss.sale.order.model.SlmBaseMaterialJPlEntity;

/**
 * 基础物料投入与产出类
 * 
 * @author Administrator
 */
public class CoreBaseMaterialPl extends DBCommon {
	/**
	 * TAB页面权限
	 * 
	 * @param deptCode
	 * @return
	 */
	public CoreReturnObject GetPagePermission(String[] validDataPurviewIds) {
		String sql = "SELECT DISTINCT T.PROCESS_CODE FROM COM_BASE_PLINE T WHERE 1=1 ";
		sql += CoreConverter.toSqlConditionString("T.DEPARTMENT_CODE",
				validDataPurviewIds);
		return this.getData(sql);
	}

	/**
	 * 查询产线权限
	 * 
	 * @param validDataPurviewIds
	 * @return
	 * @throws SQLException
	 */
	public CoreReturnObject getPlinePermission(String[] validDataPurviewIds)
			throws SQLException {
		String sql = "SELECT DISTINCT T.PLINE_CODE FROM COM_BASE_PLINE T   WHERE 1=1 ";
		sql += CoreConverter.toSqlConditionString("T.DEPARTMENT_CODE",
				validDataPurviewIds);
		return this.getData(sql);
	}

	/**
	 * 查询基础物料
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterial() {
		return this.getDataBySqlID("CoreBaseMaterialPl.getBaseMaterial");
	}
	
	/**
	 * 查询基础物料
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialByPk(String materialNo) {
		SqlBuilder b = new SqlBuilder("CoreBaseMaterialPl.getBaseMaterialByPk");
		b.Fill(new String[]{materialNo});
		return this.getData(b.toString());
	}

	/**
	 * 查询电炉
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialAPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialAPl");
		sql += CoreConverter.toSqlConditionString("A.PLINE_CODE", plines);
		sql += "    ORDER BY A.PLINE_CODE, B.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询铸造
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialBPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialBPl");
		sql += CoreConverter.toSqlConditionString("A.PLINE_CODE", plines);
		sql += "    ORDER BY A.PLINE_CODE, B.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询锻造
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialCPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialCPl");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plines);
		sql += "    ORDER BY T.PLINE_CODE, A.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询轧制
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialDPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialDPl");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plines);
		sql += "    ORDER BY T.PLINE_CODE, A.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询镦拔扩
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialEPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialEPl");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plines);
		sql += "    ORDER BY T.PLINE_CODE, A.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询热处理
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseMaterialFPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialFPl");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plines);
		sql += "    ORDER BY T.PLINE_CODE, A.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询加工
	 * 
	 * @return
	 */
	public CoreReturnObject getBaseMaterialIPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialIPl");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plines);
		sql += "     ORDER BY T.PLINE_CODE, T.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	/**
	 * 查询接箍
	 * 
	 * @return
	 */
	public CoreReturnObject getBaseMaterialHPl(String[] plines) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.getBaseMaterialHPl");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plines);
		sql += "   ORDER BY T.PLINE_CODE, A.IN_MATERIAL_DESC";
		return this.getData(sql);
	}

	public CoreReturnObject updateBaseMaterial(
			ArrayList<ArrayList<String>> listJSONs) {
		ArrayList<String> listAJSONs = listJSONs.get(0);
		ArrayList<String> listBJSONs = listJSONs.get(1);
		ArrayList<String> listCJSONs = listJSONs.get(2);
		ArrayList<String> listDJSONs = listJSONs.get(3);
		ArrayList<String> listEJSONs = listJSONs.get(4);
		ArrayList<String> listFJSONs = listJSONs.get(5);
		ArrayList<String> listJJSONs = listJSONs.get(6);
		ArrayList<String> listIJSONs = listJSONs.get(7);
		ArrayList<String> listBaseJSONs = listJSONs.get(8);
		ArrayList<String> sqllist = new ArrayList<String>();
		CrmBaseMaterialService crmBaseMaterialService = new CrmBaseMaterialService();
		for (String strJson : listAJSONs) {
			JSONObject obj = JSONObject.fromObject(strJson);
			SlmBaseMaterialAPlEntity materialPl = (SlmBaseMaterialAPlEntity) JSONObject
					.toBean(obj, SlmBaseMaterialAPlEntity.class);
			if (materialPl.getDisableFl().equals("True")) {
				materialPl.setDisableFl("1");
			} else {
				materialPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialAPl");
			sql = EntityTool.fillSqlByEntity(sql, materialPl);
			sqllist.add(sql);
		}

		for (String strJSON : listBJSONs) {
			SlmBaseMaterialBPlEntity materialBPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialBPlEntity.class);
			if (materialBPl.getDisableFl().equals("True")) {
				materialBPl.setDisableFl("1");
			} else {
				materialBPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialBPl");
			sql = EntityTool.fillSqlByEntity(sql, materialBPl);
			sqllist.add(sql);
		}

		for (String strJSON : listCJSONs) {
			SlmBaseMaterialCPlEntity materialCPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialCPlEntity.class);
			if (materialCPl.getDisableFl().equals("True")) {
				materialCPl.setDisableFl("1");
			} else {
				materialCPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialCPl");
			sql = EntityTool.fillSqlByEntity(sql, materialCPl);
			sqllist.add(sql);
		}

		for (String strJSON : listDJSONs) {
			SlmBaseMaterialDPlEntity materialDPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialDPlEntity.class);
			if (materialDPl.getDisableFl().equals("True")) {
				materialDPl.setDisableFl("1");
			} else {
				materialDPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialDPl");
			sql = EntityTool.fillSqlByEntity(sql, materialDPl);
			sqllist.add(sql);
		}

		for (String strJSON : listEJSONs) {
			SlmBaseMaterialEPlEntity materialEPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialEPlEntity.class);
			if (materialEPl.getDisableFl().equals("True")) {
				materialEPl.setDisableFl("1");
			} else {
				materialEPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialEPl");
			sql = EntityTool.fillSqlByEntity(sql, materialEPl);
			sqllist.add(sql);
		}

		for (String strJSON : listFJSONs) {
			SlmBaseMaterialFPlEntity materialFPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialFPlEntity.class);
			if (materialFPl.getDisableFl().equals("True")) {
				materialFPl.setDisableFl("1");
			} else {
				materialFPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialFPl");
			sql = EntityTool.fillSqlByEntity(sql, materialFPl);
			sqllist.add(sql);
		}

		for (String strJSON : listJJSONs) {
			SlmBaseMaterialJPlEntity materialJPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialJPlEntity.class);
			if (materialJPl.getDisableFl().equals("True")) {
				materialJPl.setDisableFl("1");
			} else {
				materialJPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialJPl");
			sql = EntityTool.fillSqlByEntity(sql, materialJPl);
			sqllist.add(sql);
		}

		for (String strJSON : listIJSONs) {
			SlmBaseMaterialIPlEntity materialIPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialIPlEntity.class);
			if (materialIPl.getDisableFl().equals("True")) {
				materialIPl.setDisableFl("1");
			} else {
				materialIPl.setDisableFl("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterialIPl");
			sql = EntityTool.fillSqlByEntity(sql, materialIPl);
			sqllist.add(sql);
		}
		
		for(String strJSON : listBaseJSONs){
			SlmBaseMaterialEntity material = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialEntity.class);
			if(material.getAutoCraft().equals("True")){
				material.setAutoCraft("1");
			}else{
				material.setAutoCraft("0");
			}
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.updateBaseMaterial");
			sql = EntityTool.fillSqlByEntity(sql, material);
			sqllist.add(sql);

			//crm迁移
			String s = crmBaseMaterialService.updateBaseMaterial(material,"1");
			sqllist.add(s);
		}

		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 查询轧管优先级
	 * 
	 * @param listMaterialDJSON
	 * @return
	 */
	public CoreReturnObject queryMaterialDLvCnt(String materialJSON,
			ArrayList<String> materialJOSNList) {
		SlmBaseMaterialDPlEntity materialDPlEntity = EntityTool
				.getEntityByJSONStr(materialJSON,
						SlmBaseMaterialDPlEntity.class);
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreBaseMaterialPl.queryMaterialDLvCnt");
		sql = EntityTool.fillSqlByEntity(sql, materialDPlEntity);
		for (String tmpJSON : materialJOSNList) {
			SlmBaseMaterialDPlEntity tmpMaterialDPlEntity = EntityTool
					.getEntityByJSONStr(tmpJSON, SlmBaseMaterialDPlEntity.class);
			sql += "AND (T.IN_MATERIAL_NO <> '"
					+ tmpMaterialDPlEntity.getInMaterialNo() + "'";
			sql += " OR T.OUT_MATERIAL_NO <> '"
					+ tmpMaterialDPlEntity.getOutMaterialNo() + "'";
			sql += " OR T.PLINE_CODE <> '"
					+ tmpMaterialDPlEntity.getPlineCode() + "')";
		}
		return this.getData(sql);
	}
	
	public CoreReturnObject deleteBaseMaterial(
			ArrayList<ArrayList<String>> listJSONs) {
		ArrayList<String> listAJSONs = listJSONs.get(0);
		ArrayList<String> listBJSONs = listJSONs.get(1);
		ArrayList<String> listCJSONs = listJSONs.get(2);
		ArrayList<String> listDJSONs = listJSONs.get(3);
		ArrayList<String> listEJSONs = listJSONs.get(4);
		ArrayList<String> listFJSONs = listJSONs.get(5);
		ArrayList<String> listJJSONs = listJSONs.get(6);
		ArrayList<String> listIJSONs = listJSONs.get(7);
		ArrayList<String> listBaseJSONs = listJSONs.get(8);
		ArrayList<String> sqllist = new ArrayList<String>();
		CrmBaseMaterialService crmBaseMaterialService =new CrmBaseMaterialService();
		for (String strJson : listAJSONs) {
			JSONObject obj = JSONObject.fromObject(strJson);
			SlmBaseMaterialAPlEntity materialPl = (SlmBaseMaterialAPlEntity) JSONObject
					.toBean(obj, SlmBaseMaterialAPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialAPl");
			sql = EntityTool.fillSqlByEntity(sql, materialPl);
			sqllist.add(sql);
		}
		for (String strJSON : listBJSONs) {
			SlmBaseMaterialBPlEntity materialBPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialBPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialBPl");
			sql = EntityTool.fillSqlByEntity(sql, materialBPl);
			sqllist.add(sql);
		}
		for (String strJSON : listCJSONs) {
			SlmBaseMaterialCPlEntity materialCPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialCPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialCPl");
			sql = EntityTool.fillSqlByEntity(sql, materialCPl);
			sqllist.add(sql);
		}
		for (String strJSON : listDJSONs) {
			SlmBaseMaterialDPlEntity materialDPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialDPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialDPl");
			sql = EntityTool.fillSqlByEntity(sql, materialDPl);
			sqllist.add(sql);
		}
		for (String strJSON : listEJSONs) {
			SlmBaseMaterialEPlEntity materialEPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialEPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialEPl");
			sql = EntityTool.fillSqlByEntity(sql, materialEPl);
			sqllist.add(sql);
		}
		for (String strJSON : listFJSONs) {
			SlmBaseMaterialFPlEntity materialFPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialFPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialFPl");
			sql = EntityTool.fillSqlByEntity(sql, materialFPl);
			sqllist.add(sql);
		}
		for (String strJSON : listJJSONs) {
			SlmBaseMaterialJPlEntity materialJPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialJPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialJPl");
			sql = EntityTool.fillSqlByEntity(sql, materialJPl);
			sqllist.add(sql);
		}
		for (String strJSON : listIJSONs) {
			SlmBaseMaterialIPlEntity materialIPl = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialIPlEntity.class);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterialIPl");
			sql = EntityTool.fillSqlByEntity(sql, materialIPl);
			sqllist.add(sql);
		}
		for (String strJSON : listBaseJSONs) {
			SlmBaseMaterialEntity material = EntityTool
					.getEntityByJSONStr(strJSON, SlmBaseMaterialEntity.class);

			//crm迁移
			String s = crmBaseMaterialService.updateBaseMaterial(material,"2");
			sqllist.add(s);

			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreBaseMaterialPl.deleteBaseMaterial");
			sql = EntityTool.fillSqlByEntity(sql, material);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}
}
