package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yh.saas.plugin.yiliangyiyun.entity.ReveExpeInfo;
import com.yh.saas.plugin.yiliangyiyun.mapper.ReveExpeInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IReveExpeInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收支明细 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2022-03-03
 */
@Service
public class ReveExpeInfoServiceImpl extends ServiceImpl<ReveExpeInfoMapper, ReveExpeInfo> implements IReveExpeInfoService {
    /**
     * 收支明细列表
     *
     * @param reveExpeInfo
     * @return
     */
    @Override
    public Page<ReveExpeInfo> selectReveExpe(ReveExpeInfo reveExpeInfo) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (reveExpeInfo.getCurrentPage() - 1)
                * reveExpeInfo.getPageSize());
        //公司id
        pageView.put("compId", reveExpeInfo.getCompId());
        pageView.put("searchKeyWord", reveExpeInfo.getSearchKeyWord());
        pageView.put("searchType", reveExpeInfo.getSearchType());
        pageView.put("startDate", reveExpeInfo.getStartDate());
        pageView.put("endDate", reveExpeInfo.getEndDate());
        pageView.put("pageSize", reveExpeInfo.getPageSize());
        pageView.put("currentPage", reveExpeInfo.getCurrentPage());
        // 查询列表总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<ReveExpeInfo> dataList = baseMapper.getListByCondition(pageView);

        if(!CollectionUtils.isEmpty(dataList)){
            for(ReveExpeInfo reveExpeInfo1 : dataList){
                //定义合同收入
                Double inContractMoney = 0.0;
                //查询
                List<ReveExpeInfo> inContractReveExpeInfoList = this.selectList(new EntityWrapper<ReveExpeInfo>()
                        .eq("comp_id",reveExpeInfo1.getCompId())
                        .eq("reex_type_key","1")
                        .eq("delete_flag","0"));
                if(inContractReveExpeInfoList != null){
                    for(int i = 0;i<inContractReveExpeInfoList.size();i++){
                        inContractMoney = inContractMoney + inContractReveExpeInfoList.get(i).getAmountMoney();
                    }
                        reveExpeInfo1.setInContractMoney(inContractMoney);
                }
                //定义合同支出
                Double outContractMoney = 0.0;
                //查询
                List<ReveExpeInfo> outContractReveExpeInfoList = this.selectList(new EntityWrapper<ReveExpeInfo>()
                        .eq("comp_id",reveExpeInfo1.getCompId())
                        .eq("reex_type_key","2")
                        .eq("delete_flag","0"));
                if(outContractReveExpeInfoList != null){
                    for(int i = 0;i<outContractReveExpeInfoList.size();i++){
                        outContractMoney = outContractMoney + outContractReveExpeInfoList.get(i).getAmountMoney();
                    }
                        reveExpeInfo1.setOutContractMoney(outContractMoney);
                }
                //定义仓库收入
                Double inWarehouseMoney = 0.0;
                //查询
                List<ReveExpeInfo> inWarehouseReveExpeInfoList = this.selectList(new EntityWrapper<ReveExpeInfo>()
                        .eq("comp_id",reveExpeInfo1.getCompId())
                        .eq("reex_type_key","3")
                        .eq("delete_flag","0"));
                if(inWarehouseReveExpeInfoList != null){
                    for(int i = 0;i<inWarehouseReveExpeInfoList.size();i++){
                        inWarehouseMoney = inWarehouseMoney + inWarehouseReveExpeInfoList.get(i).getAmountMoney();
                    }
                        reveExpeInfo1.setInWarehouseMoney(inWarehouseMoney);
                }
                //定义仓库支出
                Double outWarehouseMoney = 0.0;
                //查询
                List<ReveExpeInfo> outWarehouseReveExpeInfoList = this.selectList(new EntityWrapper<ReveExpeInfo>()
                        .eq("comp_id",reveExpeInfo1.getCompId())
                        .eq("reex_type_key","4")
                        .eq("delete_flag","0"));
                if(outWarehouseReveExpeInfoList != null){
                    for(int i = 0;i<outWarehouseReveExpeInfoList.size();i++){
                        outWarehouseMoney = outWarehouseMoney + outWarehouseReveExpeInfoList.get(i).getAmountMoney();
                    }
                        reveExpeInfo1.setOutWarehouseMoney(outWarehouseMoney);
                }
                //定义经营收入
                Double inManageMoney = 0.0;
                //查询
                List<ReveExpeInfo> inManageReveExpeInfoList = this.selectList(new EntityWrapper<ReveExpeInfo>()
                        .eq("comp_id",reveExpeInfo1.getCompId())
                        .eq("reex_type_key","5")
                        .eq("delete_flag","0"));
                if(inManageReveExpeInfoList != null){
                    for(int i = 0;i<inManageReveExpeInfoList.size();i++){
                        inManageMoney = inManageMoney + inManageReveExpeInfoList.get(i).getAmountMoney();
                    }
                        reveExpeInfo1.setInManageMoney(inManageMoney);
                }
                //定义经营支出
                Double outManageMoney = 0.0;
                //查询
                List<ReveExpeInfo> outManageReveExpeInfoList = this.selectList(new EntityWrapper<ReveExpeInfo>()
                        .eq("comp_id",reveExpeInfo1.getCompId())
                        .eq("reex_type_key","6")
                        .eq("delete_flag","0"));
                if(outManageReveExpeInfoList != null){
                    for(int i = 0;i<outManageReveExpeInfoList.size();i++){
                        outManageMoney = outManageMoney + outManageReveExpeInfoList.get(i).getAmountMoney();
                    }
                        reveExpeInfo1.setOutManageMoney(outManageMoney);
                }
            }
        }
        Page<ReveExpeInfo> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(reveExpeInfo.getCurrentPage());
        page.setSize(reveExpeInfo.getPageSize());
        return page;
    }
}
