package io.hmit.modules.serviceOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.SubListForPageUtil;
import io.hmit.modules.serviceOrder.dao.UserDao;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.dto.UserDTO;
import io.hmit.modules.serviceOrder.entity.UserEntity;
import io.hmit.modules.serviceOrder.service.RoleUserService;
import io.hmit.modules.serviceOrder.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-27
 */
@Service
public class UserServiceImpl extends CrudServiceImpl<UserDao, UserEntity, UserDTO> implements UserService {

    @Autowired
    private RoleUserService roleUserService;

    @Autowired
    private UserDao userDao;

    @Override
    public QueryWrapper<UserEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void saveOrUpdateUserRole(Long userId, List<Long> roleIdList) {
        roleUserService.saveOrUpdate(userId,roleIdList);
    }

    @Override
    public UserEntity getUserByUserId(Long userId) {
        return baseDao.getUserByUserId(userId);
    }

    @Override
    public PageData<UserEntity> getOrganizationAllUsers(Map<String, Object> params, Long userId) {
        IPage<UserEntity> page = getPage(params, Constant.CREATE_DATE, false);
        List<Long> serviceIds = baseDao.getAllOrganizationStaff(userId);
        List<UserEntity> list = new ArrayList<>();
        for (Long id : serviceIds) {
            UserEntity userEntity = baseDao.selectById(id);
            list.add(userEntity);
        }
        page.setTotal(list.size());
        return getPageData(list, page.getTotal(), UserEntity.class);
    }

    @Override
    public PageData<UserDTO> getOrgStaff(Map<String, Object> params) {
        List<UserEntity> list = userDao.getOrgStaff(Long.parseLong(params.get("orgId").toString()));
        return getPageData(SubListForPageUtil.getSubList(params, list), list.size(), UserDTO.class);
    }

}
