package com.trade.service.trade.purchasecontract.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.common.core.page.GridPage;
import com.trade.common.utils.ConstantUtil;
import com.trade.common.utils.StringUtils;
import com.trade.common.utils.bean.BeanCopyUtils;
import com.trade.service.enterprise.customer.dto.EnterpriseBaseInfoDto;
import com.trade.service.enterprise.customer.service.EnterpriseBaseService;
import com.trade.service.filestorage.service.BusinessRelateFileBaseService;
import com.trade.service.trade.filingdoc.vo.Contract4FilingDocSearch;
import com.trade.service.trade.filingdoc.vo.PurchaseContract4FilingDocVO;
import com.trade.service.trade.purchasecontract.service.PurchaseCommodityBaseService;
import com.trade.service.trade.purchasecontract.service.PurchaseContractBaseService;
import com.trade.service.trade.purchasecontract.dto.PurchaseCommodityDto;
import com.trade.service.trade.purchasecontract.dto.PurchaseContractDetailDto;
import com.trade.service.trade.purchasecontract.dto.PurchaseContractDto;
import com.trade.service.trade.purchasecontract.dto.PurchaseContractSearchBaseDto;
import com.trade.service.trade.purchasecontract.mapper.PurchaseContractMapper;
import com.trade.service.trade.purchasecontract.domain.PurchaseContract;
import com.trade.service.trade.salescontract.dto.ContractTermsDto;
import com.trade.service.trade.salescontract.service.ContractTermsService;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * 采购合同表基础表基础服务实现类
 *
 * @author hlb
 * @since 2024-09-07 16:23:15
 */
@Service("purchaseContractBaseService")
public class PurchaseContractBaseServiceImpl extends ServiceImpl<PurchaseContractMapper, PurchaseContract> implements PurchaseContractBaseService {

    private static final Logger logger = LoggerFactory.getLogger(PurchaseContractBaseServiceImpl.class);
    @Resource
    private PurchaseContractMapper purchaseContractMapper;
    @Resource
    private EnterpriseBaseService enterpriseBaseService;
    @Resource
    private BusinessRelateFileBaseService businessRelateFileBaseService;
    @Resource
    private PurchaseCommodityBaseService purchaseCommodityBaseService;
    @Resource
    private ContractTermsService contractTermsService;

    @Override
    public GridPage<PurchaseContractDto> getPurchaseContractBySearch(PurchaseContractSearchBaseDto searchDto) {
        Page<PurchaseContractDto> page = PageHelper.startPage(searchDto.getPage(), searchDto.getRows());
        if (StringUtils.isNotEmpty(searchDto.getCustomerUserInfo())) {
            List<Long> customerUserIds = enterpriseBaseService.findByCustomerInfo(searchDto.getCustomerUserInfo())
                    .stream().map(EnterpriseBaseInfoDto::getCustomerId).collect(Collectors.toList());
            searchDto.setCustomerUserIdList(customerUserIds);
        }
        List<PurchaseContractDto> rows = purchaseContractMapper.getPurchaseContractBySearch(searchDto);
        return new GridPage<>(searchDto.getPage(), searchDto.getRows(), page.getTotal(), rows);
    }

    @Override
    public PurchaseContractDetailDto findPurchaseContractDetailById(Long id) {
        // 获取采购合同基本信息
        PurchaseContractDto purchaseContractDto = purchaseContractMapper.getPurchaseContractById(id);
        if (purchaseContractDto == null) {
            throw new RuntimeException("采购合同不存在(id:" + id + ")");
        }
        return getPurchaseContractDetailDto(purchaseContractDto);
    }

    private @NotNull PurchaseContractDetailDto getPurchaseContractDetailDto(PurchaseContractDto purchaseContractDto) {
        // 获取合同文件信息
        businessRelateFileBaseService.fillBizObjFiles(purchaseContractDto, purchaseContractDto.getId(),
                ConstantUtil.PURCHASE_CONTRACT, null);
        PurchaseContractDetailDto contractDetailDto = BeanCopyUtils.copy(purchaseContractDto,
                PurchaseContractDetailDto.class);
        // 获取采购合同的商品信息
        List<PurchaseCommodityDto> commodityList = purchaseCommodityBaseService
                .getPurchaseCommodityByContractId(purchaseContractDto.getId());
        contractDetailDto.setCommodityList(commodityList);
        // 获取采购合同的合同条款信息
        ContractTermsDto contractTermsDto = contractTermsService
                .getDetailByContractIdAndType(purchaseContractDto.getId(), ConstantUtil.PURCHASE_CONTRACT_CODE);
        contractDetailDto.setContractTerms(contractTermsDto);
        return contractDetailDto;
    }

    @Override
    public PurchaseContractDetailDto findBySaleContractNo(Long customerUserId, String saleContractNo,
            String vendorEntityNo) {
        PurchaseContractDto purchaseContractDto = purchaseContractMapper.findBySaleContractNo(customerUserId,
                saleContractNo, vendorEntityNo);
        if (purchaseContractDto == null) {
            return null;
        }
        return getPurchaseContractDetailDto(purchaseContractDto);
    }

    @Override
    public PurchaseContractDetailDto findByCustomerUserIdAndPurchaseContractNo(Long customerUserId,
            String purchaseContractNo) {
        PurchaseContractDto purchaseContractDto = purchaseContractMapper
                .findByCustomerUserIdAndPurchaseContractNo(customerUserId, purchaseContractNo);
        if (purchaseContractDto == null) {
            return null;
        }
        return getPurchaseContractDetailDto(purchaseContractDto);
    }

    @Override
    public List<PurchaseContract4FilingDocVO> queryList4FilingDocBySearch(Contract4FilingDocSearch search) {
        return purchaseContractMapper.queryList4FilingDocBySearch(search);
    }
}
