CustomTemplateController.java 10.7 KB
package com.lhcredit.project.business.customTemplate.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lhcredit.common.utils.StreamUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.common.utils.text.Convert;
import com.lhcredit.project.business.frontDept.domain.FrontDept;
import com.lhcredit.project.business.frontDept.service.IFrontDeptService;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.service.IFrontUserService;
import com.lhcredit.project.business.reportMake.reportTemp.WebTempConfig;
import com.lhcredit.project.business.templateConfiguration.domain.TemplateConfiguration;
import com.lhcredit.project.business.templateConfiguration.mapper.TemplateConfigurationMapper;
import com.lhcredit.project.business.templateConfiguration.service.ITemplateConfigurationService;
import com.lhcredit.project.business.templateConfigurationParameters.domain.TemplateConfigurationParameters;
import com.lhcredit.project.business.templateConfigurationParameters.service.ITemplateConfigurationParametersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.lhcredit.framework.aspectj.lang.annotation.Log;
import com.lhcredit.framework.aspectj.lang.enums.BusinessType;
import com.lhcredit.project.business.customTemplate.domain.CustomTemplate;
import com.lhcredit.project.business.customTemplate.service.ICustomTemplateService;
import com.lhcredit.framework.web.controller.BaseController;
import com.lhcredit.framework.web.page.TableDataInfo;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.common.utils.poi.ExcelUtil;

/**
 * 定制模版信息操作处理
 *
 * @author lhcredit
 * @date 2025-04-08
 */
@Controller
@RequestMapping("/business/customTemplate")
public class CustomTemplateController extends BaseController {
    private String prefix = "business/customTemplate";

    @Autowired
    private ICustomTemplateService customTemplateService;
    @Autowired
    private IFrontDeptService frontDeptService;
    @Autowired
    private TemplateConfigurationMapper templateConfigurationMapper;

    @Autowired
    private ITemplateConfigurationParametersService templateConfigurationParametersService;

    @RequiresPermissions("business:customTemplate:view")
    @GetMapping()
    public String customTemplate() {
        return prefix + "/customTemplate";
    }



    /**
     * 查询定制模版列表
     */
    @RequiresPermissions("business:customTemplate:list")
    @PostMapping("/addTemplate")
    @ResponseBody
    public AjaxResult addTemplate(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String name = jsonObject.getString("name");
        Integer orgId = jsonObject.getInteger("orgId");
        CustomTemplate customTemplate=new CustomTemplate();
        customTemplate.setStatus(jsonObject.getInteger("status"));
        customTemplate.setOrgId(orgId);
        customTemplate.setTName(name);
        customTemplate.setCreateTime(new Date());
        customTemplateService.insertCustomTemplate(customTemplate);
        JSONArray data = jsonObject.getJSONArray("arrayContents");
        List<WebTempConfig.TemplateConfig> javaList = data.toJavaList(WebTempConfig.TemplateConfig.class);
        TemplateConfiguration templateConfiguration=new TemplateConfiguration();
        //更新主表模版 id


        return customTemplateService.addTempLate(javaList, customTemplate.getId());
    }

    /**
     * 查询定制模版列表
     */
    @RequiresPermissions("business:customTemplate:list")
    @PostMapping("/updateTemplate")
    @ResponseBody
    public AjaxResult updateTemplate(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String name = jsonObject.getString("name");
        Integer orgId = jsonObject.getInteger("orgId");
        CustomTemplate customTemplate=new CustomTemplate();
        customTemplate.setOrgId(orgId);
        customTemplate.setTName(name);
        customTemplate.setId(jsonObject.getInteger("id"));
        customTemplate.setStatus(jsonObject.getInteger("status"));
        TemplateConfiguration templateConfiguration=new TemplateConfiguration();
        //更新主表模版 id
        templateConfiguration.setOrgId(Long.valueOf(orgId));
        List<TemplateConfiguration> templateConfigurations = templateConfigurationMapper.selectTemplateConfigurationList(templateConfiguration);
         templateConfiguration = StreamUtils.of(templateConfigurations).filter(f -> StringUtils.isNotEmpty(f.getClientId()) && f.getClientId().contains(customTemplate.getId().toString())).getFirst();
        Set<Long> collect=new HashSet<>();
        if (null!=templateConfiguration&&StringUtils.isNotEmpty(templateConfiguration.getClientId())){
             collect = Arrays.stream(templateConfiguration.getClientId().split(",")).map(Long::valueOf).collect(Collectors.toSet());
            if (customTemplate.getStatus()==0){
                collect.add(Long.valueOf(customTemplate.getId()));
                templateConfiguration.setClientId(collect.stream().map(m->m.toString()).collect(Collectors.joining(",")));
            }else{
                collect.remove(Long.valueOf(customTemplate.getId()));
                templateConfiguration.setClientId(collect.stream().map(m->m.toString()).collect(Collectors.joining(",")));
            }
            templateConfigurationMapper.updateTemplateConfiguration(templateConfiguration);
            TemplateConfigurationParameters templateConfigurationParameters=new TemplateConfigurationParameters();
            templateConfigurationParameters.setClientId(customTemplate.getId());
            templateConfigurationParameters.setTypeName(customTemplate.getTName());
            templateConfigurationParameters.setStatus(customTemplate.getStatus());
            templateConfigurationParametersService.updateTemplateConfigurationParametersNameAndStatus(templateConfigurationParameters);
        }
        customTemplate.setCreateTime(new Date());
        customTemplateService.updateCustomTemplate(customTemplate);
        JSONArray data = jsonObject.getJSONArray("arrayContents");
        List<WebTempConfig.TemplateConfig> javaList = data.toJavaList(WebTempConfig.TemplateConfig.class);
        return customTemplateService.addTempLate(javaList, customTemplate.getId());
    }


