| @ -0,0 +1,64 @@ | |||
| package com.hxhq.business.controller; | |||
| import java.util.Arrays; | |||
| import java.util.List; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import com.hxhq.business.domain.SnGen; | |||
| import com.hxhq.business.service.ISnGenService; | |||
| import com.hxhq.common.core.web.controller.BaseController; | |||
| import com.hxhq.common.core.web.domain.AjaxResult; | |||
| import com.hxhq.common.core.web.page.TableDataInfo; | |||
| /** | |||
| * 每日编号生成记录Controller | |||
| * | |||
| * @author hxhq | |||
| * @date 2026-01-12 | |||
| */ | |||
| @RestController | |||
| @RequestMapping("/business/snGen") | |||
| public class SnGenController extends BaseController | |||
| { | |||
| @Autowired | |||
| private ISnGenService snGenService; | |||
| /** | |||
| * 查询每日编号生成记录列表 | |||
| */ | |||
| @GetMapping("/list") | |||
| public TableDataInfo list(SnGen snGen) | |||
| { | |||
| startPage(); | |||
| List<SnGen> list = snGenService.queryList(snGen); | |||
| return getDataTable(list); | |||
| } | |||
| /** | |||
| * 获取每日编号生成记录详细信息 | |||
| */ | |||
| @GetMapping(value = "/info") | |||
| public AjaxResult getInfo(Long id) | |||
| { | |||
| return AjaxResult.success(snGenService.getById(id)); | |||
| } | |||
| /** | |||
| * 新增每日编号生成记录信息 | |||
| */ | |||
| @PostMapping("/save") | |||
| public AjaxResult save(@RequestBody SnGen snGen) | |||
| { | |||
| return toAjax(snGenService.saveOrUpdate(snGen)); | |||
| } | |||
| /** | |||
| * 删除每日编号生成记录信息 | |||
| */ | |||
| @PostMapping("/delete") | |||
| public AjaxResult delete(@RequestBody Long[] ids) | |||
| { | |||
| return toAjax(snGenService.removeByIds(Arrays.asList(ids))); | |||
| } | |||
| } | |||
| @ -0,0 +1,42 @@ | |||
| package com.hxhq.business.domain; | |||
| import com.baomidou.mybatisplus.annotation.TableName; | |||
| import com.hxhq.common.core.domain.MpBaseEntity; | |||
| /** | |||
| * 每日编号生成记录对象 t_sn_gen | |||
| * | |||
| * @author hxhq | |||
| * @date 2026-01-12 | |||
| */ | |||
| @TableName("t_sn_gen") | |||
| public class SnGen extends MpBaseEntity | |||
| { | |||
| private static final long serialVersionUID = 1L; | |||
| /** 日期 */ | |||
| private String date; | |||
| /** 当前编号 */ | |||
| private Integer sn; | |||
| public void setDate(String date) | |||
| { | |||
| this.date = date; | |||
| } | |||
| public String getDate() | |||
| { | |||
| return date; | |||
| } | |||
| public Integer getSn() { | |||
| return sn; | |||
| } | |||
| public void setSn(Integer sn) { | |||
| this.sn = sn; | |||
| } | |||
| } | |||
| @ -0,0 +1,20 @@ | |||
| package com.hxhq.business.mapper; | |||
| import com.hxhq.business.domain.SnGen; | |||
| import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
| import org.apache.ibatis.annotations.Param; | |||
| /** | |||
| * 每日编号生成记录Mapper接口 | |||
| * | |||
| * @author hxhq | |||
| * @date 2026-01-12 | |||
| */ | |||
| public interface SnGenMapper extends BaseMapper<SnGen> | |||
| { | |||
| /** | |||
| * 递增id | |||
| * @param date | |||
| */ | |||
| void addSn(@Param("date") String date); | |||
| } | |||
| @ -0,0 +1,20 @@ | |||
| package com.hxhq.business.service; | |||
| import java.util.List; | |||
| import com.hxhq.business.domain.SnGen; | |||
| import com.baomidou.mybatisplus.extension.service.IService; | |||
| /** | |||
| * 每日编号生成记录Service接口 | |||
| * | |||
| * @author hxhq | |||
| * @date 2026-01-12 | |||
| */ | |||
| public interface ISnGenService extends IService<SnGen> | |||
| { | |||
| /** | |||
| * 获取新编号 | |||
| * @return | |||
| */ | |||
| String getNewSn(); | |||
| } | |||
| @ -0,0 +1,58 @@ | |||
| package com.hxhq.business.service.impl; | |||
| import java.util.List; | |||
| import java.util.concurrent.locks.Lock; | |||
| import java.util.concurrent.locks.ReentrantLock; | |||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
| import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||
| import com.hxhq.common.core.utils.DateUtils; | |||
| import com.hxhq.common.core.utils.SpringUtils; | |||
| import com.hxhq.common.redis.service.RedisService; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import com.hxhq.business.mapper.SnGenMapper; | |||
| import com.hxhq.business.domain.SnGen; | |||
| import com.hxhq.business.service.ISnGenService; | |||
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||
| /** | |||
| * 每日编号生成记录Service业务层处理 | |||
| * | |||
| * @author hxhq | |||
| * @date 2026-01-12 | |||
| */ | |||
| @Service | |||
| public class SnGenServiceImpl extends ServiceImpl<SnGenMapper, SnGen> implements ISnGenService | |||
| { | |||
| /** | |||
| * true表示公平锁 | |||
| */ | |||
| private final Lock lock = new ReentrantLock(true); | |||
| @Override | |||
| public String getNewSn() { | |||
| lock.lock(); | |||
| try { | |||
| Integer sort = 1; | |||
| String date = DateUtils.dateTimeNow("yyMMdd"); | |||
| QueryWrapper<SnGen> queryWrapper = new QueryWrapper<>(); | |||
| queryWrapper.eq("date",date); | |||
| if(count(queryWrapper)==0){ | |||
| SnGen snGen = new SnGen(); | |||
| snGen.setDate(date); | |||
| snGen.setSn(sort); | |||
| save(snGen); | |||
| }else{ | |||
| baseMapper.addSn(date); | |||
| sort = getOne(queryWrapper,false).getSn(); | |||
| } | |||
| return date+"-"+String.format("%04d", sort); | |||
| } catch (Exception e) { | |||
| log.error("getNewSn执行被中断", e); | |||
| } finally { | |||
| lock.unlock(); | |||
| } | |||
| return null; | |||
| } | |||
| } | |||
| @ -0,0 +1,9 @@ | |||
| <?xml version="1.0" encoding="UTF-8" ?> | |||
| <!DOCTYPE mapper | |||
| PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |||
| "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
| <mapper namespace="com.hxhq.business.mapper.SnGenMapper"> | |||
| <update id="addSn"> | |||
| update t_sn_gen set sn = sn+1 where `date`=#{date} and del_flag='0' | |||
| </update> | |||
| </mapper> | |||