voteEmp投票添加乐观锁

main
hshansha 1 month ago
parent d9e042017a
commit 68bcc4f1a6

@ -48,6 +48,18 @@ public class KhVoteEmpController extends BaseController
return getDataTable(list);
}
/**
*
*/
@PreAuthorize("@ss.hasPermi('kaohe:vote_emp:list')")
@GetMapping("/getVoteEmps")
public TableDataInfo getVoteEmps(KhVoteEmp khVoteEmp)
{
startPage();
List<KhVoteEmp> list = khVoteEmpService.selectVoteEmpsByVoteId(khVoteEmp);
return getDataTable(list);
}
/**
*
*/

@ -79,9 +79,21 @@ public class KhVoteEmp extends BaseEntity
@Excel(name = "最终分数")
private BigDecimal endScore;
/** 乐观锁版本号 */
@Excel(name = "乐观锁版本号")
private Long version;
//仅做参数 传递本次评分分数
private BigDecimal score;
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public BigDecimal getScore() {
return score;
}
@ -262,6 +274,7 @@ public class KhVoteEmp extends BaseEntity
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.append("version", getVersion())
.toString();
}
}

@ -21,9 +21,13 @@ public class KhVoteRecard extends BaseEntity
private Long id;
/** 评测id */
@Excel(name = "评测id")
// @Excel(name = "评测id")
private Long pcId;
/** 评测id */
@Excel(name = "考核任务名称")
private String pcName;
/** 考核项id */
@Excel(name = "考核项id")
private Long khitemId;
@ -32,6 +36,11 @@ public class KhVoteRecard extends BaseEntity
@Excel(name = "投票活动id")
private Long voteId;
/** 评测id */
@Excel(name = "评分主题")
private String votTitle;
/** 用户id */
@Excel(name = "用户id")
private Long userId;
@ -41,11 +50,11 @@ public class KhVoteRecard extends BaseEntity
private String userName;
/** 投票人uid */
@Excel(name = "投票人uid")
// @Excel(name = "投票人uid")
private String uid;
/** 投票人 */
@Excel(name = "投票人")
// @Excel(name = "投票人")
private String tprName;
/** 评分详情 */
@ -58,9 +67,25 @@ public class KhVoteRecard extends BaseEntity
private Date voteTime;
/** IP */
@Excel(name = "IP")
// @Excel(name = "IP")
private String ipAddress;
public String getPcName() {
return pcName;
}
public void setPcName(String pcName) {
this.pcName = pcName;
}
public String getVoteTitle() {
return votTitle;
}
public void setVoteTitle(String voteTitle) {
this.votTitle = voteTitle;
}
public void setId(Long id)
{
this.id = id;

@ -58,4 +58,6 @@ public interface KhVoteEmpMapper
* @return
*/
public int deleteKhVoteEmpByIds(Long[] ids);
List<KhVoteEmp> selectVoteEmpsByVoteId(KhVoteEmp khVoteEmp);
}

@ -63,4 +63,6 @@ public interface IKhVoteEmpService
public int deleteKhVoteEmpById(Long id);
AjaxResult voteSubmit(KhVoteRecardParam param);
List<KhVoteEmp> selectVoteEmpsByVoteId(KhVoteEmp khVoteEmp);
}