    /**
     * 查询定制模版列表
     */
    @RequiresPermissions("business:customTemplate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CustomTemplate customTemplate) {
        startPage();
        List<CustomTemplate> list = customTemplateService.selectCustomTemplateList(customTemplate);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(2000);
        rspData.setTotal(new PageInfo(list).getTotal());
        for (CustomTemplate template : list) {
            FrontDept parentDept = frontDeptService.getParentDept(Long.valueOf(template.getOrgId()));
            template.setOrgName(parentDept.getDeptName());
        }
        rspData.setRows(list);
        return rspData;
    }


    /**
     * 导出定制模版列表
     */
    @RequiresPermissions("business:customTemplate:export")
    @Log(title = "定制模版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CustomTemplate customTemplate) {
        List<CustomTemplate> list = customTemplateService.selectCustomTemplateList(customTemplate);
        ExcelUtil<CustomTemplate> util = new ExcelUtil<CustomTemplate>(CustomTemplate. class);
        return util.exportExcel(list, "customTemplate");
    }

    /**
     * 新增定制模版
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<WebTempConfig.TemplateConfig> modelConfig = WebTempConfig.getModelConfig();
        mmap.put("modelConfig", modelConfig);
        return prefix + "/add";
    }

    /**
     * 新增保存定制模版
     */
    @RequiresPermissions("business:customTemplate:add")
    @Log(title = "定制模版", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody CustomTemplate customTemplate) {
        customTemplate.setCreateBy(ShiroUtils.getLoginName());
        customTemplate.setCreateTime(new Date());
        customTemplate.setUpdateBy(ShiroUtils.getLoginName());
        customTemplate.setUpdateTime(new Date());
        return toAjax(customTemplateService.insertCustomTemplate(customTemplate));
    }

    /**
     * 修改定制模版
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        CustomTemplate customTemplate =customTemplateService.selectCustomTemplateById(id);
        FrontDept parentDept = frontDeptService.getParentDept(Long.valueOf(customTemplate.getOrgId()));
        customTemplate.setOrgName(parentDept.getDeptName());
        mmap.put("customTemplate", customTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存定制模版
     */
    @RequiresPermissions("business:customTemplate:edit")
    @Log(title = "定制模版", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CustomTemplate customTemplate) {
        customTemplate.setUpdateBy(ShiroUtils.getLoginName());
        customTemplate.setUpdateTime(new Date());
        return toAjax(customTemplateService.updateCustomTemplate(customTemplate));
    }

    /**
     * 删除定制模版
     */
    @RequiresPermissions("business:customTemplate:remove")
    @Log(title = "定制模版", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        List<TemplateConfigurationParameters> clientIds = templateConfigurationParametersService.selectTemplateConfigurationParametersClientIds(ids);
        if (!clientIds.isEmpty()) {
            return new AjaxResult(AjaxResult.Type.NODATA,"该模版已被使用,无法删除");
        }
        return toAjax(customTemplateService.deleteCustomTemplateByIds(ids));
    }

    /**
     * 根据机构id查询定制模版
     */
    @GetMapping("/getByOrgId/{orgId}")
    @ResponseBody
    public AjaxResult getByOrgId(@PathVariable("orgId") Integer orgId) {
        List<CustomTemplate> customTemplate = customTemplateService.getByOrgId(orgId);
        return AjaxResult.success();
    }
}