日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

枚举中文转数字并easyexcel导入(读取Read)数据至数据库,用监听器解决导入关联多表的问题

發(fā)布時(shí)間:2024/3/26 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 枚举中文转数字并easyexcel导入(读取Read)数据至数据库,用监听器解决导入关联多表的问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

以下內(nèi)容皆為本人學(xué)習(xí)過(guò)程中的所學(xué)的知識(shí),全根據(jù)自己所學(xué)思路來(lái)寫(xiě)的。

大致流程如下:

一、創(chuàng)建實(shí)體類,注意有一個(gè)是導(dǎo)入的模板實(shí)體模型類(CustomManageAndCustomerInformationReadVO),有一個(gè)是這個(gè)模塊的主表實(shí)體(CustomManage),模塊主表實(shí)體才會(huì)連接到數(shù)據(jù)庫(kù)的數(shù)據(jù),而模板實(shí)體模型類是導(dǎo)入所要求有的字段,并且哪個(gè)字段在哪一列從0開(kāi)始。

二、在controller層調(diào)用ExcelUtils工具類下的customerInformationReadExcel()方法。
三、進(jìn)入ExcelUtils工具類編寫(xiě)customerInformationReadExcel()方法,其中需要調(diào)用到CustomManageListener()監(jiān)聽(tīng)器也可稱之為解析器。

四、導(dǎo)入的難點(diǎn)就在于監(jiān)聽(tīng)器的編寫(xiě),如若導(dǎo)入的數(shù)據(jù)屬于一張表還不難,重點(diǎn)是我這次做的是多表的,需要進(jìn)行連表操作,調(diào)用其他表的mapper和service。不會(huì)做就覺(jué)得難。做了覺(jué)得也還行不是特別難,就像地上本沒(méi)有路走的人多了也就成了路。監(jiān)聽(tīng)器中調(diào)用了公共枚舉中文轉(zhuǎn)數(shù)字的方法。

五、進(jìn)入公共枚舉類,編寫(xiě)中文轉(zhuǎn)數(shù)字的getEnumNum()方法,實(shí)現(xiàn)中文獲取數(shù)字,導(dǎo)入,(從excel中導(dǎo)入數(shù)據(jù)至數(shù)據(jù)庫(kù))。

1.創(chuàng)建實(shí)體類,注意有一個(gè)是導(dǎo)入的模板實(shí)體模型類(CustomManageAndCustomerInformationReadVO),有一個(gè)是這個(gè)模塊的主表實(shí)體(CustomManage),模塊主表實(shí)體才會(huì)連接到數(shù)據(jù)庫(kù)的數(shù)據(jù),而模板實(shí)體模型類是導(dǎo)入所要求有的字段,并且哪個(gè)字段在哪一列從0開(kāi)始。

