package com.zhangmeng.admin.manager.service.impl;

import com.zhangmeng.admin.manager.service.RoleService;
import com.zhangmeng.admin.manager.service.UserService;
import com.zhangmeng.model.base.baseService.impl.AbstractBaseServiceImpl;
import com.zhangmeng.model.base.baseUtil.CommonUtil;
import com.zhangmeng.model.dto.query.QuerySingleParams;
import com.zhangmeng.model.entity.Role;
import com.zhangmeng.model.entity.User;
import com.zhangmeng.model.entity.UserRole;
import com.zhangmeng.model.vo.Result;
import com.zhangmeng.model.vo.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import com.zhangmeng.admin.manager.dao.UserRoleDao;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmeng
 * @version 1.0
 * @date 2021年1月7日22:32:54
 */
@Service
public class UserServiceImpl extends AbstractBaseServiceImpl<User> implements UserService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public User loadUserByUsername(String username) {
        Condition condition = new Condition(User.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("username",username);
        User user = null;
        List<User> userList = this.findByCondition(condition);
        if (userList.size() > 0){
            user = userList.get(0);
        }
        return user;
    }

    @Override
    public Result save_user(Map<String, Object> parms) {
        boolean flag = false;
        User user = null;
        String userId = CommonUtil.map_get_value(parms, "userId");
        if (userId == null || userId.equals("")) {
            user = new User();
            user.setAddTime(new Date());
            user.setUpdateTime(new Date());
            user.setDeleteStatus(false);
            flag = true;
        } else {
            user = this.findById(Long.parseLong(userId));
        }

        String username = CommonUtil.map_get_value(parms, "username");
        if (CommonUtil.isNotNull(username)) {
            user.setUsername(username);
        }
        String name = CommonUtil.map_get_value(parms, "name");
        if (CommonUtil.isNotNull(name)) {
            user.setName(name);
        }
        String email = CommonUtil.map_get_value(parms, "email");
        if (CommonUtil.isNotNull(email)) {
            user.setEmail(email);
        }

        String password = CommonUtil.map_get_value(parms, "password");
        if (CommonUtil.isNotNull(password)) {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            user.setPassword(bCryptPasswordEncoder.encode(password));
        }

        String phone = CommonUtil.map_get_value(parms, "phone");
        if (CommonUtil.isNotNull(phone)) {
            user.setTelephone(phone);
        }

        String sex = CommonUtil.map_get_value(parms, "sex");
        User.Gender gender = null;
        if (CommonUtil.isNotNull(sex)) {
            if (sex.equals("0")) {
                gender = User.Gender.male;
            }
            if (sex.equals("1")) {
                gender = User.Gender.female;
            }
        }
        user.setGender(gender);
        String status = CommonUtil.map_get_value(parms, "status");
        User.Status status1 = null;
        if (CommonUtil.isNotNull(status)) {
            switch (status) {
                case "0":
                    status1 = User.Status.DISABLED;
                    break;
                case "1":
                    status1 = User.Status.VALID;
                    break;
                case "2":
                    status1 = User.Status.LOCKED;
                    break;
            }
        }
        String message = null;
        if (flag){
            this.save(user);
            String role_name = CommonUtil.map_get_value(parms, "role_name");
            if (CommonUtil.isNotNull(role_name)) {
                Role.Type type = CommonUtil.getEnum(role_name, Role.Type.class);
                Role role = this.roleService.findBySingleParams(new QuerySingleParams("type",type, QuerySingleParams.Type.equal)).get(0);
                UserRole userRole = new UserRole();
                userRole.setAddTime(new Date());
                userRole.setUpdateTime(new Date());
                userRole.setDeleteStatus(false);
                userRole.setRole_id(role.getId());
                userRole.setUser_id(user.getId());
                this.userRoleDao.insert(userRole);
            }
            user.setStatus(status1);
            this.update(user);
            message = "添加成功";
        }else {
            message = "修改成功";
            this.update(user);
        }
        return new Result(true, StatusCode.OK,message);
    }

    @Override
    public Result reg_save(Map<String, Object> parms) {
        String username = CommonUtil.map_get_value(parms, "username");
        String telephone = CommonUtil.map_get_value(parms, "telephone");
        String password = CommonUtil.map_get_value(parms, "password");
        Condition condition = new Condition(User.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andLike("username", "%" + username + "%");
        criteria.andEqualTo("telephone", telephone);
        List<User> users = this.findByCondition(condition);

        if (users.size() > 0) {
            return new Result(false, StatusCode.ERROR, "该手机号已经注册");
        } else {
            User user  = new User();
            user.setAddTime(new Date());
            user.setUpdateTime(new Date());
            user.setDeleteStatus(false);
            user.setStatus(User.Status.VALID);
            user.setUsername(username);
            user.setTelephone(telephone);
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            user.setPassword(bCryptPasswordEncoder.encode(password));
            return new Result(true,StatusCode.OK,"注册成功");
        }
    }
}
