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.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.plugin.yiliangyiyun.entity.ContractInventoryDistribution;
import com.yh.saas.plugin.yiliangyiyun.entity.WarehousingOrder;
import com.yh.saas.plugin.yiliangyiyun.mapper.ContractInventoryDistributionMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IContractGoodsInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IContractInventoryDistributionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehousingOrderService;
import com.yh.saas.plugin.yiliangyiyun.util.CalculationUtil;
import org.activiti.engine.impl.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 记录合同库存分布详情 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2022-05-24
 */
@Service
public class ContractInventoryDistributionServiceImpl extends ServiceImpl<ContractInventoryDistributionMapper, ContractInventoryDistribution> implements IContractInventoryDistributionService {
    @Autowired
    private IWarehousingOrderService warehousingOrderService;
    /**
     * 合同/收购库存分布列表查询
     * @param contractInventoryDistribution
     * @return
     */
    @Override
    public Page<ContractInventoryDistribution> selectContractInventoryInfo(ContractInventoryDistribution contractInventoryDistribution) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (contractInventoryDistribution.getCurrentPage() - 1)
                * contractInventoryDistribution.getPageSize());
        //  公司ID
        pageView.put("compId", AuthSecurityUtils.getCurrentUserInfo().getCompId());
        pageView.put("inventoryType", contractInventoryDistribution.getInventoryType());
        pageView.put("contractNo", contractInventoryDistribution.getContractNo());
        pageView.put("warehouse", contractInventoryDistribution.getWarehouse());
        pageView.put("pageSize", contractInventoryDistribution.getPageSize());
        pageView.put("currentPage", contractInventoryDistribution.getCurrentPage());
        // 查询服务商总数
        Integer dataCount = baseMapper.getContractInventoryCountByCondition(pageView);
        List<ContractInventoryDistribution> dataList = baseMapper.getContractInventoryListByCondition(pageView);
        if("1".equals(contractInventoryDistribution.getInventoryType())){
            for (ContractInventoryDistribution contractInventoryDistribution1:dataList){
                Float proLoss=0f;
                List<WarehousingOrder> warehousingOrderList=warehousingOrderService.selectList(new EntityWrapper<WarehousingOrder>()
                .eq("warehouse_name",contractInventoryDistribution1.getWarehouse())
                .eq("contract_no",contractInventoryDistribution1.getContractNo()));
                if (!CollectionUtil.isNotEmpty(warehousingOrderList)){
                    for (WarehousingOrder warehousingOrder:warehousingOrderList){
                        proLoss=proLoss+warehousingOrder.getProfitAndLoss();
                    }
                }
                if (proLoss>0){
                    contractInventoryDistribution1.setSurplus(Double.valueOf(String.valueOf(proLoss)));
                }
                else{
                    contractInventoryDistribution1.setLoss(Double.valueOf(String.valueOf(proLoss)));
                }
            }
        }

        Page<ContractInventoryDistribution> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(contractInventoryDistribution.getCurrentPage());
        page.setSize(contractInventoryDistribution.getPageSize());
        return page;
    }
}
