关于程序员不写注释这一件事

一:我对代码注释的观点

博主个人写代码是肯定会写注释的,哪怕是简单的功能,文档注释、多行注释、单行注释怎么顺手怎么用,让一个小白也能看懂自己的代码。

 

二:我认为程序员不写注释的原因

博主认为程序员不写注释的原因无外乎以下几点:

1、太懒了,就是单纯不想写

2、平时没有养成写代码注释的习惯

3、作为一个程序员,却没有程序员的基本素养(为什么和第1点分开写呢,懂的都懂)

 

三:如何才能写出漂亮的注释

1、在类和方法上,使用文档注释

2、在方法内部,可以使用多行注释+单行注释的组合让注释有条有理

3、漂亮的注释要配合适当的代码缩进才能达到更好的效果,如果所有代码之前都没有空格和换行,那么你的代码整体会非常乱,哪怕写了注释也一样。注释和代码会混合在一起,可读性非常差。

4、和代码编写不同的是,代码注释本身并没有统一的规范,所以,其实完全可以根据自己的喜好来写,甚至可以在service层的impl实现类的方法上面加上整个业务的核心步骤并编号,然后在代码中也复写一次,类似于书的目录。

说了那么多,不如show me your code!

下面贴出的两个类,基本上就是我的代码注释风格了

package cn.edu.sgu.www.mhxysy.service.chongwu.impl;

