package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.constant.DeviceConstants;
import com.sptg.estate.api.dto.AccessCardDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.estate.api.vo.DevicesAccessPushVO;
import com.sptg.gateway.service.CsvService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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/11
 * description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CardCsvServiceImpl implements CsvService {

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

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

    private final ResidentialFeign xqFeign;
    private final UnitFeign unitFeign;
    private final RoomFeign roomFeign;
    private final UserFeign userFeign;
    private final AccessCardFeign cardFeign;

    @Override
    public ResultBean<?> save(Object data) {
        AccessCardDto accessCardDto = (AccessCardDto) data;
        accessCardDto.setDeviceAccessType(DeviceConstants.DEVICE_ACCESS_TYPE_UNIVERSAL);
        return this.cardFeign.addAccessCard(accessCardDto);
    }

    @Override
    public Object arrToObj(String[] array) {
        AccessCardDto dto = new AccessCardDto();
        String oldId = array[3];
        Assert.hasText(oldId,"原数据Id不能为空");
        dto.setOldId(oldId);
        AccessCard card = this.cardFeign.getByOldId(oldId);
        if (null != card){
            return null;
        }

        //  卡号
        String cardNo = array[0];
        Assert.hasText(cardNo,"卡号不能为空");
        dto.setCardNo(cardNo);
        //  生效时间
        String beginDate = array[1];
        Assert.hasText(beginDate,"生效时间不能为空");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            dto.setBeginDate(dateFormat.parse(beginDate));
        }catch (ParseException e){
            throw new RuntimeException("生效时间有误");
        }
        //  失效时间
        String endDate =array[2];
        Assert.hasText(endDate,"失效时间不能为空");
        try{
            dto.setEndDate(dateFormat.parse(endDate));
        }catch (ParseException e){
            throw new RuntimeException("失效时间有误");
        }
        //  类型
        String type = array[4];
        Assert.hasText(type,"类型不能为空");
        switch (type){
            //  小区
            case "3":
                this.setParameterByXqId(dto,array[5]);
                break;
            //  单元
            case "2":
                this.setParameterByUnitId(dto,array[6]);
                break;
            //  房间
            case "1":
                this.setParameterByRoomId(dto,array[7]);
                break;
            //  住户
            case "0":
                this.setParameterByUserId(dto,array[8]);
                break;
            default: throw new RuntimeException("类型错误");
        }
        dto.setType(Integer.valueOf(type));
        return dto;
    }


    private void setParameterByXqId(AccessCardDto dto,String xqId){
        Assert.hasText(xqId,"小区Id不能为空");
        Residential xq = this.xqFeign.getByOldId(xqId);
        Assert.notNull(xq,"小区Id不存在");
        dto.setResidentialId(xq.getId());
        dto.setResidentialName(xq.getName());
        dto.setOrgPosition(xq.getOrgPosition());
    }

    private void setParameterByUnitId(AccessCardDto dto,String unitId){
        Assert.hasText(unitId,"单元Id不能为空");
        Unit unit = this.unitFeign.getByOldId(unitId);
        Assert.notNull(unit,"单元Id不存在");
        dto.setResidentialId(unit.getResidentialId());
        dto.setResidentialName(unit.getResidentialName());
        dto.setUnitId(unit.getId());
        dto.setUnitName(unit.getName());
        dto.setOrgPosition(unit.getOrgPosition());
    }

    private void setParameterByRoomId(AccessCardDto dto,String roomId){
        Assert.hasText(roomId,"房间Id不能为空");
        Room room = this.roomFeign.getByOldId(roomId);
        Assert.notNull(room,"房间Id不存在");
        dto.setResidentialId(room.getResidentialId());
        dto.setResidentialName(room.getResidentialName());
        dto.setUnitId(room.getUnitId());
        dto.setUnitName(room.getUnitName());
        dto.setRoomId(room.getId());
        dto.setRoomName(room.getName());
        dto.setOrgPosition(room.getOrgPosition());
    }

    private void setParameterByUserId(AccessCardDto dto,String userId) {
        Assert.hasText(userId,"住户Id不能为空");
        User user = this.userFeign.getByOldId(userId);
        Assert.notNull(user,"住户Id不存在");
        dto.setResidentialId(user.getResidentialId());
        dto.setResidentialName(user.getResidentialName());
        dto.setUnitId(user.getUnitId());
        dto.setUnitName(user.getUnitName());
        dto.setRoomId(user.getRoomId());
        dto.setRoomName(user.getRoomName());
        dto.setUserId(user.getId().toString());
        dto.setUserName(user.getName());
        dto.setOrgPosition(user.getOrgPosition());
        dto.setCardEnable(1);
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "卡号(必填)",
            "生效时间(必填)",
            "失效时间(必填)",
            "原数据Id(必填)",
            "类型(必填、0:住户、1:房间、2:单元、3:小区)",
            "小区Id(选填)",
            "单元Id(选填)",
            "房间Id(选填)",
            "住户Id(选填)"
    ));

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