package com.trade.service.purchase.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.FileTypeEnum;
import com.trade.service.enterprise.dto.EnterpriseBaseInfoDto;
import com.trade.service.enterprise.service.EnterpriseBaseService;
import com.trade.service.filestorage.service.BusinessRelateFileBaseService;
import com.trade.service.purchase.dto.PurchaseCommodityDto;
import com.trade.service.purchase.dto.PurchaseContractDetailDto;
import com.trade.service.purchase.dto.PurchaseContractDto;
import com.trade.service.purchase.dto.PurchaseContractSearchBaseDto;
import com.trade.service.purchase.mapper.PurchaseContractMapper;
import com.trade.service.purchase.domain.PurchaseContract;
import com.trade.service.purchase.service.PurchaseCommodityBaseService;
import com.trade.service.purchase.service.PurchaseContractBaseService;
import com.trade.service.sales.dto.ContractTermsDto;
import com.trade.service.sales.service.ContractTermsService;
import com.trade.service.sales.vo.SalesContractVo;
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<SalesContractVo> 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 + ")");
        }
        // 获取合同文件信息
        businessRelateFileBaseService.fillBizObjFiles(purchaseContractDto, id, ConstantUtil.PURCHASE_CONTRACT,
                FileTypeEnum.PURCHASE_CONTRACT.getCode());
        PurchaseContractDetailDto contractDetailDto = BeanCopyUtils.copy(purchaseContractDto,
                PurchaseContractDetailDto.class);
        // 获取采购合同的商品信息
        List<PurchaseCommodityDto> commodityList = purchaseCommodityBaseService.getPurchaseCommodityByContractId(id);
        contractDetailDto.setCommodityList(commodityList);
        // 获取采购合同的合同条款信息
        ContractTermsDto contractTermsDto = contractTermsService.getDetailByContractIdAndType(id,
                ConstantUtil.PURCHASE_CONTRACT_CODE);
        contractDetailDto.setContractTerms(contractTermsDto);
        return contractDetailDto;
    }
}
