package com.fzm.mall.customer.service.customer.model;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fzm.mall.customer.service.customer.entity.dto.CustomerSaveDTO;
import com.fzm.mall.customer.service.customer.mapper.GroupUserMapper;
import com.fzm.mall.customer.service.util.UUIDUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author wulixian
 * @since 2021-04-07
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("customer_group")
@ApiModel(value = "Group对象", description = "")
public class Group implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键ID")
    private Long id;

    @ApiModelProperty(value = "客服名称")
    private String customerName;

    @ApiModelProperty(value = "客服头像")
    private String customerImage;

    @ApiModelProperty(value = "用户名称")
    private String userName;

    @ApiModelProperty(value = "用户头像")
    private String userImage;

    @ApiModelProperty(value = "群主编号")
    private String masterId;

    @ApiModelProperty(value = "群编号")
    private String groupId;

    @ApiModelProperty(value = "信息入库时间")
    @TableField(fill = FieldFill.INSERT)
    private Long createTime;

    @ApiModelProperty(value = "信息更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateTime;

    @ApiModelProperty(value = "群组用戶id")
    @TableField(exist = false)
    private List<String> userIdList;

    public void init(CustomerSaveDTO customerSaveDTO) {
        setCustomerName(customerSaveDTO.getCustomerName());
        setCustomerImage(StringUtils.isBlank(customerSaveDTO.getCustomerImage())?"":customerSaveDTO.getCustomerImage());
        setUserName(customerSaveDTO.getUserName());
        setUserImage(StringUtils.isBlank(customerSaveDTO.getUserImage())?"":customerSaveDTO.getUserImage());
        setMasterId(customerSaveDTO.getUserId());
        setGroupId(UUIDUtil.getUUID());
    }

    /**
     * 创建组内成员
     * @param groupUserMapper
     * @param customerSaveDTO
     */
    public void batchSaveGroupUser(GroupUserMapper groupUserMapper, CustomerSaveDTO customerSaveDTO) {
        List<String> userIdList = Arrays.asList(customerSaveDTO.getUserId(), customerSaveDTO.getCustomerId());

        userIdList.forEach(userId -> {
            GroupUser groupUser = new GroupUser();
            groupUser.setGroupId(getGroupId());
            groupUser.setUserId(userId);
            groupUserMapper.insert(groupUser);
        });
    }
}
