package com.seeyon.apps.src_ydctdatasync.datamap;

import com.seeyon.apps.src_ydctdatasync.dao.KsQueryDao;
import com.seeyon.apps.src_ydctdatasync.utils.EnumMapUtils;

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

public class ContractMode extends AbstractContractDataMapMode {

    @Override
    public void preProcess(Map<String, Object> map) {
        String zcType = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产类型","房屋");
        String zcBlock = EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产板块","03不动产");
        String ksName = (String) map.get("b_name");
        String zcTypeLevel2 = (String) map.get("asset_type_name");
        if(zcTypeLevel2.equals("保障房")){
            map.put("asset_type_name","保障性住房");
        }else if(zcTypeLevel2.equals("国资门面")) {
            map.put("asset_type_name","住宅");
        }else {
            map.put("asset_type_name","厂房");
        }
        map.put("zcType",zcType);
        map.put("zcBlock",zcBlock);
        map.put("asset_type_name",EnumMapUtils.getEnumItemValue(zcRootPcode,agent,"资产二级类型", (String) map.get("asset_type_name")));
        KsQueryDao ksQueryDao = new KsQueryDao();
        try {
            Map<String, Object> ksMap = ksQueryDao.queryByName(ksName, "where field0006 = ?", ksName);
            if(map != null && !map.isEmpty()) {
                map.put("u8ccode",ksMap.get("field0092"));
                map.put("signCorpNo",ksMap.get("field0029"));
            }
        } catch (Exception e) {

        }
    }

    @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;
        Map<String,Object> subTableMap = new HashMap<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        targetMap.put("subTable",subTableMap);
        targetMap.put("合同ID",sourceMap.get("contract_id")); //填充合同ID
        targetMap.put("合同编号",sourceMap.get("contract_no")); //填充合同编号
        targetMap.put("签订单位编号",sourceMap.get("signCorpNo")); //填充签订单位编号
        targetMap.put("签订单位",sourceMap.get("a_name"));//填充签订单位
        targetMap.put("合同金额",sourceMap.get("contract_total_amount"));//合同金额
        try {
            targetMap.put("合同开始日期",df.format((Date)sourceMap.get("tenant_start_time")));//合同开始日期
            targetMap.put("合同截止日期",df.format((Date)sourceMap.get("tenant_end_time")));//合同截止日期
        }catch (NullPointerException e) {

        }
        targetMap.put("U8C客商编号",sourceMap.get("u8ccode"));//U8C客商编号
        targetMap.put("客商名称",sourceMap.get("b_name"));//客商名称
        targetMap.put("联系方式",sourceMap.get("b_connect"));//联系方式
        targetMap.put("甲方名称",sourceMap.get("a_name"));//甲方名称
        targetMap.put("乙方名称",sourceMap.get("b_name"));//乙方名称
        targetMap.put("合同状态",sourceMap.get("contract_state"));//合同状态
        targetMap.put("累计已收租金",sourceMap.get("contract_receive_amount"));//累计已收租金
        targetMap.put("剩余未收租金",sourceMap.get("contract_remaining_amount"));//剩余未收租金

        //填充子表
        subTableMap.put("不动产板块",sourceMap.get("zcBlock"));//资产板块
        subTableMap.put("资产类型",sourceMap.get("zcType"));//资产类型
        subTableMap.put("资产二级类型",sourceMap.get("asset_type_name"));//资产二级类型
        subTableMap.put("所属小区",sourceMap.get("village_name")); //填充小区名
        subTableMap.put("楼栋",sourceMap.get("building_name")); //填充楼栋
        subTableMap.put("单元",sourceMap.get("unit_name")); //填充单元
        subTableMap.put("房号",sourceMap.get("house_number")); //填充房号
        subTableMap.put("住保资产编号",sourceMap.get("house_id"));
        subTableMap.put("OA资产编号",sourceMap.get("house_id"));
        StringBuilder stringBuilder = new StringBuilder("");
        if(sourceMap.get("village_name") != null) {
            stringBuilder.append(sourceMap.get("village_name"));
        }
        if(sourceMap.get("building_name") != null) {
            if(sourceMap.get("village_name") != null) {
                stringBuilder.append("_");
            }
            stringBuilder.append(sourceMap.get("building_name"));
        }
        if(sourceMap.get("variable_001") != null) {
            if(sourceMap.get("village_name") != null) {
                stringBuilder.append("_");
            }
            stringBuilder.append(sourceMap.get("variable_001"));
        }
        if(sourceMap.get("room_number") != null) {
            if(sourceMap.get("building_name") != null) {
                stringBuilder.append("_");
            }
            stringBuilder.append(sourceMap.get("room_number"));
        }
        subTableMap.put("资产名称",stringBuilder.toString()); //填充资产名称
    }
}
