package com.casic.missiles.modular.system.model;

import java.io.Serializable;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import com.casic.missiles.core.common.constant.Const;
import com.casic.missiles.core.enums.OperationEnum;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.sync.handler.base.UserAbstractRequiredModel;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author stylefeng123
 * @since 2019-08-16
 */
@TableName("bus_user_sync")
@Data
public class UserSync extends Model<UserSync> implements UserAbstractRequiredModel {

    private static final long serialVersionUID = 1L;

    /**
     * 身份证号
     */
    @TableField("identity_card")
    private String identityCard;
    /**
     * 用户名
     */
    @TableField("person_nick_name")
    private String personNickName;
    /**
     * 性别
     */
    private Integer sex;
    /**
     * 组织编码
     */
    @TableField("organization_code")
    private String organizationCode;
    private String telephone;
    /**
     * 用户ID
     */
    @TableField("person_id")
    private String personId;
    /**
     * 操作标识
     */
    @TableField("action_type")
    private String actionType;
    @TableField(exist = false)
    private String imei1;
    @TableField(exist = false)
    private String imei2;
    @JSONField(name = "device_id")
    @TableField(exist = false)
    private String targetId;
    @JSONField(name = "network_number")
    @TableField(exist = false)
    private String networkNumber;

    @Override
    public String getTargetAccount() {
        return this.identityCard;
    }

    @Override
    public String getTargetIdCard() {
        return this.identityCard;
    }

    @Override
    public String getTargetPassword() {
        //使用默认密码
        return Const.DEFAULT_PWD;
    }

    @Override
    public String getTargetName() {
        return this.personNickName;
    }

    @Override
    public Integer getTargetSex() {
        return this.sex;
    }

    @Override
    public Date getTargetBirthday() {
        //TODO 根据身份证号获取生日
        return new Date();
    }

    @Override
    public String getTargetPhone() {
        return this.telephone;
    }

    @Override
    public String getTargetDeptId() {
        return organizationCode;
    }

    @Override
    public String getTargetOperation() {
        if (null == this.actionType) {
            return OperationEnum.INIT.getValue();
        }
        //增量同步数据操作
        switch (this.actionType) {
            case "0":
                return OperationEnum.INSERT_TYPE.getValue();
            case "1":
                return OperationEnum.DEVICE_ADD.getValue();
            case "2":
                return OperationEnum.UPDATE_TYPE.getValue();
            case "3":
                return OperationEnum.DEL_TYPE.getValue();
            case "4":
                return OperationEnum.DEVICE_DEL.getValue();
            default:
                return OperationEnum.INIT.getValue();
        }
    }

    @Override
    public List<UserDevice> getDevices() {
        List<UserDevice> devices = new ArrayList<>();
        if (ToolUtil.isNotEmpty(targetId)) {
            UserDevice device = new UserDevice();
            device.setImei(this.getImei1());
            device.setTargetId(targetId);
            device.setNetworkNumber(networkNumber);
            devices.add(device);
        }
        return devices;
    }

    @Override
    protected Serializable pkVal() {
        return this.personId;
    }

    @Override
    public String toString() {
        return "UserSync{" +
                "identityCard=" + identityCard +
                ", personNickName=" + personNickName +
                ", sex=" + sex +
                ", organizationCode=" + organizationCode +
                ", telephone=" + telephone +
                ", personId=" + personId +
                ", actionType=" + actionType +
                "}";
    }
}
