package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.feigns.MemberFeign;
import com.sptg.gateway.service.CsvService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author WFT
 * @date 2020/6/16
 * description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppMemberCsvServiceImpl implements CsvService {

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

    @Override
    public String getTemplateName() {
        return "App会员导入模板";
    }

    private final MemberFeign memberFeign;

    @Override
    public ResultBean<?> save(Object data) {
        return this.memberFeign.memberRegister((Member) data);
    }

    @Override
    public Object arrToObj(String[] array) {
        String oldId = array[1];
        Assert.notNull(oldId,"原数据Id不能为空");
        //  判断数据是否已导入
        Member member = this.memberFeign.getByOldId(oldId);
        if (null != member){
            return null;
        }
        member = new Member();
        member.setOldId(oldId);
        member.setName(array[2]);
//        Assert.hasText(member.getName(),"会员名称不能为空");
        member.setTel(array[3]);
        Assert.hasText(member.getTel(),"手机号码不能为空");
        //密码md5加密
        String password = array[4];
        if (StringUtils.isEmpty(password)){
            //  给个默认密码,标记为异常账号,将来以短信方式提醒用户重置密码
            member.setPassword("159753");
            member.setErrorFlag(1);
        }
        member.setPassword(DigestUtils.md5Hex("sptg" + password));
        //  性别
        String sex = array[5];
        if (StringUtils.hasText(sex)){
            member.setSex(Integer.valueOf(sex));
        }else {
            member.setSex(0);
        }
        //  数据状态
        String dataStatus = array[6];
        if (StringUtils.hasText(dataStatus)){
            member.setStatus(Integer.valueOf(dataStatus));
        }
        //  认证状态
        String grantStatus = array[7];
        if (StringUtils.hasText(grantStatus)){
            member.setState(Integer.valueOf(grantStatus));
        }else {
            member.setState(0);
        }
        //  数据来源
        String createType = array[8];
        if (StringUtils.hasText(createType)){
            member.setCreateType(Integer.valueOf(createType));
        }else {
            member.setCreateType(2);
        }
        //  创建时间
        String createTime = array[9];
        if (StringUtils.hasText(createTime)){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                member.setCreateDate(dateFormat.parse(createTime));
            } catch (ParseException e){
                throw new RuntimeException("创建时间有误");
            }
        } else {
            member.setCreateDate(new Date());
        }
        member.setOrgId(1L);
        member.setRootOrgId(1L);
        member.setOrgPosition("0/1/");
        return member;
    }

    @Override
    public List<String> getHeaderList() {
        return new ArrayList<>(Arrays.asList(
                "ID（先不填）",
                "旧平台ID(必填)",
                "账户姓名(必填)",
                "手机号码(必填)",
                "密码(必填、明文)",
                "性别(选填:0:未定义、1:男、2:女)",
                "认证状态(选填、0:未认证、1:待审核、2:已认证)",
                "注册来源(选填、0:小程序、1:后台添加、2:后台导入、3:App)",
                "注册时间(选填、格式:yyyy-MM-dd HH:mm:ss)"
        ));
    }
}
