package com.jeeplus.test.reimbursementsys.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.reimbursementsys.domain.dto.ReimbursementBusinessTwoDTO;
import com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementDTO;
import com.jeeplus.test.reimbursementsys.mapper.ReimbursementBusinessTwoMapper;
import com.jeeplus.test.reimbursementsys.service.ReimbursementBusinessService;
import com.jeeplus.test.reimbursementsys.service.ReimbursementBusinessTwoService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class ReimbursementBusinessTwoServiceImpl implements ReimbursementBusinessTwoService {

    @Autowired
    private ReimbursementBusinessTwoMapper reimbursementBusinessTwoMapper;
    @Autowired
    private ReimbursementBusinessService reimbursementBusinessService;
    @Autowired
    private WuHanReimbursementBusinessServiceImpl wuHanReimbursementBusinessService;


    @Override
    public IPage<ReimbursementBusinessTwoDTO> findListPage(Page<ReimbursementBusinessTwoDTO> page, ReimbursementBusinessTwoDTO reimbursementBusiness) {
        return reimbursementBusinessTwoMapper.findListPage(page,reimbursementBusiness);
    }

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

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

    @Transactional(readOnly = false)
    @Override
    public void addData(ReimbursementBusinessTwoDTO reimbursementBusinessTwoDTO) {
        reimbursementBusinessTwoDTO.preInsert();
        reimbursementBusinessTwoMapper.insertReimbursementBusiness(reimbursementBusinessTwoDTO);
    }

    @Override
    public List<ReimbursementDTO> findSelectList() {
        return reimbursementBusinessTwoMapper.findSelectList();
    }

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

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

    @Override
    public List<ReimbursementDTO> findSelectCodeListByYear(String year, String reimbursementAddress) {
        List<ReimbursementDTO> selectCodeListByYear = Lists.newArrayList();
        if(StringUtils.isBlank(year) || StringUtils.isBlank(reimbursementAddress)){
            return selectCodeListByYear;
        }
        switch (reimbursementAddress){
            case "0":
                selectCodeListByYear = reimbursementBusinessTwoMapper.findSelectCodeListByYear(year);
                selectCodeListByYear.forEach(s->{
                    ReimbursementDTO businessById = reimbursementBusinessService.getBusinessById(s.getId());
                    if(null != businessById){
                        s.setAllAlreadyReimbursementQuota(businessById.getAllAlreadyReimbursementQuota());
                        s.setCanReimbursementAmount(businessById.getCanReimbursementAmount());
                        s.setSurplusReimbursementAmount(businessById.getSurplusReimbursementAmount());
                    }
                });
                break;
            case "1":
                selectCodeListByYear = reimbursementBusinessTwoMapper.findSelectCodeListByYearOnWuHan(year);
                selectCodeListByYear.forEach(s->{
                    ReimbursementDTO businessById = wuHanReimbursementBusinessService.getBusinessById(s.getId());
                    if(null != businessById){
                        s.setAllAlreadyReimbursementQuota(businessById.getAllAlreadyReimbursementQuota());
                        s.setCanReimbursementAmount(businessById.getCanReimbursementAmount());
                        s.setSurplusReimbursementAmount(businessById.getSurplusReimbursementAmount());
                    }
                });
                break;
        }
        return selectCodeListByYear;
    }

    @Override
    public ReimbursementBusinessTwoDTO findDataById(String id) {
        return reimbursementBusinessTwoMapper.findDataById(id);
    }
}
