CompanyDictServiceImpl.java 20.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
package com.subsidy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.common.ResponseData;
import com.subsidy.common.configure.VODConfig;
import com.subsidy.common.constant.VodConstant;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.administer.OperatorsDTO;
import com.subsidy.dto.company.AddCompanyDTO;
import com.subsidy.dto.company.GetCompanyMembersDTO;
import com.subsidy.mapper.AdministerMapper;
import com.subsidy.mapper.ClassDictMapper;
import com.subsidy.mapper.ClassHourDictMapper;
import com.subsidy.mapper.ClassTypeDictMapper;
import com.subsidy.mapper.CompanyDictMapper;
import com.subsidy.mapper.CompanyFieldMappingMapper;
import com.subsidy.mapper.CourseDictMapper;
import com.subsidy.mapper.DepartmentDictMapper;
import com.subsidy.mapper.FieldDictMapper;
import com.subsidy.mapper.JobDictMapper;
import com.subsidy.mapper.MemberMapper;
import com.subsidy.mapper.RankDictMapper;
import com.subsidy.mapper.RoleAdministerMappingMapper;
import com.subsidy.mapper.RotationImgDictMapper;
import com.subsidy.mapper.VodPlayHistoryMapper;
import com.subsidy.mapper.*;
import com.subsidy.model.*;
import com.subsidy.service.CompanyDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.DateFormatUtil;
import com.subsidy.util.EhCacheUtil;
import com.subsidy.util.VodUtil;
import com.subsidy.util.excel.ExcelUtil;
import com.subsidy.util.websocket.WebSocketUtil;
import com.subsidy.vo.administer.OperatorsVO;
import com.subsidy.vo.classdict.ClassSettingsVO;
import com.subsidy.vo.classdict.SystemSettings;
import com.subsidy.vo.company.CDNStatDetailsVO;
import com.subsidy.vo.company.DataOverviewVO;
import com.subsidy.vo.company.GetAllCompanyVO;
import com.subsidy.vo.company.GetCompanyMembersVO;
import com.tencentcloudapi.vod.v20180717.models.StatDataItem;
import org.apache.commons.collections.CollectionUtils;
import com.subsidy.vo.company.MemberSummaryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 企业字典表 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-10-25
 */
@Service
public class CompanyDictServiceImpl extends ServiceImpl<CompanyDictMapper, CompanyDictDO> implements CompanyDictService {


    @Autowired
    private AdministerMapper administerMapper;

    @Autowired
    private RoleAdministerMappingMapper roleAdministerMappingMapper;

    @Autowired
    private DepartmentDictMapper departmentDictMapper;

    @Autowired
    private FieldDictMapper fieldDictMapper;

    @Autowired
    private CompanyFieldMappingMapper companyFieldMappingMapper;

    @Autowired
    private JobDictMapper jobDictMapper;

    @Autowired
    private ClassTypeDictMapper classTypeDictMapper;

    @Autowired
    private ClassHourDictMapper classHourDictMapper;

    @Autowired
    private RankDictMapper rankDictMapper;

    @Autowired
    private RotationImgDictMapper rotationImgDictMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ClassDictMapper classDictMapper;

    @Autowired
    private CourseDictMapper courseDictMapper;

    @Autowired
    private VodPlayHistoryMapper vodPlayHistoryMapper;

    @Autowired
    private ClassMemberMappingMapper classMemberMappingMapper;

    @Autowired
    private VODConfig vodConfig;

    public IPage<OperatorsVO> operators(OperatorsDTO operatorsDTO) {
        Page pager = new Page(operatorsDTO.getPageNum(), operatorsDTO.getPageSize());

        AdministerDO administerDO = administerMapper.selectById(operatorsDTO.getId());

        //判断该用户的角色
        Integer count = roleAdministerMappingMapper.selectCount(new QueryWrapper<RoleAdministerMappingDO>()
                .lambda()
                .eq(RoleAdministerMappingDO::getAdministerId, operatorsDTO.getId())
                .eq(RoleAdministerMappingDO::getRoleId, 1));

        if (count > 0) {
            administerDO.setCompanyId(null);
        }
        IPage page = this.baseMapper.operators(pager, operatorsDTO.getCompanyName(), operatorsDTO.getFieldId(), administerDO.getCompanyId(), 2, operatorsDTO.getSuperviseName());
        List<OperatorsVO> operatorsVOS = page.getRecords();
        for (OperatorsVO operatorsVO : operatorsVOS) {
            //查看公司所在行业
            List<FieldDictDO> fieldDictDOS = fieldDictMapper.queryCompanyFields(operatorsVO.getCompanyId());
            operatorsVO.setField(fieldDictDOS);
        }
        page.setRecords(operatorsVOS);
        return page;
    }

