package com.huimv.receive.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.entity.User;
import com.huimv.receive.entity.dto.UserAccountDto;
import com.huimv.receive.mapper.UserMapper;
import com.huimv.receive.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject findUserAccount(String searchStr, int pageNum, int pageSize) {
        int start = (pageNum-1)*pageSize;

        if (StringUtils.isNotBlank(searchStr)) {
            List<UserAccountDto> userAccountList = userMapper.findUserAndAccountByUserName(searchStr, start,pageSize);
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("user_name",searchStr);
            int total = userMapper.selectCount(userQueryWrapper);
            JSONObject resultJo = new JSONObject();
            resultJo.put("code",10001);
            resultJo.put("totalElements",total);
            resultJo.put("data",userAccountList);
            return resultJo;
        }
        List<UserAccountDto> allUserAccountList = userMapper.findUserAndAccount(start,pageSize);
        Integer total = userMapper.selectCount(null);
        JSONObject resultJo = new JSONObject();
        resultJo.put("code",10001);
        resultJo.put("totalElements",total);
        resultJo.put("data",allUserAccountList);
        return resultJo;
    }

    /**
     * 封装对象
     */
    private Object packageUserAccountRelatedObj(Object[] userAccountObj) {
        JSONObject dataJo = new JSONObject();
        dataJo.put("id",userAccountObj[0].toString());
        dataJo.put("userName",userAccountObj[1].toString());
        if(userAccountObj[2] == null){
            dataJo.put("male","");
        }else{
            dataJo.put("male",userAccountObj[2].toString());
        }
        if(userAccountObj[3] == null){
            dataJo.put("birthday","");
        }else{
            dataJo.put("birthday",userAccountObj[3].toString());
        }
        if(userAccountObj[4] == null){
            dataJo.put("mobile","");
        }else{
            dataJo.put("mobile",userAccountObj[4].toString());
        }
        if(userAccountObj[5] == null){
            dataJo.put("address","");
        }else{
            dataJo.put("address",userAccountObj[5].toString());
        }
        if(userAccountObj[6] == null){
            dataJo.put("job","");
        }else{
            dataJo.put("job",userAccountObj[6].toString());
        }
        if(userAccountObj[7] == null){
            dataJo.put("remark","");
        }else{
            dataJo.put("remark",userAccountObj[7].toString());
        }
        if(userAccountObj[8] == null)
        {
            dataJo.put("userId","");
        }else{
            dataJo.put("userId",userAccountObj[8].toString());
        }
        if(userAccountObj[9] == null){
            dataJo.put("accountName","");
        }else{
            dataJo.put("accountName",userAccountObj[9].toString());
        }
        if(userAccountObj[10] == null)
        {dataJo.put("accountStatus","");

        }else{
            dataJo.put("accountStatus", Integer.parseInt(userAccountObj[10].toString()));
        }
        return dataJo;
    }


}
