package com.ydtech.modules.fnc.service.impl;
import cn.dev33.satoken.stp.StpUtil;
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.modules.fnc.dao.InsPlyIncomeMapper;
import com.ydtech.modules.fnc.dao.InsPlySettleDetailMapper;
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.InsPlyIncomeVo;
import com.ydtech.modules.fnc.vo.InsPlySettleVo;
import com.ydtech.modules.order.entity.BasePageResult;
import com.ydtech.modules.order.entity.InsOrders;
import com.ydtech.utils.DateUtil;
import com.ydtech.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.Date;
import java.util.List;
/**
*
* 月度结算单汇总表 服务实现类
*
*
* @author gws
* @since 2024-01-10
*/
@Service
public class InsPlySettleServiceImpl extends ServiceImpl implements InsPlySettleService {
@Autowired
private InsPlySettleDetailMapper insPlySettleDetailMapper;
@Autowired
private InsPlyIncomeMapper insPlyIncomeMapper;
@Override
@Transactional
public HttpResult generateSettleAndDetail(List plyList) {
String settleno = "";
BigDecimal commissionFeevalue=new BigDecimal(0);
BigDecimal otherFeevalue=new BigDecimal(0);
BigDecimal allFeeValue=new BigDecimal(0);
// 生成结算单号
settleno = plyList.get(0).getCompanyId()+ DateUtil.yyyyMMddHHmmss(new Date());
for(InsPlyIncome insPlyIncome:plyList){
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.setDoingFeeValue(insPlyIncome.getAllFeeValue());
insPlyIncomeMapper.updateById(insPlyIncome);
commissionFeevalue=commissionFeevalue.add(insPlyIncome.getCommissionFeevalue());
otherFeevalue=otherFeevalue.add(insPlyIncome.getOtherFeevalue());
allFeeValue=allFeeValue.add(insPlyIncome.getAllFeeValue());
}
InsPlySettle insPlySettle =new InsPlySettle();
insPlySettle.setSettleno(settleno);
insPlySettle.setCompanyId(plyList.get(0).getCompanyId());
insPlySettle.setDeptId(plyList.get(0).getDeptId());
insPlySettle.setSettleMonth(YearMonth.from(plyList.get(0).getPayDate()).toString());
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 enterSettle(InsPlySettleHx insPlySettle) {
return null;
}
}