package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.dto.RoomDto;
import com.sptg.estate.api.entity.Room;
import com.sptg.estate.api.entity.Unit;
import com.sptg.estate.api.feigns.RoomFeign;
import com.sptg.estate.api.feigns.UnitFeign;
import com.sptg.gateway.service.CsvService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author WFT
 * @date 2020/6/11
 * description:
 */
@Service
@RequiredArgsConstructor
public class RoomCsvServiceImpl implements CsvService {

    @Override
    public String getFlag() {
        return "csv_room";
    }

    @Override
    public String getTemplateName() {
        return "房间导入模板";
    }

    private final UnitFeign unitFeign;
    private final RoomFeign roomFeign;

    @Override
    public ResultBean<?> save(Object data) {
        return this.roomFeign.addRoom((RoomDto) data);
    }

    @Override
    public Object arrToObj(String[] array) {
        //  房间名称(必填)
        String oldId = array[3];
        Assert.hasText(oldId,"原数据Id不能为空");
        Room room = this.roomFeign.getByOldId(oldId);
        if (null != room){
            return null;
        }
        RoomDto dto = new RoomDto();
        dto.setOldId(oldId);
        //  单元Id(必填)
        String unitId = array[2];
        Assert.hasText(unitId,"单元Id不能为空");
        Unit unit = this.unitFeign.getByOldId(unitId);
        Assert.notNull(unit,"单元Id不存在");
        dto.setUnitId(unit.getId());
        //  门牌号(必填,必须为数字)
        String code = array[1];
        dto.setHouseNumber(code);
        //  房间名称(必填)
        String name = array[0];
        Assert.hasText(name,"原数据Id不能为空");
        dto.setName(name);
        //  户型(选填)
        String houseType = array[4];
        if (StringUtils.isEmpty(houseType)){
            dto.setHouseType(houseType);
        }
        //  楼层(选填)
        String floor = array[5];
        if (StringUtils.isEmpty(floor)){
            dto.setFloor(floor);
        }
        //  装修程度(选填)
        String ornament = array[6];
        if (StringUtils.isEmpty(ornament)){
            dto.setOrnament(ornament);
        }
        //  朝向(选填)
        String orientation = array[7];
        if (!StringUtils.isEmpty(orientation)){
            dto.setOrientation(orientation);
        }
        dto.setCreateAccountId(1L);
        return dto;
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "房间名称(必填)",
            "门牌号(选填)",
            "单元Id(必填)",
            "原数据Id(必填)",
            "户型(选填)",
            "楼层(选填)",
            "装修程度(选填)",
            "朝向(选填)"
    ));

    @Override
    public List<String> getHeaderList() {
        return HEADER_LIST;
    }
}
