package com.dbic.framework.mspps.service.proposal.business;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.dbic.framework.core.dto.quoteToPp.QuoteToProposalResDto;
import lombok.extern.slf4j.Slf4j;
/**
* Created by IntelliJ IDEA
*
* @author HuangFaye
* @date 2019/9/10 16:23
*/
@Slf4j
@Service
public class BuildPpEntityTool<T> {
private static final String ENTITY_PACKAGE_NAME = "com.dbic.framework.core.entity.pps.";
/**
* @param qoObject qo表的entity对象
* @param entityName 要生成的pp的entity的名称
* @param quoteToProposalResDto edbic返回的内容对象
* @return pp的entity列表
* @throws Exception
*/
public List<T> buildListByObj(Object qoObject,String entityName, QuoteToProposalResDto quoteToProposalResDto) throws Exception {
//qo entity的Class
Class qoClass = qoObject.getClass();
log.info(qoClass.getName());
//pp entity的Class
Class ppClass = Class.forName(ENTITY_PACKAGE_NAME+entityName);
T ppObject = (T)ppClass.newInstance();
Field ppField ;
List<T> ppList = new ArrayList<>();
Field[] fields = qoClass.getDeclaredFields();
for (Field field:fields)
{
field.setAccessible(true);
if ("quoteorderno".equals(field.getName()) || "serialVersionUID".equals(field.getName()))
{
continue;
}
//cardriver里面qo的是gender,但pp中确实sex
if("gender".equals(field.getName())){
ppField = ppClass.getDeclaredField("sex");
}
else {
ppField = ppClass.getDeclaredField(field.getName());
}
ppField.setAccessible(true);
ppField.set(ppObject,field.get(qoObject));
}
cleanList(ppList,ppObject,quoteToProposalResDto);
return ppList;
}
/**
* @param qoObjects qo表的entity对象列表
* @param entityName 要生成的pp的entity的名称
* @param quoteToProposalResDto edbic返回的内容对象
* @return pp的entity列表
* @throws Exception
*/
public List<T> buildListByList(List<T> qoObjects ,String entityName, QuoteToProposalResDto quoteToProposalResDto) throws Exception {
List<T> ppList = new ArrayList<>();
List<T> ppList1;
for (T qoObject:qoObjects)
{
//每一个Object都要转成pp
ppList1 = buildListByObj(qoObject,entityName,quoteToProposalResDto);
ppList.addAll(ppList1);
}
return ppList;
}
/**
* qo entity有riskType的情况下,对pp entity进行拆分
* @param qoObjects qo表的entity对象列表
* @param entityName 要生成的pp的entity的名称
* @param quoteToProposalResDto edbic返回的内容对象
* @return pp的entity列表
* @throws Exception
*/
public List<T> buildListByRiskType(List<T> qoObjects,String entityName, QuoteToProposalResDto quoteToProposalResDto) throws Exception {
List<T> ppList = new ArrayList<>();
String riskType = "";
Field riskTypeField;
for (T qoObject:qoObjects)
{
//qo entity的Class
Class qoClass = qoObject.getClass();
log.info(qoClass.getName());
//pp entity的Class
Class ppClass = Class.forName(ENTITY_PACKAGE_NAME+entityName);
T ppObject = (T)ppClass.newInstance();
Field ppField ;
Field[] fields = qoClass.getDeclaredFields();
for (Field field:fields)
{
field.setAccessible(true);
if ("quoteorderno".equals(field.getName()) || "risktype".equals(field.getName())|| "serialVersionUID".equals(field.getName()))
{
continue;
}
ppField = ppClass.getDeclaredField(field.getName());
ppField.setAccessible(true);
ppField.set(ppObject,field.get(qoObject));
}
riskTypeField = qoClass.getDeclaredField("risktype");
riskTypeField.setAccessible(true);
riskType = (String) riskTypeField.get(qoObject);
//1:商业 2:交强;3:财产险;4:意外险
switch (riskType)
{
case "1":
T ppObject1 = cloneObject(ppObject);
ppField = ppObject1.getClass().getDeclaredField("proposalno");
ppField.setAccessible(true);
ppField.set(ppObject1,quoteToProposalResDto.getProposalNoBI());
ppList.add(ppObject1);
break;
case "2":
T ppObject2 = cloneObject(ppObject);
ppField = ppObject2.getClass().getDeclaredField("proposalno");
ppField.setAccessible(true);
ppField.set(ppObject2,quoteToProposalResDto.getProposalNoCI());
ppList.add(ppObject2);
break;
case "3":
T ppObject3 = cloneObject(ppObject);
ppField = ppObject3.getClass().getDeclaredField("proposalno");
ppField.setAccessible(true);
ppField.set(ppObject3,quoteToProposalResDto.getProposalNoProperty());
ppList.add(ppObject3);
break;
case "4":
T ppObject4 = cloneObject(ppObject);
ppField = ppObject4.getClass().getDeclaredField("proposalno");
ppField.setAccessible(true);
ppField.set(ppObject4,quoteToProposalResDto.getPolicyNoHealth());
ppList.add(ppObject4);
break;
}
}
return ppList;
}
/**
* 克隆对象
* @param object 被克隆的对象
* @return 克隆结果
* @throws Exception
*/
private T cloneObject(T object) throws Exception {
//克隆出的目标对象
Class tClass = object.getClass();
T tObject = (T)tClass.newInstance();
//被克隆对象的Class
Class sClass = object.getClass();
Field[] fields = sClass.getDeclaredFields();
Field tField ;
for (Field field:fields)
{
field.setAccessible(true);
if ("serialVersionUID".equals(field.getName()))
{
continue;
}
tField = tClass.getDeclaredField(field.getName());
tField.setAccessible(true);
tField.set(tObject,field.get(object));
}
return tObject;
}
/**
* @param ppList 存放整理完成的对象
* @param ppObject 产生List数据的源对象
* @param quoteToProposalResDto 决定List长度的对象
*/
private void cleanList(List<T> ppList,T ppObject,QuoteToProposalResDto quoteToProposalResDto) throws Exception {
Field fieldTmp ;
if (!StringUtils.isEmpty(quoteToProposalResDto.getProposalNoBI()))
{
//克隆出刚刚赋值过的对象
T ppObject1 = cloneObject(ppObject);
//设置proprosalno值
fieldTmp = ppObject1.getClass().getDeclaredField("proposalno");
fieldTmp.setAccessible(true);
fieldTmp.set(ppObject1,quoteToProposalResDto.getProposalNoBI());
ppList.add(ppObject1);
}
if (!StringUtils.isEmpty(quoteToProposalResDto.getProposalNoCI()))
{
//克隆出刚刚赋值过的对象
T ppObject1 = cloneObject(ppObject);
//设置proprosalno值
fieldTmp = ppObject1.getClass().getDeclaredField("proposalno");
fieldTmp.setAccessible(true);
fieldTmp.set(ppObject1,quoteToProposalResDto.getProposalNoCI());
ppList.add(ppObject1);
}
if (!StringUtils.isEmpty(quoteToProposalResDto.getProposalNoHealth()))
{
//克隆出刚刚赋值过的对象
T ppObject1 = cloneObject(ppObject);
//设置proprosalno值
fieldTmp = ppObject1.getClass().getDeclaredField("proposalno");
fieldTmp.setAccessible(true);
fieldTmp.set(ppObject1,quoteToProposalResDto.getProposalNoHealth());
ppList.add(ppObject1);
}
if (!StringUtils.isEmpty(quoteToProposalResDto.getProposalNoProperty()))
{
//克隆出刚刚赋值过的对象
T ppObject1 = cloneObject(ppObject);
//设置proprosalno值
fieldTmp = ppObject1.getClass().getDeclaredField("proposalno");
fieldTmp.setAccessible(true);
fieldTmp.set(ppObject1,quoteToProposalResDto.getProposalNoProperty());
ppList.add(ppObject1);
}
}
}