ProjectJudgeServiceImpl.java 7.2 KB
package com.zhongzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongzhi.common.constant.SmsCode;
import com.zhongzhi.common.exception.HttpException;
import com.zhongzhi.common.utils.ExcelUtil;
import com.zhongzhi.common.utils.JwtUtil;
import com.zhongzhi.dto.administer.LoginDTO;
import com.zhongzhi.dto.judge.ProjectJudgePageDTO;
import com.zhongzhi.dto.projectJudge.ImportJudge;
import com.zhongzhi.model.ProjectJudgeDO;
import com.zhongzhi.dao.ProjectJudgeDAO;
import com.zhongzhi.model.ProjectReviewDO;
import com.zhongzhi.model.SmsCodeDO;
import com.zhongzhi.service.ProjectJudgeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongzhi.service.ProjectReviewService;
import com.zhongzhi.service.SmsCodeService;
import com.zhongzhi.vo.ExcelFieldVO;
import com.zhongzhi.vo.judge.JudgeInfoVO;
import com.zhongzhi.vo.judge.ProjectJudgePageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * <p>
 * 项目评审员 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-05-28
 */
@Service
public class ProjectJudgeServiceImpl extends ServiceImpl<ProjectJudgeDAO, ProjectJudgeDO> implements ProjectJudgeService {

    @Autowired
    private ProjectReviewService projectReviewService;

    @Autowired
    private SmsCodeService smsCodeService;

    @Override
    public IPage<ProjectJudgePageVO> getProjectJudgePage(ProjectJudgePageDTO projectJudgePageDTO) {
        IPage<ProjectJudgePageVO> iPage = this.baseMapper.getProjectJudgePage(new Page(projectJudgePageDTO.getPageNo(), projectJudgePageDTO.getPageSize()), projectJudgePageDTO.getUsername(), projectJudgePageDTO.getStatus());
        for (ProjectJudgePageVO record : iPage.getRecords()) {
            Integer count = projectReviewService.count(new QueryWrapper<ProjectReviewDO>()
                    .lambda()
                    .eq(ProjectReviewDO::getJudgeId, record.getId()));
            record.setAssignedQuantity(count);
        }
        return iPage;
    }

    @Override
    public void createProjectJudge(ProjectJudgeDO projectJudgeDO) {
        ProjectJudgeDO projectJudge = this.baseMapper.selectOne(new QueryWrapper<ProjectJudgeDO>()
                .lambda()
                .eq(ProjectJudgeDO::getTelephone, projectJudgeDO.getTelephone()));
        if (projectJudge != null) {
            throw new HttpException(10020);
        }

        this.baseMapper.insert(projectJudgeDO);
    }

    @Override
    public void updateProjectJudge(ProjectJudgeDO projectJudgeDO) {
        ProjectJudgeDO projectJudge = this.baseMapper.selectById(projectJudgeDO.getId());
        if (projectJudge != null) {
            if (projectJudge.getTelephone().equals(projectJudgeDO.getTelephone())) {
                this.baseMapper.updateById(projectJudgeDO);
            } else {
                ProjectJudgeDO p = this.baseMapper.selectOne(new QueryWrapper<ProjectJudgeDO>()
                        .lambda()
                        .eq(ProjectJudgeDO::getTelephone, projectJudgeDO.getTelephone()));
                if (p != null) {
                    throw new HttpException(10020);
                } else {
                    this.baseMapper.updateById(projectJudgeDO);
                }
            }
        }
    }

    @Override
    public JudgeInfoVO login(LoginDTO loginDTO) {
        JudgeInfoVO judgeInfoVO = new JudgeInfoVO();

        ProjectJudgeDO projectJudgeDO = this.baseMapper.selectOne(new QueryWrapper<ProjectJudgeDO>()
                .lambda()
                .eq(ProjectJudgeDO::getTelephone, loginDTO.getTelephone()));

        if (null != projectJudgeDO){

            if (null != projectJudgeDO.getPasswordFree() && projectJudgeDO.getPasswordFree() != 1){
                SmsCodeDO smsCodeDO = smsCodeService.getOneByTelePhone(loginDTO.getTelephone(), SmsCode.login, SmsCode.review);
                if (smsCodeDO == null) {
                    throw new HttpException(10025);
                }

                if (!smsCodeDO.getCode().equals(loginDTO.getCode())) {
                    throw new HttpException(10021);
                }

                smsCodeService.removeById(smsCodeDO.getId());

                if (projectJudgeDO == null) {
                    throw new HttpException(10024);
                }

                if (projectJudgeDO.getStatus() == 0) {
                    throw new HttpException(10024);
                }
            }
        }



        String token = JwtUtil.generateToken(projectJudgeDO.getId(), SmsCode.review);
        judgeInfoVO.setTelephone(projectJudgeDO.getTelephone());
        judgeInfoVO.setPosition(projectJudgeDO.getPosition());
        judgeInfoVO.setUnit(projectJudgeDO.getUnit());
        judgeInfoVO.setUsername(projectJudgeDO.getUsername());
        judgeInfoVO.setToken(token);
        return judgeInfoVO;
    }

    @Override
    public IPage<ProjectJudgePageVO> projectJudgesAssigned(ProjectJudgePageDTO projectJudgePageDTO) {
        IPage<ProjectJudgePageVO> iPage = this.baseMapper.projectJudgesAssigned(new Page(projectJudgePageDTO.getPageNo(), projectJudgePageDTO.getPageSize()),
                projectJudgePageDTO.getMatchId(), projectJudgePageDTO.getProjectGroup(), projectJudgePageDTO.getProjectSchedule());
        if (iPage.getRecords() != null && iPage.getRecords().size() > 0) {
            for (ProjectJudgePageVO record : iPage.getRecords()) {
                Integer count = projectReviewService.selectCount(record.getId(), projectJudgePageDTO.getMatchId());
                record.setAssignedQuantity(count);
                Integer notReviewNum = projectReviewService.selectNotReviewCount(record.getId(), projectJudgePageDTO.getMatchId());
                record.setNotReviewedNum(notReviewNum);
            }
        }
        return iPage;
    }

    @Override
    public void importJudge(MultipartFile file) {
        List<ImportJudge> list = ExcelUtil.readExcel(ImportJudge.class, file);
        if (list != null) {
            for (ImportJudge importJudge : list) {
                ProjectJudgeDO projectJudgeDO = new ProjectJudgeDO();
                BeanUtils.copyProperties(importJudge, projectJudgeDO);
                this.baseMapper.insert(projectJudgeDO);
            }
        }
    }

    @Override
    public void downloadTemplate() {
        List<ExcelFieldVO> list = ExcelUtil.getField(ImportJudge.class);
        ExcelUtil.writeExcel(list, new ArrayList<>());
    }

    @Override
    public void exportProjectJudge() {
        List<ExcelFieldVO> field = ExcelUtil.getField(ImportJudge.class);
        List<ProjectJudgeDO> list = this.baseMapper.selectList(new QueryWrapper<>());
        List<Map> data = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ProjectJudgeDO projectJudgeDO : list) {
                data.add(JSON.parseObject(JSON.toJSONString(projectJudgeDO), Map.class));
            }
        }
        ExcelUtil.writeExcel(field, data);
    }
}