package com.ydtech.modules.fnc.service.impl;
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.core.page.HttpResult;
import com.ydtech.exception.SystemException;
import com.ydtech.modules.admin.model.SysUser;
import com.ydtech.modules.esm.model.EsmInsCompany;
import com.ydtech.modules.esm.service.EsmInsCompanyService;
import com.ydtech.modules.fnc.dao.InsPlyIncomeMapper;
import com.ydtech.modules.fnc.dao.InsPlySettleDetailMapper;
import com.ydtech.modules.fnc.dao.InsPlySettleHxMapper;
import com.ydtech.modules.fnc.entity.InsPlyIncome;
import com.ydtech.modules.fnc.entity.InsPlySettle;
import com.ydtech.modules.fnc.dao.InsPlySettleMapper;
import com.ydtech.modules.fnc.entity.InsPlySettleDetail;
import com.ydtech.modules.fnc.entity.InsPlySettleHx;
import com.ydtech.modules.fnc.service.InsPlySettleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydtech.modules.fnc.vo.GenerateSettleVo;
import com.ydtech.modules.fnc.vo.InsPlyIncomeVo;
import com.ydtech.modules.fnc.vo.InsPlySettleVo;
import com.ydtech.modules.inv.utils.EasyExcelUtils;
import com.ydtech.modules.order.entity.BasePageResult;
import com.ydtech.modules.order.entity.InsOrders;
import com.ydtech.modules.order.entity.vo.PolicyInsuranceReportReqVo;
import com.ydtech.modules.order.entity.vo.PolicyInsuranceReportResVo;
import com.ydtech.modules.report.model.vo.InsPlySettleReportReqVo;
import com.ydtech.modules.report.model.vo.InsPlySettleReportResVo;
import com.ydtech.modules.report.model.vo.InsPlySettleStatisticsListResVo;
import com.ydtech.utils.DateUtil;
import com.ydtech.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.sql.Wrapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
*
* 月度结算单汇总表 服务实现类
*
*
* @author gws
* @since 2024-01-10
*/
@Service
public class InsPlySettleServiceImpl extends ServiceImpl implements InsPlySettleService {
@Value("${upload.file.path}")
private String uploadFilePath;
@Autowired
private InsPlySettleDetailMapper insPlySettleDetailMapper;
@Autowired
private InsPlyIncomeMapper insPlyIncomeMapper;
@Autowired
private InsPlySettleHxMapper insPlySettleHxMapper;
@Autowired
private EsmInsCompanyService esmInsCompanyService;
@Override
@Transactional
public HttpResult generateSettleAndDetail(GenerateSettleVo generateSettleVo) {
// 保单号数组
List plyNoList= generateSettleVo.getPlyNoList();
// 保险公司
String companyId=generateSettleVo.getCompanyId();
String deptId =generateSettleVo.getDeptId();
// 结算月份
String settleMonth=generateSettleVo.getSettleMonth();
if(StringUtils.isNullOrEmpty(companyId)
||StringUtils.isNullOrEmpty(deptId)
||StringUtils.isNullOrEmpty(settleMonth)){
return HttpResult.error("保险公司、机构、结算月份不能为空");
}
String settleno = "";
BigDecimal commissionFeevalue=new BigDecimal(0);
BigDecimal otherFeevalue=new BigDecimal(0);
BigDecimal allFeeValue=new BigDecimal(0);
if(plyNoList==null || plyNoList.size()==0){
return HttpResult.error("结算清单不能传空");
}
// 生成结算单号
// InsPlyIncome insPlyIncome_t=insPlyIncomeMapper.selectByPlyno(plyNoList.get(0));
settleno = companyId+ DateUtil.yyyyMMddHHmmss(new Date());
for(String plyNo:plyNoList){
InsPlyIncome insPlyIncome=insPlyIncomeMapper.selectByPlyno(plyNo);
InsPlySettleDetail insPlySettleDetail =new InsPlySettleDetail();
insPlySettleDetail.setSettleno(settleno);
// 订单号
insPlySettleDetail.setOrderno(insPlyIncome.getOrderno());
// 保单号
insPlySettleDetail.setPolicyno(insPlyIncome.getPolicyno());
insPlySettleDetail.setDeptId(insPlyIncome.getDeptId());
// 手续费
insPlySettleDetail.setCommissionFeevalue(insPlyIncome.getCommissionFeevalue());
// 其他费用金额
insPlySettleDetail.setOtherFeevalue(insPlyIncome.getOtherFeevalue());
// 总费用
insPlySettleDetail.setAllFeeValue(insPlyIncome.getAllFeeValue());
insPlySettleDetail.setCreateTime(LocalDateTime.now());
insPlySettleDetailMapper.insert(insPlySettleDetail);
//修改ins_ply_income 将总费用转成计算费用
insPlyIncome.setFinalFeeValue(insPlyIncome.getAllFeeValue());
LambdaQueryWrapper queryWrapper=new LambdaQueryWrapper();
queryWrapper.eq(InsPlyIncome::getPolicyno,plyNo);
insPlyIncomeMapper.update(insPlyIncome,queryWrapper);
// 手续费金额
commissionFeevalue=commissionFeevalue.add(insPlyIncome.getCommissionFeevalue());
// 其他费用金额
otherFeevalue=otherFeevalue.add(insPlyIncome.getOtherFeevalue());
// 总费用
allFeeValue=allFeeValue.add(insPlyIncome.getAllFeeValue());
}
// 月度结算单汇总表
InsPlySettle insPlySettle =new InsPlySettle();
insPlySettle.setSettleno(settleno);
insPlySettle.setCompanyId(companyId);
insPlySettle.setDeptId(deptId);
insPlySettle.setSettleMonth(settleMonth);
String settleBatch =this.baseMapper.getMaxBatch(insPlySettle);
if(StringUtils.isNullOrEmpty(settleBatch)){
settleBatch="1";
}else{
settleBatch=String.valueOf(Integer.parseInt(settleBatch)+1);
}
insPlySettle.setSettleBatch(settleBatch);
insPlySettle.setCommissionFeevalue(commissionFeevalue);
insPlySettle.setOtherFeevalue(otherFeevalue);
insPlySettle.setAllFeeValue(allFeeValue);
insPlySettle.setOperator(StpUtil.getLoginId().toString());
insPlySettle.setCreateTime(LocalDateTime.now());
insPlySettle.setStatus("0");
this.baseMapper.insert(insPlySettle);
return HttpResult.ok(settleno);
}
@Override
public HttpResult> selectSettle(InsPlySettleVo insPlySettleVo) {
try{
Page page = new Page<>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize());
PageInfo> pageHelper = null;
PageHelper.startPage(insPlySettleVo.getPageNum(),insPlySettleVo.getPageSize());
List list= this.baseMapper.selectByCondition(insPlySettleVo);
pageHelper=new PageInfo(list);
BasePageResult pageResult=new BasePageResult(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
return HttpResult.ok(pageResult);
}catch (Exception e){
e.printStackTrace();
return HttpResult.error("查询异常");
}
}
@Override
public HttpResult> selectSettleDetail(InsPlySettleVo insPlySettleVo) {
try{
Page page = new Page<>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize());
PageInfo> pageHelper = null;
PageHelper.startPage(insPlySettleVo.getPageNum(),insPlySettleVo.getPageSize());
List list= insPlyIncomeMapper.selectSettleByCondition(insPlySettleVo);
pageHelper=new PageInfo(list);
BasePageResult pageResult=new BasePageResult(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
return HttpResult.ok(pageResult);
}catch (Exception e){
e.printStackTrace();
return HttpResult.error("查询异常");
}
}
@Override
public HttpResult hxSettle(InsPlySettleHx insPlySettleHx) {
//必填校验
if(StringUtils.isNullOrEmpty(insPlySettleHx.getSettleno())
||StringUtils.isNullOrEmpty(insPlySettleHx.getFeeType())
||StringUtils.isNullOrEmpty(insPlySettleHx.getCheckno())
||StringUtils.isNullOrEmpty(insPlySettleHx.getAmount())){
return HttpResult.error("结算单号、费用类型、核销金额、发票号不能为空");
}
//核销金额不能超总费用
InsPlySettle insPlySettle=this.baseMapper.selectBySettleno(insPlySettleHx.getSettleno());
insPlySettle.setHxamount(insPlySettle.getHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getHxamount());
insPlySettle.setCommissionHxamount(insPlySettle.getCommissionHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getCommissionHxamount());
insPlySettle.setOtherHxamount(insPlySettle.getOtherHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getOtherHxamount());
// 如果等于手续费时 核销金额 比 手续费 >0
if(insPlySettleHx.getFeeType().equals("S")
&&insPlySettle.getCommissionHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getCommissionFeevalue())>0){
return HttpResult.error("核销手续费金额已超过结算单手续费,请修改");
}
// 其他费用 核销金额 比 其他费用
if(insPlySettleHx.getFeeType().equals("O")
&&insPlySettle.getOtherHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getOtherFeevalue())>0){
return HttpResult.error("核销其他费用金额已超过结算单其他费用,请修改");
}
//1.插入核销记录表
insPlySettleHx.setOperator(StpUtil.getLoginId().toString());
insPlySettleHx.setCreateTime(LocalDateTime.now());
insPlySettleHxMapper.insert(insPlySettleHx);
//2.修改核销状态 状态(0-未核销 1-部分核销 2-已核销)
InsPlySettle insPlySettle_status=new InsPlySettle();
if(insPlySettle.getHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getAllFeeValue())==0){
insPlySettle_status.setStatus("2");
}else{
insPlySettle_status.setStatus("1");
}
this.baseMapper.update(insPlySettle_status,new QueryWrapper().eq("settleno",insPlySettleHx.getSettleno()));
return HttpResult.ok("核销成功");
}
@Override
public HttpResult queryHxDetail(String settleno) {
LambdaQueryWrapper queryWrapper=new LambdaQueryWrapper();
queryWrapper.eq(InsPlySettleHx::getSettleno,settleno);
List list=insPlySettleHxMapper.selectList(queryWrapper);
return HttpResult.ok(list);
}
@Override
public BasePageResult getInsPlySettleReport( InsPlySettleReportReqVo insPlySettleReportReqVo) {
Page page = new Page<>(insPlySettleReportReqVo.getPageNum(), insPlySettleReportReqVo.getPageSize());
page.setSearchCount(false).setOptimizeCountSql(false);//关闭count查询
if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsReturnBack())&&"1".equals(insPlySettleReportReqVo.getIsReturnBack())){
//如果是返回上级时,找上级的上级往下找
LambdaQueryWrapper lqw1 = new LambdaQueryWrapper<>();
lqw1.eq(EsmInsCompany::getId, insPlySettleReportReqVo.getCompanyId());
EsmInsCompany esmInsCompany1=esmInsCompanyService.getOne(lqw1);
if(esmInsCompany1==null){
throw new SystemException("已经是最顶层机构,无法继续返回!");
}
LambdaQueryWrapper lqw2 = new LambdaQueryWrapper<>();
lqw2.eq(EsmInsCompany::getId, esmInsCompany1.getParentId());
EsmInsCompany esmInsCompany2=esmInsCompanyService.getOne(lqw2);
if(esmInsCompany2==null){
insPlySettleReportReqVo.setCompanyId(esmInsCompany1.getParentId());
}else{
insPlySettleReportReqVo.setCompanyId(esmInsCompany2.getParentId());
}
}
//正常查询,下钻查询时调整
List companyArr=esmInsCompanyService.getInsCompanyNextLevel(insPlySettleReportReqVo.getCompanyId());
if(!(companyArr!=null&&companyArr.size()>0)){
throw new SystemException("已经是最底层机构,无法继续下钻!");
}
insPlySettleReportReqVo.setGroupInsCompanyArr(companyArr);
Page policyInsuranceReportResVoPage = baseMapper.getInsPlySettleReport(page,
insPlySettleReportReqVo);
if(policyInsuranceReportResVoPage.getRecords().size()==0){
if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsDrilling())&&"1".equals(insPlySettleReportReqVo.getIsDrilling())){
throw new SystemException("已经是最底层机构,无法继续下钻!");
}
}
policyInsuranceReportResVoPage.getRecords().stream().forEach(item->{
EsmInsCompany esmInsCompany =esmInsCompanyService.getById(item.getCompanyId());
item.setCompanyName(esmInsCompany.getName());
if(StringUtils.isEmpty(item.getStatus())){
item.setStatus("全部");
}else{
if("0".equals(item.getStatus())){
item.setStatus("未核销");
}else if("1".equals(item.getStatus())){
item.setStatus("部分核销");
}else if ("2".equals(item.getStatus())){
item.setStatus("已核销");
}
}
});
Long total = baseMapper.getInsPlySettleReportCount(insPlySettleReportReqVo);
policyInsuranceReportResVoPage.setTotal(total);
return new BasePageResult<>(insPlySettleReportReqVo.getPageNum(), insPlySettleReportReqVo.getPageSize(),
policyInsuranceReportResVoPage.getTotal(), policyInsuranceReportResVoPage.getPages()
, policyInsuranceReportResVoPage.getRecords());
}
@Override
public HttpResult exportInsPlySettleReport(InsPlySettleReportReqVo insPlySettleReportReqVo) {
if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsReturnBack())&&"1".equals(insPlySettleReportReqVo.getIsReturnBack())){
//如果是返回上级时,找上级的上级往下找
LambdaQueryWrapper lqw1 = new LambdaQueryWrapper<>();
lqw1.eq(EsmInsCompany::getId, insPlySettleReportReqVo.getCompanyId());
EsmInsCompany esmInsCompany1=esmInsCompanyService.getOne(lqw1);
if(esmInsCompany1==null){
throw new SystemException("已经是最顶层机构,无法继续返回!");
}
LambdaQueryWrapper lqw2 = new LambdaQueryWrapper<>();
lqw2.eq(EsmInsCompany::getId, esmInsCompany1.getParentId());
EsmInsCompany esmInsCompany2=esmInsCompanyService.getOne(lqw2);
if(esmInsCompany2==null){
insPlySettleReportReqVo.setCompanyId(esmInsCompany1.getParentId());
}else{
insPlySettleReportReqVo.setCompanyId(esmInsCompany2.getParentId());
}
}
//正常查询,下钻查询时调整
List companyArr=esmInsCompanyService.getInsCompanyNextLevel(insPlySettleReportReqVo.getCompanyId());
if(!(companyArr!=null&&companyArr.size()>0)){
throw new SystemException("已经是最底层机构,无法继续下钻!");
}
insPlySettleReportReqVo.setGroupInsCompanyArr(companyArr);
List insPlySettleReportResVoList = baseMapper.getInsPlySettleReport(insPlySettleReportReqVo);
if(insPlySettleReportResVoList.size()==0){
if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsDrilling())&&"1".equals(insPlySettleReportReqVo.getIsDrilling())){
throw new SystemException("已经是最底层机构,无法继续下钻!");
}
}
insPlySettleReportResVoList.stream().forEach(item->{
EsmInsCompany esmInsCompany =esmInsCompanyService.getById(item.getCompanyId());
item.setCompanyName(esmInsCompany.getName());
if(StringUtils.isEmpty(item.getStatus())){
item.setStatus("全部");
}else{
if("0".equals(item.getStatus())){
item.setStatus("未核销");
}else if("1".equals(item.getStatus())){
item.setStatus("部分核销");
}else if ("2".equals(item.getStatus())){
item.setStatus("已核销");
}
}
});
String s = EasyExcelUtils.downExcel(uploadFilePath, InsPlySettleReportResVo.class, insPlySettleReportResVoList);
return HttpResult.ok("", s);
}
@Override
public HttpResult exportInsPlySettleReportList(InsPlySettleReportReqVo insPlySettleReportReqVo) {
List insPlySettleReportList = baseMapper.getInsPlySettleReportList(insPlySettleReportReqVo);
insPlySettleReportList.stream().forEach(item->{
if("0".equals(item.getStatus())){
item.setStatus("未核销");
}else if("1".equals(item.getStatus())){
item.setStatus("部分核销");
}else if ("2".equals(item.getStatus())){
item.setStatus("已核销");
}
});
String s = EasyExcelUtils.downExcel(uploadFilePath, InsPlySettleStatisticsListResVo.class, insPlySettleReportList);
return HttpResult.ok("", s);
}
}