package ink.wgink.module.map.service.grid.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.user.IUserBaseService;
import ink.wgink.module.map.pojo.dtos.grid.GridRelationDTO;
import ink.wgink.module.map.pojo.dtos.grid.GridUserDTO;
import ink.wgink.module.map.service.grid.IGridRelationService;
import ink.wgink.module.map.service.grid.IGridUserService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: GridUserServiceImpl
 * @Description: 网格用户
 * @Author: wanggeng
 * @Date: 2021/10/20 5:43 下午
 * @Version: 1.0
 */
@Service
public class GridUserServiceImpl extends DefaultBaseService implements IGridUserService {

    @Autowired
    private IUserBaseService userBaseService;
    @Autowired
    private IGridRelationService gridRelationService;

    @Override
    public void saveOfMine(String token, String gridId) {
        String userId = getAppTokenUser(token).getId();
        GridRelationDTO gridRelationDTO = gridRelationService.getByGridIdAndRelationId(gridId, userId);
        if (gridRelationDTO != null) {
            throw new SearchException("已经添加网格");
        }
        gridRelationService.save(token, gridId, Arrays.asList(userId));
    }

    @Override
    public void delete(String gridId, List<String> userIds) {
        gridRelationService.delete(gridId, userIds);
    }

    @Override
    public void deleteOfMine(String token, String gridId) {
        String userId = getAppTokenUser(token).getId();
        gridRelationService.delete(gridId, userId);
    }

    @Override
    public void update(String gridId, IdsVO idsVO) {
        // 删除全部关联关系
        gridRelationService.delete(gridId);
        List<String> userIds = idsVO.getIds();
        if (userIds.isEmpty()) {
            return;
        }
        gridRelationService.save(gridId, userIds);
    }

    @Override
    public List<String> listUserId(String gridId, Map<String, Object> params) {
        return gridRelationService.listRelationId(gridId);
    }

    @Override
    public List<GridUserDTO> list(String gridId, Map<String, Object> params) {
        List<GridRelationDTO> gridRelationDTOs = gridRelationService.listByGridId(gridId);
        if (gridRelationDTOs.isEmpty()) {
            return new ArrayList();
        }
        Set<String> userIdSet = new HashSet<>();
        for (GridRelationDTO gridRelationDTO : gridRelationDTOs) {
            userIdSet.add(gridRelationDTO.getRelationId());
        }
        List<UserDTO> userDTOs = userBaseService.listByUserIds(new ArrayList<>(userIdSet));
        if (userDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        List<GridUserDTO> gridUserDTOs = new ArrayList<>();
        for (UserDTO userDTO : userDTOs) {
            GridUserDTO gridUserDTO = new GridUserDTO();
            gridUserDTO.setGridId(gridId);
            BeanUtils.copyProperties(userDTO, gridUserDTO);
            for (GridRelationDTO gridRelationDTO : gridRelationDTOs) {
                if (StringUtils.equals(gridUserDTO.getUserId(), gridRelationDTO.getRelationId())) {
                    gridUserDTO.setGmtCreate(gridRelationDTO.getGmtCreate());
                    break;
                }
            }
            gridUserDTOs.add(gridUserDTO);
        }
        return gridUserDTOs;
    }

    @Override
    public SuccessResultList<List<GridUserDTO>> listPage(String gridId, ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<GridUserDTO> gridUserDTOs = list(gridId, page.getParams());
        PageInfo<GridUserDTO> pageInfo = new PageInfo<>(gridUserDTOs);
        return new SuccessResultList<>(gridUserDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

}
