cloudroam
4 天以前 46715d892da947c31f07796fdc79dbbef06677b3
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.mzl.flower.service.film.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.BatchDTO;
import com.mzl.flower.dto.request.film.AiContentTaskConfigDTO;
import com.mzl.flower.dto.request.film.AiContentTaskConfigQueryDTO;
import com.mzl.flower.dto.response.film.AiContentTaskConfigVO;
import com.mzl.flower.entity.film.AiContentTaskConfig;
import com.mzl.flower.entity.system.Role;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.film.AiContentTaskConfigMapper;
import com.mzl.flower.schedule.DynamicTaskManager;
import com.mzl.flower.service.film.AiContentTaskConfigService;
import com.mzl.flower.service.system.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 影视作品信息表 服务实现类
 * </p>
 *
 * @author generator@Fang
 * @since 2025-05-19
 */
@Service
@Transactional
@RequiredArgsConstructor
public class AiContentTaskConfigServiceImpl extends ServiceImpl<AiContentTaskConfigMapper, AiContentTaskConfig> implements AiContentTaskConfigService {
 
    private final AiContentTaskConfigMapper aiContentTaskConfigMapper;
    private final CustomerMapper customerMapper;
    private final RoleService roleService;
 
    private final DynamicTaskManager dynamicTaskManager;
 
    @Override
    public List<AiContentTaskConfigVO> getAiContentTaskConfigAll() {
        return aiContentTaskConfigMapper.getAiContentTaskConfigAllInfo();
    }
 
    @Override
    public List<AiContentTaskConfigVO> getEnabledAiContentTaskConfig() {
        return aiContentTaskConfigMapper.getEnabledAiContentTaskConfig();
    }
 
    @Override
    public Page<AiContentTaskConfigVO> queryPage(AiContentTaskConfigQueryDTO dto, Page page) {
        List<AiContentTaskConfigVO> list = aiContentTaskConfigMapper.queryPage(dto, page);
        // 测试前端展示用代码,部署发布不适用
        page.setRecords(list);
 
        return page;
    }
 
    @Override
    @Transactional
    public void deleteAiContentTaskConfig(String id) {
        AiContentTaskConfig filmWork = aiContentTaskConfigMapper.selectById(id);
        if (filmWork == null) {
            throw new ValidationException("找不到id为" + id + "的影视作品");
        }
        aiContentTaskConfigMapper.deleteById(id);
        dynamicTaskManager.refreshTasks(); // 每次删除后刷新定时任务
 
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                dynamicTaskManager.refreshTasks();
            }
        });
    }
 
 
    @Override
    @Transactional
    public void saveAiContentTaskConfig(AiContentTaskConfigDTO aiContentTaskConfigDTO) {
        //获取当前人员角色,判断是不是编辑角色
        List<String> roleIds = new ArrayList<>();
        List<Role> roleList = roleService.getUserRoleList(SecurityUtils.getUserId());
        for (Role role : roleList) {
            roleIds.add(role.getId());
        }
//        if (!roleIds.contains("8f9ef89f6b2d4d8e9ea1fc8d2f25ce69")) {
//            throw new ValidationException("非编辑角色不能新增");
//        }
        //  转换
        AiContentTaskConfig aiContentTaskConfig = new AiContentTaskConfig();
        BeanUtils.copyProperties(aiContentTaskConfigDTO, aiContentTaskConfig);
        aiContentTaskConfig.create();
        aiContentTaskConfigMapper.insert(aiContentTaskConfig);
 
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                dynamicTaskManager.refreshTasks();
            }
        });
    }
 
    @Override
    @Transactional
    public void updateAiContentTaskConfig(AiContentTaskConfigDTO aiContentTaskConfigDTO) {
        AiContentTaskConfig aiContentTaskConfig = new AiContentTaskConfig();
 
        List<String> roleIds = new ArrayList<>();
        List<Role> roleList = roleService.getUserRoleList(SecurityUtils.getUserId());
        for (Role role : roleList) {
            roleIds.add(role.getId());
        }
//        if (!roleIds.contains("8f9ef89f6b2d4d8e9ea1fc8d2f25ce69")) {
//            throw new ValidationException("非编辑角色不能修改");
//        }
        aiContentTaskConfig = aiContentTaskConfigMapper.selectById(aiContentTaskConfigDTO.getId());
        BeanUtils.copyProperties(aiContentTaskConfigDTO, aiContentTaskConfig,"type");
        aiContentTaskConfig.update(SecurityUtils.getUserId());
 
        aiContentTaskConfigMapper.updateById(aiContentTaskConfig);
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                dynamicTaskManager.refreshTasks();
            }
        });
    }
 
 
    @Override
    public AiContentTaskConfigVO detail(Long id) {
        // userId 可以是空,因为用户可以登录
 
        String userId = SecurityUtils.getUserId();
        AiContentTaskConfigVO aiContentTaskConfigVO = aiContentTaskConfigMapper.selectInfoById(id, userId);
        if (aiContentTaskConfigVO == null) {
            return null;
        }
//        CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(aiContentTaskConfigVO.getCreateBy());
//        if (!ObjectUtils.isEmpty(currentCustomer)) {
//            aiContentTaskConfigVO.setNickname(currentCustomer.getNickName());
//            aiContentTaskConfigVO.setAvatar(currentCustomer.getCover());
//        }
 
        return aiContentTaskConfigVO;
    }
 
    @Override
    public List<AiContentTaskConfig> getEnabledConfigs() {
        return aiContentTaskConfigMapper.getAiContentTaskConfigAll();
    }
 
    @Override
    @Transactional
    public void batchDelete(BatchDTO dto) {
        aiContentTaskConfigMapper.deleteBatchIds(dto.getIds());
 
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                dynamicTaskManager.refreshTasks();
            }
        });
    }
 
 
}