package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.dto.UnitDto;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.entity.Unit;
import com.sptg.estate.api.feigns.ResidentialFeign;
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 UnitCsvServiceImpl implements CsvService {

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

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

    private final UnitFeign unitFeign;
    private final ResidentialFeign residentialFeign;

    @Override
    public ResultBean<?> save(Object data) {
        Unit unit = this.unitFeign.addUnitByImport((UnitDto)data);
        if (null != unit){
            return ResultBean.ok();
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    public Object arrToObj(String[] array) {
        //  原数据Id(必填)
        String oldId = array[3];
        Assert.hasText(oldId,"原数据Id不能为空");
        Unit unit = this.unitFeign.getByOldId(oldId);
        if (null != unit){
            return null;
        }
        UnitDto dto = new UnitDto();
        dto.setOldId(oldId);
        //  单元名称(必填)
        String name = array[0];
        Assert.hasText(name,"单元名称不能为空");
        dto.setName(name);
        //  单元编号(必填,必须为数字)
        String code = array[1];
        dto.setUnitNumber(code);
        //  小区Id(必填)
        String xqId = array[2];
        Assert.hasText(xqId,"小区Id不能为空");
        Residential xq = this.residentialFeign.getByOldId(xqId);
        Assert.notNull(xq,"小区Id不存在");
        dto.setResidentialId(xq.getId());
        //  负责人名称(选填)
        String personName = array[4];
        if (!StringUtils.isEmpty(personName)){
            dto.setPersonName(personName);
        }
        //  负责人电话(选填)
        String personTel = array[5];
        if (!StringUtils.isEmpty(personTel)){
            dto.setPersonTel(personTel);
        }
        //  建筑年代(选填)
        String builtYear = array[6];
        if (!StringUtils.isEmpty(builtYear)){
            dto.setBuiltYear(builtYear);
        }
        //  产权性质(选填)
        String propertyNature = array[7];
        if (!StringUtils.isEmpty(propertyNature)){
            dto.setPropertyNature(propertyNature);
        }
        //  有无电梯(选填)
        String includingElevator = array[8];
        if (!StringUtils.isEmpty(includingElevator)){
            dto.setIncludingElevator(includingElevator);
        }
        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;
    }
}
