package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.gateway.service.AreaService;
import com.sptg.gateway.service.CsvService;
import com.sptg.permissions.api.dto.AreaDto;
import com.sptg.permissions.api.dto.OrgDto;
import com.sptg.permissions.api.entity.Area;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.OrgFeign;
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 OrgCsvServiceImpl implements CsvService {

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

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

    private final AreaService areaService;
    private final OrgFeign orgFeign;

    @Override
    public ResultBean<?> save(Object data) {
        return this.orgFeign.addEntity((OrgDto)data);
    }

    @Override
    public Object arrToObj(String[] array) {
        //  原数据Id
        String oldId = array[6];
        Assert.hasText(oldId,"原数据Id不能为空");
        OrgDto dto = new OrgDto();
        dto.setOldId(oldId);
        //  机构名称
        dto.setName(array[0]);
        Assert.hasText(dto.getName(),"机构名称不能为空");
        //  机构类型
        String orgType = array[1];
        Assert.hasText(orgType,"机构类型不能为空");
        try {
            dto.setOrgType(Integer.valueOf(orgType));
            switch (orgType){
                case "2":
                    dto.setOldId("gg_" + oldId);
                    break;
                case "3":
                    dto.setOldId("wy_" + oldId);
                    break;
                default:break;
            }
        }catch (NumberFormatException e){
            throw new NumberFormatException("机构类型参数有误");
        }
        //  判断机构是否存在
        Organization org = this.orgFeign.getByOldIdAndType(dto.getOldId(), orgType);
        if (null != org){
            return null;
        }
        //  获取省市区
        dto.setRegionProvince(array[2]);
        Assert.hasText(dto.getRegionProvince(),"省份名称不能为空");
        dto.setRegionCity(array[3]);
        Assert.hasText(dto.getRegionCity(),"城市名称不能为空");
        dto.setRegionArea(array[4]);
        if (StringUtils.isEmpty(dto.getRegionArea())){
            //  获取城市和省份的编号
            List<Area> areaList = this.areaService.selectCityAndProvinceByName(dto.getRegionCity());
            Assert.isTrue((null != areaList && areaList.size() == 2),"城市名称存在错误");
            dto.setRegionProvince(areaList.get(0).getCode());
            dto.setRegionCity(areaList.get(1).getCode());
            dto.setRegionArea(null);
            dto.setRegion(areaList.get(1).getName() + "-" + areaList.get(0).getName());
        } else {
            //  获取城市区编号
            AreaDto areaDto = this.areaService.selectByRegionName(dto.getRegionProvince(), dto.getRegionCity(), dto.getRegionArea());
            Assert.notNull(areaDto,"省市区名称存在错误");
            dto.setRegionProvince(areaDto.getRegionProvince());
            dto.setRegionCity(areaDto.getRegionCity());
            dto.setRegionArea(areaDto.getRegionArea());
            dto.setRegion(areaDto.getRegionName());
        }
        //  负责人电话
        dto.setPersonTel(array[5]);
        //  暂时写死,挂在跟机构下
        dto.setPid(1L);
        dto.setPosition("0/1/");
        //  创建人Id
        dto.setCreateAccountId(1L);
        dto.setFlag(1);
        dto.setCode("O");
        return dto;
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "机构名称(必填)",
            "机构类型(必填、1:代理商、3:物业)",
            "省名称(必填)",
            "市名称(必填)",
            "区名称(必填)",
            "负责人电话(选填)",
            "原数据Id(必填)"
    ));

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