import cn.edu.sgu.www.mhxysy.base.Pager;
import cn.edu.sgu.www.mhxysy.consts.IdentifierPrefixConst;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuDTO;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuTongyuDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuAttribute;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuJiadian;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuZizhi;
import cn.edu.sgu.www.mhxysy.enums.JiadianSchemas;
import cn.edu.sgu.www.mhxysy.enums.TongyuState;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.*;
import cn.edu.sgu.www.mhxysy.pager.chongwu.ChongwuPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class ChongwuServiceImpl implements ChongwuService {

	private final ChongwuAttributeMapper chongwuAttributeMapper;
	private final ChongwuJiadianMapper chongwuJiadianMapper;
	private final ChongwuSkillMapper chongwuSkillMapper;
	private final ChongwuZizhiMapper chongwuZizhiMapper;
	private final ChongwuMapper chongwuMapper;

	@Autowired
	public ChongwuServiceImpl(
			ChongwuAttributeMapper chongwuAttributeMapper,
			ChongwuJiadianMapper chongwuJiadianMapper,
			ChongwuSkillMapper chongwuSkillMapper,
			ChongwuZizhiMapper chongwuZizhiMapper,
			ChongwuMapper chongwuMapper) {
		this.chongwuAttributeMapper = chongwuAttributeMapper;
		this.chongwuJiadianMapper = chongwuJiadianMapper;
		this.chongwuSkillMapper = chongwuSkillMapper;
		this.chongwuZizhiMapper = chongwuZizhiMapper;
		this.chongwuMapper = chongwuMapper;
	}

	@Override
	public void insert(ChongwuDTO chongwuDTO) {
		// 1、添加宠物属性
		int dkfy = (chongwuDTO.getGrade() - 10) * 20; // 抵抗封印
		ChongwuAttribute attribute = new ChongwuAttribute();

		attribute.setId(IdentifierPrefixConst.PREFIX_CHONGWU_SHUXING + StringUtils.uuid());
		attribute.setDkfy((dkfy > 0)? dkfy: 1);
		
		chongwuAttributeMapper.insert(attribute);
		
		// 2、添加宠物资质、成长信息
		ChongwuZizhi zizhi = new ChongwuZizhi();
		
		zizhi.setId(IdentifierPrefixConst.PREFIX_CHONGWU_ZIZHI + StringUtils.uuid());
		
		chongwuZizhiMapper.insert(zizhi);
		
		// 3、添加宠物
		Chongwu chongwu = new Chongwu();

		chongwu.setZizhiId(null);
		chongwu.setZizhiId(zizhi.getId());
		chongwu.setAttributeId(attribute.getId());
		chongwu.setTyStatus(TongyuState.WTY.getValue());
		chongwu.setId(IdentifierPrefixConst.PREFIX_CHONGWU + StringUtils.uuid());
		// copy value from ChongwuDTO
		chongwu.setName(chongwuDTO.getName());
		chongwu.setType(chongwuDTO.getType());
		chongwu.setScore(chongwuDTO.getScore());
		chongwu.setGrade(chongwuDTO.getGrade());
		chongwu.setRoleId(chongwuDTO.getRoleId());
		chongwu.setLifespan(chongwuDTO.getLifespan());
		chongwu.setCategoryId(chongwuDTO.getCategoryId());

		chongwuMapper.insert(chongwu);
		
		// 4、添加宠物加点
		ChongwuJiadian chongwuJiadian = new ChongwuJiadian();
		
		chongwuJiadian.setId(IdentifierPrefixConst.PREFIX_CHONGWU_JIADIAN + StringUtils.uuid());
		chongwuJiadian.setJiadianSchema(JiadianSchemas.JDFAY.getValue());
		chongwuJiadian.setAttributeId(attribute.getId());
		chongwuJiadian.setChongwuId(chongwu.getId());
		chongwuJiadian.setUnlockState(1);
		chongwuJiadian.setOpenState(1);

		chongwuJiadianMapper.insert(chongwuJiadian);
	}

	@Override
	public void deleteById(String id) {
		// 通过宠物id查询宠物技能
		Chongwu chongwu = chongwuMapper.selectById(id);

		// 判断宠物是否存在,若不存在,抛出异常
		if (chongwu == null) {
			throw new GlobalException(ResponseCode.NOT_FOUND, "删除失败,宠物不存在~");
		}

		// 1、通过id删除宠物属性
		chongwuAttributeMapper.deleteById(chongwu.getAttributeId());

		// 2、通过id删除宠物资质
		chongwuZizhiMapper.deleteById(chongwu.getZizhiId());

		// 3、通过宠物id删除宠物技能
		chongwuSkillMapper.deleteByChongwuId(id);

		// 4、通过id删除宠物
		chongwuMapper.deleteById(id);
	}

	@Override
	public void updateById(Chongwu chongwu) {
		chongwuMapper.updateById(chongwu);
	}

	@Override
	public List selectAll() {
		return chongwuMapper.selectList(null);
	}

	@Override
	public Chongwu selectById(String id) {
		return chongwuMapper.selectById(id);
	}

	@Override
	public List select(Chongwu chongwu) {
		QueryWrapper wrapper = new QueryWrapper();

		wrapper.eq(
				chongwu.getCategoryId() != null,
				"category_id", chongwu.getCategoryId()
		);
		wrapper.eq(
				StringUtils.isNotEmpty(chongwu.getRoleId()),
				"role_id", chongwu.getRoleId()
		);
		wrapper.eq(
				StringUtils.isNotEmpty(chongwu.getZuoqiId()),
				"zuoqi_id", chongwu.getZuoqiId()
		);

		return chongwuMapper.selectList(wrapper);
	}

	@Override
	public List selectByRoleId(String roleId) {
		QueryWrapper wrapper = new QueryWrapper<>();

		wrapper.eq("role_id", roleId);

		return chongwuMapper.selectList(wrapper);
	}

	@Override
	public List selectImagesByZuoqiId(String zuoqiId) {
		return chongwuMapper.selectImagesByZuoqiId(zuoqiId);
	}

	@Override
	public JsonResult> selectByPage(ChongwuPager pager) {
		String skillIds = pager.getSkillIds();

		if (StringUtils.isNotEmpty(skillIds)) {
			String[] array = skillIds.split("-");
			// 得到order by语句
			String statement = Pager.getStatement(pager);

			List list = chongwuMapper.selectBySkills(pager, array, array.length, statement);
			long total = chongwuMapper.selectCountBySkills(pager, array, array.length);

			return JsonResult.restPage(total, list);
		} else {
			Page page = new Page<>(pager.getPage(), pager.getRows());
			QueryWrapper wrapper = new QueryWrapper<>();

			wrapper.eq(
					pager.getCategoryId() != null,
					"category_id", pager.getCategoryId()
			);
			wrapper.eq(
					StringUtils.isNotEmpty(pager.getRoleId()),
					"role_id", pager.getRoleId()
			);
			wrapper.eq(
					StringUtils.isNotEmpty(pager.getZuoqiId()),
					"zuoqi_id", pager.getZuoqiId()
			);
//			wrapper.orderByAsc("role_id");

			// 得到order by语句
			String statement = Pager.getOrderByStatement(pager);
			wrapper.last(statement);

			Page result = chongwuMapper.selectPage(page, wrapper);
			return JsonResult.restPage(result);
		}
	}

	@Override
	public void tongyu(Chongwu chongwu) {
		chongwu.setTyStatus(1 - chongwu.getTyStatus());

		// 【统御】操作
		if (chongwu.getTyStatus() == 1) {
			QueryWrapper wrapper = new QueryWrapper();

			wrapper.eq(
					StringUtils.isNotEmpty(chongwu.getRoleId()),
					"role_id", chongwu.getRoleId()
			);
			wrapper.eq(
					StringUtils.isNotEmpty(chongwu.getZuoqiId()),
					"zuoqi_id", chongwu.getZuoqiId()
			);
			wrapper.eq("ty_status", TongyuState.YTY.getValue());

			// 获取坐骑统御的宠物总数
			long count =  chongwuMapper.selectCount(wrapper);

			// 已经统御两只宠物,终止操作,抛出异常
			if (count >= 2) {
				throw new GlobalException(ResponseCode.BAD_REQUEST, "最多可以统御两只宠物!");
			}

			chongwuMapper.updateById(chongwu);
		} else { // [取消统御]操作
			// 修改宠物的坐骑id为null,并设置统御状态为:0-未统御
			UpdateWrapper wrapper = new UpdateWrapper<>();

			wrapper.eq("id", chongwu.getId());
			wrapper.set("zuoqi_id", null);
			wrapper.set("ty_status", TongyuState.WTY.getValue());

			chongwuMapper.update(wrapper.getEntity(), wrapper);
		}
	}

	@Override
	public void change(ChongwuTongyuDTO tongyuDTO) {
		// 判断宠物的统御状态,若为未统御,抛出异常
		if (TongyuState.WTY.getValue().equals(tongyuDTO.getTyStatus())) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "宠物状态为未统御,更换统御失败!");
		}

		// 修改宠物的统御坐骑id
		Chongwu chongwu = new Chongwu();

		chongwu.setId(tongyuDTO.getId());
		chongwu.setZuoqiId(tongyuDTO.getZuoqiId());

		chongwuMapper.updateById(chongwu);
	}

}

