package com.community.huiju.service.impl;

import com.community.commom.mode.ResponseBean;
import com.community.huiju.dao.TaUserMapper;
import com.community.huiju.exception.WisdomException;
import com.community.huiju.model.TaUser;
import com.community.huiju.service.ITaUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * app 端用户业务类
 * @author weiximei
 */
@Service("iTaUserService")
public class TaUserServiceImpl implements ITaUserService {

    @Autowired
    private TaUserMapper taUserMapper;

    @Override
    public ResponseBean getByUserTypeCount() {
        ResponseBean responseBean = new ResponseBean();

        Map<String,Object> ownerMap = Maps.newHashMap();
        Integer ownerCount = taUserMapper.getByUserTypeCount(1);
        ownerMap.put("message","业主总数");
        ownerMap.put("count",ownerCount);
        Map<String,Object> tenantMap = Maps.newHashMap();
        Integer tenantCount = taUserMapper.getByUserTypeCount(2);
        tenantMap.put("message","租客总数");
        tenantMap.put("count",tenantCount);
        Map<String,Object> relationMap = Maps.newHashMap();
        Integer relationCount = taUserMapper.getByUserTypeCount(3);
        relationMap.put("message","家属总数");
        relationMap.put("count",relationCount);

        List<Map<String,Object>> mapList = Lists.newArrayList();
        mapList.add(ownerMap);
        mapList.add(tenantMap);
        mapList.add(relationMap);

        responseBean.addSuccess(mapList);
        return responseBean;
    }

    @Override
    public ResponseBean getAll(String userName, String loginName, String idCard, Integer pageNum, Integer pageSize) {
        ResponseBean responseBean = new ResponseBean();

        Page<TaUser> page = PageHelper.startPage(pageNum, pageSize);
        List<TaUser> userList = taUserMapper.selectAll(userName, loginName, idCard);

        Map<String, Object> map = Maps.newHashMap();
        map.put("item", userList);
        map.put("total", page.getTotal());
        map.put("pageNum", page.getPageNum());
        map.put("pageSize", page.getPageSize());
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean blacklistTaUser(List<Integer> ids) {
        ResponseBean responseBean = new ResponseBean();

        ids.forEach(e -> {
            TaUser user = taUserMapper.selectByPrimaryKey(e);
            if (null == user) {
                throw new WisdomException("用户不存在");
            }
            user.setStatus("2");
            taUserMapper.updateByPrimaryKeySelective(user);
        });

        responseBean.addSuccess("操作成功");
        return responseBean;
    }
}
