package com.gunshi.project.xyt.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.gunshi.core.result.R; import com.gunshi.project.xyt.entity.so.SzRuleByLawPage; import com.gunshi.project.xyt.model.SzRuleByLaw; import com.gunshi.project.xyt.service.FileAssociationsService; import com.gunshi.project.xyt.service.SzRuleByLawService; import com.gunshi.project.xyt.validate.markers.Insert; import com.gunshi.project.xyt.validate.markers.Update; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.io.Serializable; import java.util.Calendar; import java.util.Date; import java.util.Objects; /** * 描述: 法律法规管理表 * author: xusan * date: 2024-07-08 17:40:37 */ @Tag(name = "法律法规管理表") @RestController @RequestMapping(value = "/SzRuleByLaw") public class SzRuleByLawController extends AbstractCommonFileController { @Autowired private SzRuleByLawService service; @Autowired private FileAssociationsService fileService; @Operation(summary = "新增") @PostMapping("/insert") public R insert(@Validated(Insert.class) @RequestBody SzRuleByLaw dto) { if (service.lambdaQuery().eq(SzRuleByLaw::getName, dto.getName()) .count() > 0) { throw new IllegalArgumentException("当前名称已存在"); } dto.setId(IdWorker.getId()); dto.setCreateTime(new Date()); if (CollectionUtils.isNotEmpty(dto.getFiles())){ dto.setUploadDate(new Date()); } boolean result = service.save(dto); if (result) { fileService.saveFile(dto.getFiles(), getGroupId(), String.valueOf(dto.getId())); } return R.ok(result ? dto : null); } @Operation(summary = "修改") @PostMapping("/update") public R update(@Validated(Update.class) @RequestBody SzRuleByLaw dto) { if (service.lambdaQuery() .ne(SzRuleByLaw::getId, dto.getId()) .eq(SzRuleByLaw::getName, dto.getName()) .count() > 0) { throw new IllegalArgumentException("当前名称已存在"); } dto.setCreateTime(null); dto.setCreateBy(null); dto.setCreateName(null); dto.setUpdateTime(new Date()); if (CollectionUtils.isNotEmpty(dto.getFiles())){ dto.setUploadDate(new Date()); } boolean result = service.updateById(dto); if (result) { fileService.saveFile(dto.getFiles(), getGroupId(), String.valueOf(dto.getId())); } return R.ok(result ? dto : null); } @Operation(summary = "删除") @GetMapping("/del/{id}") public R del(@Schema(name = "id") @PathVariable("id") Serializable id) { if (Objects.isNull(service.getById(id))) { throw new IllegalArgumentException("当前数据不存在"); } return R.ok(service.removeById(id)); } @Operation(summary = "分页") @PostMapping("/page") public R> page(@RequestBody @Validated SzRuleByLawPage page) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); Calendar calendar=Calendar.getInstance(); if (Objects.nonNull(page.getType())) { query.eq(SzRuleByLaw::getType, page.getType()); } if (Objects.nonNull(page.getName())) { query.like(SzRuleByLaw::getName, page.getName()); } if (Objects.nonNull(page.getFillUnit())) { query.like(SzRuleByLaw::getFillUnit, page.getFillUnit()); } if (Objects.nonNull(page.getTimeliness())) { query.eq(SzRuleByLaw::getTimeliness, page.getTimeliness()); } if (Objects.nonNull(page.getStmAd())) { query.ge(SzRuleByLaw::getCreateTime, page.getStmAd()); } Date etmAd = page.getEtmAd(); if (Objects.nonNull(etmAd)) { calendar.setTime(etmAd); calendar.set(Calendar.HOUR,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); query.le(SzRuleByLaw::getCreateTime, calendar.getTime()); } if (Objects.nonNull(page.getStmIm())) { query.ge(SzRuleByLaw::getImplementationDate, page.getStmIm()); } Date etmIm = page.getEtmIm(); if (Objects.nonNull(etmIm)) { calendar.setTime(etmIm); calendar.set(Calendar.HOUR,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); query.le(SzRuleByLaw::getImplementationDate, calendar.getTime()); } if (Objects.nonNull(page.getStmUd())) { query.ge(SzRuleByLaw::getCreateTime, page.getStmUd()); } Date etmUd = page.getEtmUd(); if (Objects.nonNull(etmUd)) { calendar.setTime(etmUd); calendar.set(Calendar.HOUR,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); query.le(SzRuleByLaw::getCreateTime, calendar.getTime()); } query.orderByDesc(SzRuleByLaw::getCreateTime); Page data = service.page(page.getPageSo().toPage(), query); data.getRecords() .forEach(item -> item.setFiles(fileService.getFiles(getGroupId(), String.valueOf(item.getId())))); return R.ok(data); } @Override public String getGroupId() { return "SzRuleByLaw"; } }