package com.trade.admin.trade.salecontract.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.admin.enterprise.customer.service.EnterpriseService;
import com.trade.admin.trade.salecontract.service.BSaleContractService;
import com.trade.common.core.page.GridPage;
import com.trade.common.utils.StringUtils;
import com.trade.service.trade.salescontract.dto.ContractSearchDto;
import com.trade.service.trade.salescontract.service.SalesContractService;
import com.trade.service.trade.salescontract.vo.SalesContractInfoVo;
import com.trade.service.trade.salescontract.vo.SalesContractVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @Author: wanglang
 * @Date: 2024/9/7 10:20
 * @Version: 1.0
 */
@Service
public class BSaleContractServiceImpl implements BSaleContractService {

    @Resource
    private SalesContractService salesContractService;
    @Resource
    private EnterpriseService enterpriseService;

    @Override
    public GridPage<SalesContractVo> getSalesContractPage(ContractSearchDto contractSearch) {
        contractSearch.setPoint(false);
        Page<SalesContractVo> page = PageHelper.startPage(contractSearch.getPage(), contractSearch.getRows());
        if (StringUtils.isNotEmpty(contractSearch.getCustomerUserInfo())) {
            List<Long> customerUserIds = enterpriseService.findByCustomerInfo(contractSearch.getCustomerUserInfo())
                    .stream().map(e -> e.getCustomerId()).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(customerUserIds)) {
                return new GridPage<>(contractSearch.getPage(), contractSearch.getRows(), page.getTotal(),
                        new ArrayList<>());
            }
            contractSearch.setCustomerUserIdList(customerUserIds);
        }
        List<SalesContractVo> rows = salesContractService.getSalesContractInfo(contractSearch);
        return new GridPage<>(contractSearch.getPage(), contractSearch.getRows(), page.getTotal(), rows);
    }

    @Override
    public SalesContractInfoVo getSalesContractDetails(Long contractId) {
        return salesContractService.getSalesContractDetails(contractId);
    }
}
