BiAnalyseBackgroundController.java 19.4 KB
package cn.com.glsx.dj.script.controller;

import cn.com.glsx.dj.peanalysis.dto.analysis.*;
import cn.com.glsx.dj.peanalysis.enums.BiErrorEnum;
import cn.com.glsx.dj.peanalysis.remote.BiAnalyseBackgroundRemote;
import cn.com.glsx.dj.script.converter.MerchantBiMatterConvert;
import cn.com.glsx.dj.script.converter.MerchantProfitRecordConvert;
import cn.com.glsx.dj.script.converter.ProvinceOrganizationConvert;
import cn.com.glsx.dj.script.request.analysis.*;
import cn.com.glsx.dj.script.request.common.CommonTemplateReq;
import cn.com.glsx.dj.script.request.common.SingleUploadRequest;
import cn.com.glsx.dj.script.response.analysis.MerchantBiMatterResp;
import cn.com.glsx.dj.script.response.analysis.MerchantProfitRecordDetailResp;
import cn.com.glsx.dj.script.response.analysis.MerchantProfitRecordResp;
import cn.com.glsx.dj.script.response.analysis.ProvinceOrganizationResp;
import cn.com.glsx.dj.script.service.BiAnalyseBackgroundService;
import cn.com.glsx.dj.script.service.ExcelAsyncImportService;
import cn.com.glsx.dj.script.utils.CommonUtil;
import cn.com.glsx.dj.smartcarlife.dto.LoginUserDTO;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.rop.response.RopResponse;
import cn.hutool.core.bean.BeanUtil;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * bi 指标分析后台服务控制器
 * @author yangwei
 * @version 1.0
 * @since 2025/12/11 17:17
 */
@Slf4j
@Api("BI指标分析后台")
@RequiredArgsConstructor
@RestController
@ServiceMethodBean(version = "1.0.0", needInSession = NeedInSessionType.YES)
public class BiAnalyseBackgroundController {

    private final ExcelAsyncImportService excelAsyncImportService;
    private final MerchantBiMatterConvert merchantBiMatterConvert;
    private final BiAnalyseBackgroundRemote biAnalyseBackgroundRemote;
    private final BiAnalyseBackgroundService biAnalyseBackgroundService;
    private final MerchantProfitRecordConvert merchantProfitRecordConvert;
    private final ProvinceOrganizationConvert provinceOrganizationConvert;

