package com.gic.haoban.manage.service.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.ToolUtil;
import com.gic.haoban.manage.api.dto.HaobanUserDTO;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanUserMapper;
import com.gic.haoban.manage.service.entity.TabHaobanUser;
import com.gic.haoban.manage.service.service.HaobanUserService;

@Service
public class HaobanUserServiceImpl implements HaobanUserService {

    @Autowired
    private TabHaobanUserMapper tabHaobanUserMapper;

    @Override
    public HaobanUserDTO insert(HaobanUserDTO dto) {
        if (StringUtils.isAnyBlank(dto.getWxEnterpriseId(), dto.getPhoneNumber())) {
            return null;
        }
        HaobanUserDTO oldUser = this.selectByPhone(dto.getWxEnterpriseId(), dto.getPhoneNumber());
        if (null != oldUser) {
            return oldUser;
        }
        dto.setUserId(ToolUtil.randomUUID());
        TabHaobanUser user = EntityUtil.changeEntity(TabHaobanUser.class, dto);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setStatusFlag(1);
        this.tabHaobanUserMapper.insert(user);
        return dto;
    }

    @Override
    public HaobanUserDTO selectByPhone(String wxEnterpriseId, String phone) {
        TabHaobanUser user = this.tabHaobanUserMapper.selectByPhone(wxEnterpriseId, phone);
        if (null != user) {
            return EntityUtil.changeEntity(HaobanUserDTO.class, user);
        }
        return null;
    }

    @Override
    public void updateDefaultGicEid(String staffid, String gicEnterpriseId) {
        this.tabHaobanUserMapper.updateDefaultGicEid(staffid, gicEnterpriseId);
    }
    
    @Override
    public HaobanUserDTO selectById(String userId) {
        TabHaobanUser user = this.tabHaobanUserMapper.selectById(userId);
        if (null != user) {
            return EntityUtil.changeEntity(HaobanUserDTO.class, user);
        }
        return null;
    }
}
