xiaoyong931011
2022-06-07 7fa0a141a3c88ca90c67c966260bf26f03f255da
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
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddBelongDto;
import com.xcong.farmer.cms.modules.system.dto.AdminBelongDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateBelongDto;
import com.xcong.farmer.cms.modules.system.entity.BelongEntity;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.BelongMapper;
import com.xcong.farmer.cms.modules.system.service.IBelongService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.modules.system.vo.AdminBelongVo;
import com.xcong.farmer.cms.modules.system.vo.AdminSeeBelongInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
 
import cn.hutool.core.util.ObjectUtil;
import org.springframework.transaction.annotation.Transactional;
 
@Service
@Slf4j
public class BelongServiceImpl extends ServiceImpl<BelongMapper, BelongEntity> implements IBelongService {
    @Override
    public Result getBelongInPage(AdminBelongDto adminBelongDto) {
        Page<AdminBelongVo> page = new Page<>(adminBelongDto.getPageNum(), adminBelongDto.getPageSize());
        BelongEntity belongEntity = new BelongEntity();
        String name = adminBelongDto.getName();
        if(StrUtil.isNotEmpty(name)){
            belongEntity.setName(name);
        }
        IPage<AdminBelongVo> list = this.baseMapper.selectAdminBelongInPage(page,belongEntity);
        return Result.ok(list);
    }
 
    @Override
    @Transactional
    public Result addBelong(AdminAddBelongDto adminAddBelongDto) {
        String name = adminAddBelongDto.getName();
        if(StrUtil.isEmpty(name)){
            return Result.fail("请输入名称");
        }
        String code = adminAddBelongDto.getCode();
        if(StrUtil.isEmpty(code)){
            return Result.fail("请输入编码");
        }
        QueryWrapper<BelongEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("code",code);
        List<BelongEntity> belongEntities = this.baseMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(belongEntities)){
            return Result.fail("编码已存在,请重新输入");
        }
        BelongEntity belongEntity = new BelongEntity();
        belongEntity.setName(name);
        belongEntity.setCode(code);
        String webAddress = adminAddBelongDto.getWebAddress();
        if(StrUtil.isNotEmpty(webAddress)){
            belongEntity.setWebAddress(webAddress);
        }
        String remark = adminAddBelongDto.getRemark();
        if(StrUtil.isNotEmpty(remark)){
            belongEntity.setRemark(remark);
        }
        String pic = adminAddBelongDto.getPic();
        if(StrUtil.isNotEmpty(pic)){
            belongEntity.setPic(pic);
        }
        this.baseMapper.insert(belongEntity);
        return Result.ok("添加成功");
    }
 
    @Override
    public Result seeBelongInfo(Long id) {
        BelongEntity belongEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(belongEntity)){
            Result.fail("所属公司不存在");
        }
        AdminSeeBelongInfoVo adminSeeBelongInfoVo = new AdminSeeBelongInfoVo();
        adminSeeBelongInfoVo.setId(belongEntity.getId());
        adminSeeBelongInfoVo.setName(belongEntity.getName());
        adminSeeBelongInfoVo.setCode(belongEntity.getCode());
        adminSeeBelongInfoVo.setRemark(belongEntity.getRemark());
        adminSeeBelongInfoVo.setWebAddress(belongEntity.getWebAddress());
        adminSeeBelongInfoVo.setPic(belongEntity.getPic());
        return Result.ok(adminSeeBelongInfoVo);
    }
 
    @Override
    @Transactional
    public Result updateBelong(AdminUpdateBelongDto adminUpdateBelongDto) {
        Long id = adminUpdateBelongDto.getId();
        BelongEntity belongEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(belongEntity)){
            Result.fail("所属公司不存在");
        }
        String name = adminUpdateBelongDto.getName();
        if(StrUtil.isEmpty(name)){
            return Result.fail("请输入名称");
        }
        belongEntity.setName(name);
        String code = adminUpdateBelongDto.getCode();
        if(StrUtil.isEmpty(code)){
            return Result.fail("请输入编码");
        }
        if(!belongEntity.getCode().equals(code)){
            QueryWrapper<BelongEntity> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("code",code);
            List<BelongEntity> belongEntities = this.baseMapper.selectList(objectQueryWrapper);
            if(CollUtil.isNotEmpty(belongEntities)){
                return Result.fail("编码已存在,请重新输入");
            }
            belongEntity.setCode(code);
        }
        belongEntity.setWebAddress(adminUpdateBelongDto.getWebAddress());
        belongEntity.setRemark(adminUpdateBelongDto.getRemark());
        belongEntity.setPic(adminUpdateBelongDto.getPic());
        this.baseMapper.updateById(belongEntity);
        return Result.ok("更新成功");
    }
 
    @Override
    public Result getBelongInList() {
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        Long belongId = userlogin.getBelongId();
        QueryWrapper<BelongEntity> objectQueryWrapper = new QueryWrapper<>();
        if(belongId != 0L){
            objectQueryWrapper.eq("id",belongId);
        }
        List<BelongEntity> belongEntities = this.baseMapper.selectList(objectQueryWrapper);
        List<AdminBelongVo> adminBelongVos = new ArrayList<>();
        if(CollUtil.isNotEmpty(belongEntities)){
            for(BelongEntity belongEntity : belongEntities){
                AdminBelongVo adminBelongVo = new AdminBelongVo();
                adminBelongVo.setId(belongEntity.getId());
                adminBelongVo.setName(belongEntity.getName());
                adminBelongVo.setCode(belongEntity.getCode());
                adminBelongVos.add(adminBelongVo);
            }
        }
        return Result.ok(adminBelongVos);
    }
}