package cn.edu.sgu.www.mhxysy.service.chongwu.impl;

import cn.edu.sgu.www.mhxysy.base.Pager;
import cn.edu.sgu.www.mhxysy.consts.IdentifierPrefixConst;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuSkillDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuSkill;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuSkillCategory;
import cn.edu.sgu.www.mhxysy.enums.AuthStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuSkillMapper;
import cn.edu.sgu.www.mhxysy.pager.chongwu.ChongwuSkillPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuSkillService;
import cn.edu.sgu.www.mhxysy.util.NumberUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.chongwu.ChongwuSkillVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class ChongwuSkillServiceImpl implements ChongwuSkillService {

	private final ChongwuMapper chongwuMapper;
	private final ChongwuSkillMapper chongwuSkillMapper;
	private final ChongwuSkillCategoryMapper chongwuSkillCategoryMapper;

	@Autowired
	public ChongwuSkillServiceImpl(
			ChongwuMapper chongwuMapper,
			ChongwuSkillMapper chongwuSkillMapper,
			ChongwuSkillCategoryMapper chongwuSkillCategoryMapper) {
		this.chongwuMapper = chongwuMapper;
		this.chongwuSkillMapper = chongwuSkillMapper;
		this.chongwuSkillCategoryMapper = chongwuSkillCategoryMapper;
	}

	@Override
	public void insert(ChongwuSkillDTO chongwuSkillDTO) {
		// 得到宠物ID和技能ID
		String chongwuId = chongwuSkillDTO.getChongwuId();
		Integer skillId = chongwuSkillDTO.getSkillId();

		// 验证宠物技能是否重复
		int location = validate(chongwuId, skillId);

		if (location > 0) {
			// 添加宠物技能
			ChongwuSkill chongwuSkill = new ChongwuSkill();

			chongwuSkill.setSkillId(skillId);
			chongwuSkill.setLocation(location);
			chongwuSkill.setChongwuId(chongwuId);
			chongwuSkill.setAuthStatus(AuthStatus.WRZ.getValue());
			chongwuSkill.setId(IdentifierPrefixConst.PREFIX_CWJN + StringUtils.uuid());

			chongwuSkillMapper.insert(chongwuSkill);
		}
	}

	@Override
	public void updateById(ChongwuSkill chongwuskill) {
		chongwuSkillMapper.updateById(chongwuskill);
	}

	@Override
	public Page selectByPage(ChongwuSkillPager pager) {
		QueryWrapper wrapper = new QueryWrapper<>();
		Page page = Pager.ofPage(pager);

		wrapper.eq(
				StringUtils.isNotEmpty(pager.getChongwuId()),
				"chongwu_id", pager.getChongwuId()
		);
		wrapper.eq(
				pager.getSkillId() != null,
				"skill_id", pager.getSkillId()
		);

		return chongwuSkillMapper.selectPage(page, wrapper);
	}

	@Override
	public List getLearnedSkills(String chongwuId) {
		return chongwuSkillMapper.getLearnedSkills(chongwuId);
	}

	@Override
	public ChongwuSkill getAuthedSkill(String chongwuId) {
		QueryWrapper wrapper = new QueryWrapper<>();

		wrapper.eq("chongwu_id", chongwuId);
		wrapper.eq("auth_status", AuthStatus.YRZ.getValue());

		return chongwuSkillMapper.selectOne(wrapper);
	}

	@Override
	public List getCanAuthedSkills(String chongwuId) {
		return chongwuSkillMapper.getCanAuthedSkills(chongwuId);
	}

	/**
	 * 学习技能
	 * @param chongwuSkillDTO 宠物技能信息
	 * @return String 学习的新技能名称
	 */
	@Override
	public String study(ChongwuSkillDTO chongwuSkillDTO) {
		Integer skillId = chongwuSkillDTO.getSkillId();
		String chongwuId = chongwuSkillDTO.getChongwuId();

		/*
		 * 1、判断是否宠物已学习的技能
		 */
		int location = validate(chongwuId, skillId);

		/*
		 * 2、进入宠物学习技能流程
		 */
		// 查询宠物技能
		List chongwuSkills = selectByChongwuId(chongwuId);
		// 删除已认证的技能(1号位 && 认证状态为:1-已认证)
		chongwuSkills.removeIf(chongwuSkill -> chongwuSkill.getAuthStatus() == 1 && chongwuSkill.getLocation() == 1);

		// 学习之前的技能名称
		String name = null;

		/*
			新增技能
			1、宠物尚未学习技能
			2、宠物已学习技能数 < 3
			3、只有一个已认证的技能(理论上不存在这种可能,因为认证会生成一个新技能)
		*/
		if (chongwuSkills.isEmpty() || location <= 3) {
			ChongwuSkill chongwuSkill = new ChongwuSkill();

			chongwuSkill.setSkillId(skillId);
			chongwuSkill.setLocation(location);
			chongwuSkill.setChongwuId(chongwuId);
			chongwuSkill.setAuthStatus(AuthStatus.WRZ.getValue());
			chongwuSkill.setId(IdentifierPrefixConst.PREFIX_CWJN + StringUtils.uuid());

			chongwuSkillMapper.insert(chongwuSkill);
		} else {
			/*
				替换技能:随机替换宠物学习的一个技能
			*/
			// 生成随机下标
			int index = NumberUtils.random(0, chongwuSkills.size() - 1);
			// 得到指定下标的元素
			ChongwuSkill chongwuSkill = chongwuSkills.get(index);
			// 查询学习之前当前位置的的技能名称
			name = chongwuSkillCategoryMapper.selectById(chongwuSkill.getSkillId()).getName();

			// 修改宠物技能信息
			chongwuSkill.setSkillId(skillId);
			chongwuSkillMapper.updateById(chongwuSkill);
		}

		// 返回学习之前的技能名称
		return name;
	}

	/**
	 * 法术认证
	 * 1、判断技能是否存在:不存在则认证失败,抛出异常
	 * 2、判断技能是否可认证:不可认证则认证失败,抛出异常
	 * 3、判断技能的认证状态:已认证则认证失败,抛出异常
	 * 4、判断宠物是否已有认证的技能,每只宠物只能认证一个技能:若有,则认证失败,抛出异常
	 * 5、判断认证后宠物学习的技能数量是否大于13:大于13则认证失败,抛出异常
	 * 6、在其前面的技能整体后移1位
	 * 7、修改认证的技能位置及认证状态为1
	 * 8、为宠物随机添加一个未学习的低级技能
	 */
	@Override
	public void authenticate(ChongwuSkill chongwuSkill) {
		/*
		 * 1、判断技能是否存在:不存在则认证失败,抛出异常
		 */
		Integer location = chongwuSkill.getLocation();
		String chongwuId = chongwuSkill.getChongwuId();

		// 获取宠物指定位置的技能信息
		QueryWrapper wrapper = new QueryWrapper<>();
		wrapper.eq("chongwu_id", chongwuId);
		wrapper.eq("location", location);
		List list = chongwuSkillMapper.selectList(wrapper);

		// 查询结果大于1,数据异常
		int count = list.size();
		ChongwuSkill dbResult;

		if (count == 0){
			throw new GlobalException(ResponseCode.NOT_FOUND, "技能不存在,认证失败");
		} else if (count > 1) {
			String msg = "数据错误,查询到宠物" + chongwuId + "在" + location + "号位的技能超过1个,请联系管理员处理";

			throw new GlobalException(ResponseCode.BAD_REQUEST, msg);
		} else {
			dbResult = list.get(0);
		}
		/*
		 * 2、判断技能是否可认证,不可认证则认证失败,抛出异常
		 */
		ChongwuSkillCategory category = chongwuSkillCategoryMapper.selectById(dbResult.getSkillId());
		String name = category.getName();

		// 技能无法认证
		if (category.getCanAuthed() == 0) {
			throw new GlobalException(ResponseCode.FORBIDDEN,
					"技能" + name + "无法认证,选择其他技能试试吧o_o");
		}

		/*
		 * 3、判断技能的认证状态,已认证则认证失败,抛出异常
		 */
		// 技能已认证
		if (AuthStatus.YRZ.getValue().equals(dbResult.getAuthStatus())) {
			throw new GlobalException(ResponseCode.FORBIDDEN,
					"技能" + name + "已经认证,少侠不要戏弄我了");
		}

		/*
		 * 4、判断宠物是否已有认证的技能,每只宠物只能认证一个技能:若有,则认证失败,抛出异常
		 */
		// 查询宠物已认证的技能
		wrapper = new QueryWrapper<>();

		wrapper.eq("chongwu_id", chongwuId);
		wrapper.eq("auth_status", AuthStatus.YRZ.getValue());

		long size = chongwuSkillMapper.selectCount(wrapper);

		if (size > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "已经有一个已认证的技能了");
		}

		/*
		 * 5、判断认证后宠物学习的技能数量是否大于13:大于13则认证失败,抛出异常
		 */
		// 查询宠物学习的技能总数
		int total = chongwuSkillMapper.selectCountByChongwuId(chongwuId);

		// 判断count + 1 > 13
		if (total > 12) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "宠物技能数量已达上限");
		}

		/*
		 * 6、在其前面的技能整体后移1位
		 */
		// 如果位置大于1,则其前面还有技能,修改其前面的技能位置+1
		if (location > 1) {
			chongwuSkillMapper.increaseLocations(chongwuId, location);
		}

		/*
		 * 7、修改认证的技能位置及认证状态为1
		 */
		UpdateWrapper updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("id", dbResult.getId());
		updateWrapper.set("location", 1);
		updateWrapper.set("auth_status", AuthStatus.YRZ.getValue());

		chongwuSkillMapper.update(updateWrapper.getEntity(), updateWrapper);

		/*
		 * 8、为宠物随机添加一个宠物未学习的低级技能
		 */
		// 查询宠物已学习的技能的ID
		List skillIds = chongwuSkillMapper.selectLearnedSkillIds(chongwuId);

		// 查询宠物未学习的低级技能
		QueryWrapper queryWrapper = new QueryWrapper<>();

		queryWrapper.notIn("id", skillIds);
		queryWrapper.eq("type", 0);
		queryWrapper.eq("can_authed", 0);

		List categories = chongwuSkillCategoryMapper.selectList(queryWrapper);

		// 得到随机下标
		int index = NumberUtils.random(0, categories.size() - 1);

		// 添加一个新的宠物技能
		ChongwuSkill skill = new ChongwuSkill();

		skill.setLocation(total + 1);
		skill.setChongwuId(chongwuId);
		skill.setAuthStatus(AuthStatus.WRZ.getValue());
		skill.setSkillId(categories.get(index).getId());
		skill.setId(IdentifierPrefixConst.PREFIX_CWJN + StringUtils.uuid());

		chongwuSkillMapper.insert(skill);
	}

	/**
	 * 取消法术认证
	 * 1、查询宠物已认证的技能:若查询结果为空,则抛出异常
	 * 2、删除已认证的技能
	 * 3、其他技能的位置向前推进1格
	 * @param chongwuId 宠物id
	 * @return String
	 */
	@Override
	public String cancelAuth(String chongwuId) {
		/*
		 * 1、查询宠物已认证的技能:若查询结果为空,则抛出异常
		 */
		ChongwuSkill chongwuSkill = beforAuth(chongwuId);

		/*
		 * 2、删除已认证的技能
		 */
		chongwuSkillMapper.deleteById(chongwuSkill.getId());

		/*
		 * 3、其他技能的位置向前推进1格
		 */
		chongwuSkillMapper.decreaseLocations(chongwuId);


		// 返回宠物名称和等级拼接的字符串
		Chongwu chongwu = chongwuMapper.selectById(chongwuId);

		return chongwu.getName() + "(" + chongwu.getGrade() + "级)";
	}

	/**
	 * 认证替换
	 * 1、查询宠物已认证的技能:若查询结果为空,抛出异常
	 * 2、修改已认证技能的宠物技能类型id
	 * @param chongwuSkillDTO 认证信息
	 */
	@Override
	public void replaceAuth(ChongwuSkillDTO chongwuSkillDTO) {
		/*
		 * 1、查询宠物已认证的技能:若查询结果为空,则抛出异常
		 */
		ChongwuSkill chongwuSkill = beforAuth(chongwuSkillDTO.getChongwuId());

		/*
		 * 2、修改已认证技能的宠物技能类型id
		 */
		UpdateWrapper updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("skill_id", chongwuSkillDTO.getSkillId());
		updateWrapper.eq("id", chongwuSkill.getId());

		chongwuSkillMapper.update(updateWrapper.getEntity(), updateWrapper);
	}

	@Override
	public boolean hasAuthedSkills(String chongwuId) {
		QueryWrapper wrapper = new QueryWrapper<>();

		wrapper.eq("chongwu_id", chongwuId);
		wrapper.eq("auth_status", AuthStatus.YRZ.getValue());

		return chongwuSkillMapper.exists(wrapper);
	}

	/**
	 * 验证宠物技能上限及是否重复
	 * @param chongwuId 宠物ID
	 * @param skillId 技能ID
	 * @return int 宠物技能的下一个位置
	 */
	private int validate(String chongwuId, Integer skillId) {
		// 通过宠物ID查询宠物技能
		List chongwuSkills = selectByChongwuId(chongwuId);

		// 宠物技能不为空
		if (!chongwuSkills.isEmpty()) {
			// 检查宠物已学习的技能数量
			if (chongwuSkills.size() > 12) {
				throw new GlobalException(ResponseCode.BAD_REQUEST, "该宠物的技能数量已经上限!");
			}

			// 判断技能是否重复,当前的技能ID是否在技能列表
			for (ChongwuSkill chongwuSkill : chongwuSkills) {
				if (skillId.equals(chongwuSkill.getSkillId())) {
					throw new GlobalException(ResponseCode.BAD_REQUEST, "已经学习了相同的技能,别浪费了~");
				}
			}
		}

		return chongwuSkills.size() + 1;
	}

	/**
	 * 通过宠物id查询宠物技能
	 * @param chongwuId 宠物id
	 * @return List
	 */
	private List selectByChongwuId(String chongwuId) {
		QueryWrapper wrapper = new QueryWrapper<>();

		wrapper.eq("chongwu_id", chongwuId);

		return chongwuSkillMapper.selectList(wrapper);
	}

	/**
	 * 认证/取消认证前的检查
	 * @param chongwuId 宠物id
	 */
	private ChongwuSkill beforAuth(String chongwuId) {
		// 查询宠物已认证的技能
		QueryWrapper wrapper = new QueryWrapper<>();
		wrapper.eq("chongwu_id", chongwuId);
		wrapper.eq("auth_status", AuthStatus.YRZ.getValue());
		List chongwuSkills = chongwuSkillMapper.selectList(wrapper);

		// 查询结果数
		int count = chongwuSkills.size();

		if (count == 0) { // 查询结果为空
			throw new GlobalException(ResponseCode.NOT_FOUND, "当前宠物没有已认证的技能");
		} else if (count > 1) { // 查询结果数大于1
			throw new GlobalException(ResponseCode.CONFLICT, "数据异常,宠物已认证的技能数大于1");
		} else {
			return chongwuSkills.get(0);
		}
	}

}

你可能感兴趣的:(pandas)