package com.zh.project0512.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper; import com.fasterxml.jackson.annotation.JsonFormat; import com.zh.project0512.annotation.tokenValid; import com.zh.project0512.model.*; import com.zh.project0512.service.IReferenceBrandService; import com.zh.project0512.service.IReferenceGroupService; import com.zh.project0512.service.IReferenceService; import com.zh.project0512.service.IReferenceTagService; import com.zh.project0512.utils.FileTypeUtil; import com.zh.project0512.utils.JwtUtil; import com.zh.project0512.utils.MybatisPlusUtil; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; import io.jsonwebtoken.Claims; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.HashMap; import java.util.List; import java.util.Map; /** *

* 前端控制器 *

* * @author zh * @since 2022-05-23 */ @RestController @RequestMapping("/reference") @Tag(name = "素材") public class ReferenceController { @Autowired private IReferenceService referenceService; @Autowired private IReferenceTagService referenceTagService; @Autowired private IReferenceBrandService referenceBrandService; @Operation(summary = "添加素材") @PostMapping("/add") public Result add(@Validated @RequestBody Reference reference) { String fileName = reference.getFileUrl(); reference.setType(FileTypeUtil.getcontentTypeNum(fileName.substring(fileName.lastIndexOf(".")))); referenceService.save(reference.setCreatAt(LocalDateTime.now())); referenceTagService.addGroup(reference.getTagList(), reference.getId()); referenceBrandService.addGroup(reference.getBrandList(), reference.getId()); return Result.success("添加完成!"); } @Data static class AddTagParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; @NotNull(message = "tagId不能为空") @Min(value = 1, message = "tagId最小值为1") @Schema(title = "标签id") private Integer tagId; } @Operation(summary = "素材新增标签") @PostMapping("/addTag") public Result addTag(@Validated @RequestBody AddTagParam param) { if (null != referenceTagService.getMap(Wrappers.query().lambda().eq(ReferenceTag::getReferenceId, param.getId()).eq(ReferenceTag::getTagId, param.getTagId()))) { return Result.success("数据已存在"); } referenceTagService.saveOrUpdate(new ReferenceTag().setReferenceId(param.getId()).setTagId(param.getTagId())); return Result.success("添加完成"); } @Data static class AddBrandParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; @NotNull(message = "brandId不能为空") @Min(value = 1, message = "brandId最小值为1") @Schema(title = "品牌id") private Integer brandId; } @Operation(summary = "素材新增品牌") @PostMapping("/addBrand") public Result addBrand(@Validated @RequestBody AddBrandParam param) { if (null != referenceBrandService.getMap(Wrappers.query().lambda().eq(ReferenceBrand::getReferenceId, param.getId()).eq(ReferenceBrand::getBrandId, param.getBrandId()))) { return Result.success("数据已存在"); } referenceBrandService.saveOrUpdate(new ReferenceBrand().setReferenceId(param.getId()).setBrandId(param.getBrandId())); return Result.success("添加完成"); } @Data static class DelRParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; } @Operation(summary = "删除素材") @PostMapping("/del") public Result del(@Validated @RequestBody DelRParam param) { return MybatisPlusUtil.sqlResult(referenceService.removeById(param.getId()), "删除"); } @Data static class DelTagParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; @NotNull(message = "tagId不能为空") @Min(value = 1, message = "tagId最小值为1") @Schema(title = "标签id") private Integer tagId; } @Operation(summary = "素材删除标签") @PostMapping("/delTag") public Result delTag(@Validated @RequestBody DelTagParam param) { return MybatisPlusUtil.sqlResult(referenceTagService.remove(Wrappers.query().lambda().eq(ReferenceTag::getReferenceId, param.getId()).eq(ReferenceTag::getTagId, param.getTagId())), "删除"); } @Data static class DelBrandParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; @NotNull(message = "brandId不能为空") @Min(value = 1, message = "brandId最小值为1") @Schema(title = "品牌id") private Integer brandId; } @Operation(summary = "素材删除品牌") @PostMapping("/delBrand") public Result delBrand(@Validated @RequestBody DelBrandParam param) { return MybatisPlusUtil.sqlResult(referenceBrandService.remove(Wrappers.query().lambda().eq(ReferenceBrand::getReferenceId, param.getId()).eq(ReferenceBrand::getBrandId, param.getBrandId())), "删除"); } @Data static class UpdRParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; @Schema(title = "名称") private String title; @Schema(title = "文件地址") private String fileUrl; @Schema(title = "封面图片地址") private String coverUrl; } @Operation(summary = "更新素材") @PostMapping("/upd") public Result upd(@Validated @RequestBody UpdRParam param) { int id = param.getId(); Reference reference = new Reference().setTitle(param.getTitle()).setFileUrl(param.getFileUrl()).setCoverUrl(param.getCoverUrl()).setUpdateAt(LocalDateTime.now()); if (param.getFileUrl() != null) { String fileName = param.getFileUrl(); reference.setType(FileTypeUtil.getcontentTypeNum(fileName.substring(fileName.lastIndexOf(".")))); } UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id", id); return MybatisPlusUtil.sqlResult(referenceService.update(reference, updateWrapper), "修改"); } @Data static class ListRParam { private int pageNum; private int pageSize; @Schema(title = "素材分组id") private Integer groupId; @Schema(title = "月份区间") private Integer month; } @Operation(summary = "素材列表") @PostMapping("/list") public Result list(@Validated @RequestBody ListRParam param,@RequestHeader("token") @Parameter(name = "登录token") String token) { Claims claims = new JwtUtil().parseJWT(token); QueryWrapper queryWrapper = new QueryWrapper<>(); JSONObject obj = new JSONObject(); obj.put("pageNum",param.getPageNum()); obj.put("pageSize",param.getPageSize()); Integer groupId = param.groupId; if(groupId != null){ queryWrapper.eq("groupId",groupId); } queryWrapper.orderByDesc("creatAt"); IPage iPage = referenceService.selectPage(MybatisPlusUtil.SetPage(obj), queryWrapper); return Result.success(iPage); } @Data static class DetPParam { @NotNull(message = "id不能为空") @Min(value = 1, message = "id最小值为1") @Schema(title = "素材id") private Integer id; } @Operation(summary = "详情") @PostMapping("/detail") public Result detail(@Validated @RequestBody DetPParam param) { int id =param.getId(); Reference reference = referenceService.getById(id); return reference != null ? Result.success(reference.setBrandList(referenceBrandService.selByReferenceId(id)).setTagList(referenceTagService.selByReferenceId(id))) : Result.fail(HttpStatusEnum.NOT_FOUND); } @Data static class DetaPParam { @NotNull(message = "日期不能为空") @Schema(title = "日期") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") private LocalDateTime date; } @Operation(summary = "按日期查询") @PostMapping("/date") public Result detail(@Validated @RequestBody DetaPParam param) { LocalDateTime dayBegin = LocalDateTime.of(param.getDate().toLocalDate(), LocalTime.MIN); LocalDateTime dayLast = LocalDateTime.of(param.getDate().toLocalDate(), LocalTime.MAX); List list = referenceService.lambdaQuery().between(Reference::getCreatAt, dayBegin, dayLast).list(); return Result.success(list); } }