/*
NAME: 
DESCRIPTION: ;
PARAMETER:
    [
		{
			name : 'step_filter',
			title : '工作step',
			type : 'LineEdit',
			property : {tool_tip : '未设定,则手动选择',},
		},
		{
			name : 'units',
			title : '单位',
			type : 'RadioBox',
			property : {
				size_policy:'Expanding,Fixed',
				item_list:[
					{name:'inch',text:'Inch'},
					{name:'mm',text:'MM'},
				],
				tool_tip:'未设定，默认为Inch'
			},
			pack : {row:1,column:1},
		}
	]
	
 VERSION_HISTORY:
	V1.00 2020-01-22 Shyer Wang
	    1.新版本
		
 HELP:
 	<html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		<p> 名称 </p>
		<br>
		<font size="3" color="#003DB2"><p>参数配置</p></font>
		<p> 无 </p>
		<br>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		<p> 无 </p>
		<br>
	</body></html>	
*/
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

var $ = require('topcam.scriptfunc').argv();
var _ = require('lodash');

var database = require("topsin.database");
database.addConnection($.conf.database_conf, "DFM");
var QDfm = database.query("DFM");
if ($.conf.product_type == "aimdfm") {
	QDfm.updateRow({
		table: "pdm_aimdfm_task",
		data: {
			current_process_title: $.process_title
		},
		where: { id: $.task_id }
	});
}

var JOB = $.job_name;
var GEN = $.gen;
var PAR = {};
if ($.hasOwnProperty('script_parameter')){
	PAR = JSON.parse($.script_parameter);
}
if (!$.hasOwnProperty(PAR["save_job"])) {
	PAR["save_job"] = 'No';
}

var step_filter = _.toString(PAR["step_filter"]) == '' ? 'net|array|edit' : PAR["step_filter"];
var Status = 'ok';
var resultData = [];
var Units = _.toString(PAR["units"]) == '' ? 'inch' : PAR["units"] ;

try {
    if(_.isEmpty(JOB)) throw "没有传入料号名!";
    if(!GEN.isJobExists({job: JOB})) throw "料号:" + JOB + "在Genensis中不存在!";
    if(!GEN.isJobOpen({job: JOB})) GEN.openJob({job: JOB});
	GEN.checkInout({ "job": JOB, "mode": 'out' });
	var steps = GEN.getStepList({ job: JOB });
	var work_steps = [];
	//将匹配到的step放入work_steps
	steps.filter(function (value, index, array) {
		var pattern = new RegExp(step_filter);
		if (pattern.test(value)) {
			work_steps.push(value)
		}
	});
	//循环work_steps
		//检查是否存在 T2_ori_analysis 的checklist，存在则删除重新创建
		//T2_ori_analysis的checklist中存在如下分析Drill Checks：
			/**
			  	T2_ori_analysis_drill
				Board - Drill Checks: T2_ori_HDI_dill_analysis
				Signal Layer Checks: T2_ori_analysis_signal
				Solder Mask Checks: T2_ori_analysis_sm
			*/
		//运行T2_ori_analysis的checklist

	//保存料号

	var chklist = 't2_ori_analysis';
	var run_nactnum = [1,2,4,7];
	work_steps.filter(function (step) {
		GEN.openStep({ job: JOB, step: step });
		GEN.clearLayers();
		GEN.affectedLayer({ mode: 'all', affected: 'no' });
		GEN.COM("sel_options,clear_mode=clear_after,display_mode=all_layers,area_inout=inside,area_select=select,select_mode=standard,area_touching_mode=exclude");
		GEN.units({ type: Units });
		GEN.zoomHome();
		//
		// var nactNum = 1;
		if (!GEN.isChklistExists({ "job": JOB, "step": step, "chklist": chklist })) GEN.COM("chklist_from_lib,chklist=" + chklist);
		GEN.chklistShow({ chklist: chklist});
		run_nactnum.forEach(function (nact_num) { 
			GEN.chklistRun({ chklist: chklist, nact: nact_num });
		})

		GEN.clearLayers();
		GEN.affectedLayer({ mode: 'all', affected: 'no' });
	});

	GEN.saveJob({ "job": JOB });
	GEN.checkInout({"job": JOB,"mode":'in'});
	// GEN.closeJob({"job": JOB});
	QDfm.updateRow({
		table: "pdm_aimdfm_task",
		data: {
			progress: 33.33
		},
		where: { id: $.task_id }
	});
	if (GEN.hasError()) {
		Status = 'error';
		resultData.push({ type: "error", title: "GEN错误!", detail: [{ desc: _.join(GEN.STATUS, "\n") }] });
		return {
			status: Status,
			result_data: resultData
		};
	} else {
		resultData.push({ type: "info", title: "操作完成, 请注意检查!" });
		return {
			status: Status,
			result_data: resultData
		};
	}

} catch (e){
    Status = 'error';
    resultData.push({type: "error", title: "脚本执行出错!", detail: [{desc: _.toString(e)}]});
    return {status: Status, result_data: resultData};
}
