package com.yem.em.bd.devicefiles.from;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;


import com.yem.em.utils.EmptyUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeClosedEvent;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;


/**
 * @author 夜子
 * @ClassName DevicefilesEdit
 * @description: 设备档案表单插件
 * @date 2023年07月11日
 * @version: 1.0
 */
public class DevicefilesEdit extends AbstractBillPlugIn{

    @Override
    public void beforeClosed(BeforeClosedEvent e) {
        e.setCheckDataChange(false);
        this.getModel().setDataChanged(false);
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IDataModel model = this.getModel();
        DynamicObject createorg = (DynamicObject) model.getValue("createorg");
        if (createorg != null) {
            String orgno = createorg.getString("number");
            if (orgno.equals("Org-400") || orgno.equals("Org-600")) {// TTC&T和易大宗
                this.getView().setVisible(true, "yem_isvehicle");
                this.getView().setVisible(true, "yem_advconap");
            } else {
                this.getView().setVisible(false, "yem_isvehicle");
                this.getView().setVisible(false, "yem_advconap");
                if (orgno.equals("Org-500")) {// 中水十局
                    this.getView().setVisible(Boolean.TRUE,
                            "yem_projectdeskaccount", "yem_devicework");
                } else {
                    this.getView().setVisible(Boolean.FALSE,
                            "yem_projectdeskaccount", "yem_devicework");
                }
            }
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IDataModel model = this.getModel();
        DynamicObject group = (DynamicObject) model.getValue("group");
        if (group != null) {
            DynamicObject projectgroup = BusinessDataServiceHelper.loadSingle(group.getPkValue(), "yem_em_comlefttree");
            if (projectgroup != null) {
                DynamicObject equipment = projectgroup.getDynamicObject("yem_model");
                if (equipment != null) {
                    model.setValue("yem_model", equipment.getPkValue());
                }
                String type = projectgroup.getString("yem_newtype");
                if (!type.equals("B")) {
                    if (equipment != null) {
                        equipment = BusinessDataServiceHelper.loadSingle(equipment.getPkValue(), "yem_em_etcmanual");
                        model.setValue("yem_picture", equipment.getString("yem_picture"));
                    }
                }
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        IDataModel model = this.getModel();
        if (EmptyUtils.isNotEmpty(operateKey)) {
            BillShowParameter billShowParameter = new BillShowParameter();
            billShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            billShowParameter.setStatus(OperationStatus.VIEW);
            switch (operateKey) {
                case "equipercords": {// 查看设备台账
                    String number = (String) model.getValue("number");
                    QFilter qFilter = new QFilter("number", QCP.equals, EmptyUtils.isEmpty(number) ? "" : number);
                    DynamicObject devicefiles = BusinessDataServiceHelper.loadSingle("yem_em_equipercords", "id,number",
                            new QFilter[]{qFilter});
                    if (null != devicefiles) {
                        billShowParameter.setPkId(devicefiles.getPkValue());
                    }
                    billShowParameter.setFormId("yem_em_equipercords");
                    this.getView().showForm(billShowParameter);
                    break;
                }
                case "etcmanuallist": {// 查看电子目录
                    DynamicObject etcmanual = (DynamicObject) model.getValue("yem_model");
                    QFilter qFilter = new QFilter("id", QCP.equals, etcmanual == null ? 0L : etcmanual.getPkValue());
                    DynamicObject equdocmanage = BusinessDataServiceHelper.loadSingle("yem_em_etcmanual", "id,number,name",
                            new QFilter[]{qFilter});
                    if (null != equdocmanage) {
                        billShowParameter.setPkId(equdocmanage.getPkValue());
                    }
                    billShowParameter.setFormId("yem_em_etcmanual");
                    this.getView().showForm(billShowParameter);
                    break;
                }
                case "syncpic":
                    this.getView().invokeOperation("refresh");
                    break;
                case "gebase": // 查看知识库
                    ListShowParameter listShowParameter = new ListShowParameter();
                    listShowParameter.setBillFormId("yem_em_doc");
                    listShowParameter.setCaption(ResManager.loadKDString("知识库列表", "DevicefilesEdit_0", "bfcl_equipment"));
                    listShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    listShowParameter.setMultiSelect(false);
                    this.getView().showForm(listShowParameter);
                    break;
                case "datarepair":
                    this.getView().updateView();
                    break;
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IDataModel model = this.getModel();
        switch (e.getProperty().getName()) {
            case "yem_activationdate":// 投用日期
            case "yem_warrantytime":// 质保年限
                Date yem_activationdate = (Date) model.getValue("yem_activationdate");
                BigDecimal yem_warrantytime = (BigDecimal) model.getValue("yem_warrantytime");
                if (yem_activationdate != null) {
                    Calendar ca = Calendar.getInstance();
                    ca.setTime(yem_activationdate);
                    ca.add(Calendar.DATE, yem_warrantytime.intValue());
                    model.setValue("yem_warrantyenddate", ca.getTime());
                }
                break;
            default:
                break;
        }
    }
}
