354 lines
13 KiB
Java
354 lines
13 KiB
Java
package org.dromara.crm.controller;
|
|
|
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import jakarta.validation.constraints.NotEmpty;
|
|
import jakarta.validation.constraints.NotNull;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.dromara.common.core.domain.R;
|
|
import org.dromara.common.core.validate.AddGroup;
|
|
import org.dromara.common.core.validate.EditGroup;
|
|
import org.dromara.common.excel.utils.ExcelUtil;
|
|
import org.dromara.common.idempotent.annotation.RepeatSubmit;
|
|
import org.dromara.common.log.annotation.Log;
|
|
import org.dromara.common.log.enums.BusinessType;
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
import org.dromara.common.web.core.BaseController;
|
|
import org.dromara.crm.domain.bo.*;
|
|
import org.dromara.crm.domain.vo.CrmContractVo;
|
|
import org.dromara.crm.service.ICrmContractService;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 合同管理Controller
|
|
*
|
|
* @author AI Assistant
|
|
* @date 2025-10-04
|
|
*/
|
|
@Validated
|
|
@RequiredArgsConstructor
|
|
@RestController
|
|
@RequestMapping("/crm/contract")
|
|
public class CrmContractController extends BaseController {
|
|
|
|
private final ICrmContractService contractService;
|
|
|
|
/**
|
|
* 查询合同列表(分页)
|
|
*/
|
|
// @SaCheckPermission("crm:contract:list")
|
|
@GetMapping("/list")
|
|
public TableDataInfo<CrmContractVo> list(CrmContractBo bo, PageQuery pageQuery) {
|
|
return contractService.queryPageList(bo, pageQuery);
|
|
}
|
|
|
|
/**
|
|
* 查询合同详情
|
|
*/
|
|
// @SaCheckPermission("crm:contract:query")
|
|
@GetMapping("/{id}")
|
|
public R<CrmContractVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
|
|
return R.ok(contractService.queryById(id));
|
|
}
|
|
|
|
/**
|
|
* 新增合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:add")
|
|
@Log(title = "合同管理", businessType = BusinessType.INSERT)
|
|
@PostMapping()
|
|
public R<Long> add(@Validated(AddGroup.class) @RequestBody CrmContractBo bo) {
|
|
boolean result = contractService.insertByBo(bo);
|
|
if (result) {
|
|
return R.ok("新增成功", bo.getId());
|
|
}
|
|
return R.fail("新增失败");
|
|
}
|
|
|
|
/**
|
|
* 修改合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:edit")
|
|
@Log(title = "合同管理", businessType = BusinessType.UPDATE)
|
|
@PutMapping()
|
|
public R<Void> edit(@Validated(EditGroup.class) @RequestBody CrmContractBo bo) {
|
|
return toAjax(contractService.updateByBo(bo));
|
|
}
|
|
|
|
/**
|
|
* 删除合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:remove")
|
|
@Log(title = "合同管理", businessType = BusinessType.DELETE)
|
|
@DeleteMapping("/{ids}")
|
|
public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
|
|
return toAjax(contractService.deleteWithValidByIds(List.of(ids), true));
|
|
}
|
|
|
|
/**
|
|
* 提交审批
|
|
*/
|
|
// @SaCheckPermission("crm:contract:submit")
|
|
@Log(title = "提交审批", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/submitApproval")
|
|
public R<Void> submitApproval(@Validated @RequestBody CrmContractSubmitApprovalBo bo) {
|
|
return toAjax(contractService.submitApproval(bo.getId(), bo.getApproverIds(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 批量提交审批
|
|
*/
|
|
// @SaCheckPermission("crm:contract:submit")
|
|
@Log(title = "批量提交审批", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/batchSubmitApproval")
|
|
public R<Integer> batchSubmitApproval(@Validated @RequestBody CrmContractBatchSubmitApprovalBo bo) {
|
|
return R.ok(contractService.batchSubmitApproval(bo.getIds(), bo.getApproverIds(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 保存合同模板内容
|
|
*/
|
|
// @SaCheckPermission("crm:contract:edit")
|
|
@Log(title = "保存合同模板", businessType = BusinessType.UPDATE)
|
|
@RepeatSubmit()
|
|
@PostMapping("/saveTemplate")
|
|
public R<Void> saveTemplate(@RequestBody Map<String, Object> params) {
|
|
Object idObj = params.get("id");
|
|
if (idObj == null) {
|
|
return R.fail("合同ID不能为空");
|
|
}
|
|
Long id = Long.valueOf(idObj.toString());
|
|
String templateContent = (String) params.get("templateContent");
|
|
String settlementClauseDrafts = (String) params.get("settlementClauseDrafts");
|
|
return toAjax(contractService.saveTemplate(id, templateContent, settlementClauseDrafts));
|
|
}
|
|
|
|
/**
|
|
* 获取合同模板内容
|
|
*/
|
|
// @SaCheckPermission("crm:contract:query")
|
|
@GetMapping("/getTemplate/{id}")
|
|
public R<String> getTemplate(@NotNull(message = "主键不能为空") @PathVariable Long id) {
|
|
String templateContent = contractService.getTemplate(id);
|
|
// 使用 R.ok(msg, data) 确保数据在 data 字段中
|
|
return R.ok("获取模板成功", templateContent);
|
|
}
|
|
|
|
/**
|
|
* 撤回审批
|
|
*/
|
|
// @SaCheckPermission("crm:contract:edit")
|
|
@Log(title = "撤回审批", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/withdrawApproval")
|
|
public R<Void> withdrawApproval(@Validated @RequestBody CrmContractStatusChangeBo bo) {
|
|
return toAjax(contractService.withdrawApproval(bo.getId(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 开始执行
|
|
*/
|
|
// @SaCheckPermission("crm:contract:execute")
|
|
@Log(title = "开始执行合同", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/startExecute")
|
|
public R<Void> startExecute(@Validated @RequestBody CrmContractStatusChangeBo bo) {
|
|
return toAjax(contractService.startExecute(bo.getId(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 完成合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:execute")
|
|
@Log(title = "完成合同", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/finish")
|
|
public R<Void> finish(@Validated @RequestBody CrmContractStatusChangeBo bo) {
|
|
return toAjax(contractService.finishContract(bo.getId(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 作废合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:invalid")
|
|
@Log(title = "作废合同", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/invalid")
|
|
public R<Void> invalid(@Validated @RequestBody CrmContractInvalidBo bo) {
|
|
return toAjax(contractService.invalidContract(bo.getId(), bo.getInvalidReason(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 上传合同文件
|
|
*/
|
|
// @SaCheckPermission("crm:contract:edit")
|
|
@Log(title = "上传合同文件", businessType = BusinessType.UPDATE)
|
|
@PostMapping("/uploadFile")
|
|
public R<Void> uploadFile(@RequestBody Map<String, String> params) {
|
|
Long id = Long.valueOf(params.get("id"));
|
|
String fileUrl = params.get("fileUrl");
|
|
return toAjax(contractService.uploadFile(id, fileUrl));
|
|
}
|
|
|
|
/**
|
|
* 上传合同扫描件(支持多个附件,支持分类)
|
|
*/
|
|
// @SaCheckPermission("crm:contract:upload")
|
|
@Log(title = "上传合同扫描件", businessType = BusinessType.UPDATE)
|
|
@PostMapping("/uploadAttachments")
|
|
public R<Void> uploadAttachments(@RequestBody Map<String, Object> params) {
|
|
Long id = Long.valueOf(params.get("id").toString());
|
|
|
|
// 支持新格式:带分类的附件列表
|
|
if (params.containsKey("attachments") && params.get("attachments") != null) {
|
|
@SuppressWarnings("unchecked")
|
|
java.util.List<java.util.Map<String, Object>> attachments =
|
|
(java.util.List<java.util.Map<String, Object>>) params.get("attachments");
|
|
return toAjax(contractService.uploadAttachmentsWithCategory(id, attachments));
|
|
}
|
|
|
|
// 兼容旧格式:逗号分隔的ossId
|
|
if (params.containsKey("ossIds") && params.get("ossIds") != null) {
|
|
String ossIds = params.get("ossIds").toString();
|
|
return toAjax(contractService.uploadAttachments(id, ossIds));
|
|
}
|
|
|
|
return R.fail("附件参数不能为空");
|
|
}
|
|
|
|
/**
|
|
* 导出合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:export")
|
|
@Log(title = "合同管理", businessType = BusinessType.EXPORT)
|
|
@PostMapping("/export")
|
|
public void export(CrmContractBo bo, HttpServletResponse response) {
|
|
List<CrmContractVo> list = contractService.queryList(bo);
|
|
ExcelUtil.exportExcel(list, "合同数据", CrmContractVo.class, response);
|
|
}
|
|
|
|
/**
|
|
* 合同统计概览
|
|
*/
|
|
// @SaCheckPermission("crm:contract:list")
|
|
@GetMapping("/statistics/overview")
|
|
public R<Map<String, Object>> statisticsOverview(
|
|
@RequestParam(required = false) Long ownerUserId,
|
|
@RequestParam(required = false) String beginTime,
|
|
@RequestParam(required = false) String endTime) {
|
|
Map<String, Object> overview = contractService.statisticsOverview(ownerUserId, beginTime, endTime);
|
|
return R.ok(overview);
|
|
}
|
|
|
|
/**
|
|
* 合同状态分布
|
|
*/
|
|
// @SaCheckPermission("crm:contract:list")
|
|
@GetMapping("/statistics/status")
|
|
public R<List<Map<String, Object>>> statisticsByStatus() {
|
|
List<Map<String, Object>> result = contractService.statisticsByStatus();
|
|
return R.ok(result);
|
|
}
|
|
|
|
/**
|
|
* 合同类型分布
|
|
*/
|
|
// @SaCheckPermission("crm:contract:list")
|
|
@GetMapping("/statistics/type")
|
|
public R<List<Map<String, Object>>> statisticsByType() {
|
|
List<Map<String, Object>> result = contractService.statisticsByType();
|
|
return R.ok(result);
|
|
}
|
|
|
|
/**
|
|
* 合同趋势分析
|
|
*/
|
|
// @SaCheckPermission("crm:contract:list")
|
|
@GetMapping("/statistics/trend")
|
|
public R<List<Map<String, Object>>> statisticsTrend(
|
|
@RequestParam(defaultValue = "month") String type,
|
|
@RequestParam(required = false) String beginTime,
|
|
@RequestParam(required = false) String endTime) {
|
|
List<Map<String, Object>> result = contractService.statisticsTrend(type, beginTime, endTime);
|
|
return R.ok(result);
|
|
}
|
|
|
|
/**
|
|
* 合同到期提醒
|
|
*/
|
|
// @SaCheckPermission("crm:contract:list")
|
|
@GetMapping("/statistics/expiring")
|
|
public R<List<CrmContractVo>> queryExpiring(@RequestParam(defaultValue = "30") Integer days) {
|
|
return R.ok(contractService.queryExpiring(days));
|
|
}
|
|
|
|
/**
|
|
* 合同续签
|
|
*/
|
|
// @SaCheckPermission("crm:contract:renew")
|
|
@Log(title = "合同续签", businessType = BusinessType.INSERT)
|
|
@PostMapping("/renew/{parentContractId}")
|
|
public R<Long> renewContract(
|
|
@NotNull(message = "原合同ID不能为空") @PathVariable Long parentContractId,
|
|
@Validated(AddGroup.class) @RequestBody CrmContractBo bo) {
|
|
Long newContractId = contractService.renewContract(parentContractId, bo);
|
|
return R.ok(newContractId);
|
|
}
|
|
|
|
/**
|
|
* 从商机创建合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:add")
|
|
@Log(title = "商机转合同", businessType = BusinessType.INSERT)
|
|
@PostMapping("/createFromOpportunity/{opportunityId}")
|
|
public R<Void> createFromOpportunity(
|
|
@NotNull(message = "商机ID不能为空") @PathVariable Long opportunityId,
|
|
@Validated(AddGroup.class) @RequestBody CrmContractBo bo) {
|
|
return toAjax(contractService.createFromOpportunity(opportunityId, bo));
|
|
}
|
|
|
|
/**
|
|
* 从报价单创建合同
|
|
*/
|
|
// @SaCheckPermission("crm:contract:add")
|
|
@Log(title = "报价单转合同", businessType = BusinessType.INSERT)
|
|
@PostMapping("/createFromQuote/{quoteId}")
|
|
public R<Void> createFromQuote(
|
|
@NotNull(message = "报价单ID不能为空") @PathVariable Long quoteId,
|
|
@Validated(AddGroup.class) @RequestBody CrmContractBo bo) {
|
|
return toAjax(contractService.createFromQuote(quoteId, bo));
|
|
}
|
|
|
|
/**
|
|
* 合同签订标记
|
|
*/
|
|
// @SaCheckPermission("crm:contract:sign")
|
|
@Log(title = "合同签订", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/sign")
|
|
public R<Void> signContract(@Validated @RequestBody CrmContractSignBo bo) {
|
|
return toAjax(
|
|
contractService.signContract(bo.getId(), bo.getActualSignDate(), bo.getContractFile(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 批量分配负责人
|
|
*/
|
|
// @SaCheckPermission("crm:contract:assign")
|
|
@Log(title = "批量分配合同", businessType = BusinessType.UPDATE)
|
|
@PutMapping("/batch/assign")
|
|
public R<Void> batchAssign(@Validated @RequestBody CrmContractBatchAssignBo bo) {
|
|
return toAjax(contractService.batchAssignOwner(bo.getIds(), bo.getOwnerUserId(), bo.getRemark()));
|
|
}
|
|
|
|
/**
|
|
* 下载合同PDF
|
|
*/
|
|
// @SaCheckPermission("crm:contract:query")
|
|
@GetMapping("/download/{id}")
|
|
public R<String> downloadPdf(@NotNull(message = "主键不能为空") @PathVariable Long id) {
|
|
String filePath = contractService.downloadContractPdf(id);
|
|
return R.ok(filePath);
|
|
}
|
|
}
|