package cn.com.ctop.common.service.impl;

import cn.com.ctop.common.entity.UserAllocation;
import cn.com.ctop.common.mapper.UserAllocationMapper;
import cn.com.ctop.common.service.IUserAllocationService;
import cn.com.ctop.common.utils.Check;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户分配
 * @Author: jeecg-boot
 * @Date: 2019-08-08
 * @Version: V1.0
 */
@Service
public class UserAllocationServiceImpl extends ServiceImpl<UserAllocationMapper, UserAllocation> implements IUserAllocationService {
    @Resource
    private UserAllocationMapper userAllocationMapper;

    @Override
    public IPage<UserAllocation> queryListWithPermission(Integer pageSize, Integer pageNo) {
        Page<UserAllocation> page = new Page<>(pageNo, pageSize);
        //就是调用这个QueryGenerator.installAuthJdbc方法获取权限sql
        String sql = QueryGenerator.installAuthJdbc(UserAllocation.class);
        return this.baseMapper.queryListWithPermission(page, sql);
    }

    @Override
    public Map<Long, List<JSONObject>> getAccountListByProjectList(List<JSONObject> projectList,String designUserId) {
        Map<Long, List<JSONObject>> map = new HashMap<>();
        List list = new ArrayList();
        for (int i = 0; i < projectList.size(); i++) {
            JSONObject jsonObject = projectList.get(i);
            if (!Check.isNull(jsonObject)) {
                list.add(jsonObject.getLong("projectId"));
                map.put(jsonObject.getLong("projectId"), new ArrayList<>());
            }
        }
        List<JSONObject> jsonList = userAllocationMapper.getAccountListByProjectList(list,designUserId);
        for (int i = 0; i < jsonList.size(); i++) {
            JSONObject jsonObject = jsonList.get(i);
            if (!Check.isNull(jsonObject)) {
                Long projectId = jsonObject.getLong("projectId");
                List<JSONObject> jsonObjects = map.get(projectId);
                jsonObjects.add(jsonObject);
            }
        }
        return map;
    }
}