    @ApiOperation("BI-门店毛利额分页记录")
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.page", needInSession = NeedInSessionType.YES)
    public RopResponse<RpcPagination<MerchantProfitRecordResp>> merchantProfitRecordPage(MerchantProfitRecordQryPageReq request) {
        try {
            RpcPagination<MerchantProfitRecordQryDTO> rpcPagination = CommonUtil.getRpcPagination(MerchantProfitRecordQryDTO.class);
            MerchantProfitRecordQryDTO condition = BeanUtil.copyProperties(request, MerchantProfitRecordQryDTO.class);
            rpcPagination.setCondition(condition);
            RpcResponse<RpcPagination<MerchantProfitRecordDTO>> response = biAnalyseBackgroundRemote.merchantProfitRecordPage(rpcPagination);
            RpcPagination<MerchantProfitRecordDTO> result = response.getResult();
            if (response.isFailed()) {
                throw new RpcServiceException(response.getError(), response.getMessage());
            }

            return RopResponse.success(merchantProfitRecordConvert.merchantProfitRecordDto2ResponsePage(result));
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.profit.record.page occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.profit.record.page occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-门店毛利额详情")
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.detail", needInSession = NeedInSessionType.YES)
    public RopResponse<MerchantProfitRecordDetailResp> merchantProfitRecordDetail(MerchantProfitRecordSingleIdReq request) {
        try {

            MerchantProfitRecordSingleIdDTO rpcDTO = MerchantProfitRecordSingleIdDTO.builder().recordId(request.getRecordId()).build();
            RpcResponse<MerchantProfitRecordDetailDTO> rpcResponse = biAnalyseBackgroundRemote.merchantProfitRecordDetail(rpcDTO);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            MerchantProfitRecordDetailResp merchantProfitRecordDetailResp = merchantProfitRecordConvert.merchantProfitRecordDetailDto2Response(rpcResponse.getResult());
            return RopResponse.success(merchantProfitRecordDetailResp);
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.profit.record.detail occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.profit.record.detail occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-门店毛利额新增")
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.add", needInSession = NeedInSessionType.YES)
    public RopResponse<Boolean> merchantProfitRecordAdd(MerchantProfitRecordAddReq request) {
        try {
            String userName = Optional.ofNullable(CommonUtil.getLoginUser())
                    .orElseThrow(() -> new RpcServiceException(BiErrorEnum.LOGIN_USER_NOT_EXIST))
                    .getUserName();

            MerchantProfitRecordOperationDTO rpcDto = BeanUtil.copyProperties(request, MerchantProfitRecordOperationDTO.class);
            rpcDto.setOperator(userName);
            RpcResponse<Boolean> rpcResponse = biAnalyseBackgroundRemote.merchantProfitRecordAdd(rpcDto);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            return RopResponse.success(rpcResponse.getResult());
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.profit.record.add occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.profit.record.add occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-门店毛利额修改")
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.upd", needInSession = NeedInSessionType.YES)
    public RopResponse<Boolean> merchantProfitRecordUpd(MerchantProfitRecordUpdReq request) {
        try {
            String userName = Optional.ofNullable(CommonUtil.getLoginUser())
                    .orElseThrow(() -> new RpcServiceException(BiErrorEnum.LOGIN_USER_NOT_EXIST))
                    .getUserName();

            MerchantProfitRecordOperationDTO rpcDto = BeanUtil.copyProperties(request, MerchantProfitRecordOperationDTO.class);
            rpcDto.setOperator(userName);
            RpcResponse<Boolean> rpcResponse = biAnalyseBackgroundRemote.merchantProfitRecordUpd(rpcDto);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            return RopResponse.success(rpcResponse.getResult());
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.profit.record.upd occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.profit.record.upd occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-门店毛利额删除")
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.delete", needInSession = NeedInSessionType.YES)
    public RopResponse<Boolean> merchantProfitRecordDelete(MerchantProfitRecordSingleIdReq request) {
        try {
            String userName = Optional.ofNullable(CommonUtil.getLoginUser())
                    .orElseThrow(() -> new RpcServiceException(BiErrorEnum.LOGIN_USER_NOT_EXIST))
                    .getUserName();
            MerchantProfitRecordOperationDTO rpcDto = MerchantProfitRecordOperationDTO.builder()
                    .recordId(request.getRecordId()).operator(userName).build();

            RpcResponse<Boolean> rpcResponse = biAnalyseBackgroundRemote.merchantProfitRecordDelete(rpcDto);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            return RopResponse.success(rpcResponse.getResult());
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.profit.record.delete occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.profit.record.delete occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }


    @ApiOperation(
            value = "BI-门店毛利额记录异步导出",
            notes = "异步导出门店毛利额记录,返回生成的文件UUID,可通过该UUID下载导出文件"
    )
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.async.export", needInSession = NeedInSessionType.YES)
    public RopResponse<String> merchantProfitRecordAsyncExport(MerchantProfitRecordQryReq request) {
        try {
            return excelAsyncImportService.exportAsync(request, biAnalyseBackgroundService::exportMerchantProfitRecord);
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.profit.record.async.export occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.profit.record.async.export occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("通用模版导出-用于已存在的excel模版")
    @ServiceMethod(method = "dj.api.script.common.template.async.export.async.export", needInSession = NeedInSessionType.YES)
    public RopResponse<String> commonTemplateAsyncExport(CommonTemplateReq request) {
        try {
            return excelAsyncImportService.exportAsync(request, biAnalyseBackgroundService::commonTemplateAsyncExport);
        } catch (RpcServiceException e) {
            log.info("dj.api.script.common.template.async.export.async.export occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.common.template.async.export.async.export occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation(
            value = "BI-门店毛利额记录异步导入",
            notes = "门店毛利额记录异步导入,返回生成的导入进度条UUID,可通过该UUID获取进度条、导入成功条数,导入失败条数,导入失败文件的url"
    )
    @ServiceMethod(method = "dj.api.script.merchant.profit.record.async.import", needInSession = NeedInSessionType.YES)
    public RopResponse<String> merchantProfitRecordAsyncImport(SingleUploadRequest request) {
        try {
            String processId = biAnalyseBackgroundService.merchantProfitRecordAsyncImport(request);
            return RopResponse.success(processId);
        } catch (RpcServiceException e) {
            log.info("dj.api.script.common.template.async.export.async.import occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.common.template.async.export.async.import occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-区域运营管理列表")
    @ServiceMethod(method = "dj.api.script.province.organization.list", needInSession = NeedInSessionType.YES)
    @PostMapping("/dj.api.script.province.organization.list/1.0.0")
    public RopResponse<RpcPagination<ProvinceOrganizationResp>> provinceOrganizationList(ProvinceOrganizationQryPageReq request) {
        try {
            RpcPagination<?> rpcPagination = CommonUtil.getRpcPagination(MerchantProfitRecordQryDTO.class);

            RpcResponse<RpcPagination<ProvinceOrganizationDTO>> response = biAnalyseBackgroundRemote.provinceOrganizationList(rpcPagination);
            if (response.isFailed()) {
                throw new RpcServiceException(response.getError(), response.getMessage());
            }

            return RopResponse.success(provinceOrganizationConvert.provinceOrganization2ResponsePage(response.getResult()));
        } catch (RpcServiceException e) {
            log.info("dj.api.script.province.organization.list occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.province.organization.list occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-区域运营管理编辑")
    @ServiceMethod(method = "dj.api.script.province.organization.upd", needInSession = NeedInSessionType.YES)
    @PostMapping("/dj.api.script.province.organization.upd/1.0.0")
    public RopResponse<Boolean> provinceOrganizationUpd(ProvinceOrganizationBaseReq request) {
        try {
            String userName = Optional.ofNullable(CommonUtil.getLoginUser())
                    .orElseThrow(() -> new RpcServiceException(BiErrorEnum.LOGIN_USER_NOT_EXIST))
                    .getUserName();

            ProvinceOrganizationBaseDTO rpcDto = BeanUtil.copyProperties(request, ProvinceOrganizationBaseDTO.class);
            rpcDto.setOperator(userName);
            RpcResponse<Boolean> rpcResponse = biAnalyseBackgroundRemote.provinceOrganizationUpd(rpcDto);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            return RopResponse.success(rpcResponse.getResult());
        } catch (RpcServiceException e) {
            log.info("dj.api.script.province.organization.upd occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.province.organization.upd occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-事项管理分页列表")
    @ServiceMethod(method = "dj.api.script.merchant.bi.matter.page", needInSession = NeedInSessionType.YES)
    public RopResponse<RpcPagination<MerchantBiMatterResp>> merchantBiMatterPage(MerchantBiMatterQryPageReq request) {
        try {
            RpcPagination<MerchantBiMatterBaseQryDTO> rpcPagination = CommonUtil.getRpcPagination(MerchantBiMatterBaseQryDTO.class);
            MerchantBiMatterBaseQryDTO condition = BeanUtil.copyProperties(request, MerchantBiMatterBaseQryDTO.class);
            rpcPagination.setCondition(condition);

            RpcResponse<RpcPagination<MerchantBiMatterDTO>> response = biAnalyseBackgroundRemote.merchantBiMatterPage(rpcPagination);
            if (response.isFailed()) {
                throw new RpcServiceException(response.getError(), response.getMessage());
            }
            return RopResponse.success(merchantBiMatterConvert.merchantBiMatterDto2ResponsePage(response.getResult()));
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.bi.matter.page occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.bi.matter.page occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-事项管理处理")
    @ServiceMethod(method = "dj.api.script.merchant.bi.matter.handle", needInSession = NeedInSessionType.YES)
    public RopResponse<Boolean> merchantBiMatterHandle(MerchantBiMatterHandleReq request) {
        try {
            LoginUserDTO loginUserDTO = Optional.ofNullable(CommonUtil.getLoginUser())
                    .orElseThrow(() -> new RpcServiceException(BiErrorEnum.LOGIN_USER_NOT_EXIST));

            String userName = loginUserDTO.getUserName();
            String uuid = loginUserDTO.getUuid();

            MerchantBiMatterHandleDTO merchantBiMatterHandleDTO = BeanUtil.copyProperties(request, MerchantBiMatterHandleDTO.class);
            merchantBiMatterHandleDTO.setOperator(userName);
            merchantBiMatterHandleDTO.setOperatorUniqueKey(uuid);
            RpcResponse<Boolean> rpcResponse = biAnalyseBackgroundRemote.merchantBiMatterHandle(merchantBiMatterHandleDTO);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            return RopResponse.success(rpcResponse.getResult());
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.bi.matter.handle occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.bi.matter.handle occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation("BI-事项管理删除")
    @ServiceMethod(method = "dj.api.script.merchant.bi.matter.delete", needInSession = NeedInSessionType.YES)
    public RopResponse<Boolean> merchantBiMatterDelete(MerchantBiMatterSingleIdReq request) {
        try {
            MerchantBiMatterSingleIdDTO merchantBiMatterSingleIdDTO = BeanUtil.copyProperties(request, MerchantBiMatterSingleIdDTO.class);
            RpcResponse<Boolean> rpcResponse = biAnalyseBackgroundRemote.merchantBiMatterDelete(merchantBiMatterSingleIdDTO);
            if (rpcResponse.isFailed()) {
                throw new RpcServiceException(rpcResponse.getError(), rpcResponse.getMessage());
            }

            return RopResponse.success(rpcResponse.getResult());
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.bi.matter.delete occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.bi.matter.delete occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    @ApiOperation(
            value = "BI-事项管理记录异步导出",
            notes = "异步导出事项管理记录,返回生成的文件UUID,可通过该UUID下载导出文件"
    )
    @ServiceMethod(method = "dj.api.script.merchant.bi.matter.async.export", needInSession = NeedInSessionType.YES)
    public RopResponse<String> merchantBiMatterAsyncExport(MerchantBiMatterBaseQryReq request) {
        try {
            return excelAsyncImportService.exportAsync(request, biAnalyseBackgroundService::merchantBiMatterAsyncExport);
        } catch (RpcServiceException e) {
            log.info("dj.api.script.merchant.bi.matter.async.export occur error!", e);
            return RopResponse.error(e);
        } catch (Exception e) {
            log.info("dj.api.script.merchant.bi.matter.async.export occur error!", e);
            return RopResponse.result(DefaultErrorEnum.SERVICE_ERROR);
        }
    }


}