@ -59,6 +59,11 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
return khVoteEmpMapper.selectKhVoteEmpList(khVoteEmp);
}
@Override
public List<KhVoteEmp> selectVoteEmpsByVoteId(KhVoteEmp khVoteEmp) {
return khVoteEmpMapper.selectVoteEmpsByVoteId(khVoteEmp);
}
/**
*
*
@ -96,6 +101,7 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
/**
*
*
*
* @param id
* @return
@ -109,6 +115,7 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
@Transactional
public AjaxResult voteSubmit(KhVoteRecardParam param) {
Map<String, String> xuanxiangDetails = new LinkedHashMap<>();
Map<String, BigDecimal> pingFenDetails = new LinkedHashMap<>();
BigDecimal num = new BigDecimal("1");//投票人数+1
List<KhVoteEmp> voteEmpList = param.getVoteEmpList();
Date nowDate = DateUtils.getNowDate();
@ -142,8 +149,12 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
}
}
for (KhVoteEmp voteEmp : voteEmpList) {
// 最大重试次数
// final int MAX_RETRY_COUNT = 3;
int retryCount = 0;
while (true) {
voteEmp.setUpdateTime(nowDate);
//根据id查询数据得到之前的值
//根据id查询数据得到原值及版本号
KhVoteEmp oldvoteEmp = khVoteEmpMapper.selectKhVoteEmpById(voteEmp.getId());
//存储投票详情到map
if (voteEmp.getOptionA().compareTo(BigDecimal.ZERO) == 1) {
@ -180,10 +191,31 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
} else {
voteEmp.setEndScore(avgScore);
}
khVoteEmpMapper.updateKhVoteEmp(voteEmp);
voteEmp.setVersion(oldvoteEmp.getVersion());
int result = khVoteEmpMapper.updateKhVoteEmp(voteEmp);
if (result > 0) {
break;
} else {
// 更新失败,版本冲突,增加重试计数
retryCount++;
System.out.println("-----------版本冲突," + voteEmp.getId() + "第" + retryCount + "次重试");
// if (retryCount < MAX_RETRY_COUNT) {
// 等待一段时间后重试,避免立即重试导致的持续冲突
try {
Thread.sleep(100); // 重试间隔递增
//Thread.sleep(200 * retryCount); // 重试间隔递增
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("重试过程被中断-------------", e);
}
// }
}
}
}
} else if (typeid.equals(2L)) {//占比评分
for (KhVoteEmp voteEmp : voteEmpList) {
int retryCount = 0;
while (true) {
voteEmp.setUpdateTime(nowDate);
//根据id查询数据得到之前的值
KhVoteEmp oldvoteEmp = khVoteEmpMapper.selectKhVoteEmpById(voteEmp.getId());
@ -193,10 +225,30 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
voteEmp.setVoteNum(oldvoteEmp.getVoteNum().add(num));
voteEmp.setAvgScore(sumScore.divide(voteEmp.getVoteNum(), 4, RoundingMode.HALF_UP));
voteEmp.setEndScore(voteEmp.getAvgScore().multiply(oldvoteEmp.getPercentage()));
khVoteEmpMapper.updateKhVoteEmp(voteEmp);
voteEmp.setVersion(oldvoteEmp.getVersion());
int result = khVoteEmpMapper.updateKhVoteEmp(voteEmp);
if (result > 0) {
break;
} else {
// 更新失败,版本冲突,增加重试计数
retryCount++;
System.out.println("-----------版本冲突," + voteEmp.getId() + "第" + retryCount + "次重试");
// if (retryCount < MAX_RETRY_COUNT) {
// 等待一段时间后重试,避免立即重试导致的持续冲突
try {
Thread.sleep(100); // 重试间隔递增
//Thread.sleep(200 * retryCount); // 重试间隔递增
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("重试过程被中断-------------", e);
}
}
}
}
} else {//单独评分 不占比 以平均分为最后得分
for (KhVoteEmp voteEmp : voteEmpList) {
int retryCount = 0;
while (true) {
voteEmp.setUpdateTime(nowDate);
//根据id查询数据得到之前的值
KhVoteEmp oldvoteEmp = khVoteEmpMapper.selectKhVoteEmpById(voteEmp.getId());
@ -206,7 +258,25 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
voteEmp.setVoteScore(sumScore);
voteEmp.setAvgScore(sumScore.divide(voteEmp.getVoteNum(), 4, RoundingMode.HALF_UP));
voteEmp.setEndScore(voteEmp.getAvgScore());
khVoteEmpMapper.updateKhVoteEmp(voteEmp);
voteEmp.setVersion(oldvoteEmp.getVersion());
int result = khVoteEmpMapper.updateKhVoteEmp(voteEmp);
if (result > 0) {
break;
} else {
// 更新失败,版本冲突,增加重试计数
retryCount++;
System.out.println("-----------版本冲突," + voteEmp.getId() + "第" + retryCount + "次重试");
// if (retryCount < MAX_RETRY_COUNT) {
// 等待一段时间后重试,避免立即重试导致的持续冲突
try {
Thread.sleep(100); // 重试间隔递增
//Thread.sleep(200 * retryCount); // 重试间隔递增
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("重试过程被中断-------------", e);
}
}
}
}
}
@ -222,14 +292,15 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
} else {
for (KhVoteEmp voteEmp : voteEmpList) {
//xuanxiangDetails.put(voteEmp.getBkhdxId().toString(), voteEmp.getBkhdxName()+":"+voteEmp.getScore());
xuanxiangDetails.put(voteEmp.getBkhdxName(), voteEmp.getScore().toString());
recard.setVoteDetails(JSON.toJSONString(xuanxiangDetails));
pingFenDetails.put(voteEmp.getBkhdxName()/*+voteEmp.getBkhdxId()*/, voteEmp.getScore());
recard.setVoteDetails(JSON.toJSONString(pingFenDetails));
}
/*List<String> details = voteEmpList.stream()
.map(emp -> emp.getBkhdxName() + " : " + emp.getScore())
.collect(Collectors.toList());
recard.setVoteDetails(JSON.toJSONString(details));*/
}
recard.setRemark(JSON.toJSONString(voteEmpList));
khVoteRecardMapper.insertKhVoteRecard(recard);
return AjaxResult.success();
}

