package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.Utils.ImageUtil;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.thmz.FileUpload;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.mapper.thmz.FileUploadMapper;
import cn.hnthyy.thmz.mapper.thmz.UserMapper;
import cn.hnthyy.thmz.service.thmz.UserService;
import cn.hnthyy.thmz.vo.UserVo;
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.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @SuppressWarnings("all")
    @Autowired
    private UserMapper userMapper;
    @SuppressWarnings("all")
    @Autowired
    private FileUploadMapper fileUploadMapper;

    @Override
    public List<User> queryAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public List<User> queryUserPage(UserVo userVo) {
        return userMapper.selectUserPage(userVo);
    }

    @Override
    public int countUser(UserVo userVo) {
        return userMapper.countUser(userVo);
    }

    @Override
    public User queryUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User queryUserByCode(String userCode) {
        return userMapper.selectUserByCode(userCode);
    }

    @Transactional
    @Override
    public int saveUser(User user) {
        if(user!=null){
            if(StringUtils.isBlank(user.getPassword())){
                user.setPassword(Constants.DEFAULT_PASSWORD);
            }
           if(user.getCreateUser()==null){
                user.setCreateUser(Constants.DEFAULT_USER_ID);
           }
           if (user.getCreateTime()==null){
                user.setCreateTime(new Date());
           }
        }
        int num=userMapper.insertUser(user);
        if(StringUtils.isNotBlank(user.getUserName())){
            byte[] bytes=ImageUtil.generateImg(user.getUserName());
            FileUpload newFile = new FileUpload(user.getUserName()+".png", bytes, "image/jpeg");
            newFile.setSquareFile(bytes);
            newFile.setCreateUser(user.getId());
            fileUploadMapper.insertFileUpload(newFile);
            User updateUser = new User();
            updateUser.setId(user.getId());
            updateUser.setProfileImage(newFile.getId());
            updateUser.setUpdateUser(Constants.DEFAULT_USER_ID);
            updateUser.setUpdateTime(user.getCreateTime());
            userMapper.updateUser(updateUser);
        }
        return num;
    }

    @Override
    public int updateUser(User user) {
        if(user==null || user.getId()==null){
            return 0;
        }
        return userMapper.updateUser(user);
    }

    @Override
    public User queryUserByUserIdCode(String userIdCode) {
        return userMapper.selectUserByUserIdCode(userIdCode);
    }
}
