package com.xjrsoft.module.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.student.dto.PbVXssfdetailExcelDto;
import com.xjrsoft.module.student.dto.PbVXssfdetailPageDto;
import com.xjrsoft.module.student.dto.PbVXsxxsfytbPageDto;
import com.xjrsoft.module.student.dto.PersonalInfoDto;
import com.xjrsoft.module.student.entity.PbVXssfdetail;
import com.xjrsoft.module.student.entity.PbVXsxxsfytb;
import com.xjrsoft.module.student.mapper.PbVXssfdetailMapper;
import com.xjrsoft.module.student.service.IPbVXssfdetailService;
import com.xjrsoft.module.student.service.IPbVXsxxsfytbService;
import com.xjrsoft.module.student.service.IStudentManagerService;
import com.xjrsoft.module.student.vo.PbVXssfdetailExcelVo;
import com.xjrsoft.module.student.vo.PbVXssfdetailPageVo;
import com.xjrsoft.module.student.vo.PersonalPortraitPersonalInfoVo;
import com.xjrsoft.module.student.vo.StudentPersonalInfoVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
* @title: 
* @Author dzx
* @Date: 2024-03-13
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class PbVXssfdetailServiceImpl extends MPJBaseServiceImpl<PbVXssfdetailMapper, PbVXssfdetail> implements IPbVXssfdetailService {

    private final PbVXssfdetailMapper pbVXssfdetailMapper;

    private final IStudentManagerService studentManagerService;

    private final IPbVXsxxsfytbService pbVXsxxsfytbService;

    @Override
    public Page<PbVXssfdetailPageVo> getPage(Page<PbVXssfdetailPageVo> page, PbVXssfdetailPageDto dto) {
        if(dto.getClassIds() != null && !dto.getClassIds().equals("")){
            String[] strArray = dto.getClassIds().split(",");
            List<String> strList = Arrays.asList(strArray);
            dto.setClassIdList(strList);
        }

        Page<PbVXssfdetailPageVo> voPage = pbVXssfdetailMapper.getPage(page, dto);
        for (PbVXssfdetailPageVo record : voPage.getRecords()) {
            if(record.getMny() > 0){
                record.setCategory("缴费");
            }else if(record.getMny() < 0){
                record.setCategory("退费");
            }
        }
        return voPage;
    }

    @Override
    public List<PbVXssfdetailExcelVo> getList(PbVXssfdetailExcelDto dto) {
        return pbVXssfdetailMapper.getList(dto);
    }

    @Override
    public PersonalPortraitPersonalInfoVo getPersonalInfo(PersonalInfoDto dto) {
        //获取学生基本信息
        PersonalPortraitPersonalInfoVo personalPortraitPersonalInfoVo = (PersonalPortraitPersonalInfoVo) studentManagerService.getPersonalInfo(dto.getUserId());

        //TODO 获取学生一卡通余额
        personalPortraitPersonalInfoVo.setTUnion(new BigDecimal("0"));

        //获取学生结余
        getCashSurplus(personalPortraitPersonalInfoVo);

        return personalPortraitPersonalInfoVo;
    }

    private void getCashSurplus(PersonalPortraitPersonalInfoVo personalPortraitPersonalInfoVo){
        //获取所有收费信息
        LambdaQueryWrapper<PbVXsxxsfytb> pbVXsxxsfytbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pbVXsxxsfytbLambdaQueryWrapper
                .eq(PbVXsxxsfytb::getPersonalid, personalPortraitPersonalInfoVo.getCredentialNumber());
        List<PbVXsxxsfytb> pbVXsxxsfytbList = pbVXsxxsfytbService.list(pbVXsxxsfytbLambdaQueryWrapper);

        BigDecimal cashSurplus = new BigDecimal("0");
        for (PbVXsxxsfytb pbVXsxxsfytb : pbVXsxxsfytbList){
            cashSurplus = cashSurplus.add(pbVXsxxsfytb.getQfje());
        }

        personalPortraitPersonalInfoVo.setCashSurplus(cashSurplus);
    }
}
