import express from 'express'
const app = express();

// 使用中间件解析JSON格式的请求体
app.use(express.json());
import Service from './Service.js';
import {copy} from './utils.js';
import {handleBeizhu} from './editor.js';
app.post('/api/transform', (req, res) => {
   let data = copy(req.body);
   Service.setQufei([{ "children": [{"children":[{
   
   "key": "1",
   "利润（%）": "",
   "名称": "",
   "备注": "",
   "工程类型": "",
   "工程类别": "",
   "管理费（%）": ""
   
   }]}]}])
   Service.setQufeiEntry("1")
   Service.processQingdanmingxi([{
      "主材费": "",
      "人工费": "",
      "利润": "",
      "单位": data['dw'],
      "合价": "",
      "名称": data['mc'],
      "备注": "",
      "序号": "1",
      "数量": data['sl'],
      "暂估价": "",
      "机械费": "",
      "清单编码": data['bianma'],
      "管理费": "",
      "综合人工工日": "",
      "综合单价": "",
      "计算规则": "",
      "设备费": "",
      "辅材费": "",
      "项目特征": data['tz'],
      "操作": "",
      "材料费": ""
   }])
   let result = copy(data['result'])
   delete data.result
   if (data['bianma'].startsWith('031301017')){
      console.log('安装脚手架');
      let de04=result.filter(x=>x['清单编码'].startsWith('0304'));
      let json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"4-F1\"}, \"GCLMC\": {\"111\": \"第四册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de04)
      let de05=result.filter(x=>x['清单编码'].startsWith('0305'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"5-F1\"}, \"GCLMC\": {\"111\": \"第五册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de05)
      let de06=result.filter(x=>x['清单编码'].startsWith('0306'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"6-F1\"}, \"GCLMC\": {\"111\": \"第六册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de06)
      let de07=result.filter(x=>x['清单编码'].startsWith('0307'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"7-F1\"}, \"GCLMC\": {\"111\": \"第七册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de07)
      let de08=result.filter(x=>x['清单编码'].startsWith('0308'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"8-F1\"}, \"GCLMC\": {\"111\": \"第八册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de08)
      let de09=result.filter(x=>x['清单编码'].startsWith('0309'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"9-F1\"}, \"GCLMC\": {\"111\": \"第九册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de09)
      let de10=result.filter(x=>x['清单编码'].startsWith('0310'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"10-F1\"}, \"GCLMC\": {\"111\": \"第十册\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de10)
      let de111=result.filter(x=>x['清单编码'].startsWith('031201'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"11-F1\"}, \"GCLMC\": {\"111\": \"第十一册刷油工程(定额11-51～11-329)\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de111)
      let de112=result.filter(x=>x['清单编码'].startsWith('031202') || x['清单编码'].startsWith('031203') || x['清单编码'].startsWith('031204') || x['清单编码'].startsWith('031205') || x['清单编码'].startsWith('031206') || x['清单编码'].startsWith('031207'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"11-F1\"}, \"GCLMC\": {\"111\": \"第十一册防腐蚀工程(定额11-330～11-1689)\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de112)
      let de113=result.filter(x=>x['清单编码'].startsWith('031208'));
      json_ = "{\"reverse\": \"2076\", \"DEBH\": {\"111\": \"11-F1\"}, \"GCLMC\": {\"111\": \"第十一册绝热工程(定额11-1690～11-2327)\"}, \"DW\": {\"111\": \"项\"}, \"rgde\": [], \"jxde\": [], \"clde\": [], \"actual_zhuanye\": 30, \"bz_selected\": {\"BZBH\": {}}, \"bz_selected2\": {\"BZBH\": {}}}"
      Service.azfy_mock("1", json_, de113)
   }
   else{
	for (let i = 0; i < result.length; i++) {
       console.log(result[i][0])
       console.log(result[i][1])
       console.log((result[i][2]))
       let selected = data['fuzhu'][i][0]
	let selected2=[]
	  for(let j = 0; j < selected.length; j++){
	  selected2.push(selected[j].charCodeAt(0) - 64)
	  }
       let fk = data['fuzhu'][i][1]
       let zk = data['fuzhu'][i][2]
	let zk2 = []
	 for(let j = 0;j<zk.length;j++){
	 zk2.push({
	  'id': j+1,
	  'key': j+1,
	  '序号':j+1,
	  '数量':1,
	   '编号':zk[j][1],
	   '说明':zk[j][2]
	 })
	 }
       let r = Service.changguidinge(JSON.parse(result[i][2]), "1")
       if (selected2.length>0){
         let detail = handleBeizhu(fk,r[2],selected2,zk2)
         Service.updateBeizhu(r[2],detail,selected2.map(x=>x.toString().concat('*1')))
       }
       let current = Service.current()[0]
       current = current['_children'].filter(x=>x['key']==r[2])[0]
       current = current['dercj']
       let shajiang = false
       for(let j=1;j<current.length;j++){
            if(current[j][2].includes('灰浆搅拌机'))shajiang=true;
       }
	   
       if (shajiang){
           Service.handleYuban(r[2], ['2']);
       }
       let r_ = Service.updateShuliang(result[i][1], r[2])
        let hit = r_[1][0]['_children'].filter(x=>x['key']==r[2])[0]
       let dercj = copy(hit['dercj'])
       for(let j = 1; j < dercj.length; j++){
            let bianma = dercj[j][1]
	    if (data['replace'].hasOwnProperty(bianma)){
	      dercj[j][2] = data['replace'][bianma] 
            }
	    for(let k = 0; k < result[i][3].length;k++){
	        let old_bianma = result[i][3][k]['bianma']
	        let new_bianma = result[i][3][k]['new_bianma']
	        let new_mc = result[i][3][k]['mc']
	        let new_jg = result[i][3][k]['jg']
		if (old_bianma == bianma){
		  dercj[j][1] = new_bianma
		  dercj[j][2] = new_mc
		  dercj[j][5] = new_jg
			
		}
	    }

       }
       Service.updateDercj(r[2], dercj)
   }
   console.log(data['extra']);
   if (data['extra'].length >= 4) {
        Service.danxiangdinge("1", data['n'], data['extra'])
   }
   if (data['fy']=='{}'){}
   else Service.changguidinge(JSON.parse(data['fy']), "1")
   }
   res.status(200).send(Service.cache);
});
app.listen(3000, () => {
   console.log('Server is running on port 3000');
});