package com.theiavis.workcloud.project.sale.bean;import java.io.Serializable; import java.util.Date;import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.theiavis.workcloud.common.entity.BaseEntity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; import lombok.experimental.Accessors; import org.springframework.web.bind.annotation.ModelAttribute;/*** custom_manage :*/ @Data @Accessors(chain=true) @AllArgsConstructor @NoArgsConstructor @ApiModel("客戶管理對(duì)象") public class CustomManage extends BaseEntity implements Serializable {private static final long serialVersionUID = 1L;/*** 主鍵 : id,*/@ExcelIgnore@TableId(value = "id", type = IdType.AUTO)private Integer id;/*** sale_time, 銷售時(shí)間,kpi關(guān)聯(lián),需拆分-年*/@ExcelIgnore@ExcelProperty(value = {"年份"}, index = 0)@ApiModelProperty(value = "銷售時(shí)間-年", required = true)private String saleTimeYear;/*** sale_time, 銷售時(shí)間,kpi關(guān)聯(lián),需拆分-月*/@ExcelIgnore@ExcelProperty(value = {"所屬月份"}, index = 1)@ApiModelProperty(value = "銷售時(shí)間-月", required = true)private String saleTimeMonth;/*** proj_num, project-》projo_num,創(chuàng)建時(shí)間與項(xiàng)目表關(guān)聯(lián)*/@ExcelIgnore@ExcelProperty(value = {"創(chuàng)建時(shí)間與項(xiàng)目表關(guān)聯(lián)"}, index = 2)@ApiModelProperty(value = "創(chuàng)建時(shí)間與項(xiàng)目表關(guān)聯(lián)", required = true)private String projNum;/*** follow_id, 跟進(jìn)人標(biāo)識(shí)user->user_id*/@ExcelIgnore@ExcelProperty(value = {"跟進(jìn)人標(biāo)識(shí)user->user_id"}, index = 3)@ApiModelProperty(value = "跟進(jìn)人標(biāo)識(shí)user->user_id", required = true)private String followId;/*** create_id, 創(chuàng)建人標(biāo)識(shí)user->user_id*/@ExcelIgnore@ExcelProperty(value = {"創(chuàng)建人標(biāo)識(shí)user->user_id"}, index = 4)@ApiModelProperty(value = "創(chuàng)建人標(biāo)識(shí)user->user_id", required = true)private String createId;/*** company, 對(duì)接公司名稱*/@ExcelIgnore@ExcelProperty(value = {"公司名稱"}, index = 5)@ApiModelProperty(value = "對(duì)接公司名稱", required = true)private String company;/*** custom_type, 客戶類型(0、商業(yè)公司1、政府2、地產(chǎn)商3、規(guī)劃院)*/@ExcelIgnore@ExcelProperty(value = {"客戶類型(0、商業(yè)公司1、政府2、地產(chǎn)商3、規(guī)劃院)"}, index = 6)@ApiModelProperty(value = "客戶類型(0、商業(yè)公司1、政府2、地產(chǎn)商3、規(guī)劃院)", required = true)private String customType;/*** city, 客戶所在城市*/@ExcelIgnore@ExcelProperty(value = {"客戶所在城市"}, index = 7)@ApiModelProperty(value = "客戶所在城市", required = true)private String city;/*** custom_department, 客戶所在部門(mén)*/@ExcelIgnore@ExcelProperty(value = {"客戶所在部門(mén)"}, index = 8)@ApiModelProperty(value = "客戶所在部門(mén)", required = true)private String customDepartment;/*** contact, 聯(lián)系人名字(全稱)*/@ExcelIgnore@ExcelProperty(value = {"聯(lián)系人名字(全稱)"}, index = 9)@ApiModelProperty(value = "聯(lián)系人名字(全稱)", required = true)private String contact;/*** mobile, 聯(lián)系人電話*/@ExcelIgnore@ExcelProperty(value = {"聯(lián)系人電話"}, index = 10)@ApiModelProperty(value = "聯(lián)系人電話", required = true)private String mobile;/*** email, 聯(lián)系人郵箱*/@ExcelIgnore@ExcelProperty(value = {"聯(lián)系人郵箱"}, index = 11)@ApiModelProperty(value = "聯(lián)系人郵箱", required = true)private String email;/*** custom_source, 新舊客戶類型(0.移交客戶1.新客戶)*/@ExcelIgnore@ExcelProperty(value = {"新舊客戶類型(0.移交客戶1.新客戶)"}, index = 12)@ApiModelProperty(value = "新舊客戶類型(0.移交客戶1.新客戶)", required = true)private String customSource;/*** custom_id, 客戶id*/@ExcelIgnore@ExcelProperty(value = {"客戶id"}, index = 13)@ApiModelProperty(value = "客戶id", required = false)private String customId;/*** permit_code, 權(quán)限碼*/@ExcelIgnore@ExcelProperty(value = {"權(quán)限碼"}, index = 14)@ApiModelProperty(value = "權(quán)限碼", required = true)private String permitCode;/*** dept_id, 部門(mén)id*/@ExcelIgnore// @ExcelProperty(value = {"部門(mén)id"}, index = 15)@ApiModelProperty(value = "部門(mén)id", required = true)private Integer deptId;/*** agreement_id, proj_agreement->id合同標(biāo)識(shí)*/@ExcelIgnore// @ExcelProperty(value = {"id合同標(biāo)識(shí)"}, index = 16)@ApiModelProperty(value = "id合同標(biāo)識(shí)", required = false)private Integer agreementId;/*** creat_Time, create_Time創(chuàng)建時(shí)間*/@ExcelIgnore@ExcelProperty(value = {"create_Time創(chuàng)建時(shí)間"}, index = 17)@ApiModelProperty(value = "create_Time創(chuàng)建時(shí)間", required = false)@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")private Date createTime;/*** is_delete, 是否刪除*/@ExcelIgnore@ExcelProperty(value = {"是否刪除0.否1.是"}, index = 18)@ApiModelProperty(value = "是否刪除0.否1.是", required = true)private String isDelete;} package com.theiavis.workcloud.project.sale.vo;import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.TableField; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors;import java.util.Date;/*** 根據(jù)"客戶信息導(dǎo)入模板"批量導(dǎo)入* @author xql*/ @Data //@Accessors(chain=true) @AllArgsConstructor @NoArgsConstructor public class CustomManageAndCustomerInformationReadVO {/*** stage, 商機(jī)尋找(0.商機(jī)線索,1.在談項(xiàng)目 2.簽約項(xiàng)目 3.已歸檔項(xiàng)目4.前期介入5.項(xiàng)目報(bào)價(jià)6.關(guān)鍵人物接觸7.方案匯報(bào)8.方案階段9.投標(biāo)階段10.合同階段11.項(xiàng)目暫停)*/@ApiModelProperty(value = "商機(jī)尋找(0.商機(jī)線索,1.在談項(xiàng)目 2.簽約項(xiàng)目 3.已歸檔項(xiàng)目4.前期介入5.項(xiàng)目報(bào)價(jià)6.關(guān)鍵人物接觸7.方案匯報(bào)8.方案階段9.投標(biāo)階段10.合同階段11.項(xiàng)目暫停)", required = true)@ExcelProperty(value = "所屬階段", index = 0)private String stage;/*** sale_time, 銷售時(shí)間,kpi關(guān)聯(lián),需拆分-年*/@ExcelProperty(value = "年份", index = 1)private String saleTimeYear;/*** sale_time, 銷售時(shí)間,kpi關(guān)聯(lián),需拆分-月*/@ExcelProperty(value = "所屬月份", index = 2)private String saleTimeMonth;/*** 客戶來(lái)源*/@ExcelProperty(value = "客戶來(lái)源", index = 3)@TableField(exist = false)@ApiModelProperty(value = "客戶來(lái)源", required = true)private String source;/*** company, 對(duì)接公司名稱*/@ExcelProperty(value = "公司名稱", index = 4)private String company;/*** 項(xiàng)目名 項(xiàng)目名稱*/@ExcelProperty(value = "項(xiàng)目名稱", index = 5)private String projName;/*** 項(xiàng)目地址*/@ExcelProperty(value = "項(xiàng)目地址", index = 6)@TableField(exist = false)@ApiModelProperty(value = "項(xiàng)目地址", required = true)private String address;/*** 項(xiàng)目類型*/@ExcelProperty(value = "項(xiàng)目類型", index = 7)@TableField(exist = false)@ApiModelProperty(value = "項(xiàng)目類型(項(xiàng)目類型(0、數(shù)字大屏1、數(shù)字沙盤(pán)2、數(shù)字展項(xiàng)3、數(shù)字展廳4、CIM 5、VRXR 6:其他)", required = true)private String projectType;/*** custom_type, 客戶類型(0、商業(yè)公司1、政府2、地產(chǎn)商3、規(guī)劃院)*/@ExcelProperty(value = "客戶類型", index = 8)@ApiModelProperty(value = "客戶類型(0、商業(yè)公司1、政府2、地產(chǎn)商3、規(guī)劃院)", required = true)private String customType;/*** city, 客戶所在城市*/@ExcelProperty(value = {"客戶所在城市"}, index = 9)@ApiModelProperty(value = "客戶所在城市", required = true)private String city;/*** custom_department, 客戶所在部門(mén)*/@ExcelProperty(value = {"客戶所屬部門(mén)"}, index = 10)@ApiModelProperty(value = "客戶所在部門(mén)", required = true)private String customDepartment;/*** contact, 聯(lián)系人名字(全稱)*/@ExcelProperty(value = "聯(lián)系人", index = 11)private String contact;/*** email, 聯(lián)系人郵箱*/@ExcelProperty(value = {"聯(lián)系人郵箱"}, index = 12)@ApiModelProperty(value = "聯(lián)系人郵箱", required = true)private String email;/*** mobile, 聯(lián)系人電話*/@ExcelProperty(value = "聯(lián)系人電話", index = 13)private String mobile;/*** follow_id, 跟進(jìn)人名稱*/@ExcelProperty(value = "跟進(jìn)人", index = 14)private String followName;/*** 跟進(jìn)結(jié)果*/@ExcelProperty(value = "跟進(jìn)結(jié)果", index = 15)@TableField(exist = false)@ApiModelProperty(value = "跟進(jìn)結(jié)果", required = true)private String followResult;/*** 失敗原因*/@ExcelProperty(value = "失敗原因", index = 16)@TableField(exist = false)@ApiModelProperty(value = "失敗原因", required = true)private String followFailResult;/*** 計(jì)劃開(kāi)始時(shí)間*/@TableField(exist = false)@ExcelProperty(value = "計(jì)劃開(kāi)始時(shí)間(yyyy/mm/dd)", index = 17)@JsonIgnoreprivate Date planStartTime;/*** proj_need, 項(xiàng)目需求*/@TableField(exist = false)@ExcelProperty(value = "項(xiàng)目需求", index = 18)@ApiModelProperty(value = "項(xiàng)目需求", required = true)private String projNeed;/*** 計(jì)劃結(jié)束時(shí)間*/@TableField(exist = false)@ExcelProperty(value = "計(jì)劃結(jié)束時(shí)間(yyyy/mm/dd)", index = 19)@JsonIgnoreprivate Date planEndTime;/*** custom_source, 新舊客戶類型(0.移交客戶1.新客戶)*/@TableField(exist = false)@ExcelProperty(value = "新舊客戶類型", index = 20)private String customSource;/*** product_count, 產(chǎn)品數(shù)量*/@TableField(exist = false)@ExcelProperty(value = "產(chǎn)品數(shù)量", index = 21)@ApiModelProperty(value = "產(chǎn)品數(shù)量", required = true)private String productCount;/*** contract_cash, 預(yù)計(jì)合同金額*/@TableField(exist = false)@ExcelProperty(value = "預(yù)計(jì)合同金額(萬(wàn)元)", index = 22)@ApiModelProperty(value = "預(yù)計(jì)合同金額", required = true)private String contractCash;/*** place_contract, 是否簽訂合同(0.未簽約1.已簽約)*/@TableField(exist = false)@ExcelProperty(value = "是否簽訂保密協(xié)議", index = 23)@ApiModelProperty(value = "是否簽訂保密協(xié)議(0.未簽約1.已簽約)", required = true)private String placeContract;/*** effect_pj, 是否當(dāng)?shù)赜绊懥?0.否1.是)*/@TableField(exist = false)@ExcelProperty(value = "是否當(dāng)?shù)赜绊懥?#34;, index = 24)@ApiModelProperty(value = "是否當(dāng)?shù)赜绊懥?0.否1.是)", required = true)private String effectPj;/*** 項(xiàng)目進(jìn)度*/@TableField(exist = false)@ExcelProperty(value = "項(xiàng)目進(jìn)度",index = 25)@ApiModelProperty(value = "項(xiàng)目進(jìn)度(0:前期介入1:項(xiàng)目報(bào)價(jià)2:關(guān)鍵人物接觸3:方案制作階段4:方案匯報(bào)5:DEMO制作6:投標(biāo)階段7:簽約階段8:項(xiàng)目站廳)", required = true)private String projProgress;/*** star, 項(xiàng)目星級(jí)(數(shù)字標(biāo)識(shí)1-5)*/@TableField(exist = false)@ExcelProperty(value = "項(xiàng)目靠譜程度",index = 26)@ApiModelProperty(value = "項(xiàng)目靠譜程度", required = true)private String star;/*** contract_cash_count, 合同金額*/@TableField(exist = false)@ExcelProperty(value = "合同金額(萬(wàn)元)",index = 27)@ApiModelProperty(value = "合同金額", required = true)private String contractCashCount;/*** realcontract_cash, 合同實(shí)際金額*/@TableField(exist = false)@ExcelProperty(value = "合同實(shí)際金額(萬(wàn)元)",index = 28)@ApiModelProperty(value = "合同實(shí)際金額", required = true)private String realcontractCash;/*** contract_mode, 合同模式(0.分期付款1.背靠背式2:賒賬)*/@TableField(exist = false)@ExcelProperty(value = "合同模式",index = 29)@ApiModelProperty(value = "合同模式(0.分期付款1.背靠背式2:賒賬)", required = true)private String contractMode;/*** intexamine_time, 初驗(yàn)收時(shí)間*/@TableField(exist = false)@ExcelProperty(value = "初驗(yàn)收時(shí)間(yyyy/mm/dd)",index =30)@ApiModelProperty(value = "初驗(yàn)收時(shí)間", required = true)private Date intexamineTime;/*** init_cash, 初步收入金額*/@TableField(exist = false)@ExcelProperty(value = "初步收入金額(萬(wàn)元)",index =31)@ApiModelProperty(value = "初步收入金額", required = true)private String initCash;/*** endexamine_time, 最終驗(yàn)收時(shí)間*/@TableField(exist = false)@ExcelProperty(value = "最終驗(yàn)收時(shí)間(yyyy/mm/dd)",index =32)@ApiModelProperty(value = "最終驗(yàn)收時(shí)間", required = true)private Date endexamineTime;/*** end_cash, 最終金額*/@TableField(exist = false)@ExcelProperty(value = "最終金額(萬(wàn)元)",index =33)@ApiModelProperty(value = "最終金額", required = true)private String endCash;/*** receive_cash_time, 收款最終時(shí)間*/@TableField(exist = false)@ExcelProperty(value = "收款日期(yyyy/mm/dd)",index =34)@ApiModelProperty(value = "收款最終時(shí)間", required = true)private Date receiveCashTime;/*** contract_deposit, 合同預(yù)付款(定金)*/@TableField(exist = false)@ExcelProperty(value = "預(yù)付款(萬(wàn)元)",index =35)@ApiModelProperty(value = "合同預(yù)付款(定金)", required = true)private String contractDeposit;/*** proj_state, 項(xiàng)目狀態(tài)(0項(xiàng)目進(jìn)行中/1暫停項(xiàng)目/2催款中/3已完成需催款/4已清項(xiàng)目)*/@TableField(exist = false)@ExcelProperty(value = "項(xiàng)目狀態(tài)",index =36)@ApiModelProperty(value = "項(xiàng)目狀態(tài)(0項(xiàng)目進(jìn)行中/1暫停項(xiàng)目/2催款中/3已完成需催款/4已清項(xiàng)目)", required = true)private String projState;/*** back_cash, 回款跟催狀態(tài)(1.催款中2.已完成催款3.已清項(xiàng)目4.異常5.拖款)*/@TableField(exist = false)@ExcelProperty(value = "回款跟催階段",index =37)@ApiModelProperty(value = "回款跟催狀態(tài)(0. 正在催款 1. 常規(guī)(預(yù)計(jì)三個(gè)月內(nèi)收款 ) 2. 異常(預(yù)計(jì)超半年催款) 3.拖款(預(yù)計(jì)3~6個(gè)月回款))", required = true)private String backCash;/*** market_commision, 市場(chǎng)提成系數(shù)*/@TableField(exist = false)@ExcelProperty(value = "市場(chǎng)提成系數(shù)",index =38)@ApiModelProperty(value = "市場(chǎng)提成系數(shù)", required = true)private String marketCommision;/*** commision_month, 市場(chǎng)提成月數(shù)*/@TableField(exist = false)@ExcelProperty(value = "市場(chǎng)提成月份",index =39)@ApiModelProperty(value = "市場(chǎng)提成月數(shù)", required = true)private String commisionMonth;/*** feedback, 具體反饋(0.成果質(zhì)量1.服務(wù)態(tài)度2.過(guò)程技術(shù)配合3.時(shí)間配合4.交付時(shí)間)*/@TableField(exist = false)@ExcelProperty(value = "具體反饋",index =40)@ApiModelProperty(value = "具體反饋(0.成果質(zhì)量1.服務(wù)態(tài)度2.過(guò)程技術(shù)配合3.時(shí)間配合4.交付時(shí)間)", required = true)private String feedback;/*** remark, 備注(項(xiàng)目注意事宜)*/@TableField(exist = false)@ExcelProperty(value = "備注",index =41)@ApiModelProperty(value = "備注(項(xiàng)目注意事宜)", required = true)private String remark;/*** detail_need,詳細(xì)需求*/@TableField(exist = false)@ExcelProperty(value = "詳細(xì)需求",index =42)@ApiModelProperty(value = "詳細(xì)需求", required = true)private String detailNeed;}

2.在controller層調(diào)用ExcelUtils工具類下的customerInformationReadExcel()方法。

/*** 根據(jù)客戶信息導(dǎo)入模板批量導(dǎo)入** @param* @return* @throws Exception*/@PreAuthorize("hasPermit('sale:client')")@RequestMapping(value = "/customerInformationReadExcel", method = RequestMethod.POST, produces = "application/octet-stream")@ApiOperation(value = "客戶信息導(dǎo)入(按照模板導(dǎo)入)", tags = {"銷售模塊", "客戶信息導(dǎo)入(按照模板導(dǎo)入)"})public Result customerInformationReadExcel(@RequestPart @RequestParam(value = "file", required = false) MultipartFile file)throws Exception {try {ExcelUtils.customerInformationReadExcel(file.getInputStream());return Result.success(ResultCode.成功, ResultCode.成功.getCode());} catch (Exception e) {return Result.error(ResultCode.失敗.getMsg(), ResultCode.失敗.getCode(), null);}}

3.進(jìn)入ExcelUtils工具類編寫(xiě)customerInformationReadExcel()方法,其中需要調(diào)用到CustomManageListener()監(jiān)聽(tīng)器也可稱之為解析器。

/*** 根據(jù)客戶信息導(dǎo)入模板批量導(dǎo)入* @param inputStream*/public static void customerInformationReadExcel(InputStream inputStream) {EasyExcel.read(inputStream, CustomManageAndCustomerInformationReadVO.class,new CustomManageListener()).registerConverter(new LocalDateTimeConverter()).sheet().doRead();}

4.導(dǎo)入的難點(diǎn)就在于監(jiān)聽(tīng)器的編寫(xiě),如若導(dǎo)入的數(shù)據(jù)屬于一張表還不難,重點(diǎn)是我這次做的是多表的,需要進(jìn)行連表操作,調(diào)用其他表的mapper和service。不會(huì)做就覺(jué)得難。做了覺(jué)得也還行不是特別難,就像地上本沒(méi)有路走的人多了也就成了路。監(jiān)聽(tīng)器中調(diào)用了公共枚舉中文轉(zhuǎn)數(shù)字的方法。

package com.theiavis.workcloud.project.sale.enums;import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.StringUtils; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.theiavis.security.jwt.UserOperator; import com.theiavis.workcloud.project.agreement.bean.ProjAgreement; import com.theiavis.workcloud.project.agreement.mapper.ProjAgreementMapper; import com.theiavis.workcloud.project.agreement.service.IProjAgreementService; import com.theiavis.workcloud.project.dingtalk.bean.User; import com.theiavis.workcloud.project.dingtalk.mapper.UserMapper; import com.theiavis.workcloud.project.proj.VO.proj.ProjIdOutputVo; import com.theiavis.workcloud.project.proj.VO.proj.ProjInsertVO; import com.theiavis.workcloud.project.proj.bean.Project; import com.theiavis.workcloud.project.proj.mapper.ProjectMapper; import com.theiavis.workcloud.project.proj.service.ProjectService; import com.theiavis.workcloud.project.sale.bean.CustomManage; import com.theiavis.workcloud.project.sale.mapper.CustomManageMapper; import com.theiavis.workcloud.project.sale.service.ICustomManageService; import com.theiavis.workcloud.project.sale.vo.CustomManageAndCustomerExportVO; import com.theiavis.workcloud.project.sale.vo.CustomManageAndCustomerInformationReadVO; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional;import javax.annotation.PostConstruct; import java.util.List; import java.util.logging.Logger;@Slf4j @Component public class CustomManageListener implements ReadListener<CustomManageAndCustomerInformationReadVO> {private static final Logger logger = Logger.getLogger(CustomManageListener.class.getName());/*** 每隔5條存儲(chǔ)數(shù)據(jù)庫(kù),實(shí)際使用中可以100條,然后清理list ,方便內(nèi)存回收*/private static final int BATCH_COUNT = 100;/*** 緩存的數(shù)據(jù)*/private List<CustomManageAndCustomerInformationReadVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);@AutowiredICustomManageService iCustomManageService;//相當(dāng)于注冊(cè)到spring的一個(gè)bean@AutowiredUserOperator userOperator;@AutowiredUserMapper userMapper;//相當(dāng)于注冊(cè)到spring的一個(gè)bean@AutowiredIProjAgreementService iProjAgreementService;//相當(dāng)于注冊(cè)到spring的一個(gè)bean@AutowiredProjectService projectService;//相當(dāng)于注冊(cè)到spring的一個(gè)bean//把他自身的類實(shí)例化,并定義成靜態(tài)類public static CustomManageListener customManageListener;@PostConstructpublic void init(){//當(dāng)前指針去指向這個(gè)類的對(duì)象,因?yàn)閟pring容器里面有相同的對(duì)象customManageListener =this;}/*** 這個(gè)每一條數(shù)據(jù)解析都會(huì)來(lái)調(diào)用** @param customManageAndCustomerInformationReadVO one row value. Is is same as {@link AnalysisContext#readRowHolder()}* @param analysisContext*/@Transactional(rollbackFor = Exception.class)@Overridepublic void invoke(CustomManageAndCustomerInformationReadVO customManageAndCustomerInformationReadVO, AnalysisContext analysisContext) {logger.info("解析到一條數(shù)據(jù):{}");cachedDataList.add(customManageAndCustomerInformationReadVO);// 達(dá)到BATCH_COUNT了,需要去存儲(chǔ)一次數(shù)據(jù)庫(kù),防止數(shù)據(jù)幾萬(wàn)條數(shù)據(jù)在內(nèi)存,容易OOMif (cachedDataList.size() >= BATCH_COUNT) {saveData();// 存儲(chǔ)完成清理 listcachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);}}@Transactional(rollbackFor = Exception.class)public void saveData() {cachedDataList.stream().forEach(i->{logger.info("{}條數(shù)據(jù),開(kāi)始存儲(chǔ)數(shù)據(jù)庫(kù)!");try { // String 數(shù)字=StatusEnum.getEnumNum("錯(cuò)誤","Source"); // User createId=customManageListener.userMapper.selectOne(new QueryWrapper<User>().groupBy("user_id").like("name",i.getCreaterName()));//查詢出的結(jié)果絕對(duì)有一個(gè)用selectOne()方法(如若有多條數(shù)據(jù)就會(huì)報(bào)錯(cuò)),QueryWrapper<User>()方法是MybatisPlus函數(shù)的用法,實(shí)現(xiàn)查詢的對(duì)象封裝操作類。groupBy()分組,like()模糊查詢。根據(jù)followId(也就是user_id)查詢對(duì)應(yīng)的followName。主表中沒(méi)有followName,只有followId,所以在user表(userMapper)中查詢對(duì)應(yīng)的name。User followId=customManageListener.userMapper.selectOne(new QueryWrapper<User>().groupBy("user_id").like("name",i.getFollowName()));//鏈?zhǔn)骄幊獭?shù)據(jù)存在主表(custom_manage)中CustomManage customManage=new CustomManage().setFollowId(followId.getUserId()!=null?followId.getUserId():null).setSaleTimeYear(i.getSaleTimeYear()).setSaleTimeMonth(i.getSaleTimeMonth()).setCompany(i.getCompany()).setCustomType(StatusEnum.getEnumNum(i.getCustomType(),"CustomType")).setCity(i.getCity()).setCustomDepartment(i.getCustomDepartment()).setContact(i.getContact()).setEmail(i.getEmail()).setMobile(i.getMobile()).setCustomSource(StatusEnum.getEnumNum(i.getCustomSource(),"CustomSource"));if (StringUtils.isNotBlank(i.getProjName())){//數(shù)據(jù)存儲(chǔ)在project次表中ProjIdOutputVo projIdOutputVo =customManageListener.projectService.insertProj(new ProjInsertVO().setProjName(i.getProjName()));//數(shù)據(jù)存儲(chǔ)在proj_agreement次表中int projAgreementId=customManageListener.iProjAgreementService.addProjAgreement(new ProjAgreement().setStage(StatusEnum.getEnumNum(i.getStage(),"Stage")).setSource(StatusEnum.getEnumNum( i.getSource(),"Source")).setAddress(i.getAddress()).setProjectType(StatusEnum.getEnumNum( i.getProjectType(),"ProjectType")).setFollowResult(StatusEnum.getEnumNum( i.getFollowResult(),"FollowResult")).setFollowFailResult(i.getFollowFailResult()).setPlanStartTime(i.getPlanStartTime()).setProjNeed(i.getProjNeed()).setPlanEndTime(i.getPlanEndTime()).setProductCount(i.getProductCount()).setContractCash(i.getContractCash()).setPlaceContract(StatusEnum.getEnumNum(i.getPlaceContract(),"PlaceContract")).setEffectPj(StatusEnum.getEnumNum(i.getEffectPj(),"EffectPj")).setProjProgress(StatusEnum.getEnumNum(i.getProjProgress(),"ProjProgress")).setStar(i.getStar()).setContractCashCount(i.getContractCashCount()).setRealcontractCash(i.getRealcontractCash()).setContractMode(StatusEnum.getEnumNum(i.getContractMode(),"ContractMode")).setIntexamineTime(i.getIntexamineTime()).setInitCash(i.getInitCash()).setEndexamineTime(i.getEndexamineTime()).setEndCash(i.getEndCash()).setReceiveCashTime(i.getReceiveCashTime()).setContractDeposit(i.getContractDeposit()).setProjState(StatusEnum.getEnumNum(i.getProjState(),"ProjState")).setBackCash(StatusEnum.getEnumNum(i.getBackCash(),"BackCash")).setMarketCommision(i.getMarketCommision()).setCommisionMonth(i.getCommisionMonth()).setFeedback(StatusEnum.getEnumNum(i.getFeedback(),"Feedback")).setRemark(i.getRemark()).setDetailNeed(i.getDetailNeed()));customManageListener.iCustomManageService.addCustomManage(customManage.setProjNum(projIdOutputVo.getProjNum()).setAgreementId(projAgreementId));//指定到靜態(tài)的Usermapper注入的對(duì)象}} catch (Exception e) {e.printStackTrace();}});logger.info("存儲(chǔ)數(shù)據(jù)庫(kù)成功!");}/*** 在轉(zhuǎn)換異常 獲取其他異常下會(huì)調(diào)用本接口。拋出異常則停止讀取。如果這里不拋出異常則 繼續(xù)讀取下一行。* // * @param exception // * @param context* @throws Exception*/ // @Override // public void onException(Exception exception, AnalysisContext context) { // log.error("解析失敗,但是繼續(xù)解析下一行:{}", exception.getMessage()); // // 如果是某一個(gè)單元格的轉(zhuǎn)換異常 能獲取到具體行號(hào) // // 如果要獲取頭的信息 配合invokeHeadMap使用 // if (exception instanceof ExcelDataConvertException) { // ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; // log.error("第{}行,第{}列解析異常,數(shù)據(jù)為:{}", excelDataConvertException.getRowIndex(), // excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData()); // } // }@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {// 這里也要保存數(shù)據(jù),確保最后遺留的數(shù)據(jù)也存儲(chǔ)到數(shù)據(jù)庫(kù)saveData();logger.info("所有數(shù)據(jù)解析完成!");} }

5.進(jìn)入公共枚舉類,編寫(xiě)中文轉(zhuǎn)數(shù)字的getEnumNum()方法,實(shí)現(xiàn)中文獲取數(shù)字,導(dǎo)入,(從excel中導(dǎo)入數(shù)據(jù)至數(shù)據(jù)庫(kù))。

package com.theiavis.workcloud.project.sale.enums;import com.baomidou.mybatisplus.annotation.EnumValue; import com.fasterxml.jackson.annotation.JsonValue; import lombok.Getter; import lombok.NoArgsConstructor;/**** @author lyh and xql*/ @NoArgsConstructor @Getter public enum StatusEnum {/*** 客戶來(lái)源(頁(yè)面名稱從0開(kāi)始) 項(xiàng)目信息來(lái)源(數(shù)據(jù)庫(kù)名稱從1開(kāi)始)*/信息("Source","0"),公司轉(zhuǎn)交("Source","1"),自我開(kāi)拓("Source","2"),合作客戶轉(zhuǎn)介紹("Source","3"),絲路分公司("Source","4"),展會(huì)信息("Source","5"),協(xié)會(huì)資源轉(zhuǎn)換("Source","6"),其他渠道("Source","7"),/*** 項(xiàng)目類型 頁(yè)面字段與數(shù)據(jù)庫(kù)不符*/數(shù)字大屏("ProjectType","0"),數(shù)字沙盤(pán)("ProjectType","1"),數(shù)字展項(xiàng)("ProjectType","2"),數(shù)字展廳("ProjectType","3"),CIM("ProjectType","4"),VRXR("ProjectType","5"),其他("ProjectType","6"),/*** 客戶類型*/商業(yè)公司("CustomType","0"),政府("CustomType","1"),地產(chǎn)商("CustomType","2"),規(guī)劃院("CustomType","3"),/*** 跟進(jìn)結(jié)果*/成功("FollowResult","0"),失敗("FollowResult","1"),跟進(jìn)中("FollowResult","2"),/*** 新舊客戶類型*/移交客戶("CustomSource","0"),新客戶("CustomSource","1"),/*** 是否簽訂保密協(xié)議*/未簽約("PlaceContract","0"),已簽約("PlaceContract","1"),/*** 是否當(dāng)?shù)赜绊懥?/否("EffectPj","0"),是("EffectPj","1"),/*** 項(xiàng)目進(jìn)度*/前期介入("ProjProgress","0"),項(xiàng)目報(bào)價(jià)("ProjProgress","1"),關(guān)鍵人物接觸("ProjProgress","2"),方案制作階段("ProjProgress","3"),方案匯報(bào)("ProjProgress","4"),DEMO制作("ProjProgress","5"),投標(biāo)階段("ProjProgress","6"),簽約階段("ProjProgress","7"),/*** 合同模式*/背靠背("ContractMode","0"),分期付款("ContractMode","1"),賒銷("ContractMode","2"),/*** 項(xiàng)目狀態(tài)*/項(xiàng)目進(jìn)行中("ProjState","0"),暫停項(xiàng)目("ProjState","1"),催款中("ProjState","2"),已完成需催款("ProjState","3"),已清項(xiàng)目("ProjState","4"),/*** 回款跟催階段*/正在催款("BackCash","0"),常規(guī)("BackCash","1"),異常("BackCash","2"),拖款("BackCash","3"),/*** 具體反饋*/成果質(zhì)量("Feedback","0"),服務(wù)態(tài)度("Feedback","1"),過(guò)程技術(shù)配合("Feedback","2"),時(shí)間配合("Feedback","3"),交付時(shí)間("Feedback","4"),/*** 所處階段,所屬階段*/商機(jī)線索("Stage","0"),在談項(xiàng)目("Stage","1"),簽約項(xiàng)目("Stage","2"),已歸檔("Stage","3"),/*** 狀態(tài)*/新領(lǐng)取("Status","0"),占用("Status","1"),新歸還("Status","2"),空閑("Status","3"),接收中("Status","4"),/*** 資產(chǎn)類型0筆記本1顯示器2ipad3AR/VA設(shè)備4相機(jī)5鼠標(biāo)6其他*/筆記本("Type","0"),顯示器("Type","1"),ipad("Type","2"),AR或VA設(shè)備("Type","3"),相機(jī)("Type","4"),鼠標(biāo)("Type","5"),其他設(shè)備("Type","6"),/*** 申報(bào)視圖的申報(bào)狀態(tài)*/未申報(bào)("DeclareStatus","0"),已申報(bào)("DeclareStatus","1"),;/*** 字段 (英文)*/@EnumValueprivate String key;/*** 屬性 (數(shù)字)*/@JsonValueprivate String value;StatusEnum(String key, String value) {this.value = value;this.key = key;}public String getKey() {return key;}public String getValue() {return value;}/*** 數(shù)字獲取中文,導(dǎo)出,從數(shù)據(jù)庫(kù)中導(dǎo)出數(shù)據(jù)至excel* @param key 英文,對(duì)應(yīng)bean的字段名* @param value 數(shù)字,對(duì)應(yīng)數(shù)據(jù)庫(kù)的0123的標(biāo)識(shí)注釋* @return*/public static String getEnum(String key,String value){//用數(shù)組存放這些屬性(也就是枚舉的值)StatusEnum[] applicationStateEnums = values();//循環(huán)判斷key和value是否相等,相等就返回枚舉中對(duì)應(yīng)的中文namefor (StatusEnum itemEnum : applicationStateEnums) {if (itemEnum.key.equals(key) && itemEnum.value.equals(value)) {if (key.equals("Source")&&value!=null){return itemEnum.name().replace("信息","400信息");}else{return itemEnum.name();}}}return null;}/*** 中文獲取數(shù)字,導(dǎo)入,從excel中導(dǎo)入數(shù)據(jù)至數(shù)據(jù)庫(kù)中* @param name 中文,枚舉中有定義,對(duì)應(yīng)模板的下拉選項(xiàng)* @param key 英文,對(duì)應(yīng)bean的字段名* @return*/public static String getEnumNum(String name,String key){//用數(shù)組存放這些屬性(也就是枚舉的值)StatusEnum[] applicationStateEnums = values();//循環(huán)判斷name和key是否相等,相等就返回枚舉中對(duì)應(yīng)的數(shù)字valuefor (StatusEnum itemEnum : applicationStateEnums) {if (key.equals("Source")&& name.equals("400信息")){return "0";}if (itemEnum.name().equals(name)&& itemEnum.key.equals(key)) {return itemEnum.value;}}return null;}}

6.進(jìn)入主表的mappe進(jìn)行連表操作,需要用到的字段都要在前面標(biāo)明是哪個(gè)表里的什么字段as別名是什么,進(jìn)行連表的字段還需在主表也就是本模塊的實(shí)體類中標(biāo)注注解此字段不在本表中,不然導(dǎo)出的數(shù)據(jù)會(huì)出現(xiàn)有些字段無(wú)數(shù)據(jù)。

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.theiavis.workcloud.project.sale.mapper.CustomManageMapper"><!-- 通用設(shè)置 --><!-- 通用查詢列 --><sql id="Base_Column_List">a.id,a.sale_time_year,a.sale_time_month,a.proj_num,a.follow_id,a.create_id,a.company,a.custom_type,a.city,a.custom_department,a.contact,a.mobile,a.email,a.custom_source,a.custom_id,a.permit_code,a.dept_id,a.agreement_id,a.create_time,a.is_delete</sql><select id="findCustomManageByAll" resultType="com.theiavis.workcloud.project.sale.vo.CustomManageAndProjectVO">SELECT<include refid="Base_Column_List"/>,a.custom_type as CustomType,b.proj_name,f.name as follow_name,d.name as creater_name,c.stage,c.source as Source,c.address,c.project_type,c.follow_result,c.follow_fail_result,c.plan_start_time,c.proj_need,c.plan_end_time,c.product_count,c.contract_cash,c.place_contract,c.effect_pj,c.proj_progress,c.star,c.contract_cash_count,c.realcontract_cash,c.contract_mode,c.intexamine_time,c.init_cash,c.endexamine_time,c.end_cash,c.receive_cash_time,c.contract_deposit,c.proj_state,c.back_cash,c.market_commision,c.commision_month,c.feedback,c.remark,c.detail_need,d.avatar as creater_avatar,f.avatar as follow_avatar<include refid="Condition"/></select><sql id="Condition">FROM custom_manage AS aLEFT JOIN project b On b.proj_num=a.proj_numLEFT JOIN proj_agreement c ON c.id =a.agreement_idLEFT JOIN user d On d.user_id=a.create_idLEFT JOIN user f On f.user_id=a.follow_idwhere<if test="customManage!=null and customManage.id !=null and customManage.id !=''">a.id =#{customManage.id}and</if><if test="customManage!=null and customManage.stage !=null and customManage.stage !=''">c.stage likeCONCAT('%',#{customManage.stage}, '%') and</if><if test="customManage!=null and customManage.company !=null and customManage.company !=''">a.company likeCONCAT('%',#{customManage.company}, '%') or</if><if test="customManage!=null and customManage.projName !=null and customManage.projName !=''">b.proj_name likeCONCAT('%',#{customManage.projName}, '%') and</if><if test="customManage!=null and customManage.saleTimeYear !=null and customManage.saleTimeYear !=''">a.sale_time_year likeCONCAT('%',#{customManage.saleTimeYear}, '%') and</if><if test="customManage!=null and customManage.saleTimeMonth !=null and customManage.saleTimeMonth !=''">a.sale_time_month likeCONCAT('%',#{customManage.saleTimeMonth}, '%') and</if><if test="customManage!=null and customManage.followId !=null and customManage.followId !=''">a.follow_id =#{customManage.followId} and</if><if test="customManage!=null and customManage.createId !=null and customManage.createId !=''">a.create_id =#{customManage.createId} and</if><if test="customManage!=null and customManage.userId !=null and customManage.userId !=''">(a.create_id =#{customManage.userId} or a.follow_id = #{customManage.userId}) and</if>a.is_delete=0 and a.idOrder By a.create_time Desc</sql>

總結(jié)

以上是生活随笔為你收集整理的枚举中文转数字并easyexcel导入(读取Read)数据至数据库,用监听器解决导入关联多表的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。