/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  javax.annotation.Resource
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.apache.struts2.convention.annotation.Action
 *  org.apache.struts2.convention.annotation.Result
 *  org.dom4j.Document
 *  org.dom4j.Element
 *  org.dom4j.io.SAXReader
 *  org.springframework.context.annotation.Scope
 *  org.springframework.stereotype.Controller
 */
package com.hst.taxService.action;

import com.hst.common.action.ExtendAction;
import com.hst.common.dao.imp.SaveDhXmlDaoImp;
import com.hst.common.domain.BaseAppServiceDomain;
import com.hst.common.domain.TaxCommonAppServiceDomain;
import com.hst.common.domain.TaxCommonReturnDomain;
import com.hst.common.service.BaseBusinessService;
import com.hst.common.util.HstJsonUtil;
import com.hst.framework.constants.AppServiceConstants;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.util.HstToolsUtil;
import com.hst.taxService.domain.DhXmlChannelDom;
import com.hst.taxService.domain.DhXmlDepartMentDom;
import com.hst.taxService.domain.DhXmlDeviceDom;
import com.hst.taxService.domain.DhXmlSaveDom;
import com.hst.taxService.service.TaxCommonService;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope(value="prototype")
@Action(value="/service/taxCommon", results={@Result(name="doService", type="json"), @Result(name="doZxwj", type="json"), @Result(name="doZcjd", type="json")})
public class TaxCommonAppAction
extends ExtendAction {
    private static final long serialVersionUID = 1L;
    public static Log _logger = LogFactory.getLog(TaxCommonAppAction.class);
    private TaxCommonAppServiceDomain domain;

    public String doService() throws Exception {
        TaxCommonService service = (TaxCommonService)this.getService();
        service.dealGt3Log();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("C:\\Users\\zxc\\Desktop\\1.xml"));
        ArrayList<DhXmlSaveDom> saveList = new ArrayList<DhXmlSaveDom>();
        Element rootElement = document.getRootElement();
        Element department = rootElement.element("Department");
        if (department != null) {
            DhXmlSaveDom saveDom;
            DhXmlDepartMentDom departMentDm = new DhXmlDepartMentDom();
            departMentDm.setName(department.attributeValue("name"));
            departMentDm.setCoding(department.attributeValue("coding"));
            List devices = department.elements("Device");
            List channels = department.elements("Channel");
            for (Element e : devices) {
                String deviceId = e.attributeValue("id");
                DhXmlDeviceDom deviceDom = new DhXmlDeviceDom();
                deviceDom.setId(deviceId);
                for (Element c : channels) {
                    String channelId = c.attributeValue("id");
                    if (!channelId.startsWith(deviceId)) continue;
                    DhXmlChannelDom channelDom = new DhXmlChannelDom();
                    channelDom.setId(channelId);
                    deviceDom.getChannelDoms().add(channelDom);
                    saveDom = new DhXmlSaveDom();
                    saveDom.setChannelId(channelId);
                    saveDom.setSwjgMc(department.attributeValue("name"));
                    saveDom.setDeviceId(deviceId);
                    saveList.add(saveDom);
                }
                departMentDm.getDeviceDoms().add(deviceDom);
            }
            List departments2nd = department.elements("Department");
            for (Element e : departments2nd) {
                DhXmlDepartMentDom departMentDom2nd = new DhXmlDepartMentDom();
                departMentDom2nd.setName(e.attributeValue("name"));
                departMentDom2nd.setCoding(e.attributeValue("coding"));
                List devices2 = e.elements("Device");
                List channels2nd = e.elements("Channel");
                for (Element e2 : devices2) {
                    String deviceId = e2.attributeValue("id");
                    DhXmlDeviceDom deviceDom2nd = new DhXmlDeviceDom();
                    deviceDom2nd.setId(deviceId);
                    for (Element c2 : channels2nd) {
                        String channelId = c2.attributeValue("id");
                        if (!channelId.startsWith(deviceId)) continue;
                        DhXmlChannelDom channelDom2n = new DhXmlChannelDom();
                        channelDom2n.setId(channelId);
                        deviceDom2nd.getChannelDoms().add(channelDom2n);
                        saveDom = new DhXmlSaveDom();
                        saveDom.setChannelId(channelId);
                        saveDom.setSwjgMc(e.attributeValue("name"));
                        saveDom.setDeviceId(deviceId);
                        saveList.add(saveDom);
                    }
                    departMentDm.getDeviceDoms().add(deviceDom2nd);
                }
                List departments3rd = e.elements("Department");
                for (Element dep3 : departments3rd) {
                    DhXmlDepartMentDom departMentDom3rd = new DhXmlDepartMentDom();
                    departMentDom3rd.setName(dep3.attributeValue("name"));
                    departMentDom3rd.setCoding(dep3.attributeValue("coding"));
                    List devices3rd = dep3.elements("Device");
                    List channels3rd = dep3.elements("Channel");
                    for (Element e3 : devices3rd) {
                        String deviceId = e3.attributeValue("id");
                        DhXmlDeviceDom deviceDom3rd = new DhXmlDeviceDom();
                        deviceDom3rd.setId(deviceId);
                        for (Element c3 : channels3rd) {
                            String channelId = c3.attributeValue("id");
                            if (!channelId.startsWith(deviceId)) continue;
                            DhXmlChannelDom channelDom3rd = new DhXmlChannelDom();
                            channelDom3rd.setId(channelId);
                            deviceDom3rd.getChannelDoms().add(channelDom3rd);
                            saveDom = new DhXmlSaveDom();
                            saveDom.setChannelId(channelId);
                            saveDom.setSwjgMc(String.valueOf(e.attributeValue("name")) + "-" + dep3.attributeValue("name"));
                            saveDom.setDeviceId(deviceId);
                            saveList.add(saveDom);
                        }
                        departMentDom3rd.getDeviceDoms().add(deviceDom3rd);
                    }
                    departMentDom2nd.getDepartMentDoms().add(departMentDom3rd);
                }
                departMentDm.getDepartMentDoms().add(departMentDom2nd);
            }
            Element Devices = rootElement.element("Devices");
            if (Devices != null) {
                HashMap<String, String> map = new HashMap<String, String>();
                HashMap<String, String> typeMap = new HashMap<String, String>();
                List deviceElemnts = Devices.elements("Device");
                for (Element device : deviceElemnts) {
                    System.out.println(String.valueOf(device.attributeValue("name")) + "-" + device.attributeValue("ip"));
                    List UnitNodes = device.elements("UnitNodes");
                    for (Element UnitNode : UnitNodes) {
                        String type = UnitNode.attributeValue("type");
                        List channles = UnitNode.elements("Channel");
                        for (Element channle : channles) {
                            map.put(channle.attributeValue("id"), channle.attributeValue("name"));
                            typeMap.put(channle.attributeValue("id"), type);
                        }
                    }
                }
                for (DhXmlSaveDom dom : saveList) {
                    dom.setChannelMc((String)map.get(dom.getChannelId()));
                    dom.setType((String)typeMap.get(dom.getChannelId()));
                }
                SaveDhXmlDaoImp dao = (SaveDhXmlDaoImp)WebConstants.WEB_APP_CONTEXT.getBean("saveDhXmlDaoImp");
                dao.saveDhXml(saveList);
            }
        }
        return "doService";
    }

    public String commonHandle(BaseAppServiceDomain appDomain) throws Exception {
        return AppServiceConstants.TAX_APP_SUCCESS;
    }

    public void handleException(TaxCommonReturnDomain returnDomain, String code) throws Exception {
        returnDomain.setCode(code);
        String message = this.handleExceptionMessage(returnDomain.getCode());
        returnDomain.setMess(message);
        this.domain.setRetJson(HstJsonUtil.beanToJson(returnDomain));
        if (WebConstants.LOG_KZ_ERRORS.equals("Y")) {
            _logger.error((Object)message);
        }
    }

    public void handleServiceException(TaxCommonReturnDomain returnDomain, String code, String error) throws Exception {
        returnDomain.setCode(code);
        String message = this.handleExceptionMessage(returnDomain.getCode());
        returnDomain.setMess(message);
        this.domain.setRetJson(HstJsonUtil.beanToJson(returnDomain));
    }

    public String handleExceptionMessage(String code) throws Exception {
        String message = "";
        if (HstToolsUtil.isNullOrEmpty(code)) {
            return message;
        }
        message = AppServiceConstants.TAX_CODE_MESS_MAP.containsKey(code) ? AppServiceConstants.TAX_CODE_MESS_MAP.get(code) : "\u672a\u5b9a\u4e49\u7684\u9519\u8bef\uff01";
        return message;
    }

    @Override
    @Resource(name="taxCommonServiceImp")
    public void setService(BaseBusinessService service) {
        super.setService(service);
    }

    @Override
    public TaxCommonAppServiceDomain getDomain() {
        if (this.domain == null) {
            this.domain = new TaxCommonAppServiceDomain();
        }
        return this.domain;
    }

    public void setDomain(TaxCommonAppServiceDomain domain) {
        this.domain = domain;
    }
}