    public String addOperator(AddCompanyDTO addCompanyDTO) {

        Integer count = this.baseMapper.selectCount(new QueryWrapper<CompanyDictDO>()
                .lambda()
                .eq(CompanyDictDO::getCompanyName, addCompanyDTO.getAccountName()));

        if (count > 0) {
            throw new HttpException(20002);
        }

        Integer count1 = administerMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addCompanyDTO.getAccountName()));

        if (count1 > 0) {
            throw new HttpException(20002);
        }

        CompanyDictDO companyDictDO = new CompanyDictDO();
        BeanUtils.copyProperties(addCompanyDTO, companyDictDO);
        this.baseMapper.insert(companyDictDO);

        AdministerDO administerDO = new AdministerDO();
        administerDO.setAccountName(addCompanyDTO.getAccountName());
        administerDO.setCompanyId(companyDictDO.getId());
        administerDO.setPassword("admin123");
        administerDO.setStatus("1");
        //administerDO.setRole(1);
        administerMapper.insert(administerDO);

        RoleAdministerMappingDO roleAdministerMappingDO = new RoleAdministerMappingDO();
        roleAdministerMappingDO.setAdministerId(administerDO.getId());
        roleAdministerMappingDO.setRoleId(2L);
        roleAdministerMappingMapper.insert(roleAdministerMappingDO);

        //添加默认部门  名称为公司名称
        DepartmentDictDO departmentDictDO = new DepartmentDictDO();
        departmentDictDO.setCompanyId(companyDictDO.getId());
        departmentDictDO.setDepartmentName(companyDictDO.getCompanyName());
        departmentDictDO.setFullName(companyDictDO.getCompanyName());
        //departmentDictDO.setParentId(1L);
        departmentDictMapper.insert(departmentDictDO);

        //class_hour_dict
        ClassHourDictDO classHourDictDO2 = new ClassHourDictDO();
        classHourDictDO2.setCompanyId(companyDictDO.getId());
        classHourDictDO2.setStatus(0);
        classHourDictDO2.setClassHour(6);

        classHourDictDO2.setInterruptStatus(0);
        classHourDictDO2.setInterrupt(15);

        classHourDictDO2.setRepeatStatus(0);
        classHourDictDO2.setRepeatTime(3);
        //redisUtil.set(RedisPrefixConstant.SUBSIDY_SETTINGS_PREFIX + companyDictDO.getId(), classHourDictDO2);
        classHourDictMapper.insert(classHourDictDO2);

        //行业
        List<Long> fields = addCompanyDTO.getField();
        for (Long lg : fields) {
            CompanyFieldMappingDO companyFieldMappingDO = new CompanyFieldMappingDO();
            companyFieldMappingDO.setCompanyId(companyDictDO.getId());
            companyFieldMappingDO.setFieldId(lg);
            companyFieldMappingMapper.insert(companyFieldMappingDO);
        }

        //初始化平台的岗位到企业的岗位
        List<JobDictDO> jobDictDOS = jobDictMapper.selectList(new QueryWrapper<JobDictDO>()
                .lambda()
                .isNull(JobDictDO::getCompanyId));
        for (JobDictDO jobDictDO : jobDictDOS) {
            jobDictDO.setCompanyId(companyDictDO.getId());
            jobDictMapper.insert(jobDictDO);
        }

        //初始化运营者的职级
        List<RankDictDO> rankDictDOS = rankDictMapper.selectList(new QueryWrapper<RankDictDO>()
                .lambda()
                .isNull(RankDictDO::getCompanyId));
        for (RankDictDO rankDictDO : rankDictDOS) {
            rankDictDO.setCompanyId(companyDictDO.getId());
            rankDictMapper.insert(rankDictDO);
        }

        //初始化轮播图
        List<RotationImgDictDO> rotationImgDictDOS = rotationImgDictMapper.selectList(new QueryWrapper<RotationImgDictDO>()
                .lambda()
                .isNull(RotationImgDictDO::getCompanyId));
        for (RotationImgDictDO rotationImgDictDO : rotationImgDictDOS) {
            rotationImgDictDO.setCompanyId(companyDictDO.getId());
            rotationImgDictMapper.insert(rotationImgDictDO);
        }

        //班级类型
        ClassTypeDictDO classTypeDictDO = new ClassTypeDictDO();
        classTypeDictDO.setClassType("企业内训");
        classTypeDictDO.setCompanyId(companyDictDO.getId());
        classTypeDictMapper.insert(classTypeDictDO);

        classTypeDictDO.setClassType("社会化、学徒制");
        classTypeDictMapper.insert(classTypeDictDO);

        classTypeDictDO.setClassType("补贴培训");
        classTypeDictMapper.insert(classTypeDictDO);
        return ConstantUtils.ADD_SUCCESS;

    }

    public String deleteAdminister(CompanyDictDO companyDictDO) {
        this.baseMapper.deleteById(companyDictDO.getId());

        administerMapper.delete(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getCompanyId, companyDictDO.getId()));

        //redisUtil.del(RedisPrefixConstant.SUBSIDY_SETTINGS_PREFIX + companyDictDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateAdminister(AddCompanyDTO addCompanyDTO) {

        CompanyDictDO companyDictDO = new CompanyDictDO();
        BeanUtils.copyProperties(addCompanyDTO, companyDictDO);
        this.baseMapper.updateById(companyDictDO);

        if (null != addCompanyDTO.getField() && addCompanyDTO.getField().size() > 0) {
            companyFieldMappingMapper.delete(new QueryWrapper<CompanyFieldMappingDO>()
                    .lambda()
                    .eq(CompanyFieldMappingDO::getCompanyId, addCompanyDTO.getId()));

            List<Long> longs = addCompanyDTO.getField();
            for (Long lg : longs) {
                CompanyFieldMappingDO companyFieldMappingDO = new CompanyFieldMappingDO();
                companyFieldMappingDO.setFieldId(lg);
                companyFieldMappingDO.setCompanyId(addCompanyDTO.getId());
                companyFieldMappingMapper.insert(companyFieldMappingDO);
            }
        }

        //查找企业管理员
        AdministerDO administerDO = roleAdministerMappingMapper.getAdminister(addCompanyDTO.getId());
        if (null != administerDO) {
            if (StringUtils.isNotBlank(addCompanyDTO.getAccountName())) {
                administerDO.setAccountName(addCompanyDTO.getAccountName());
            }
            if (StringUtils.isBlank(addCompanyDTO.getPassword())) {
                addCompanyDTO.setPassword(addCompanyDTO.getPassword());
            }
            administerMapper.updateById(administerDO);
        }
        return ConstantUtils.SET_SUCCESS;
    }

    public String updateCompanySettings(CompanyDictDO companyDictDO) {
        this.baseMapper.updateById(companyDictDO);

        CompanyDictDO companyDictDO1 = this.baseMapper.selectById(companyDictDO.getId());
        SystemSettings systemSettings = new SystemSettings();
        BeanUtils.copyProperties(companyDictDO1, systemSettings);

        List<ClassMemberMappingDO> list = classMemberMappingMapper.getCLassMemberByCompanyId(companyDictDO.getId());
        if (CollectionUtils.isNotEmpty(list)) {
            for (ClassMemberMappingDO classMemberMappingDO : list) {
                WebSocketSession webSocketSession = WebSocketUtil.webSocketMap.get(classMemberMappingDO.getMemberId());
                if (null != webSocketSession && webSocketSession.isOpen()) {
                    try {
                        List<ClassSettingsVO> classSettings = classDictMapper.getClassSettings(classMemberMappingDO.getMemberId());
                        systemSettings.setClassSettingsVOS(classSettings);
                        String data = JSONObject.toJSONString(ResponseData.generateCreatedResponse(0, systemSettings), SerializerFeature.WriteMapNullValue);
                        webSocketSession.sendMessage(new TextMessage(data));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return ConstantUtils.SET_SUCCESS;
    }

    public CompanyDictDO getCompanyInfo(CompanyDictDO companyDictDO) {
        return this.baseMapper.selectById(companyDictDO.getId());
    }

    public List<GetAllCompanyVO> getAllCompany(CompanyDictDO companyDictDO) {
        return this.baseMapper.getAllCompany(companyDictDO);
    }

    public IPage<GetCompanyMembersVO> getCompanyMembers(GetCompanyMembersDTO getCompanyMembersDTO) {
        Page pager = new Page(getCompanyMembersDTO.getPageNum(), getCompanyMembersDTO.getPageSize());
        return this.baseMapper.getCompanyMembers(pager, getCompanyMembersDTO.getCompanyId(), getCompanyMembersDTO.getUserName());
    }

    public void exportCompanyMembers(GetCompanyMembersDTO getCompanyMembersDTO) {
        Page pager = new Page(1, -1);
        List<GetCompanyMembersVO> getCompanyMembersVOS = this.baseMapper.getCompanyMembers(pager, getCompanyMembersDTO.getCompanyId(), getCompanyMembersDTO.getUserName()).getRecords();
        ExcelUtil.writeExcel(getCompanyMembersVOS, GetCompanyMembersVO.class);
    }

    @Override
    public DataOverviewVO getCompanyDataOverview() {
        DataOverviewVO dataOverviewVO = new DataOverviewVO();
        int companyNum = this.baseMapper.selectCount(new QueryWrapper<>());
        /* 机构数量 */
        dataOverviewVO.setCompanyNum(companyNum);

        List<MemberDO> memberList = memberMapper.selectList(new QueryWrapper<>());
        if (CollectionUtils.isNotEmpty(memberList)) {
            int realNameNum = (int) memberList.stream().filter(m -> StringUtils.isNotBlank(m.getIdCard())).count();
            /* 注册人数 */
            dataOverviewVO.setRegistrantsNum(memberList.size());
            /* 实名注册人数 */
            dataOverviewVO.setRealNameNum(realNameNum);
        }

        /* 培训项目数量 */
        int classNum = classDictMapper.getClassCount();
        dataOverviewVO.setClassNum(classNum);

        List<CourseDictDO> courseList = courseDictMapper.selectList(new QueryWrapper<>());
        if (CollectionUtils.isNotEmpty(courseList)) {
            /* 资源数量 */
            dataOverviewVO.setCourseNum(courseList.size());

            /* 专项资源数量 */
            int subsidyCourseNum = (int) courseList.stream().filter(courseDictDO -> StringUtils.isNotBlank(courseDictDO.getCourseType()) &&
                    courseDictDO.getCourseType().equals("补贴培训")).count();
            dataOverviewVO.setSubsidyCourseNum(subsidyCourseNum);
        }

        /* 累计学习时长(小时) */
        double studyTotal = vodPlayHistoryMapper.getStudyTotal();
        if (studyTotal > 0) {
            dataOverviewVO.setStudyTotal(new BigDecimal(studyTotal).divide(new BigDecimal(Math.pow(60, 2)), 2, BigDecimal.ROUND_UP).doubleValue());
        }

        /* 专项资源时(小时) */
        double subsidyStudyTotal = vodPlayHistoryMapper.getSubsidyStudyTotal();
        if (subsidyStudyTotal > 0) {
            dataOverviewVO.setSubsidyStudyTotal(new BigDecimal(subsidyStudyTotal).divide(new BigDecimal(Math.pow(60, 2)), 2, BigDecimal.ROUND_UP).doubleValue());
        }

        /* 学习过程记录人次  */
        int studyHistoryNum = vodPlayHistoryMapper.getStudyHistoryNum();
        dataOverviewVO.setStudyHistoryNum(studyHistoryNum);
        return dataOverviewVO;
    }

    public MemberSummaryVO memberSummary(GetCompanyMembersDTO getCompanyMembersDTO) {

        MemberSummaryVO memberSummaryVO = new MemberSummaryVO();

        //总数
        Integer total = memberMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getCompanyId, getCompanyMembersDTO.getCompanyId()));
        memberSummaryVO.setTotal(total);

        //短信验证
        Integer smsCnt = memberMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getCompanyId, getCompanyMembersDTO.getCompanyId())
                .isNull(MemberDO::getFirstLogin));
        memberSummaryVO.setSmsCnt(smsCnt);

        memberSummaryVO.setImgCnt(total - smsCnt);
        return memberSummaryVO;
    }

    @Override
    public List<CDNStatDetailsVO> getCDNBandwidthStatDetails() {
        /*
         * 根据今天时间查询七天前的数据
         * 先从缓存中获取数据,如果缓存中没有数据,从远程请求中获取, 避免频繁请求
         * 缓存28800秒(8小时)失效,重新远程请求更新数据
         * 返回数据单位:bps, 转换成Mbps, 1Mbps = 1000000bps
         */
        List<CDNStatDetailsVO> data = new ArrayList<>();
        if (EhCacheUtil.getEhCache(VodConstant.CDN_STAT_DETAILS_CACHE_NAME, VodConstant.BANDWIDTH_CACHE_KEY) != null) {
            return (List<CDNStatDetailsVO>) EhCacheUtil.getEhCache(VodConstant.CDN_STAT_DETAILS_CACHE_NAME, VodConstant.BANDWIDTH_CACHE_KEY);
        } else {
            String startDate = LocalDateTime.now().minusDays(6).withNano(0) + "+08:00";
            String endDate = LocalDateTime.now().withNano(0) + "+08:00";
            StatDataItem[] statDataItems = VodUtil.DescribeCDNStatDetails(vodConfig, VodConstant.BANDWIDTH, startDate, endDate);
            for (StatDataItem statDataItem : statDataItems) {
                CDNStatDetailsVO sd = new CDNStatDetailsVO();
                LocalDateTime localDateTime = LocalDateTime.parse(statDataItem.getTime().replace("+08:00", ""));
                Date date = DateFormatUtil.localDateTimeToDate(localDateTime);
                sd.setTime(DateFormatUtil.format(date, DateFormatUtil.FMT_sdf_yMd));
                BigDecimal bigDecimal = new BigDecimal(statDataItem.getValue()).divide(new BigDecimal(1000000));
                sd.setValue(bigDecimal.setScale(2, BigDecimal.ROUND_UP));
                data.add(sd);
            }
            EhCacheUtil.putEhCache(VodConstant.CDN_STAT_DETAILS_CACHE_NAME, VodConstant.BANDWIDTH_CACHE_KEY, data);
            return data;
        }
    }

    @Override
    public List<CDNStatDetailsVO> getCDNFluxStatDetails() {
        /*
         * 根据今天时间查询七天前的数据
         * 先从缓存中获取数据,如果缓存中没有数据,从远程请求中获取,避免频繁请求
         * 缓存28800秒(8小时)失效,重新远程请求更新数据
         * 返回数据单位:B, 转换成GB
         */
        List<CDNStatDetailsVO> data = new ArrayList<>();
        if (EhCacheUtil.getEhCache(VodConstant.CDN_STAT_DETAILS_CACHE_NAME, VodConstant.FLUX_CACHE_KEY) != null) {
            return (List<CDNStatDetailsVO>) EhCacheUtil.getEhCache(VodConstant.CDN_STAT_DETAILS_CACHE_NAME, VodConstant.FLUX_CACHE_KEY);
        } else {
            String startDate = LocalDateTime.now().minusDays(6).withNano(0) + "+08:00";
            String endDate = LocalDateTime.now().withNano(0) + "+08:00";
            StatDataItem[] statDataItems = VodUtil.DescribeCDNStatDetails(vodConfig, VodConstant.FLUX, startDate, endDate);
            for (StatDataItem statDataItem : statDataItems) {
                CDNStatDetailsVO sd = new CDNStatDetailsVO();
                LocalDateTime localDateTime = LocalDateTime.parse(statDataItem.getTime().replace("+08:00", ""));
                Date date = DateFormatUtil.localDateTimeToDate(localDateTime);
                sd.setTime(DateFormatUtil.format(date, DateFormatUtil.FMT_sdf_yMd));
                BigDecimal bigDecimal = new BigDecimal(statDataItem.getValue()).divide(new BigDecimal(Math.pow(1000, 3)));
                sd.setValue(bigDecimal.setScale(2, BigDecimal.ROUND_UP));
                data.add(sd);
            }
            EhCacheUtil.putEhCache(VodConstant.CDN_STAT_DETAILS_CACHE_NAME, VodConstant.FLUX_CACHE_KEY, data);
            return data;
        }
    }
}