mitao
13 小时以前 d4e4dc4a180cea919dfc86a1a3e5af0abe8b6d36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.ruoyi.web.controller.api;
 
 
import cn.idev.excel.FastExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.dto.asset.AssetAdDTO;
import com.ruoyi.system.dto.asset.AssetAdRentalRecordDTO;
import com.ruoyi.system.model.AssetAdRentalRecord;
import com.ruoyi.system.query.AssetAdQuery;
import com.ruoyi.system.service.AssetAdRentalRecordService;
import com.ruoyi.system.service.AssetAdService;
import com.ruoyi.system.vo.asset.AssetAdDetailVO;
import com.ruoyi.system.vo.asset.AssetAdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;
 
/**
 * <p>
 * 广告无形资产表 前端控制器
 * </p>
 *
 * @author WuGuanFengYue
 * @since 2025-09-15
 */
@Api(tags = {"广告无形资产相关接口"})
@Slf4j
@Validated
@RestController
@RequestMapping("/asset-ad")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class AssetAdController {
 
    private final AssetAdService assetAdService;
    private final AssetAdRentalRecordService assetAdRentalRecordService;
 
    @ApiOperation(value = "新增广告无形资产")
    @Log(title = "新增广告无形资产", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<?> addAssetAd(@Valid @RequestBody AssetAdDTO dto) {
        assetAdService.addAssetAd(dto);
        return R.ok();
    }
 
    @ApiOperation(value = "编辑广告无形资产")
    @Log(title = "编辑广告无形资产", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<?> editAssetAd(@Valid @RequestBody AssetAdDTO dto) {
        assetAdService.editAssetAd(dto);
        return R.ok();
    }
 
    @ApiOperation("获取分页列表")
    @PostMapping("/page")
    public R<IPage<AssetAdVO>> getPageList(@RequestBody AssetAdQuery query) {
        return R.ok(assetAdService.getPageList(query));
    }
 
    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public R<AssetAdDetailVO> getDetail(@ApiParam(name = "id", value = "广告无形资产ID") @PathVariable Integer id) {
        return R.ok(assetAdService.getDetail(id));
    }
 
    @ApiOperation("下载导入模板")
    @GetMapping("/template")
    public void getTemplate(HttpServletResponse response){
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = null;
        try {
            fileName = URLEncoder.encode("广告无形资产导入模板", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            FastExcel.write(response.getOutputStream(), AssetAdDTO.class)
                    .sheet("广告无形资产")
                    .doWrite(Collections.emptyList());
        } catch (IOException e) {
            log.error("下载导入模板异常", e);
        }
    }
 
    @ApiOperation("导入")
    @Log(title = "导入广告无形资产", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<?> importAssetAd(@RequestPart("file") MultipartFile file){
        if (file.isEmpty()) {
            return R.fail("请选择一个文件上传!");
        }
        try {
            List<AssetAdDTO> list = FastExcel.read(file.getInputStream()).head(AssetAdDTO.class)
                    .sheet()
                    .doReadSync();
            assetAdService.importAssetAd(list);
            return R.ok();
        } catch (IOException e) {
            log.error("文件处理失败", e);
            return R.fail("文件处理失败!");
        }
    }
 
    @ApiOperation("删除")
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/{id}")
    public R<?> delete(@ApiParam(name = "id", value = "广告无形资产ID") @PathVariable Integer id) {
        assetAdService.removeById(id);
        assetAdRentalRecordService.lambdaUpdate().eq(AssetAdRentalRecord::getAssetAdId, id).remove();
        return R.ok();
    }
 
    @ApiOperation("添加出租记录")
    @PostMapping("/rental/add")
    public R<?> addRentalRecord(@Valid @RequestBody AssetAdRentalRecordDTO dto){
        assetAdRentalRecordService.addRentalRecord(dto);
        return R.ok();
    }
 
    @ApiOperation("编辑出租记录")
    @PostMapping("/rental/edit")
    public R<?> editRentalRecord(@Valid @RequestBody AssetAdRentalRecordDTO dto){
        assetAdRentalRecordService.editRentalRecord(dto);
        return R.ok();
    }
 
    @ApiOperation("删除出租记录")
    @DeleteMapping("/rental/delete/{id}")
    public R<?> deleteRentalRecord(@ApiParam(name = "id", value = "广告无形资产ID") @PathVariable Integer id){
        assetAdRentalRecordService.removeById(id);
        return R.ok();
    }
}