package com.ydtech.modules.admin.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ydtech.core.page.HttpResult; import com.ydtech.exception.SystemException; import com.ydtech.modules.admin.model.dto.SysUserDto; import com.ydtech.modules.admin.model.po.SysOrganizationQuotation; import com.ydtech.modules.admin.model.po.SysQuotationTheme; import com.ydtech.modules.admin.model.vo.SysQuotationThemeVo; import com.ydtech.modules.admin.model.vo.SysUserBaseVO; import com.ydtech.modules.admin.service.SysOrganizationQuotationService; import com.ydtech.modules.admin.service.SysQuotationThemeService; import com.ydtech.modules.admin.service.SysUserService; import com.ydtech.modules.base.controller.BaseController; import com.ydtech.modules.base.model.dto.PageDto; import com.ydtech.modules.protocol.utils.AssertionUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController @RequestMapping("/sys/quotation") @RequiredArgsConstructor @Api(tags = "报价单管理") public class SysQuotationController extends BaseController { @Autowired private SysOrganizationQuotationService sysOrganizationQuotationService; @Autowired private SysQuotationThemeService sysQuotationThemeService; @Autowired private SysUserService sysUserService; @ApiOperation(value = "分页查询报价单") @PostMapping(value = "page") public HttpResult> getpage(@RequestBody SysQuotationThemeVo sysQuotationThemeVo) { Page page = sysQuotationThemeService.getPage(sysQuotationThemeVo); return HttpResult.ok(page); } @ApiOperation(value = "获取所有报价单主题") @PostMapping(value = "list") public HttpResult> getlist() { List list = sysQuotationThemeService.list(); return HttpResult.ok(list); } @ApiOperation(value = "添加报价单主题") @PostMapping(value = "/save/sysQuotationTheme") public HttpResult save(@RequestBody SysQuotationTheme sysQuotationTheme) { SysQuotationTheme one = sysQuotationThemeService.getOne(new LambdaQueryWrapper().eq(SysQuotationTheme::getThemeCode, sysQuotationTheme.getThemeCode())); if (one != null) { return HttpResult.error("code重复,请重新输入"); } sysQuotationTheme.setUserId(getUserId()); sysQuotationTheme.setCreateTime(new Date()); boolean save = sysQuotationThemeService.save(sysQuotationTheme); AssertionUtils.isSucceed(save, "添加失败"); return HttpResult.ok("添加成功"); } @ApiOperation(value = "修改报价单主题") @PostMapping(value = "/update/sysQuotationTheme") public HttpResult update(@RequestBody SysQuotationTheme sysQuotationTheme) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysQuotationTheme::getThemeCode, sysQuotationTheme.getThemeCode()); SysQuotationTheme one = sysQuotationThemeService.getOne(wrapper); if (one != null && !one.getId().equals(sysQuotationTheme.getId())) { return HttpResult.error("code重复,请重新输入"); } sysQuotationTheme.setUserId(getUserId()); sysQuotationTheme.setCreateTime(new Date()); boolean update = sysQuotationThemeService.updateById(sysQuotationTheme); AssertionUtils.isSucceed(update, "修改失败"); return HttpResult.ok(sysQuotationTheme); } @ApiOperation(value = "删除报价单主题") @PostMapping(value = "/delete/sysQuotationTheme") public HttpResult delete(@RequestBody SysQuotationTheme sysQuotationTheme) { boolean delete = sysQuotationThemeService.removeById(sysQuotationTheme); AssertionUtils.isSucceed(delete, "删除失败"); return HttpResult.ok("删除成功"); } @ApiOperation(value = "用户查询分页") @PostMapping(value = "/findUserList") public HttpResult findUserList(@RequestBody PageDto pageDto) { if (pageDto.getDto().getDeptId() == null) { BaseController baseController = new BaseController(); pageDto.getDto().setDeptId(baseController.getDeptId()); } IPage userList = sysUserService.findAllUserList(pageDto); if (!userList.getRecords().isEmpty()) { sysOrganizationQuotationService.findUserList(userList); } return HttpResult.ok(userList); } @ApiOperation(value = "报价单配置") @PostMapping(value = "/config") public HttpResult config(@RequestBody SysOrganizationQuotation sysOrganizationQuotation) { if (sysOrganizationQuotation.getAuthorizationIds().isEmpty()) { return HttpResult.error("配置失败,未选中用户"); } // 构造查询条件,根据提供的用户ID集合查询出对应的报价单授权信息,然后进行删除 LambdaQueryWrapper removeWrapper = new LambdaQueryWrapper<>(); removeWrapper.in(SysOrganizationQuotation::getAuthorizationId, sysOrganizationQuotation.getAuthorizationIds()); sysOrganizationQuotationService.remove(removeWrapper); List authorizationIds = sysOrganizationQuotation.getAuthorizationIds(); List themecodes = sysOrganizationQuotation.getThemecodes(); // 遍历用户ID集合,为每个用户ID构造新的报价单授权信息,并添加到待保存的列表中 List quotations = new ArrayList<>(); for (String authorizationId : authorizationIds) { for (String themecode : themecodes) { SysOrganizationQuotation quotation = new SysOrganizationQuotation(); quotation.setAuthorizationId(authorizationId); quotation.setThemeCode(themecode); quotation.setUserId(getUserId()); quotation.setCreateTime(new Date()); quotations.add(quotation); } } // 批量保存新的报价单授权信息 boolean saveBatch = sysOrganizationQuotationService.saveBatch(quotations); // 断言添加操作成功,若失败则抛出异常 AssertionUtils.isSucceed(saveBatch, "配置失败"); return HttpResult.ok("配置成功"); } @ApiOperation(value = "获取用户对应报价单") @GetMapping(value = "/getThemeByUserId") public HttpResult< List> getThemeByUserId(String userId) { if(StringUtils.isBlank(userId)){ throw new SystemException("userId不能为空"); } LambdaQueryWrapper oQueryWrapper = new LambdaQueryWrapper<>(); oQueryWrapper.eq(SysOrganizationQuotation::getAuthorizationId, userId); List list = sysOrganizationQuotationService.list(oQueryWrapper); List themeCodes = new ArrayList<>(); if (list == null || list.isEmpty()) { themeCodes.add("QD"); } else { for (SysOrganizationQuotation quotation : list) { themeCodes.add(quotation.getThemeCode()); } } return HttpResult.ok(themeCodes); } }