package com.ydtech.modules.fnc.service.impl;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ydtech.aop.exception.DescribeException;
import com.ydtech.constants.SysConstants;
import com.ydtech.core.page.HttpResult;
import com.ydtech.modules.admin.dao.SysDeptMapper;
import com.ydtech.modules.admin.dao.SysUserMapper;
import com.ydtech.modules.admin.model.SysDept;
import com.ydtech.modules.admin.model.SysUser;
import com.ydtech.modules.esm.dao.EsmInsCompanyMapper;
import com.ydtech.modules.esm.model.EsmInsCompany;
import com.ydtech.modules.esm.service.EsmUserReferrerService;
import com.ydtech.modules.fnc.dao.InsPlyIncomeMapper;
import com.ydtech.modules.fnc.entity.InsPlyIncome;
import com.ydtech.modules.fnc.entity.InsPlyMain;
import com.ydtech.modules.fnc.dao.InsPlyMainMapper;
import com.ydtech.modules.fnc.service.InsPlyMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydtech.modules.fnc.vo.InsPlyIncomeVo;
import com.ydtech.modules.fnc.vo.InsPlyMainVo;
import com.ydtech.modules.ins.model.InsCmain;
import com.ydtech.modules.ins.model.InsCommission;
import com.ydtech.modules.ins.model.InsCompanyFee;
import com.ydtech.modules.ins.model.InsDeptFee;
import com.ydtech.modules.ins.model.vo.InsCmainExcelVO;
import com.ydtech.modules.ins.service.InsCommissionService;
import com.ydtech.modules.ins.service.InsCompanyFeeService;
import com.ydtech.modules.ins.service.InsDeptFeeService;
import com.ydtech.modules.order.entity.BasePageResult;
import com.ydtech.modules.order.entity.InsAreaCompany;
import com.ydtech.modules.order.entity.InsFeeOrder;
import com.ydtech.modules.order.entity.InsOrders;
import com.ydtech.security.PubUserInfo;
import com.ydtech.utils.BigDecimalUtils;
import com.ydtech.utils.DateUtil;
import com.ydtech.utils.StringUtils;
import com.ydtech.utils.excel.ExcelUtils2;
import org.jsoup.helper.DataUtil;
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.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author gws
* @since 2024-01-23
*/
@Service
public class InsPlyMainServiceImpl extends ServiceImpl implements InsPlyMainService {
@Autowired
private EsmInsCompanyMapper esmInsCompanyMapper;
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private InsPlyIncomeMapper insPlyIncomeMapper;
@Autowired
private InsDeptFeeService insDeptFeeService;
@Autowired
private InsCommissionService insCommissionService;
@Autowired
private InsCompanyFeeService insCompanyFeeService;
@Autowired
private EsmUserReferrerService esmUserReferrerService;
public HttpResult> queryPly(InsPlyMainVo insPlyMainVo) {
try {
PageInfo> pageHelper = null;
PageHelper.startPage(insPlyMainVo.getPageNum(), insPlyMainVo.getPageSize());
LambdaQueryWrapper wrapper=new LambdaQueryWrapper();
wrapper.likeRight(StringUtils.isNotEmpty(insPlyMainVo.getBatchno()),InsPlyMain::getBatchno,insPlyMainVo.getBatchno())
.likeRight(StringUtils.isNotEmpty(insPlyMainVo.getDeptId()),InsPlyMain::getDeptId,insPlyMainVo.getDeptId())
.eq(StringUtils.isNotEmpty(insPlyMainVo.getPolicyno()),InsPlyMain::getPolicyno,insPlyMainVo.getPolicyno())
.between(!ObjectUtils.isEmpty(insPlyMainVo.getStartDate()) && !ObjectUtils.isEmpty(insPlyMainVo.getEndDate()),
InsPlyMain::getCreatetime, insPlyMainVo.getStartDate(), insPlyMainVo.getEndDate())
.eq(StringUtils.isNotEmpty(insPlyMainVo.getCompanyId()),InsPlyMain::getCompanyId,insPlyMainVo.getCompanyId());
List list = this.baseMapper.selectList(wrapper);
pageHelper = new PageInfo(list);
BasePageResult pageResult = new BasePageResult(insPlyMainVo.getPageNum(), insPlyMainVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
return HttpResult.ok(pageResult);
} catch (Exception e) {
e.printStackTrace();
return HttpResult.error("查询异常");
}
}
/**
* 保单导入
*
* @param file excel文件
* @return
*/
@Transactional
@Override
public HttpResult excelImport(MultipartFile file) throws Exception {
String msg = "导入成功";
try {
List insPlyMainList = ExcelUtils2.importExcel(file, 0, 1, InsPlyMain.class);
// 校验保单信息
Date date = new Date();
int count = 0;
String batchno = DateUtil.formatFullTime(LocalDateTime.now())+StpUtil.getLoginId().toString();
for (int i = 0; i < insPlyMainList.size(); i++) {
InsPlyMain excelVo = insPlyMainList.get(i);
/**
* 1.校验是否为空
*/
isNullCmain(i, excelVo);
/**
* 2.码值校验:保险公司、协议号、机构代码、业务员工号
*/
checkData(i, excelVo);
excelVo.setAllFeeValue(excelVo.getCommissionFeevalue().add(excelVo.getOtherFeevalue()));
excelVo.setBatchno(batchno);
excelVo.setCreator(StpUtil.getLoginId().toString());
excelVo.setCreatetime(LocalDateTime.now());
this.baseMapper.insert(excelVo);
//生成保司费用(应收费用)
gene_orderIncome( excelVo);
//生成机构费用
gene_deptfee( excelVo);
//生成分销费用
saveRetail(excelVo);
//个代费用生成
saveInsCompanyFee(excelVo);
count++;
}
if (insPlyMainList.size() != count) {
throw new DescribeException("导入数据错误,应导入" + insPlyMainList.size() + "条数据,实际导入" + count + "条数据,请检查excel数据格式后重新导入或请联系相关人员!");
}
msg = "导入成功,批次号:"+batchno+" 成功导入<" + insPlyMainList.size() + ">条数据!";
} catch (IOException e) {
e.printStackTrace();
throw e;
}
return HttpResult.ok(msg, null);
}
/**
* 生成保司费用(应收费用)
* @throws Exception
*/
private void gene_orderIncome(InsPlyMain excelVo) throws Exception{
InsPlyIncome insPlyIncome =new InsPlyIncome();
insPlyIncome.setOrderno(excelVo.getOrderno());
insPlyIncome.setDeptId(excelVo.getDeptId());
insPlyIncome.setCompanyId(excelVo.getCompanyId());
insPlyIncome.setAgreementId(excelVo.getAgreementId());
insPlyIncome.setLicenseno(excelVo.getLicenseno());
insPlyIncome.setPayDate(StringUtils.isNullOrEmpty(excelVo.getPayDate())?excelVo.getCreatetime():excelVo.getPayDate());
insPlyIncome.setCreateTime(LocalDateTime.now() );
insPlyIncome.setPolicyno(excelVo.getPolicyno());
insPlyIncome.setRiskcode(excelVo.getRiskcode());
insPlyIncome.setTaxamount(excelVo.getTaxamount());
insPlyIncome.setNoTaxPremium(excelVo.getNoTaxPremium());
insPlyIncome.setCommissionFeerate(excelVo.getCommissionFeerate());
insPlyIncome.setOtherFeerate(excelVo.getOtherFeerate());
insPlyIncome.setCommissionFeevalue(excelVo.getCommissionFeevalue());
insPlyIncome.setOtherFeevalue(excelVo.getOtherFeevalue());
insPlyIncome.setAllFeeValue(excelVo.getAllFeeValue());
insPlyIncomeMapper.insert(insPlyIncome);
}
/**
* 生成机构费用
* @throws Exception
*/
private void gene_deptfee( InsPlyMain excelVo) throws Exception{
InsDeptFee deptFee = new InsDeptFee();
deptFee.setOrderno(excelVo.getOrderno());
deptFee.setAgreeid(excelVo.getAgreementId());
deptFee.setAuditstatus("1");
deptFee.setSettlestatus("0");
deptFee.setInscompany(excelVo.getCompanyId());
deptFee.setPolicyno(excelVo.getPolicyno());
deptFee.setRiskcode(excelVo.getRiskcode());
deptFee.setLicenseno(excelVo.getLicenseno());
geneInsDeptFee(deptFee,excelVo);
}
private void geneInsDeptFee(InsDeptFee deptFee,InsPlyMain excelVo) {
if (excelVo.getFirstDeptPremiums().doubleValue() > 0) {
deptFee.setComcode(excelVo.getDeptId().substring(0, 2)); //99140302M01D01
deptFee.setKeeprate(excelVo.getFirstDeptProportion().doubleValue() );
deptFee.setKeepamount(excelVo.getFirstDeptPremiums().doubleValue());
deptFee.setComlevel("一级");
insDeptFeeService.insert(deptFee);
}
if (excelVo.getSecondDeptPremiums().doubleValue() > 0) {
deptFee.setComcode(excelVo.getDeptId().substring(0, 4)); //99140302M01D01
deptFee.setKeeprate(excelVo.getSecondDeptProportion().doubleValue() );
deptFee.setKeepamount(excelVo.getSecondDeptPremiums().doubleValue());
deptFee.setComlevel("二级");
insDeptFeeService.insert(deptFee);
}
if (excelVo.getThirdDeptPremiums().doubleValue() > 0) {
deptFee.setComcode(excelVo.getDeptId().substring(0, 6)); //99140302M01D01
deptFee.setKeeprate(excelVo.getThirdDeptProportion().doubleValue() );
deptFee.setKeepamount(excelVo.getThirdDeptPremiums().doubleValue());
deptFee.setComlevel("三级");
insDeptFeeService.insert(deptFee);
}
if (excelVo.getForthDeptPremiums().doubleValue() > 0) {
deptFee.setComcode(excelVo.getDeptId().substring(0, 8)); //99140302M01D01
deptFee.setKeeprate(excelVo.getForthDeptProportion().doubleValue() );
deptFee.setKeepamount(excelVo.getForthDeptPremiums().doubleValue());
deptFee.setComlevel("四级");
insDeptFeeService.insert(deptFee);
}
if (excelVo.getFifthDeptPremiums().doubleValue() > 0) {
deptFee.setComcode(excelVo.getDeptId().substring(0, 11)); //99140302M01D01
deptFee.setKeeprate(excelVo.getFifthDeptProportion().doubleValue() );
deptFee.setKeepamount(excelVo.getFifthDeptPremiums().doubleValue());
deptFee.setComlevel("五级");
insDeptFeeService.insert(deptFee);
}
}
/**
* 三级分销费用保存
*
* @return
*/
public void saveRetail(InsPlyMain excelVo) throws Exception{
List userlist=new ArrayList<>();
esmUserReferrerService.getUpUser(excelVo.getUserId(),userlist);
//userlist:包括本人在内向上所有人
if(userlist.size()-1>0){
if(userlist.size()-1>=1
&& excelVo.getThirdDetailProportion().doubleValue()>0)
{//三级分销
SysUser user=sysUserMapper.selectById(userlist.get(1));
InsCommission commission = new InsCommission();
commission.setOrderno(excelVo.getOrderno());
commission.setDeptid(excelVo.getDeptId());
commission.setDeptname(excelVo.getDeptName());
commission.setInscompany(excelVo.getCompanyId());
commission.setAgreeid(excelVo.getAgreementId());
commission.setRefereescode(user.getId());
commission.setRefereesname(user.getName());
commission.setLevel("3");
commission.setAuditstatus("1"); //0未审核 1已审核
commission.setSettlestatus("0"); //0未结算 1已结算
commission.setInsuredtime(excelVo.getPayDate().toString()); //承保确认时间
commission.setPolicyno(excelVo.getPolicyno());
commission.setRiskcode(excelVo.getRiskcode());
commission.setLicenseno(excelVo.getLicenseno());
commission.setCommissionrate(excelVo.getThirdDetailProportion().doubleValue());
commission.setCommissionamount(excelVo.getThirdDetailPremiums().doubleValue());
insCommissionService.insert(commission);
}
if(userlist.size()-1>=2
&&excelVo.getSecondDetailPremiums().doubleValue()>0)
{//二级分销
SysUser user=sysUserMapper.selectById(userlist.get(1));
InsCommission commission = new InsCommission();
commission.setOrderno(excelVo.getOrderno());
commission.setDeptid(excelVo.getDeptId());
commission.setDeptname(excelVo.getDeptName());
commission.setInscompany(excelVo.getCompanyId());
commission.setAgreeid(excelVo.getAgreementId());
commission.setRefereescode(user.getId());
commission.setRefereesname(user.getName());
commission.setLevel("2");
commission.setAuditstatus("1"); //0未审核 1已审核
commission.setSettlestatus("0"); //0未结算 1已结算
commission.setInsuredtime(excelVo.getPayDate().toString()); //承保确认时间
commission.setPolicyno(excelVo.getPolicyno());
commission.setRiskcode(excelVo.getRiskcode());
commission.setLicenseno(excelVo.getLicenseno());
commission.setCommissionrate(excelVo.getSecondDetailProportion().doubleValue());
commission.setCommissionamount(excelVo.getSecondDetailPremiums().doubleValue());
insCommissionService.insert(commission);
}
if(userlist.size()-1>=3
&&excelVo.getFirstDetailPremiums().doubleValue()>0)
{//一级分销
SysUser user=sysUserMapper.selectById(userlist.get(1));
InsCommission commission = new InsCommission();
commission.setOrderno(excelVo.getOrderno());
commission.setDeptid(excelVo.getDeptId());
commission.setDeptname(excelVo.getDeptName());
commission.setInscompany(excelVo.getCompanyId());
commission.setAgreeid(excelVo.getAgreementId());
commission.setRefereescode(user.getId());
commission.setRefereesname(user.getName());
commission.setLevel("1");
commission.setAuditstatus("1"); //0未审核 1已审核
commission.setSettlestatus("0"); //0未结算 1已结算
commission.setInsuredtime(excelVo.getPayDate().toString()); //承保确认时间
commission.setPolicyno(excelVo.getPolicyno());
commission.setRiskcode(excelVo.getRiskcode());
commission.setLicenseno(excelVo.getLicenseno());
commission.setCommissionrate(excelVo.getFirstDetailProportion().doubleValue());
commission.setCommissionamount(excelVo.getFirstDetailPremiums().doubleValue());
insCommissionService.insert(commission);
}
}
}
/**
* 生成个代费用
* @throws Exception
*/
public void saveInsCompanyFee(InsPlyMain excelVo) throws Exception {
SysDept sysDept=sysDeptMapper.selectById(excelVo.getDeptId());
SysUser sysUser=sysUserMapper.selectById(excelVo.getUserId());
InsCompanyFee insCompanyFee = new InsCompanyFee();
insCompanyFee.setOrderno(excelVo.getOrderno());
// insCompanyFee.setFrameNo(excelVo.getFrameno());
insCompanyFee.setLicenseNo(excelVo.getLicenseno());
insCompanyFee.setInscompany(excelVo.getCompanyId());
insCompanyFee.setAgreeid(excelVo.getAgreementId());
insCompanyFee.setUserid(excelVo.getUserId());
insCompanyFee.setUsername(sysUser.getName());
insCompanyFee.setDeptid(excelVo.getDeptId());
insCompanyFee.setDeptname(sysDept.getName());
insCompanyFee.setSettletype("B");
insCompanyFee.setSettlestatus("0");
insCompanyFee.setCreatetime(DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_SPLIT_PATTERN));
BigDecimal dept_rate=excelVo.getFirstDeptProportion()
.add(excelVo.getSecondDeptProportion())
.add(excelVo.getThirdDeptProportion())
.add(excelVo.getForthDeptProportion())
.add(excelVo.getFifthDeptProportion());
BigDecimal retail_rate=excelVo.getFirstDetailProportion()
.add(excelVo.getSecondDetailProportion())
.add(excelVo.getThirdDetailProportion());
insCompanyFee.setPolicyno(excelVo.getPolicyno());
insCompanyFee.setRiskcode(excelVo.getRiskcode());
if("交强".equals(excelVo.getRiskcode())){
//交强费用比例=总费用比例-五级机构管理费比例-三级分销比例
insCompanyFee.setFeerate(excelVo.getCommissionFeerate().add(excelVo.getOtherFeerate()).subtract(dept_rate).subtract(retail_rate).doubleValue());
insCompanyFee.setFeeamount(excelVo.getNoTaxPremium().multiply(new BigDecimal(insCompanyFee.getFeerate())).divide(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
insCompanyFee.setJqpremium(excelVo.getNoTaxPremium().doubleValue());
//保存业务员费用
insCompanyFeeService.insert(insCompanyFee);
}
if("商业".equals(excelVo.getRiskcode())){
//商业费用比例=总费用比例-五级机构管理费比例-三级分销比例
insCompanyFee.setDisrate(excelVo.getCommissionFeerate().add(excelVo.getOtherFeerate()).subtract(dept_rate).subtract(retail_rate).doubleValue());
insCompanyFee.setDisamount(excelVo.getNoTaxPremium().multiply(new BigDecimal(insCompanyFee.getDisrate())).divide(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
insCompanyFee.setSypremium(excelVo.getNoTaxPremium().doubleValue());
//保存业务员费用
insCompanyFeeService.insert(insCompanyFee);
}
}
private void isNullCmain(int i, InsPlyMain excelVo) {
if (StringUtils.isNullOrEmpty(excelVo.getOrderno())
||StringUtils.isNullOrEmpty(excelVo.getPolicyno())
||StringUtils.isNullOrEmpty(excelVo.getCompanyId())
||StringUtils.isNullOrEmpty(excelVo.getDeptId())
||StringUtils.isNullOrEmpty(excelVo.getUserId())
||StringUtils.isNullOrEmpty(excelVo.getRiskcode())
||StringUtils.isNullOrEmpty(excelVo.getLicenseno())
||StringUtils.isNullOrEmpty(excelVo.getPayDate())
||StringUtils.isNullOrEmpty(excelVo.getPremium())
||StringUtils.isNullOrEmpty(excelVo.getTaxamount())
||StringUtils.isNullOrEmpty(excelVo.getNoTaxPremium())
||StringUtils.isNullOrEmpty(excelVo.getCommissionFeerate())
||StringUtils.isNullOrEmpty(excelVo.getCommissionFeevalue())
||StringUtils.isNullOrEmpty(excelVo.getOtherFeerate())
||StringUtils.isNullOrEmpty(excelVo.getOtherFeevalue())
||StringUtils.isNullOrEmpty(excelVo.getFirstDeptProportion())
||StringUtils.isNullOrEmpty(excelVo.getFirstDeptPremiums())
||StringUtils.isNullOrEmpty(excelVo.getSecondDeptProportion())
||StringUtils.isNullOrEmpty(excelVo.getSecondDeptPremiums())
||StringUtils.isNullOrEmpty(excelVo.getThirdDeptProportion())
||StringUtils.isNullOrEmpty(excelVo.getThirdDeptPremiums())
||StringUtils.isNullOrEmpty(excelVo.getForthDeptProportion())
||StringUtils.isNullOrEmpty(excelVo.getForthDeptPremiums())
||StringUtils.isNullOrEmpty(excelVo.getFifthDeptProportion())
||StringUtils.isNullOrEmpty(excelVo.getFifthDeptPremiums())
||StringUtils.isNullOrEmpty(excelVo.getFirstDetailProportion())
||StringUtils.isNullOrEmpty(excelVo.getFirstDetailPremiums())
||StringUtils.isNullOrEmpty(excelVo.getSecondDetailProportion())
||StringUtils.isNullOrEmpty(excelVo.getSecondDetailPremiums())
||StringUtils.isNullOrEmpty(excelVo.getThirdDetailProportion())
||StringUtils.isNullOrEmpty(excelVo.getThirdDetailPremiums())
||StringUtils.isNullOrEmpty(excelVo.getUserFeeProportion())
||StringUtils.isNullOrEmpty(excelVo.getUserFeelPremiums())
) {
throw new DescribeException("第" + (i + 1) + "行 必填字段 不可为空");
}
}
private void checkData(int i, InsPlyMain excelVo) {
EsmInsCompany esmInsCompany =esmInsCompanyMapper.selectById(excelVo.getCompanyId());
if(StringUtils.isNullOrEmpty(esmInsCompany)){
throw new DescribeException("第" + (i + 1) + "行 保司编码错误,请核对!");
}
SysDept sysDept=sysDeptMapper.selectById(excelVo.getDeptId());
if(StringUtils.isNullOrEmpty(sysDept)){
throw new DescribeException("第" + (i + 1) + "行 机构编码错误,请核对!");
}
SysUser sysUser=sysUserMapper.selectById(excelVo.getUserId());
if(StringUtils.isNullOrEmpty(sysUser)){
throw new DescribeException("第" + (i + 1) + "行 人员编码错误,请核对!");
}
if(!"交强".equals(excelVo.getRiskcode())&&!"商业".equals(excelVo.getRiskcode())){
throw new DescribeException("第" + (i + 1) + "行 险种错误,只能填写 【交强】、【商业】!");
}
}
}