| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package com.izouma.nineth.web;
- import com.izouma.nineth.domain.Company;
- import com.izouma.nineth.domain.UserBalance;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.CompanyRepo;
- import com.izouma.nineth.repo.UserBalanceRepo;
- import com.izouma.nineth.service.CompanyService;
- import com.izouma.nineth.utils.ObjUtils;
- import com.izouma.nineth.utils.SecurityUtils;
- import com.izouma.nineth.utils.excel.ExcelUtils;
- import lombok.AllArgsConstructor;
- import org.springframework.data.domain.Page;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.List;
- import java.util.Optional;
- @RestController
- @RequestMapping("/company")
- @AllArgsConstructor
- public class CompanyController extends BaseController {
- private CompanyService companyService;
- private CompanyRepo companyRepo;
- private UserBalanceRepo userBalanceRepo;
- @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
- @PostMapping("/save")
- public Company save(@RequestBody Company record) {
- if (record.getId() != null) {
- Company orig = companyRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
- ObjUtils.merge(orig, record);
- return companyRepo.save(orig);
- }
- return companyRepo.save(record);
- }
- @PreAuthorize("hasRole('ADMIN') || hasRole('META')")
- @PostMapping("/all")
- public Page<Company> all(@RequestBody PageQuery pageQuery) {
- return companyService.all(pageQuery);
- }
- @GetMapping("/get/{id}")
- public Company get(@PathVariable Long id) {
- return companyRepo.findById(id).orElseThrow(new BusinessException("无记录"));
- }
- @PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- companyRepo.softDelete(id);
- }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<Company> data = all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'SAAS')")
- @PostMapping("/addAdmin")
- public void addAdmin(@RequestParam Long companyId, @RequestParam String username, @RequestParam String password) {
- companyService.addAdmin(companyId, username, password);
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'SAAS')")
- @PostMapping("/delAdmin")
- public void delAdmin(@RequestParam Long companyId, @RequestParam Long userId) {
- companyService.delAdmin(companyId, userId);
- }
- @GetMapping("/balance")
- public UserBalance balance() {
- Long companyId = Optional.ofNullable(SecurityUtils.getAuthenticatedUser().getCompanyId()).orElseThrow(new BusinessException("非企业账号"));
- return userBalanceRepo.findByUserId(companyId).orElse(new UserBalance(companyId));
- }
- }
|