puzhibing
2023-07-12 aa43a92c7ec9053dbaef92fe5ccb3011b670442c
cloud-server-competition/src/main/java/com/dsh/competition/service/impl/ParticipantServiceImpl.java
@@ -1,10 +1,28 @@
package com.dsh.competition.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.competition.entity.Participant;
import com.dsh.competition.feignclient.account.StudentClient;
import com.dsh.competition.feignclient.account.model.Student;
import com.dsh.competition.feignclient.course.CoursePackagePaymentClient;
import com.dsh.competition.mapper.ParticipantMapper;
import com.dsh.competition.model.AddParticipant;
import com.dsh.competition.model.EditParticipant;
import com.dsh.competition.model.ParticipantVo;
import com.dsh.competition.model.SaveParticipant;
import com.dsh.competition.service.IParticipantService;
import com.dsh.competition.util.JuHeUtil;
import com.dsh.competition.util.ResultUtil;
import com.dsh.competition.util.ToolUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author zhibing.pu
@@ -12,4 +30,150 @@
 */
@Service
public class ParticipantServiceImpl extends ServiceImpl<ParticipantMapper, Participant> implements IParticipantService {
    @Resource
    private CoursePackagePaymentClient coursePackagePaymentClient;
    @Resource
    private StudentClient studentClient;
    /**
     * 添加参赛人员信息
     * @param uid
     * @param addParticipant
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil addParticipant(Integer uid, AddParticipant addParticipant) throws Exception {
        Participant one = this.getOne(new QueryWrapper<Participant>().eq("appUserId", uid).eq("phone", addParticipant.getPhone()).eq("state", 1));
        if(null != one){
            return ResultUtil.error("电话号码重复");
        }
        if(ToolUtil.isNotEmpty(addParticipant.getName()) && ToolUtil.isNotEmpty(addParticipant.getIdcard())){
            Boolean aBoolean = JuHeUtil.idcardAuthentication(addParticipant.getIdcard(), addParticipant.getName());
            if(!aBoolean){
                return ResultUtil.error("身份证和姓名不匹配");
            }
        }
        Participant participant = new Participant();
        BeanUtils.copyProperties(addParticipant, participant);
        participant.setAppUserId(uid);
        participant.setState(1);
        participant.setInsertTime(new Date());
        this.save(participant);
        return ResultUtil.success();
    }
    /**
     * 获取参赛人员列表
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<ParticipantVo> queryParticipantList(Integer uid) throws Exception {
        List<Participant> list = this.list(new QueryWrapper<Participant>().eq("appUserId", uid).eq("state", 1));
        List<ParticipantVo> listVo = new ArrayList<>();
        SimpleDateFormat sdf_year = new SimpleDateFormat("yyyy");
        for (Participant participant : list) {
            ParticipantVo participantVo = new ParticipantVo();
            participantVo.setId(participant.getId());
            participantVo.setName(participant.getName());
            participantVo.setIdcard(participant.getIdcard());
            Integer age = Integer.valueOf(sdf_year.format(new Date())) - Integer.valueOf(sdf_year.format(participant.getBirthday()));
            participantVo.setAge(age);
            Student student = studentClient.queryStudentByPhone(participant.getPhone());
            if(null != student){
                Integer integer = coursePackagePaymentClient.queryResidueClassHour(student.getId());
                participantVo.setResidueClassHour(integer);
            }else{
                participantVo.setResidueClassHour(0);
            }
            listVo.add(participantVo);
        }
        return listVo;
    }
    /**
     * 修改参赛人员信息
     * @param editParticipant
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil editParticipant(Integer uid, EditParticipant editParticipant) throws Exception {
        Participant one = this.getOne(new QueryWrapper<Participant>().eq("appUserId", uid).eq("phone", editParticipant.getPhone()).eq("state", 1));
        if(null != one && one.getId().compareTo(editParticipant.getId()) != 0){
            return ResultUtil.error("电话号码重复");
        }
        if(ToolUtil.isNotEmpty(editParticipant.getName()) && ToolUtil.isNotEmpty(editParticipant.getIdcard())){
            Boolean aBoolean = JuHeUtil.idcardAuthentication(editParticipant.getIdcard(), editParticipant.getName());
            if(!aBoolean){
                return ResultUtil.error("身份证和姓名不匹配");
            }
        }
        Participant participant = this.getById(editParticipant.getId());
        participant.setHeight(editParticipant.getHeight());
        participant.setWeight(editParticipant.getWeight());
        participant.setPhone(editParticipant.getPhone());
        this.updateById(participant);
        return ResultUtil.success();
    }
    /**
     * 删除参赛人员信息
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil delParticipant(Integer id) throws Exception {
        Participant participant = this.getById(id);
        participant.setState(3);
        this.updateById(participant);
        return ResultUtil.success();
    }
    @Override
    public void saveParticipant(SaveParticipant saveParticipant) throws Exception {
        Participant one = this.getOne(new QueryWrapper<Participant>().eq("appUserId", saveParticipant.getAppUserId()).eq("phone", saveParticipant.getPhone()).eq("state", 1));
        if(null != one){
            if(ToolUtil.isNotEmpty(saveParticipant.getName())){
                one.setName(saveParticipant.getName());
            }
            if(ToolUtil.isNotEmpty(saveParticipant.getBirthday())){
                one.setBirthday(saveParticipant.getBirthday());
            }
            if(ToolUtil.isNotEmpty(saveParticipant.getGender())){
                one.setGender(saveParticipant.getGender());
            }
            if(ToolUtil.isNotEmpty(saveParticipant.getHeight())){
                one.setHeight(saveParticipant.getHeight());
            }
            if(ToolUtil.isNotEmpty(saveParticipant.getWeight())){
                one.setWeight(saveParticipant.getWeight());
            }
            if(ToolUtil.isNotEmpty(saveParticipant.getPhone())){
                one.setPhone(saveParticipant.getPhone());
            }
            if(ToolUtil.isNotEmpty(saveParticipant.getIdcard())){
                one.setIdcard(saveParticipant.getIdcard());
            }
            this.updateById(one);
        }else{
            one = new Participant();
            BeanUtils.copyProperties(saveParticipant, one);
            this.save(one);
        }
    }
}