1
luodangjia
2025-01-21 a42b76216efd31c3f322432e01232b4e0b7c8128
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
176
177
178
179
180
package com.ruoyi.company.service.impl;
 
import cn.idev.excel.FastExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.page.BeanUtils;
import com.ruoyi.common.core.page.PageDTO;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.company.api.domain.Company;
import com.ruoyi.company.api.domain.User;
import com.ruoyi.company.api.domain.dto.MgtCompanyDTO;
import com.ruoyi.company.api.domain.excel.MgtCompanyExcel;
import com.ruoyi.company.api.domain.query.MgtCompanyQuery;
import com.ruoyi.company.api.domain.vo.MgtCompanyVO;
import com.ruoyi.company.mapper.CompanyMapper;
import com.ruoyi.company.service.CompanyService;
import com.ruoyi.company.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;
 
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
    private final UserService userService;
    private final HttpServletResponse response;
    /**
     * 获取企业列表
     * @param query
     * @return
     */
    @Override
    public PageDTO<MgtCompanyVO> queryPage(MgtCompanyQuery query) {
        Page<Company> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(query.getCompanyInfo()), Company::getCompanyName, query.getCompanyInfo())
                .like(StringUtils.isNotBlank(query.getCompanyInfo()), Company::getSocialCode, query.getCompanyInfo())
                .like(StringUtils.isNotBlank(query.getLegalPersonInfo()), Company::getLegalPersonName, query.getLegalPersonInfo())
                .like(StringUtils.isNotBlank(query.getLegalPersonInfo()), Company::getIdCardNumber, query.getLegalPersonInfo())
                .like(StringUtils.isNotBlank(query.getContactInfo()), Company::getContactName, query.getContactInfo())
                .like(StringUtils.isNotBlank(query.getContactInfo()), Company::getContactPhone, query.getContactInfo())
                .page(new Page<>(query.getPageCurr(), query.getPageSize()));
        return PageDTO.of(page,MgtCompanyVO.class);
    }
 
    /**
     * 获取企业详情
     * @param id
     * @return
     */
    @Override
    public MgtCompanyVO queryCompanyDetail(Long id) {
        Company company = this.getById(id);
        MgtCompanyVO mgtCompanyVO = BeanUtils.copyBean(company, MgtCompanyVO.class);
        if (Objects.isNull(mgtCompanyVO)) {
            mgtCompanyVO = new MgtCompanyVO();
            return mgtCompanyVO;
        }
        User user = userService.getById(company.getUserId());
        if (Objects.nonNull(user)) {
            mgtCompanyVO.setPhone(user.getPhone());
            mgtCompanyVO.setAccountName(user.getAccountName());
        }
        return mgtCompanyVO;
    }
 
    /**
     * 新增企业
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveCompany(MgtCompanyDTO dto) {
        Long accountCount = userService.lambdaQuery().eq(User::getAccountName, dto.getAccountName()).count();
        if (accountCount > 0) {
            throw new ServiceException("账户名重复");
        }
        //添加用户信息
        User user = BeanUtils.copyBean(dto, User.class);
        user.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
        userService.save(user);
        // TODO 校验身份证信息
        Company company = BeanUtils.copyBean(dto, Company.class);
        company.setUserId(user.getUserId());
        //根据公司名称查询数据库
        Long count = this.lambdaQuery().eq(Company::getCompanyName, company.getCompanyName()).count();
        if (count > 0) {
            throw new ServiceException("该公司账号已存在");
        }
        this.save(company);
    }
 
    /**
     * 编辑企业
     * @param dto
     */
    @Override
    public void editCompany(MgtCompanyDTO dto) {
        if (Objects.isNull(dto.getId())){
            throw new ServiceException("企业id不能为空");
        }
        //查询企业
        Company company = this.getById(dto.getId());
        if (Objects.isNull(company)) {
            throw new ServiceException("该企业不存在");
        }
        //查询企业账号
        User user = userService.getById(company.getUserId());
        if (Objects.isNull(user)) {
            throw new ServiceException("该企业账号不存在");
        }
        Long accountCount = userService.lambdaQuery().ne(User::getUserId, user.getUserId()).eq(User::getAccountName, dto.getAccountName()).count();
        if (accountCount > 0) {
            throw new ServiceException("账户名重复");
        }
        //用户信息
        User userUpd = BeanUtils.copyBean(dto, User.class);
        userUpd.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
        userUpd.setUserId(user.getUserId());
        userUpd.setUpdateBy(SecurityUtils.getUserId());
        userService.updateById(userUpd);
        //根据公司名称查询数据库
        Long count = this.lambdaQuery().ne(Company::getId,dto.getId()).eq(Company::getCompanyName, company.getCompanyName()).count();
        if (count > 0) {
            throw new ServiceException("该公司账号已存在");
        }
        Company companyUpd = BeanUtils.copyBean(dto, Company.class);
        companyUpd.setId(company.getId());
        this.updateById(companyUpd);
    }
 
    /**
     * 删除企业
     * @param id
     */
    @Override
    public void deleteCompany(Long id) {
        //查询企业
        Company company = this.getById(id);
        if (Objects.isNull(company)) {
            throw new ServiceException("删除失败,该企业不存在");
        }
        //删除企业账号
         userService.removeById(company.getUserId());
 
        //删除企业信息
        this.removeById(id);
 
    }
 
    @Override
    public void export(MgtCompanyQuery query) throws IOException {
        List<Company> list = this.lambdaQuery()
                .like(StringUtils.isNotBlank(query.getCompanyInfo()), Company::getCompanyName, query.getCompanyInfo())
                .like(StringUtils.isNotBlank(query.getCompanyInfo()), Company::getSocialCode, query.getCompanyInfo())
                .like(StringUtils.isNotBlank(query.getLegalPersonInfo()), Company::getLegalPersonName, query.getLegalPersonInfo())
                .like(StringUtils.isNotBlank(query.getLegalPersonInfo()), Company::getIdCardNumber, query.getLegalPersonInfo())
                .like(StringUtils.isNotBlank(query.getContactInfo()), Company::getContactName, query.getContactInfo())
                .like(StringUtils.isNotBlank(query.getContactInfo()), Company::getContactPhone, query.getContactInfo()).list();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding
 
                ("utf-8");
        String fileName = URLEncoder.encode("企业信息导出数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
 
        FastExcel.write(response.getOutputStream(), MgtCompanyExcel.class)
                .sheet("企业信息导出数据")
                .doWrite(BeanUtils.copyList(list, MgtCompanyExcel.class));
    }
}