@ -26,10 +26,11 @@
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="version" column="version" />
</resultMap>
<sql id="selectKhVoteEmpVo">
select id, khitem_typeId, vote_id, vote_title, bkhdx_id, bkhdx_name, content, vote_num,option_a, option_b, option_c, option_d, vote_score, avg_score, percentage, end_score, create_by, create_time, update_by, update_time, remark from kh_vote_emp
select id, khitem_typeId, vote_id, vote_title, bkhdx_id, bkhdx_name, content, vote_num,option_a, option_b, option_c, option_d, vote_score, avg_score, percentage, end_score, create_by, create_time, update_by, update_time, remark, version from kh_vote_emp
</sql>
<select id="selectKhVoteEmpList" parameterType="KhVoteEmp" resultMap="KhVoteEmpResult">
@ -50,6 +51,14 @@
<if test="avgScore != null "> and avg_score = #{avgScore}</if>
<if test="percentage != null "> and percentage = #{percentage}</if>
<if test="endScore != null "> and end_score = #{endScore}</if>
<!--<if test="version != null "> and version = #{version}</if>-->
</where>
</select>
<!--投票页面查询考核及考核对象信息-->
<select id="selectVoteEmpsByVoteId" parameterType="KhVoteEmp" resultMap="KhVoteEmpResult">
select id, khitem_typeId, vote_id, bkhdx_id, bkhdx_name from kh_vote_emp
<where>
<if test="voteId != null "> and vote_id = #{voteId}</if>
</where>
</select>
@ -81,6 +90,7 @@
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
<!--<if test="version != null">version,</if>-->
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="khitemTypeid != null">#{khitemTypeid},</if>
@ -103,6 +113,7 @@
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
<!--<if test="version != null">#{version},</if>-->
</trim>
</insert>
@ -129,8 +140,10 @@
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
version = version + 1
</trim>
where id = #{id}
<!-- 其他需要更新的字段 -->
WHERE id = #{id} AND version = #{version}
</update>
<delete id="deleteKhVoteEmpById" parameterType="Long">

