package com.community.huiju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.huiju.common.base.ResponseBean;
import com.community.huiju.enums.ResponseErrorsMessages;
import com.community.huiju.model.TaUser;
import com.community.huiju.dao.TaUserMapper;
import com.community.huiju.service.ITaUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 住户app端用户表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-15
 */
@Service
public class TaUserServiceImpl extends ServiceImpl<TaUserMapper, TaUser> implements ITaUserService {

    @Autowired
    private TaUserMapper taUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean addTaUser(String userName, String idCard, String gender, String phone) {
        ResponseBean responseBean = new ResponseBean();

        // 校验手机号 -> 是否重复
        checkPhone(phone);

        if (StringUtils.isBlank(userName)) {
            responseBean.addError(ResponseErrorsMessages.NOT_USER_NAME.getCode(), ResponseErrorsMessages.NOT_USER_NAME.getMsg());
            return responseBean;
        }
        if (StringUtils.isBlank(idCard)) {
            responseBean.addError(ResponseErrorsMessages.NOT_ID_CARD.getCode(), ResponseErrorsMessages.NOT_ID_CARD.getMsg());
            return responseBean;
        }
        if (StringUtils.isBlank(gender)) {
            responseBean.addError(ResponseErrorsMessages.NOT_GENDER.getCode(), ResponseErrorsMessages.NOT_GENDER.getMsg());
            return responseBean;
        }

        TaUser one = getByPhone(phone);
        if (null != one) {
            responseBean.addError(ResponseErrorsMessages.EXISTING_GENDER.getCode(), ResponseErrorsMessages.EXISTING_GENDER.getMsg());
            return responseBean;
        }

        TaUser taUser = new TaUser();
        taUser.setUserName(userName);
        taUser.setIdCard(idCard);
        taUser.setLoginName(phone);
        taUser.setCreateDate(new Date());

        boolean saveBool = this.save(taUser);
        if (saveBool) {
            taUser.setCreateUser(taUser.getUserId());
            this.updateById(taUser);
            responseBean.addSuccess(taUser);
        }
        return responseBean;
    }

    private TaUser getByPhone(String phone) {
        QueryWrapper<TaUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", phone);
        TaUser one = this.getOne(queryWrapper);
       return one;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateTaUser(Integer userId, String userName, String idCard, String gender, String phone) {
        ResponseBean responseBean = new ResponseBean();
        TaUser user = this.getById(userId);
        if (null == user) {
            responseBean.addError(ResponseErrorsMessages.NOT_USER.getCode(), ResponseErrorsMessages.NOT_USER.getMsg());
            return responseBean;
        }

        TaUser one = getByPhone(phone);
        if (null != one) {
            responseBean.addError(ResponseErrorsMessages.EXISTING_GENDER.getCode(), ResponseErrorsMessages.EXISTING_GENDER.getMsg());
            return responseBean;
        }

        UpdateWrapper<TaUser> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", userId);
        userUpdateWrapper.set(StringUtils.isNotBlank(userName), "user_name", userName);
        userUpdateWrapper.set(StringUtils.isNotBlank(idCard), "id_card", idCard);
        userUpdateWrapper.set(StringUtils.isNotBlank(gender), "gender", gender);
        userUpdateWrapper.set(StringUtils.isNotBlank(phone), "login_name", phone);
        userUpdateWrapper.set("update_user", userId);
        userUpdateWrapper.set("update_date", new Date());

        this.update(userUpdateWrapper);
        user = this.getById(userId);
        responseBean.addSuccess(user);
        return responseBean;
    }

    @Override
    public ResponseBean getTaUser(Integer userId) {
        ResponseBean responseBean = new ResponseBean();
        TaUser user = this.getById(userId);
        if (null == user) {
            responseBean.addError(ResponseErrorsMessages.NOT_USER.getCode(), ResponseErrorsMessages.NOT_USER.getMsg());
            return responseBean;
        }

        responseBean.addSuccess(user);
        return responseBean;
    }

    @Override
    public ResponseBean getTaUserByPhone(String phone) {
        ResponseBean responseBean = new ResponseBean();
        checkPhone(phone);
        TaUser user = getByPhone(phone);
        if (null == user) {
            responseBean.addError(ResponseErrorsMessages.NOT_USER.getCode(), ResponseErrorsMessages.NOT_USER.getMsg());
            return responseBean;
        }

        responseBean.addSuccess(user);
        return responseBean;
    }
}
