mitao
1 天以前 5b13db7e2c95385d69b8bdb449560bbbfe359146
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
package com.ruoyi.system.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.constants.AssetDeptConstant;
import com.ruoyi.system.dto.asset.AssetAdMaterialPriceDTO;
import com.ruoyi.system.mapper.AssetAdMaterialPriceMapper;
import com.ruoyi.system.model.AssetAdMaterialPrice;
import com.ruoyi.system.query.AssetAdMaterialPriceQuery;
import com.ruoyi.system.service.AssetAdMaterialPriceService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.vo.asset.AssetAdMaterialPriceVO;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 广告物料单价参考表 服务实现类
 * </p>
 *
 * @author WuGuanFengYue
 * @since 2025-09-15
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class AssetAdMaterialPriceServiceImpl extends ServiceImpl<AssetAdMaterialPriceMapper, AssetAdMaterialPrice> implements AssetAdMaterialPriceService {
    private final ISysUserService sysUserService;
    @Override
    public void addAssetAdMaterialPrice(AssetAdMaterialPriceDTO dto) {
        AssetAdMaterialPrice entity = BeanUtil.copyProperties(dto, AssetAdMaterialPrice.class);
        entity.setDeptId(Integer.parseInt(SecurityUtils.getLoginUser().getUser().getDeptId()));
        entity.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        entity.setCreateTime(LocalDateTime.now());
        save(entity);
    }
 
    @Override
    public void editAssetAdMaterialPrice(AssetAdMaterialPriceDTO dto) {
        AssetAdMaterialPrice entity = BeanUtil.copyProperties(dto, AssetAdMaterialPrice.class);
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        entity.setUpdateTime(LocalDateTime.now());
        updateById(entity);
    }
 
    @Override
    public IPage<AssetAdMaterialPriceVO> getPageList(AssetAdMaterialPriceQuery query) {
        // 数据权限:超级管理员/资产管理部查看所有数据,其他部门查看当前及下级部门的数据
        Long userId = SecurityUtils.getUserId();
        boolean isAdmin = SecurityUtils.isAdmin(userId);
 
        if (!isAdmin) {
            try {
                // 获取当前用户的部门名称
                String deptName = sysUserService.selectUserById(userId).getDeptName();
 
                // 非超级管理员且非资产管理部,设置部门权限
                if (!AssetDeptConstant.ASSET_INVENTORY_DEPT_NAMES.contains(deptName)) {
                    query.setDeptId(Integer.valueOf(SecurityUtils.getLoginUser().getDeptId()));
                }
            } catch (Exception e) {
                // 如果获取部门信息失败,默认设置部门权限
                try {
                    query.setDeptId(Integer.valueOf(SecurityUtils.getLoginUser().getDeptId()));
                } catch (Exception ex) {
                    // ignore parse, leave null if cannot parse
                }
            }
        }
        // 直接使用Mapper的分页查询方法
        Page<AssetAdMaterialPriceVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return baseMapper.getPageList(page, query);
    }
 
    @Override
    public AssetAdMaterialPriceVO getDetail(Integer id) {
        AssetAdMaterialPrice entity = getById(id);
        if (entity == null) {
            return null;
        }
        return BeanUtil.copyProperties(entity, AssetAdMaterialPriceVO.class);
    }
 
    @Override
    public void importAssetAdMaterialPrice(List<AssetAdMaterialPriceDTO> list) {
        if (CollUtil.isNotEmpty(list)) {
            String currentUser = SecurityUtils.getLoginUser().getUser().getNickName();
            LocalDateTime now = LocalDateTime.now();
 
            List<AssetAdMaterialPrice> entities = list.stream().map(dto -> {
                AssetAdMaterialPrice entity = BeanUtil.copyProperties(dto, AssetAdMaterialPrice.class);
                entity.setCreateBy(currentUser);
                entity.setCreateTime(now);
                return entity;
            }).collect(Collectors.toList());
 
            saveBatch(entities);
        }
    }
}