package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.device.api.dto.FlowCardDto;
import com.sptg.device.api.entity.FlowCard;
import com.sptg.device.api.feigns.FlowCardFeign;
import com.sptg.gateway.service.CsvService;
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.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/22
 * description:
 */
@Service
@RequiredArgsConstructor
public class FlowCardCsvServiceImpl implements CsvService {
    @Override
    public String getFlag() {
        return "csv_flow_card";
    }

    @Override
    public String getTemplateName() {
        return "设备流量卡导入模板";
    }

    @Override
    public ResultBean<?> save(Object data) {
        return this.flowCardFeign.add((FlowCardDto)data);
//        FlowCardDto dto = (FlowCardDto)data;
//        dto.setId(IdWorker.getId());
//        ResultBean<Boolean> resultBean = this.flowCardFeign.add(dto);
//        if (resultBean.getData()){
//            //  保存设备与流量卡的关联
//            if (StringUtils.hasText(dto.getDeviceOldId())){
//                //  获取设备信息
//                Device device = this.deviceFeign.getByOldId(dto.getDeviceOldId());
//                dto.setDeviceId(device.getId());
//                this.flowCardFeign.bind(dto);
//            }
//        }
//        return resultBean;
    }

    private final FlowCardFeign flowCardFeign;
    private final OrgFeign orgFeign;

    @Override
    public Object arrToObj(String[] array) {
        FlowCardDto dto = new FlowCardDto();
        //  原数据Id
        String oldId = array[0];
        Assert.hasText(oldId,"原数据Id不能为空");
        //  判断数据是否已存在
        FlowCard card = this.flowCardFeign.getByOldId(oldId);
        if (null != card){
            return null;
        }
        dto.setOldId(oldId);
        //  流量卡号
        String cardNo = array[1];
        Assert.hasText(cardNo,"流量卡卡号不能为空");
        dto.setCardNum(cardNo);
        //  运营商(必填,0:中国移动,1:中国联通,2:中国电信,3:其他)
        String business = array[2];
        Assert.hasText(business,"运营商不能为空");
        switch (business){
            case "0": dto.setOperator(0); break;
            case "1": dto.setOperator(1); break;
            case "2": dto.setOperator(2); break;
            case "3": dto.setOperator(3); break;
            default: throw new RuntimeException("运营商参数异常");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  卡号生效时间(必填,yyyy-MM-dd HH:mm:ss)
        String beginTime = array[3];
        if (StringUtils.isEmpty(beginTime)){
            dto.setBeginDate(new Date());
        }else {
            try {
                dto.setBeginDate(dateFormat.parse(beginTime));
            } catch (ParseException e) {
                throw new RuntimeException("生效时间有误");
            }
        }
        //  卡号失效时间(必填,yyyy-MM-dd HH:mm:ss)
        String endTime = array[4];
        Assert.hasText(endTime,"失效时间不能为空");
        try {
            dto.setEndDate(dateFormat.parse(endTime));
        }catch (ParseException e){
            throw new RuntimeException("失效时间有误");
        }
        //  所属机构Id
        String orgId = array[5];
        if (!StringUtils.hasText(orgId)){
            orgId = "1";
        }
        Organization org = this.orgFeign.getByOldIdAndType(orgId, "0");
        Assert.notNull(org,"机构Id不存在");
        dto.setOrgId(org.getId());
        dto.setOrgName(org.getName());
        dto.setOrgPosition(org.getPosition());
        dto.setCreateAccountId(1L);
        return dto;
    }

    @Override
    public List<String> getHeaderList() {
        return new ArrayList<>(Arrays.asList(
                "原数据的Id(必填)",
                "流量卡卡号(必填)",
                "运营商(必填、0:中国移动、1:中国联通、2:中国电信、3:其他)",
                "卡号生效时间(必填、yyyy-MM-dd HH:mm:ss)",
                "卡号失效时间(必填、yyyy-MM-dd HH:mm:ss)",
                "所属机构Id(必填)"
        ));
    }
}
