package org.jeecg.modules.demo.contract.controller;
|
|
import java.io.UnsupportedEncodingException;
|
import java.io.IOException;
|
import java.net.URLDecoder;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
import com.alibaba.fastjson.JSONObject;
|
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import jakarta.websocket.server.PathParam;
|
import org.jeecg.modules.demo.customer.entity.Customer;
|
import org.jeecg.modules.demo.customer.service.ICustomerService;
|
import org.jeecg.modules.system.controller.SysUserController;
|
import org.jeecg.modules.system.entity.SysUser;
|
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
import org.jeecgframework.poi.excel.entity.ExportParams;
|
import org.jeecgframework.poi.excel.entity.ImportParams;
|
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
import org.jeecg.common.system.vo.LoginUser;
|
import org.apache.shiro.SecurityUtils;
|
import org.jeecg.common.api.vo.Result;
|
import org.jeecg.common.system.query.QueryGenerator;
|
import org.jeecg.common.system.query.QueryRuleEnum;
|
import org.jeecg.common.util.oConvertUtils;
|
import org.jeecg.modules.demo.contract.entity.ContractFile;
|
import org.jeecg.modules.demo.contract.entity.SemanticWord;
|
import org.jeecg.modules.demo.contract.entity.Contract;
|
import org.jeecg.modules.demo.contract.vo.ContractPage;
|
import org.jeecg.modules.demo.contract.service.IContractService;
|
import org.jeecg.modules.demo.contract.service.IContractFileService;
|
import org.jeecg.modules.demo.contract.service.ISemanticWordService;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import lombok.extern.slf4j.Slf4j;
|
import com.alibaba.fastjson.JSON;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.Operation;
|
import org.jeecg.common.aspect.annotation.AutoLog;
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
/**
|
* @Description: 合同
|
* @Author: jeecg-boot
|
* @Date: 2025-10-11
|
* @Version: V1.0
|
*/
|
@Tag(name="合同")
|
@RestController
|
@RequestMapping("/contract/contract")
|
@Slf4j
|
public class ContractController {
|
@Autowired
|
private IContractService contractService;
|
@Autowired
|
private IContractFileService contractFileService;
|
@Autowired
|
private ISemanticWordService semanticWordService;
|
@Autowired
|
private ICustomerService customerService;
|
|
/**
|
* 分页列表查询
|
*
|
* @param contract
|
* @param pageNo
|
* @param pageSize
|
* @param req
|
* @return
|
*/
|
//@AutoLog(value = "合同-分页列表查询")
|
@Operation(summary="合同-分页列表查询")
|
@GetMapping(value = "/list")
|
public Result<IPage<Contract>> queryPageList(Contract contract,
|
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
|
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
|
@RequestParam(name="role", defaultValue="无") String role,
|
@RequestParam(name="user", defaultValue="无") String user,
|
HttpServletRequest req) {
|
// 自定义查询规则
|
Map<String, QueryRuleEnum> customeRuleMap = new HashMap<>();
|
// 自定义多选的查询规则为:LIKE_WITH_OR
|
customeRuleMap.put("reviewStatus", QueryRuleEnum.LIKE_WITH_OR);
|
QueryWrapper<Contract> queryWrapper = QueryGenerator.initQueryWrapper(contract, req.getParameterMap(),customeRuleMap);
|
if (role.equals("无"));
|
else if (role.equals("1972228581703651330")){//销售
|
if (user.equals("无")){
|
return Result.error("请输入销售名称");//销售根据用户筛选
|
}
|
queryWrapper.eq("create_by", user);//该销售创建的
|
queryWrapper.or();//或
|
queryWrapper.eq("agent_auditor", user);//代理商分配给该销售的
|
}else if (role.equals("1972228688033452034")){//财务
|
queryWrapper.ne("review_status", "8");
|
}
|
Page<Contract> page = new Page<Contract>(pageNo, pageSize);
|
IPage<Contract> pageList = contractService.page(page, queryWrapper);
|
List<SemanticWord> semanticWordList = null;
|
for (Contract contract1 : pageList.getRecords()){
|
semanticWordList = semanticWordService.selectByMainId(contract1.getId());
|
for (SemanticWord semanticWord : semanticWordList){
|
contract1.setSemanticWordList(contract1.getSemanticWordList() == null ? semanticWord.getWord() : contract1.getSemanticWordList() + ", " + semanticWord.getWord());
|
}
|
QueryWrapper<Customer> qw = new QueryWrapper<>();
|
qw.eq("enterprise_name",contract1.getCustomerName());
|
contract1.setCustomer(customerService.getOne(qw));
|
}
|
return Result.OK(pageList);
|
}
|
|
/**
|
* 添加
|
*
|
* @param contractPage
|
* @return
|
*/
|
@AutoLog(value = "合同-添加")
|
@Operation(summary="合同-添加")
|
@RequiresPermissions("contract:contract:add")
|
@PostMapping(value = "/add")
|
public Result<String> add(@RequestBody ContractPage contractPage) {
|
Contract contract = new Contract();
|
BeanUtils.copyProperties(contractPage, contract);
|
contractService.saveMain(contract, contractPage.getContractFileList(),contractPage.getSemanticWordList());
|
return Result.OK("添加成功!");
|
}
|
|
/**
|
* 编辑
|
*
|
* @param contractPage
|
* @return
|
*/
|
@AutoLog(value = "合同-编辑")
|
@Operation(summary="合同-编辑")
|
@RequiresPermissions("contract:contract:edit")
|
@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
|
public Result<String> edit(@RequestBody ContractPage contractPage) {
|
Contract contract = new Contract();
|
if (contract.getCustomer() == null ||contract.getCustomer().getEnterpriseName() == null || !contract.getCustomer().getEnterpriseName().equals("")) {
|
BeanUtils.copyProperties(contractPage, contract);
|
Contract contractEntity = contractService.getById(contract.getId());
|
if (contractEntity.getReviewStatus() != null && contract.getReviewStatus() != null && !contractEntity.getReviewStatus().equals(contract.getReviewStatus())) {
|
contractEntity.setChangerTime(new Date());
|
contract.setChangerTime(new Date());
|
}
|
if(contractEntity==null) {
|
return Result.error("未找到对应数据");
|
}
|
contractService.updateMain(contract, contractPage.getContractFileList(),contractPage.getSemanticWordList());
|
return Result.OK("编辑成功!");
|
}
|
BeanUtils.copyProperties(contractPage, contract);
|
Contract contractEntity = contractService.getById(contract.getId());
|
|
if (contractEntity.getReviewStatus() != null && contract.getReviewStatus() != null && !contractEntity.getReviewStatus().equals(contract.getReviewStatus())) {
|
contractEntity.setChangerTime(new Date());
|
contract.setChangerTime(new Date());
|
}
|
|
contractEntity.setCustomer(contract.getCustomer());
|
contract.setCustomerName(contract.getCustomer().getEnterpriseName());
|
contractEntity.setCustomerName(contract.getCustomer().getEnterpriseName());
|
QueryWrapper<Customer> qw = new QueryWrapper<>();
|
qw.eq("enterprise_name",contract.getCustomer().getEnterpriseName());
|
if (customerService.count(qw) > 0 && !contractPage.getCustomerName().equals(contract.getCustomerName())) {
|
return Result.error("重复的公司名称");
|
}
|
if(contractEntity==null) {
|
return Result.error("未找到对应数据");
|
}
|
contractService.updateMain(contract, contractPage.getContractFileList(),contractPage.getSemanticWordList());
|
customerService.updateById(contractEntity.getCustomer());
|
return Result.OK("编辑成功!");
|
}
|
|
/**
|
* 通过id删除
|
*
|
* @param id
|
* @return
|
*/
|
@AutoLog(value = "合同-通过id删除")
|
@Operation(summary="合同-通过id删除")
|
@RequiresPermissions("contract:contract:delete")
|
@DeleteMapping(value = "/delete")
|
public Result<String> delete(@RequestParam(name="id",required=true) String id) {
|
contractService.delMain(id);
|
return Result.OK("删除成功!");
|
}
|
|
/**
|
* 批量删除
|
*
|
* @param ids
|
* @return
|
*/
|
@AutoLog(value = "合同-批量删除")
|
@Operation(summary="合同-批量删除")
|
@RequiresPermissions("contract:contract:deleteBatch")
|
@DeleteMapping(value = "/deleteBatch")
|
public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
|
this.contractService.delBatchMain(Arrays.asList(ids.split(",")));
|
return Result.OK("批量删除成功!");
|
}
|
|
/**
|
* 通过id查询
|
*
|
* @param id
|
* @return
|
*/
|
//@AutoLog(value = "合同-通过id查询")
|
@Operation(summary="合同-通过id查询")
|
@GetMapping(value = "/queryById")
|
public Result<Contract> queryById(@RequestParam(name="id",required=true) String id) {
|
Contract contract = contractService.getById(id);
|
if(contract==null) {
|
return Result.error("未找到对应数据");
|
}
|
QueryWrapper<Customer> qw = new QueryWrapper<>();
|
qw.eq("enterprise_name",contract.getCustomerName());
|
contract.setCustomer(customerService.getOne(qw));
|
return Result.OK(contract);
|
|
}
|
|
/**
|
* 通过id查询
|
*
|
* @param id
|
* @return
|
*/
|
//@AutoLog(value = "合同附件通过主表ID查询")
|
@Operation(summary="合同附件主表ID查询")
|
@GetMapping(value = "/queryContractFileByMainId")
|
public Result<List<ContractFile>> queryContractFileListByMainId(@RequestParam(name="id",required=true) String id) {
|
List<ContractFile> contractFileList = contractFileService.selectByMainId(id);
|
return Result.OK(contractFileList);
|
}
|
/**
|
* 通过id查询
|
*
|
* @param id
|
* @return
|
*/
|
//@AutoLog(value = "语义词通过主表ID查询")
|
@Operation(summary="语义词主表ID查询")
|
@GetMapping(value = "/querySemanticWordByMainId")
|
public Result<List<SemanticWord>> querySemanticWordListByMainId(@RequestParam(name="id",required=true) String id) {
|
List<SemanticWord> semanticWordList = semanticWordService.selectByMainId(id);
|
return Result.OK(semanticWordList);
|
}
|
|
/**
|
* 导出excel
|
*
|
* @param request
|
* @param contract
|
*/
|
@RequiresPermissions("contract:contract:exportXls")
|
@RequestMapping(value = "/exportXls")
|
public ModelAndView exportXls(HttpServletRequest request, Contract contract) {
|
|
// Step.1 组装查询条件查询数据
|
QueryWrapper<Contract> queryWrapper = QueryGenerator.initQueryWrapper(contract, request.getParameterMap());
|
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
//配置选中数据查询条件
|
String selections = request.getParameter("selections");
|
if(oConvertUtils.isNotEmpty(selections)) {
|
List<String> selectionList = Arrays.asList(selections.split(","));
|
queryWrapper.in("id",selectionList);
|
}
|
//Step.2 获取导出数据
|
List<Contract> contractList = contractService.list(queryWrapper);
|
|
// Step.3 组装pageList
|
List<ContractPage> pageList = new ArrayList<ContractPage>();
|
for (Contract main : contractList) {
|
ContractPage vo = new ContractPage();
|
BeanUtils.copyProperties(main, vo);
|
List<ContractFile> contractFileList = contractFileService.selectByMainId(main.getId());
|
vo.setContractFileList(contractFileList);
|
List<SemanticWord> semanticWordList = semanticWordService.selectByMainId(main.getId());
|
vo.setSemanticWordList(semanticWordList);
|
pageList.add(vo);
|
}
|
|
// Step.4 AutoPoi 导出Excel
|
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
|
mv.addObject(NormalExcelConstants.FILE_NAME, "合同列表");
|
mv.addObject(NormalExcelConstants.CLASS, ContractPage.class);
|
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("合同数据", "导出人:"+sysUser.getRealname(), "合同"));
|
mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
|
return mv;
|
}
|
|
/**
|
* 通过excel导入数据
|
*
|
* @param request
|
* @param response
|
* @return
|
*/
|
@RequiresPermissions("contract:contract:importExcel")
|
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
|
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
// 获取上传文件对象
|
MultipartFile file = entity.getValue();
|
ImportParams params = new ImportParams();
|
params.setTitleRows(2);
|
params.setHeadRows(1);
|
params.setNeedSave(true);
|
try {
|
List<ContractPage> list = ExcelImportUtil.importExcel(file.getInputStream(), ContractPage.class, params);
|
for (ContractPage page : list) {
|
Contract po = new Contract();
|
BeanUtils.copyProperties(page, po);
|
contractService.saveMain(po, page.getContractFileList(),page.getSemanticWordList());
|
}
|
return Result.OK("文件导入成功!数据行数:" + list.size());
|
} catch (Exception e) {
|
log.error(e.getMessage(),e);
|
return Result.error("文件导入失败:"+e.getMessage());
|
} finally {
|
try {
|
file.getInputStream().close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return Result.OK("文件导入失败!");
|
}
|
/**
|
* 查询快过期的合同
|
*
|
* @param days 过期天数阈值(默认30天)
|
* @return
|
*/
|
@AutoLog(value = "合同-查询快过期合同")
|
@Operation(summary = "合同-查询快过期合同")
|
@GetMapping(value = "/expiring")
|
public Result<List<Map<String, Object>>> queryExpiringContracts(
|
@RequestParam(name = "days", defaultValue = "30") Integer days) {
|
try {
|
// 计算日期范围
|
java.time.LocalDate today = java.time.LocalDate.now();
|
java.util.Date startDate = java.sql.Date.valueOf(today);
|
java.util.Date endDateLimit = java.sql.Date.valueOf(today.plusDays(days));
|
|
// 构建查询条件
|
QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
|
queryWrapper.ge("end_date", startDate); // end_date >= 今天
|
queryWrapper.le("end_date", endDateLimit); // end_date <= 今天+指定天数
|
|
// 按结束日期升序排列,即将过期的排前面
|
queryWrapper.orderByAsc("end_date");
|
|
// 执行查询
|
List<Contract> contractList = contractService.list(queryWrapper);
|
|
// 构建返回结果,包含过期天数信息
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
|
for (Contract contract : contractList) {
|
Map<String, Object> contractMap = new HashMap<>();
|
|
// 复制合同基本信息
|
contractMap.put("id", contract.getId());
|
contractMap.put("contractName", contract.getContractName());
|
contractMap.put("customerName", contract.getCustomerName());
|
contractMap.put("endDate", contract.getEndDate());
|
contractMap.put("reviewStatus", contract.getReviewStatus());
|
|
// 计算距离过期的天数
|
if (contract.getEndDate() != null) {
|
// 将java.util.Date转换为LocalDate
|
java.time.LocalDate endDate = contract.getEndDate().toInstant()
|
.atZone(java.time.ZoneId.systemDefault())
|
.toLocalDate();
|
long daysRemaining = java.time.temporal.ChronoUnit.DAYS.between(today, endDate);
|
contractMap.put("daysRemaining", daysRemaining);
|
} else {
|
contractMap.put("daysRemaining", null);
|
}
|
|
// 设置客户信息
|
if (contract.getCustomerName() != null) {
|
QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
|
customerQueryWrapper.eq("enterprise_name", contract.getCustomerName());
|
Customer customer = customerService.getOne(customerQueryWrapper);
|
contractMap.put("customer", customer);
|
}
|
|
// 设置语义词列表
|
List<SemanticWord> semanticWordList = semanticWordService.selectByMainId(contract.getId());
|
if (semanticWordList != null && !semanticWordList.isEmpty()) {
|
String semanticWords = semanticWordList.stream()
|
.map(SemanticWord::getWord)
|
.collect(Collectors.joining(", "));
|
contractMap.put("semanticWordList", semanticWords);
|
}
|
|
resultList.add(contractMap);
|
}
|
|
return Result.OK(resultList);
|
} catch (Exception e) {
|
log.error("查询快过期合同失败", e);
|
return Result.error("查询失败: " + e.getMessage());
|
}
|
}
|
@RequiresPermissions("contract:contract:importAgentContractWord")
|
@RequestMapping(value = "/importAgentContractWord", method = RequestMethod.POST)
|
public Result<String> importAgentContractWord(@RequestBody Contract contract) {
|
if (contract.getReviewStatus() == null) {
|
contract.setReviewStatus("8");
|
}
|
|
QueryWrapper queryWrapper = new QueryWrapper();
|
|
queryWrapper.eq("contract_name",contract.getContractName());
|
|
long count = contractService.count(queryWrapper);
|
|
if (count > 0) {
|
return Result.error("合同名称已存在");
|
}
|
|
contractService.save(contract);
|
|
QueryWrapper<Contract> queryWrapper1 = new QueryWrapper<>();
|
queryWrapper1.eq("contract_name",contract.getContractName());
|
|
|
Contract one1 = contractService.getOne(queryWrapper1);
|
for (SemanticWord semanticWord : contract.getSemanticWordObjs()){
|
semanticWord.setContractId(one1.getId());
|
}
|
semanticWordService.saveBatch(contract.getSemanticWordObjs());
|
|
for (ContractFile contractFile : contract.getContractFileList()){
|
contractFile.setContractId(one1.getId());
|
contractFileService.save(contractFile);
|
}
|
|
return Result.OK("添加成功!");
|
}
|
|
@Autowired
|
private SysUserController sysUserController;
|
|
@RequiresPermissions("contract:contract:assignCustomers")
|
@RequestMapping(value = "/assignCustomers/{contractId}", method = RequestMethod.POST)
|
public Result<String> assignCustomers(@RequestBody Customer customer, @PathVariable("contractId") String contractId) {
|
QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("enterprise_name",customer.getEnterpriseName());
|
QueryWrapper<SysUser> querySysUser = new QueryWrapper<>();
|
querySysUser.eq("username",customer.getEnterpriseName());
|
|
if (customerService.count(queryWrapper) == 0) {
|
customerService.save(customer);
|
}
|
|
if (sysUserController.getSysUserService().count(querySysUser) == 0) {
|
JSONObject jsonObject = new JSONObject();
|
String filteredName = customer.getEnterpriseName().replaceAll("[\u4e00-\u9fa5]", "").replaceAll("\\s+", "");
|
jsonObject.put("workNo", "KH" + filteredName + customer.getCustomerPhone());
|
jsonObject.put("password", "123456");
|
jsonObject.put("confirmPassword", "123456");
|
jsonObject.put("selectedroles", "1972228625018228737");
|
jsonObject.put("activitiSync", "1");
|
jsonObject.put("departIds", "");
|
jsonObject.put("userIdentity", "1");
|
String phone = customer.getCustomerPhone(); ;
|
|
// 校验电话号码:非空且为数字,且长度≤20
|
if (phone != null && phone.matches("\\d{1,20}")) {
|
// 校验通过,放入JSONObject
|
jsonObject.put("phone", phone);
|
} else {
|
return Result.error("请输入正确的电话号码");
|
}
|
jsonObject.put("email" , customer.getCustomerEmail());
|
jsonObject.put("username" , customer.getEnterpriseName());
|
jsonObject.put("realname" , customer.getEnterpriseName());
|
try {
|
Result<SysUser> add = sysUserController.add(jsonObject);
|
if (add.getCode() != 200) {
|
return Result.error("电话号码或邮箱重复");
|
}
|
}catch (Exception e) {
|
if (e.getMessage().equals("Subject does not have permission [system:user:add]")){
|
return Result.error("未分配权限:创建客户用户");
|
}
|
return Result.error("电话号码或邮箱重复");
|
}
|
}
|
|
Contract contract = new Contract();
|
contract.setId(contractId);
|
contract.setCustomerName(customer.getEnterpriseName());
|
|
contractService.updateById(contract);
|
|
return Result.OK("添加成功!可通过" + customer.getEnterpriseName() + "/123456访问客户视图");
|
}
|
|
}
|