@ -56,11 +56,11 @@
</where>
</select>
<!--生成评测账号登录时筛选符合条件的vote展示评分 ————废弃———— -->
<select id="getListByPcIdAndItemIds" parameterType="KhVote" resultMap="KhVoteResult">
<!--<select id="getListByPcIdAndItemIds" parameterType="KhVote" resultMap="KhVoteResult">
select v.*,it.type_id from kh_vote v left join kh_items it on v.khitem_id =it.id
<where>
<if test="pingceId != null "> and pingce_id = #{pingceId}</if>
<!--状态值传1也就是展示所有状态不等于2非完成状态的数据-->
&lt;!&ndash;状态值传1也就是展示所有状态不等于2非完成状态的数据&ndash;&gt;
<if test="state != null and state != ''"> and state != #{state}</if>
<if test="itemIds != null"> and khitem_id in
<foreach item="itemId" collection="itemIds" open="(" separator="," close=")">
@ -68,12 +68,13 @@
</foreach>
</if>
</where>
</select>
</select>-->
<!-- 评测账号登录时筛选符合条件的vote展示评分 -->
<select id="getListByVoteIds" parameterType="KhVote" resultMap="KhVoteResult">
select v.*,it.type_id from kh_vote v left join kh_items it on v.khitem_id =it.id
<where>
<if test="pingceId != null "> and pingce_id = #{pingceId}</if>
<if test="pingceName != null and pingceName != ''"> and pingce_name like concat('%', #{pingceName}, '%')</if>
<if test="voteTitle != null and voteTitle != ''"> and vote_title like concat('%', #{voteTitle}, '%') </if>
<!--状态值传1也就是展示所有状态不等于2非完成状态的数据-->
<if test="state != null and state != ''"> and state != #{state}</if>
<if test="voteIds != null"> and v.id in

@ -6,9 +6,11 @@
<resultMap type="KhVoteRecard" id="KhVoteRecardResult">
<result property="id" column="id" />
<result property="pcId" column="pc_id" />
<result property="pcId" column="pingce_id" />
<result property="pcName" column="pingce_name" />
<result property="khitemId" column="khitem_id" />
<result property="voteId" column="vote_id" />
<result property="votTitle" column="vote_title" />
<result property="userId" column="user_id" />
<result property="userName" column="user_name" />
<result property="uid" column="uid" />
@ -28,18 +30,21 @@
</sql>
<select id="selectKhVoteRecardList" parameterType="KhVoteRecard" resultMap="KhVoteRecardResult">
<include refid="selectKhVoteRecardVo"/>
select r.id,v.pingce_id,r.vote_id,r.user_name,r.vote_details,r.vote_time,v.pingce_name,v.vote_title
from kh_vote_recard r join kh_vote v on r.vote_id=v.id
<where>
<if test="pcId != null "> and pc_id = #{pcId}</if>
<if test="khitemId != null "> and khitem_id = #{khitemId}</if>
<if test="voteId != null "> and vote_id = #{voteId}</if>
<if test="userId != null "> and user_id = #{userId}</if>
<if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
<if test="uid != null "> and uid = #{uid}</if>
<if test="tprName != null and tprName != ''"> and tpr_name like concat('%', #{tprName}, '%')</if>
<if test="voteDetails != null and voteDetails != ''"> and vote_details = #{voteDetails}</if>
<if test="voteTime != null "> and vote_time = #{voteTime}</if>
<if test="ipAddress != null and ipAddress != ''"> and ip_address = #{ipAddress}</if>
<if test="pcId != null "> and v.pingce_id = #{pcId}</if>
<if test="pcName != null and pcName != '' "> and v.pingce_name like concat('%', #{pcName}, '%')</if>
<if test="khitemId != null "> and r.khitem_id = #{khitemId}</if>
<if test="voteId != null "> and r.vote_id = #{voteId}</if>
<if test="voteTitle != null and voteTitle != '' "> and v.vote_title like concat('%', #{voteTitle}, '%')</if>
<if test="userId != null "> and r.user_id = #{userId}</if>
<if test="userName != null and userName != ''"> and r.user_name like concat('%', #{userName}, '%')</if>
<if test="uid != null "> and r.uid = #{uid}</if>
<if test="tprName != null and tprName != ''"> and r.tpr_name like concat('%', #{tprName}, '%')</if>
<!-- <if test="voteDetails != null and voteDetails != ''"> and r.vote_details = #{voteDetails}</if>-->
<if test="voteTime != null "> and r.vote_time = #{voteTime}</if>
<if test="ipAddress != null and ipAddress != ''"> and r.ip_address = #{ipAddress}</if>
</where>
</select>

Loading…
Cancel
Save