xiaoyong931011
2022-06-13 089f77fc53b619f4e00f706370a9ade523221f5d
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
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.core.toolkit.StringPool;
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.AdminAddCompanyDto;
import com.xcong.farmer.cms.modules.system.dto.AdminBelongDto;
import com.xcong.farmer.cms.modules.system.dto.AdminDeleteDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateCompanyDto;
import com.xcong.farmer.cms.modules.system.entity.CompanyEntity;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
import com.xcong.farmer.cms.modules.system.service.ICompanyService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.modules.system.vo.AdminCompanyVo;
import com.xcong.farmer.cms.modules.system.vo.AdminSeeCompanyInfoVo;
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 CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyEntity> implements ICompanyService {
    @Override
    public Result getBelongInPage(AdminBelongDto adminBelongDto) {
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        Long belongId = userlogin.getCompanyId();
        Page<AdminCompanyVo> page = new Page<>(adminBelongDto.getPageNum(), adminBelongDto.getPageSize());
        CompanyEntity companyEntity = new CompanyEntity();
        String name = adminBelongDto.getName();
        if(StrUtil.isNotEmpty(name)){
            companyEntity.setName(name);
        }
        if(belongId != UserEntity.USER_BELONG_TOP){
            companyEntity.setId(belongId);
        }
        IPage<AdminCompanyVo> list = this.baseMapper.selectAdminBelongInPage(page, companyEntity);
        return Result.ok(list);
    }
 
    @Override
    @Transactional
    public Result addBelong(AdminAddCompanyDto adminAddCompanyDto) {
        String name = adminAddCompanyDto.getName();
        String code = adminAddCompanyDto.getCode();
        QueryWrapper<CompanyEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("code",code);
        List<CompanyEntity> belongEntities = this.baseMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(belongEntities)){
            return Result.fail("编码已存在,请重新输入");
        }
        CompanyEntity companyEntity = new CompanyEntity();
        companyEntity.setName(name);
        companyEntity.setCode(code);
        String webAddress = adminAddCompanyDto.getWebAddress();
            companyEntity.setWebAddress(webAddress);
        String remark = adminAddCompanyDto.getRemark();
            companyEntity.setRemark(remark);
        String pic = adminAddCompanyDto.getPic();
            companyEntity.setPic(pic);
        this.baseMapper.insert(companyEntity);
        return Result.ok("添加成功");
    }
 
    @Override
    public Result seeBelongInfo(Long id) {
        CompanyEntity companyEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(companyEntity)){
            Result.fail("所属公司不存在");
        }
        AdminSeeCompanyInfoVo adminSeeCompanyInfoVo = new AdminSeeCompanyInfoVo();
        adminSeeCompanyInfoVo.setId(companyEntity.getId());
        adminSeeCompanyInfoVo.setName(companyEntity.getName());
        adminSeeCompanyInfoVo.setCode(companyEntity.getCode());
        adminSeeCompanyInfoVo.setRemark(companyEntity.getRemark());
        adminSeeCompanyInfoVo.setWebAddress(companyEntity.getWebAddress());
        adminSeeCompanyInfoVo.setPic(companyEntity.getPic());
        return Result.ok(adminSeeCompanyInfoVo);
    }
 
    @Override
    @Transactional
    public Result updateBelong(AdminUpdateCompanyDto adminUpdateCompanyDto) {
        Long id = adminUpdateCompanyDto.getId();
        CompanyEntity companyEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(companyEntity)){
            Result.fail("所属公司不存在");
        }
        String name = adminUpdateCompanyDto.getName();
        companyEntity.setName(name);
        String code = adminUpdateCompanyDto.getCode();
        if(!companyEntity.getCode().equals(code)){
            QueryWrapper<CompanyEntity> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("code",code);
            List<CompanyEntity> belongEntities = this.baseMapper.selectList(objectQueryWrapper);
            if(CollUtil.isNotEmpty(belongEntities)){
                return Result.fail("编码已存在,请重新输入");
            }
            companyEntity.setCode(code);
        }
        companyEntity.setWebAddress(adminUpdateCompanyDto.getWebAddress());
        companyEntity.setRemark(adminUpdateCompanyDto.getRemark());
        companyEntity.setPic(adminUpdateCompanyDto.getPic());
        this.baseMapper.updateById(companyEntity);
        return Result.ok("更新成功");
    }
 
    @Override
    public Result getBelongInList() {
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        Long companyId = userlogin.getCompanyId();
        QueryWrapper<CompanyEntity> objectQueryWrapper = new QueryWrapper<>();
        if(companyId != UserEntity.USER_BELONG_TOP){
            objectQueryWrapper.eq("id",companyId);
        }
        List<CompanyEntity> belongEntities = this.baseMapper.selectList(objectQueryWrapper);
        List<AdminCompanyVo> adminCompanyVos = new ArrayList<>();
        if(CollUtil.isNotEmpty(belongEntities)){
            for(CompanyEntity companyEntity : belongEntities){
                AdminCompanyVo adminCompanyVo = new AdminCompanyVo();
                adminCompanyVo.setId(companyEntity.getId());
                adminCompanyVo.setName(companyEntity.getName());
                adminCompanyVo.setCode(companyEntity.getCode());
                adminCompanyVos.add(adminCompanyVo);
            }
        }
        return Result.ok(adminCompanyVos);
    }
 
    @Override
    public Result delObjs(AdminDeleteDto adminDeleteDto) {
        String ids = adminDeleteDto.getIds();
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        if(userlogin.getCompanyId() != UserEntity.USER_BELONG_TOP){
            return Result.fail("没有删除权限");
        }
        if(StrUtil.isNotEmpty(ids)){
            String[] companyIds = ids.split(StringPool.COMMA);
            for(String companyIdStr : companyIds){
                Long companyId = Long.valueOf(companyIdStr);
                this.baseMapper.deleteById(companyId);
            }
        }
        return Result.ok("删除成功");
    }
}