package com.bonus.canteen.core.android.service.impl;

import com.bonus.canteen.core.android.dto.AppDTO;
import com.bonus.canteen.core.android.mapper.UserMapper;
import com.bonus.canteen.core.android.service.UserService;
import com.bonus.canteen.core.android.vo.UserInfoVo;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.utils.SM4EncryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;
    /**
     * 获取人员信息
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getUserInfo(AppDTO dto) {
        List<UserInfoVo> list = mapper.getUserInfo(dto);
        List<UserInfoVo> moneyList = mapper.getMoneyList();
        if (!list.isEmpty()){
            list.forEach(userInfoVo -> {
                if (userInfoVo.getPhone().length() != 11){
                    userInfoVo.setPhone(SM4EncryptUtils.sm4Decrypt(userInfoVo.getPhone()));
                }
                UserInfoVo money = moneyList.stream().filter(moneyVo -> moneyVo.getUserId().equals(userInfoVo.getUserId())).findFirst().orElse(null);
                if (money != null){
                    userInfoVo.setPersonalWalletBalance(money.getPersonalWalletBalance());
                    userInfoVo.setSubsidiesBalance(money.getSubsidiesBalance());
                }
            });
            System.out.println(list);
        }else {
            return AjaxResult.success(new ArrayList<UserInfoVo>());
        }
        return AjaxResult.success(list);
    }

}
