| | |
| | | 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.UserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | 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 |
| | | public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService { |
| | | private final UserService userService; |
| | | private final HttpServletResponse response; |
| | | /** |
| | | * 获取企业列表 |
| | | * @param query |
| | |
| | | } |
| | | 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)); |
| | | } |
| | | } |