package com.casic.missiles.modular.group.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.group.service.IGroupService;
import com.casic.missiles.modular.group.service.IGroupUserService;
import com.casic.missiles.modular.group.service.IGroupWellService;
import com.casic.missiles.modular.system.dao.GroupMapper;
import com.casic.missiles.modular.system.dto.BusWellInfoDto;
import com.casic.missiles.modular.system.dto.UserDTO;
import com.casic.missiles.modular.system.dto.WellDTO;
import com.casic.missiles.modular.system.model.Group;
import com.casic.missiles.modular.system.model.GroupUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lwh123
 * @since 2020-03-20
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {

    @Autowired
    private ICommonPermissionService permissionService;

    /**
     * 告警组用户管理
     */
    @Autowired
    private IGroupUserService groupUserService;

    /**
     * 告警组井管理
     */
    @Autowired
    private IGroupWellService groupWellService;

    /**
     * @param page    分页参数
     * @param wrapper 检索条件
     * @return
     */
    @Override
    public Page<Group> selectPage(Page<Group> page, Wrapper<Group> wrapper) {
        return super.selectPage(page, wrapper);
    }

    @Override
    public List<BusWellInfoDto> noGroupWellList(Page<BusWellInfoDto> page, DataScope dataScope, String keywords, String wellType) {
        List<BusWellInfoDto> busWellInfoDtos = this.baseMapper.noGroupWellList(page, dataScope, keywords, wellType);
        busWellInfoDtos.forEach(busWellInfoDto -> {
            busWellInfoDto.setWellTypeName(permissionService.getDictNameByCode(("sluicewellType"), busWellInfoDto.getWellType()));
        });
        return busWellInfoDtos;
    }

    @Override
    public List<Group> selectPageList(Page<Group> page, DataScope dataScope, String keywords, String maxLength) {

        List<Group> groupList = this.baseMapper.selectPageList(page, dataScope, keywords, maxLength);
        for (Group group : groupList) {
            List<String> wellListStr = groupWellService.findListByGroupid(group.getId().toString());
            List<String> userListStr = groupUserService.findUserByGroupid(group.getId().toString());

            String wellListStrs = "", userListStrs = "";
            if (ToolUtil.isNotEmpty(maxLength)) {
                long limits = Long.valueOf(maxLength);
                for (int i = 0; i < (wellListStr != null && wellListStr.size() > limits ? limits : wellListStr.size()); i++) {
                    wellListStrs += wellListStr.get(i) + ",";
                }
                for (int j = 0; j < (userListStr != null && userListStr.size() > limits ? limits : userListStr.size()); j++) {
                    userListStrs += userListStr.get(j) + ",";
                }
                userListStrs += (userListStr.size() > limits ? "等" : "");
                wellListStrs += (wellListStr.size() > limits ? "等" : "");
            } else {
                for (int i = 0; i < wellListStr.size(); i++) {
                    wellListStrs += wellListStr.get(i) + ",";
                }
                for (int j = 0; j < userListStr.size(); j++) {
                    userListStrs += userListStr.get(j) + ",";
                }
            }
            group.setUserNameList(ToolUtil.isEmpty(userListStrs) ? "" :
                    (userListStrs.indexOf("等") == -1 ? (userListStrs.indexOf(",") != -1?userListStrs.substring(0, userListStrs.length() - 1):userListStrs):
                            userListStrs.substring(0, userListStrs.length() - 2) + userListStrs.substring(userListStrs.length() - 1)));
            group.setWellNameList(ToolUtil.isEmpty(wellListStrs) ? "" :
                    wellListStrs.indexOf("等") == -1 ? (wellListStrs.indexOf(",") !=-1 ?wellListStrs.substring(0, wellListStrs.length() - 1):wellListStrs) :
                            wellListStrs.substring(0, wellListStrs.length() - 2) + wellListStrs.substring(wellListStrs.length() - 1));
        }
        return groupList;
    }

    @Override
    public List<UserDTO> userList(Page<UserDTO> page, DataScope dataScope, String groupId) {
        return this.baseMapper.userList(page, dataScope, groupId);
    }

    @Override
    public List<WellDTO> welllist(Page<WellDTO> page, DataScope dataScope, String groupId) {
        List<WellDTO> welllist = this.baseMapper.wellList(page, dataScope, groupId);
        for (WellDTO wellDTO : welllist) {
            wellDTO.setWellTypeName(permissionService.getDictNameByCode("sluicewellType", wellDTO.getWellType()));
        }
        return welllist;
    }
}
