package com.jeeplus.test.reimbursementProject.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementBusinessTwoDTO;
import com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementDTO;
import com.jeeplus.test.reimbursementProject.mapper.ProjectReimbursementBusinessTwoMapper;
import com.jeeplus.test.reimbursementProject.service.ProjectReimbursementBusinessService;
import com.jeeplus.test.reimbursementProject.service.ProjectReimbursementBusinessTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional(readOnly = true)
public class ProjectReimbursementBusinessTwoServiceImpl implements ProjectReimbursementBusinessTwoService {

    @Autowired
    private ProjectReimbursementBusinessTwoMapper ProjectReimbursementBusinessTwoMapper;
    @Autowired
    private ProjectReimbursementBusinessService reimbursementBusinessService;


    @Override
    public IPage<ProjectReimbursementBusinessTwoDTO> findListPage(Page<ProjectReimbursementBusinessTwoDTO> page, ProjectReimbursementBusinessTwoDTO ProjectReimbursementBusiness) {
        return ProjectReimbursementBusinessTwoMapper.findListPage(page,ProjectReimbursementBusiness);
    }

    @Transactional(readOnly = false)
    @Override
    public void delPhysicsById(String id) {
        ProjectReimbursementBusinessTwoMapper.delPhysicsById(id);
    }

    @Transactional(readOnly = false)
    @Override
    public void deleteByList(List <String> idList) {//删除发票信息
        if(idList.size()>0){
            ProjectReimbursementBusinessTwoMapper.deleteByList(idList);
        }
    }

    @Transactional(readOnly = false)
    @Override
    public void addData(ProjectReimbursementBusinessTwoDTO ProjectReimbursementBusinessTwoDTO) {
        ProjectReimbursementBusinessTwoDTO.preInsert();
        ProjectReimbursementBusinessTwoMapper.insertProjectReimbursementBusiness(ProjectReimbursementBusinessTwoDTO);
    }

    @Override
    public List<ProjectReimbursementDTO> findSelectList() {
        return ProjectReimbursementBusinessTwoMapper.findSelectList();
    }

    @Transactional(readOnly = false)
    @Override
    public void modifyBindCode(String ids,String businessCode) {
        ProjectReimbursementBusinessTwoMapper.modifyBindCode(ids,businessCode);
    }

    @Override
    public List<String> findSelectYearList() {
        return ProjectReimbursementBusinessTwoMapper.findSelectYearList();
    }

    @Override
    public List<ProjectReimbursementDTO> findSelectCodeListByYear(String year) {
        List<ProjectReimbursementDTO> selectCodeListByYear = ProjectReimbursementBusinessTwoMapper.findSelectCodeListByYear(year);
        selectCodeListByYear.forEach(s->{
            ProjectReimbursementDTO businessById = reimbursementBusinessService.getBusinessById(s.getId());
            if(null != businessById){
                s.setAllAlreadyReimbursementQuota(businessById.getAllAlreadyReimbursementQuota());
                s.setCanReimbursementAmount(businessById.getCanReimbursementAmount());
                s.setSurplusReimbursementAmount(businessById.getSurplusReimbursementAmount());
            }
        });
        return selectCodeListByYear;
    }

    @Override
    public ProjectReimbursementBusinessTwoDTO findDataById(String id) {
        return ProjectReimbursementBusinessTwoMapper.findDataById(id);
    }
}
