package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.gateway.service.AreaService;
import com.sptg.gateway.service.CsvService;
import com.sptg.permissions.api.dto.AreaDto;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * @author WFT
 * @date 2020/6/11
 * description:
 */
@Service
@RequiredArgsConstructor
public class XqCsvServiceImpl implements CsvService {
    @Override
    public String getFlag() {
        return "csv_xq";
    }

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

    private final AreaService areaService;
    private final OrgFeign orgFeign;
    private final ResidentialFeign xqFeign;

    @Override
    public ResultBean<?> save(Object data) {
        return this.xqFeign.addResidential((ResidentialDto) data);
    }

    @Override
    public Object arrToObj(String[] array) {
        //  原数据Id(必填)
        String oldId = array[9];
        Assert.hasText(oldId,"原数据Id不能为空");
        //  判断小区Id是否已存在
        Residential xq = this.xqFeign.getByOldId(oldId);
        if (null != xq){
            return null;
        }
        ResidentialDto dto = new ResidentialDto();
        dto.setOldId(oldId);
        //  小区名称(必填)
        dto.setName(array[0]);
        Assert.hasText(dto.getName(),"小区名称不能为空");
        //  物业Id(必填)
        String estateOrgId = array[1];
        Assert.hasText(estateOrgId,"物业Id不能为空");
        estateOrgId = "wy_" + estateOrgId;
        //  校验物业Id是否存在
        Organization estate = this.orgFeign.getByOldIdAndType(estateOrgId, "3");
        Assert.notNull(estate,"物业Id不存在");
        dto.setEstatePid(estate.getId());
        //  省市区(必填)
        String province = array[2];
        String city = array[3];
        String area = array[4];
        //  查询省市区编号
        AreaDto areaDto = this.areaService.selectByRegionName(province, city, area);
        Assert.notNull(areaDto,"省市区名称存在错误");
        dto.setRegionProvince(areaDto.getRegionProvince());
        dto.setRegionCity(areaDto.getRegionCity());
        dto.setRegionArea(areaDto.getRegionArea());
        dto.setRegion(areaDto.getRegionName());
        //  代理商Id(选填)
        String agentOrgId = array[5];
        if (StringUtils.isNotEmpty(agentOrgId)){
            //  校验代理商Id是否存在
            Organization agent = this.orgFeign.getByOldIdAndType(agentOrgId, "1");
            Assert.notNull(agent,"代理商Id不存在");
            dto.setOrgPid(agent.getId());
            dto.setOrgPosition(agent.getPosition());
        }
        //  详细地址(选填)
        String address = array[6];
        dto.setAddress(address);
        //  负责人名称(选填)
        String personName = array[7];
        dto.setPersonName(personName);
        //  负责人电话(选填)
        String personTel = array[8];
        dto.setPersonTel(personTel);
        dto.setCreateAccountId(1L);
        dto.setCreatorType(1);
        return dto;
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "小区名称(必填)",
            "物业Id(必填)",
            "省名称(必填)",
            "市名称(必填)",
            "区名称(必填)",
            "代理商Id(选填)",
            "详细地址(选填)",
            "负责人名称(选填)",
            "负责人电话(选填)",
            "原数据Id(必填)"
    ));

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