package com.seeyon.apps.src_ydctdatasync.datamap;

import com.seeyon.apps.src_ydctdatasync.dao.FormExportUtil;
import com.seeyon.apps.src_ydctdatasync.datasource.DataSource;
import com.seeyon.apps.src_ydctdatasync.utils.EnumMapUtils;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.bean.FormExport;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

//保障房资产映射OA资产表模式
public class BzfAssetMode extends AbstractAssetDataMapMode{

    private DataSource localDataSource;
    private FormFactory formFactory;

    public FormFactory getFormFactory() {
        if (formFactory == null) {
            formFactory = (FormFactory) AppContext.getBean("formFactory");
        }
        return formFactory;
    }

    public void setFormFactory(FormFactory formFactory) {
        this.formFactory = formFactory;
    }

    public DataSource getLocalDataSource() {
        return localDataSource;
    }

    public void setLocalDataSource(DataSource localDataSource) {
        this.localDataSource = localDataSource;
    }

    private void enumMap(Map<String, Object> sourceMap)  {
        String xqzcpLevelValue = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产层级","父级");
        String zcTypeFwValue = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产类型","房屋");
        String zcTypeLevel2 = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产二级类型","保障性住房");
        String xqzcBlock = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产板块","03不动产");
        String xqzcStatus = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产状态","待出租");
        String xqzcFeature = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产性质","非流动资产");
        String xqzz = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"用途","住宅");
        String house_use = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"用途", (String) sourceMap.get("house_use"));
        String rental_type = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"租售类型",(String) sourceMap.get("rental_type"));
        String xqrental_type = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"租售类型","只租不售");
        String xqhs = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"房屋体系","保租房");
        String hs = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"房屋体系",(String) sourceMap.get("house_system"));
        String land_nature = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"土地性质", (String) sourceMap.get("land_nature"));
        String house_type = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"户型", (String) sourceMap.get("house_type"));
        String has_lift = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"有无电梯", (String) sourceMap.get("has_lift"));
        String raise_way = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"筹集方式", (String) sourceMap.get("raise_way"));
        sourceMap.put("xqzcpLevel",xqzcpLevelValue);
        sourceMap.put("zcTypeFwValue",zcTypeFwValue);
        sourceMap.put("zcTypeLevel2",zcTypeLevel2);
        sourceMap.put("xqzcBlock",xqzcBlock);
        sourceMap.put("xqzcStatus",xqzcStatus);
        sourceMap.put("xqzcFeature",xqzcFeature);
        sourceMap.put("xqrental_type",xqrental_type);
        sourceMap.put("xqhs",xqhs);
        sourceMap.put("xqzz",xqzz);
        sourceMap.put("house_use",house_use);
        sourceMap.put("land_nature",land_nature);
        sourceMap.put("rental_type",rental_type);
        sourceMap.put("house_system",hs);
        sourceMap.put("house_type",house_type);
        sourceMap.put("has_lift",has_lift);
        sourceMap.put("raise_way",raise_way);
    }

    @Override
    public void preProcess(Map<String, Object> sourceMap) {
        FormExport formExport = new FormExport();
        String loginName = (String) sourceMap.get("loginName");
        //查询客商档案以及填充
        //处理小区资产档案信息
        super.preProcess(sourceMap);
        enumMap(sourceMap);
        try {
            List<String> params = new ArrayList<>();
            params.add((String) sourceMap.get("xqzcpLevelValue"));
            params.add((String) sourceMap.get("zcTypeFwValue"));
            params.add((String) sourceMap.get("village_name"));
            String table = (String) sourceMap.get("zcTable");
            String villageQuerySql = "SELECT 1 FROM "+ table +" WHERE field0027 = ? and field0023 = ? and field0037 =?;";
            agent.execute(villageQuerySql, params);
            List resList = agent.resultSetToList();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(resList == null || resList.size() == 0) {
                //保障性住房小区的资产信息若不存在，则插入
                Map<String,String> villageInsertInfo = new HashMap<>();
                villageInsertInfo.put("建档部门",getCreatorDept(agent,loginName));//建档部门
                villageInsertInfo.put("建档人员","系统管理员");//建档人员
                villageInsertInfo.put("建档日期",df.format(new Date()));//建档日期
                villageInsertInfo.put("资产板块",sourceMap.get("xqzcBlock")+"");//资产板块
                villageInsertInfo.put("建档单位","住保置业");//建档单位
                villageInsertInfo.put("资产层级",sourceMap.get("xqzcpLevelValue")+"");//资产层级
                villageInsertInfo.put("数据来源","住保资产系统(自定义)");//数据来源
                villageInsertInfo.put("资产名称",sourceMap.get("village_name") + "");//资产名称
                villageInsertInfo.put("资产状态",sourceMap.get("xqzcStatus") + "");//资产状态
                villageInsertInfo.put("资产性质",sourceMap.get("xqzcFeature") + "");//资产性质
                villageInsertInfo.put("资产类型",sourceMap.get("zcTypeFwValue") + "");//资产类型
                villageInsertInfo.put("资产二级类型",sourceMap.get("zcTypeLevel2") + "");//资产二级类型
                villageInsertInfo.put("资产种类","保障性住房");//资产种类
                villageInsertInfo.put("所属小区",sourceMap.get("village_name") +"");//小区
                villageInsertInfo.put("用途",sourceMap.get("house_use") +"");//用途
                villageInsertInfo.put("租售类型",sourceMap.get("xqrental_type") +"");//租售类型
                villageInsertInfo.put("房屋体系",sourceMap.get("house_system") +"");//房屋体系
//                villageInsertInfo.put("field0058",sourceMap.get(""));//坐落位置
                villageInsertInfo.put("土地性质",sourceMap.get("land_nature") +"");//土地性质
                villageInsertInfo.put("是否产权抵押",sourceMap.get("assetBacked") +""); //是否产权抵押
                formExport.setValues(FormExportUtil.setFormValue(villageInsertInfo));
                getFormFactory().importBusinessFormData(loginName, "zcbd1",formExport, new String[] {});
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    private String getCreatorDept(JDBCAgent agent,String loginName) {
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        try {
            V3xOrgMember member = orgManager.getMemberByLoginName(loginName);
            Long departmentId = member.getOrgDepartmentId();
            V3xOrgDepartment department = orgManager.getDepartmentById(departmentId);
            return department == null ? "" : department.getName();
        } catch (BusinessException e) {
            return null;
        }
    }

    @Override
    public Map<String, Function<Object, Object>> getFieldProcessors() {
        return null;
    }

    @Override
    public boolean support() {
        return false;
    }

    @Override
    public void transfer(Object target, Object source) {
        commonAttrTransfer(target,source);
        Map<String, Object> targetMap = (Map<String, Object>) target;
        Map<String, Object> sourceMap = (Map<String, Object>) source;
        targetMap.put("资产类型",sourceMap.get("zcTypeFwValue"));//填充资产类型
        targetMap.put("资产二级类型",sourceMap.get("zcTypeLevel2"));//填充资产二级类型
        targetMap.put("楼栋",sourceMap.get("building_name"));//填充楼栋
        targetMap.put("单元",sourceMap.get("unit_name"));//填充单元
        targetMap.put("具体楼层",sourceMap.get("specific_floor"));//填充具体楼层
        targetMap.put("房屋朝向",sourceMap.get("house_orientation"));//填充房屋朝向
        targetMap.put("户型",sourceMap.get("house_type"));//填充户型
        targetMap.put("有无电梯",sourceMap.get("has_lift"));//填充有无电梯
        targetMap.put("用途",sourceMap.get("house_use"));//填充用途
        targetMap.put("房屋体系",sourceMap.get("house_system"));//填充房屋体系
        targetMap.put("租售类型",sourceMap.get("rental_type"));//填充租售类型
        targetMap.put("筹集方式",sourceMap.get("raise_way"));//填充筹集方式
        targetMap.put("所属小区",sourceMap.get("village_name"));//填充所属小区
        targetMap.put("土地性质",sourceMap.get("land_nature"));//填充土地性质

        targetMap.put("房号",sourceMap.get("room_number"));//填充房号
    }
}
