|
|
@@ -1,16 +1,40 @@
|
|
|
package jp.yamoto.farm.sankin.biz.service.impl;
|
|
|
|
|
|
-import jp.yamoto.farm.common.utils.DecryptUtils;
|
|
|
-import jp.yamoto.farm.common.utils.SecurityUtils;
|
|
|
-import jp.yamoto.farm.common.utils.ValueUtils;
|
|
|
+import jp.yamoto.farm.common.biz.domain.entity.MastAddressEntity;
|
|
|
+import jp.yamoto.farm.common.biz.domain.entity.MastCustomerEntity;
|
|
|
+import jp.yamoto.farm.common.biz.enums.SankinPgIdEnum;
|
|
|
+import jp.yamoto.farm.common.biz.mapper.MastCustomerBaseMapper;
|
|
|
+import jp.yamoto.farm.common.biz.service.IMastAddressBaseService;
|
|
|
+import jp.yamoto.farm.common.constant.Constants;
|
|
|
+import jp.yamoto.farm.common.core.domain.ImportFileErrorVo;
|
|
|
+import jp.yamoto.farm.common.exception.ServiceException;
|
|
|
+import jp.yamoto.farm.common.utils.*;
|
|
|
+import jp.yamoto.farm.common.utils.file.FileUtils;
|
|
|
+import jp.yamoto.farm.common.utils.uuid.IdUtils;
|
|
|
+import jp.yamoto.farm.sankin.biz.constants.SankinConstants;
|
|
|
+import jp.yamoto.farm.sankin.biz.domain.bo.MastCustomerImportBo;
|
|
|
import jp.yamoto.farm.sankin.biz.domain.bo.MastCustomerSearchBo;
|
|
|
import jp.yamoto.farm.sankin.biz.domain.vo.MastCustomerVo;
|
|
|
import jp.yamoto.farm.sankin.biz.mapper.MastCustomerMapper;
|
|
|
import jp.yamoto.farm.sankin.biz.service.IMastCustomerService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.csv.CSVFormat;
|
|
|
+import org.apache.commons.csv.CSVParser;
|
|
|
+import org.apache.commons.csv.CSVRecord;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.Reader;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* 顧客マスタ Service業務処理
|
|
|
@@ -18,11 +42,18 @@ import java.util.List;
|
|
|
* @author nextosd
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class MastCustomerServiceImpl implements IMastCustomerService {
|
|
|
|
|
|
@Autowired
|
|
|
private MastCustomerMapper mastCustomerMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MastCustomerBaseMapper mastCustomerBaseMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IMastAddressBaseService mastAddressBaseService;
|
|
|
+
|
|
|
/**
|
|
|
* 顧客マスタの検索
|
|
|
*
|
|
|
@@ -69,4 +100,239 @@ public class MastCustomerServiceImpl implements IMastCustomerService {
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 顧客マスタ情報を取込む
|
|
|
+ *
|
|
|
+ * @param csvFile エクセルファイル
|
|
|
+ * @return エラー情報リスト
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public List<ImportFileErrorVo> importCsvData(MultipartFile csvFile) {
|
|
|
+ List<ImportFileErrorVo> errorInfoList = new ArrayList<>();
|
|
|
+ String fileName = csvFile.getOriginalFilename();
|
|
|
+
|
|
|
+ if (fileName == null || !StringUtils.toCamelCase(fileName).endsWith("csv")) {
|
|
|
+ // 拡張子チェック CSVファイルを選択してください。
|
|
|
+ throw new ServiceException(MessageUtils.message("E0009", MessageUtils.message("csvExtension")));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!FileUtils.hasShiftJIS(csvFile)) {
|
|
|
+ // {0}CSVファイルのフォーマット({1})が不正です。正しいフォーマットのCSVファイルを選択してください。
|
|
|
+ throw new ServiceException(MessageUtils.message("E0026", csvFile.getOriginalFilename(), "Shift_JIS"));
|
|
|
+ }
|
|
|
+
|
|
|
+ long totalSize = csvFile.getSize();
|
|
|
+
|
|
|
+ if (totalSize > SankinConstants.NUMBER_10 * SankinConstants.NUMBER_1024 * SankinConstants.NUMBER_1024) {
|
|
|
+ // ファイルのサイズは10MB以下を選択してください。
|
|
|
+ throw new ServiceException(MessageUtils.message("E0024", "10MB"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ヘッダー情報と明細情報のエラー情報を格納するリスト
|
|
|
+ List<ImportFileErrorVo> errorResultDetailInfoList = new ArrayList<>();
|
|
|
+ List<MastCustomerImportBo> mastCustomerImportDataList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 選択されたファイルのサイズ = 0MB または CSVにデータが0件の場合
|
|
|
+ String csvFileName = csvFile.getOriginalFilename();
|
|
|
+ if (!Objects.requireNonNull(csvFileName).startsWith("customer")
|
|
|
+ && !Objects.requireNonNull(csvFileName).startsWith("customer")) {
|
|
|
+ // アップロード対象が顧客情報の場合、顧客情報以外ファイル(不正のファイル名)を選択しないでください。
|
|
|
+ String param = MessageUtils.message("customer");
|
|
|
+ throw new ServiceException(MessageUtils.message("E0027", param, param, csvFileName));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (csvFile.getSize() == 0) {
|
|
|
+ // {不正のファイル名}ファイルのサイズが0Mであるか。データを追加した後、再度アップロードしてください。
|
|
|
+ throw new ServiceException(MessageUtils.message("E0025", csvFileName));
|
|
|
+ }
|
|
|
+
|
|
|
+ try (Reader fileReader = new InputStreamReader(csvFile.getInputStream(), Charset.forName(Constants.SHIFT_JIS));
|
|
|
+ // 区切り文字をカンマに設定
|
|
|
+ // 引用文字をダブルクォートに設定
|
|
|
+ // エスケープ文字をバックスラッシュに設定を無視
|
|
|
+ // フィールド周囲の空白文字を無視
|
|
|
+ // 空行を無視しない
|
|
|
+ CSVParser csvParser = new CSVParser(fileReader, CSVFormat.DEFAULT.builder()
|
|
|
+ .setDelimiter(Constants.COMMA)
|
|
|
+ .setQuote('\"')
|
|
|
+ .setIgnoreSurroundingSpaces(true)
|
|
|
+ .setIgnoreEmptyLines(false)
|
|
|
+ .build());
|
|
|
+ ) {
|
|
|
+
|
|
|
+ List<CSVRecord> csvRecords = csvParser.getRecords();
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(csvRecords) && csvRecords.size() == 2) {
|
|
|
+ // {不正のファイル名}ファイルのサイズが0Mであるか。データを追加した後、再度アップロードしてください。
|
|
|
+ throw new ServiceException(MessageUtils.message("E0025", csvFileName));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (CSVRecord record : csvRecords) {
|
|
|
+ // レコードのフィールド数を取得
|
|
|
+ int fieldCount = record.size();
|
|
|
+ long csvRowLine = record.getRecordNumber();
|
|
|
+ String fileRowLine = ConvertUtils.toStr(csvRowLine);
|
|
|
+
|
|
|
+ if (csvRowLine <= 1) {
|
|
|
+ // 最初の2行をスキップ
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ MastCustomerImportBo mastCustomerImportBo = new MastCustomerImportBo();
|
|
|
+
|
|
|
+ // システム顧客番号 sys_customer_id
|
|
|
+ mastCustomerImportBo.setSysCustomerId(record.get(0));
|
|
|
+ validateFieldLength(mastCustomerImportBo.getSysCustomerId(), "label.sysCustomerId", 32, csvFileName, fileRowLine, errorInfoList);
|
|
|
+
|
|
|
+ // 会員ID
|
|
|
+ mastCustomerImportBo.setMemberId(record.get(1));
|
|
|
+ validateFieldLength(mastCustomerImportBo.getMemberId(), "label.memberId", 20, csvFileName, fileRowLine, errorInfoList);
|
|
|
+
|
|
|
+ // 氏名(姓)
|
|
|
+ mastCustomerImportBo.setFirstName(record.get(2));
|
|
|
+ validateFieldLength(mastCustomerImportBo.getFirstName(), "label.firstName", 64, csvFileName, fileRowLine, errorInfoList);
|
|
|
+
|
|
|
+ // 氏名(名)
|
|
|
+ mastCustomerImportBo.setLastName(record.get(3));
|
|
|
+ validateFieldLength(mastCustomerImportBo.getLastName(), "label.lastName", 32, csvFileName, fileRowLine, errorInfoList);
|
|
|
+
|
|
|
+ // 氏名フリガナ(セイ)
|
|
|
+ mastCustomerImportBo.setFuriganaSei(record.get(4));
|
|
|
+ validateFieldLength(mastCustomerImportBo.getFuriganaSei(), "label.furiganaSei", 56, csvFileName, fileRowLine, errorInfoList);
|
|
|
+
|
|
|
+ // 氏名フリガナ(メイ)
|
|
|
+ mastCustomerImportBo.setFuriganaMei(record.get(5));
|
|
|
+ validateFieldLength(mastCustomerImportBo.getFuriganaMei(), "label.furiganaMei", 56, csvFileName, fileRowLine, errorInfoList);
|
|
|
+
|
|
|
+ // 会社名
|
|
|
+ mastCustomerImportBo.setCompanyName(record.get(6));
|
|
|
+ // 部署名
|
|
|
+ mastCustomerImportBo.setDepartmentName(record.get(7));
|
|
|
+ // 郵便番号
|
|
|
+ mastCustomerImportBo.setPostalCode(record.get(8));
|
|
|
+ // 都道府県
|
|
|
+ mastCustomerImportBo.setKenName(record.get(9));
|
|
|
+ // 市区町村
|
|
|
+ mastCustomerImportBo.setCityName(record.get(10));
|
|
|
+ // 町域・番地
|
|
|
+ mastCustomerImportBo.setTownName(record.get(11));
|
|
|
+ // ビル等
|
|
|
+ mastCustomerImportBo.setBuildingEtc(record.get(12));
|
|
|
+ // メールアドレス
|
|
|
+ mastCustomerImportBo.setMailAddress(record.get(13));
|
|
|
+ // 電話番号
|
|
|
+ mastCustomerImportBo.setPhoneNumber(record.get(14));
|
|
|
+ // 携帯電話番号
|
|
|
+ mastCustomerImportBo.setMobile(record.get(15));
|
|
|
+ // FAX番号
|
|
|
+ mastCustomerImportBo.setFax(record.get(16));
|
|
|
+
|
|
|
+ mastCustomerImportDataList.add(mastCustomerImportBo);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("CSVファイルを読み取る際にエラーが発生しました。", e);
|
|
|
+ // I0004=取込に失敗しました。
|
|
|
+ throw new ServiceException(MessageUtils.message("I0004"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(errorInfoList)) {
|
|
|
+ return errorInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(MastCustomerImportBo data : mastCustomerImportDataList){
|
|
|
+ MastCustomerEntity mastCustomerEntity = new MastCustomerEntity();
|
|
|
+ BeanUtils.copyProperties(data, mastCustomerEntity);
|
|
|
+
|
|
|
+ // 都道府県
|
|
|
+ if(ValueUtils.isNotEmpty(data.getKenName())){
|
|
|
+ MastAddressEntity mastAddressQuery = new MastAddressEntity();
|
|
|
+ mastAddressQuery.setKenName(data.getKenName());
|
|
|
+ MastAddressEntity mastAddressEntity = mastAddressBaseService.selectMastAddressByName(mastAddressQuery);
|
|
|
+ if(ValueUtils.isNotEmpty(mastAddressEntity)) {
|
|
|
+ mastCustomerEntity.setKenId(mastAddressEntity.getKenId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 市区町村
|
|
|
+ if(ValueUtils.isNotEmpty(data.getCityName())){
|
|
|
+ MastAddressEntity mastAddressQuery = new MastAddressEntity();
|
|
|
+ mastAddressQuery.setCityName(data.getCityName());
|
|
|
+ MastAddressEntity mastAddressEntity = mastAddressBaseService.selectMastAddressByName(mastAddressQuery);
|
|
|
+ if(ValueUtils.isNotEmpty(mastAddressEntity)) {
|
|
|
+ mastCustomerEntity.setCityId(mastAddressEntity.getCityId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // メールアドレス
|
|
|
+ if(ValueUtils.isNotEmpty(data.getMailAddress())){
|
|
|
+ mastCustomerEntity.setMailAddress(DecryptUtils.encryptAES(data.getMailAddress()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 電話番号
|
|
|
+ if(ValueUtils.isNotEmpty(data.getPhoneNumber())){
|
|
|
+ mastCustomerEntity.setPhoneNumber(DecryptUtils.encryptAES(data.getPhoneNumber()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ID
|
|
|
+ mastCustomerEntity.setId(IdUtils.nextIdStr());
|
|
|
+ // 顧客名
|
|
|
+ mastCustomerEntity.setCustomerName(StringUtils.join(data.getFirstName(), data.getLastName()));
|
|
|
+ // 顧客ID = 会員ID
|
|
|
+ mastCustomerEntity.setCustomerId(data.getMemberId());
|
|
|
+ // 住所
|
|
|
+ mastCustomerEntity.setAddress(StringUtils.join(data.getKenName(), data.getCityName(), data.getTownName(), data.getBuildingEtc()));
|
|
|
+ // 農家ID
|
|
|
+ mastCustomerEntity.setFarmerId(SecurityUtils.getUsername());
|
|
|
+ // 登録プログラムID
|
|
|
+ mastCustomerEntity.setCreatePgId(SankinPgIdEnum.N015.getCode());
|
|
|
+ // 更新プログラムID
|
|
|
+ mastCustomerEntity.setUpdatePgId(SankinPgIdEnum.N015.getCode());
|
|
|
+ // Version
|
|
|
+ mastCustomerEntity.setVersion(1);
|
|
|
+ // システムソースフラグ = 1:らくうるカート
|
|
|
+ mastCustomerEntity.setSystemSourceFlg("1");
|
|
|
+
|
|
|
+ mastCustomerBaseMapper.insert(mastCustomerEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ return errorInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 検査結果詳細情報のアップロード検証
|
|
|
+ *
|
|
|
+ * @param fieldValue フィールド値
|
|
|
+ * @param fieldName フィールド名
|
|
|
+ * @param maxLength 最大桁数
|
|
|
+ * @param detailFileName ファイル名
|
|
|
+ * @param fileRowLine 行番号
|
|
|
+ * @param errorInfoList エラー情報リスト
|
|
|
+ */
|
|
|
+ private void validateFieldLength(String fieldValue, String fieldName, int maxLength, String detailFileName,
|
|
|
+ String fileRowLine, List<ImportFileErrorVo> errorInfoList) {
|
|
|
+ if (fieldValue.length() > maxLength) {
|
|
|
+ // E0051={0}の桁数は{1}桁以上、もしくはサロゲートペアが含まれ最長バイト数を超過
|
|
|
+ String message = MessageUtils.message("E0051", MessageUtils.message(fieldName), maxLength);
|
|
|
+ makeErrorInfo(detailFileName, fileRowLine, message, errorInfoList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * エラー情報を生成する
|
|
|
+ *
|
|
|
+ * @param fileName 不正ファイル名
|
|
|
+ * @param fileRowLine 不正行番号
|
|
|
+ * @param message メッセージ
|
|
|
+ * @param errorList 格納エラーリスト
|
|
|
+ */
|
|
|
+ private void makeErrorInfo(String fileName, String fileRowLine, String message, List<ImportFileErrorVo> errorList) {
|
|
|
+ ImportFileErrorVo errorInfo = new ImportFileErrorVo();
|
|
|
+ errorInfo.setObjectName(fileName);
|
|
|
+ errorInfo.setRowLine(fileRowLine);
|
|
|
+ errorInfo.setMessage(message);
|
|
|
+ errorList.add(errorInfo);
|
|
|
+ }
|
|
|
}
|