package com.trade.client.order.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.client.consumer.domain.CustomerUser;
import com.trade.client.consumer.service.CustomerUserService;
import com.trade.client.order.service.CPurchaseOrderService;
import com.trade.common.constant.HttpStatus;
import com.trade.common.core.page.GridPage;
import com.trade.common.exception.ValidatorException;
import com.trade.common.utils.LoggerUtil;
import com.trade.service.filestorage.entity.TFileStorage;
import com.trade.service.filestorage.enums.FileModuleEnum;
import com.trade.service.filestorage.service.OSSFileService;
import com.trade.service.filestorage.util.MultipartFileUtil;
import com.trade.service.order.dto.PurchaseOrderLogisticsDto;
import com.trade.service.order.dto.PurchaseOrderLogisticsSearchDto;
import com.trade.service.order.dto.PurchaseSearchDto;
import com.trade.service.order.service.PurchaseOrderLogisticsService;
import com.trade.service.order.service.PurchaseOrderService;
import com.trade.service.order.vo.PurchaseOrderDetailsVo;
import com.trade.service.order.vo.PurchaseOrderLogisticsPageVo;
import com.trade.service.order.vo.PurchaseOrderVo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @description: 采购订单服务实现类
 * @Author: wanglang
 * @Date: 2024/9/11 9:50
 * @Version: 1.0
 */
@Service
public class CPurchaseOrderServiceImpl implements CPurchaseOrderService {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Resource
    private OSSFileService ossFileService;
    @Resource
    private CustomerUserService customerUserService;
    @Resource
    private PurchaseOrderLogisticsService purchaseOrderLogisticsService;

    @Override
    public Boolean saveOrderLogistics(PurchaseOrderLogisticsDto purchaseOrderLogisticsDto, HttpServletRequest request) {
        MultipartFile transferFile = null;
        if (request instanceof MultipartHttpServletRequest) {
            transferFile = ((MultipartHttpServletRequest) request).getFile("file");
        }
        if (transferFile == null) {
            throw new ValidatorException(HttpStatus.ERROR, "请您运输证明！");
        }
        boolean meetLimit = MultipartFileUtil.checkFileSize(transferFile, 20, "M");
        if (!meetLimit) {
            throw new ValidatorException(HttpStatus.ERROR, "合同文件大小请控制在20M以内");
        }
        TFileStorage fileStorage = null;
        try {
            fileStorage = ossFileService.addStorage(transferFile, FileModuleEnum.PURCHASE_ORDER);
        } catch (IOException e) {
            log.error("purchaseOrderId={}文件上传失败={}", purchaseOrderLogisticsDto.getPurchaseOrderId(), e);
            throw new ValidatorException(HttpStatus.ERROR, "文件上传失败");
        }
        purchaseOrderLogisticsDto.setTransportCertificateFileId(fileStorage.getId());
        purchaseOrderService.saveOrderLogistics(purchaseOrderLogisticsDto);
        return true;
    }

    @Override
    public GridPage<PurchaseOrderVo> queryListByPage(PurchaseSearchDto purchaseSearch) {
        CustomerUser customerUser = customerUserService.getCustomerUser();
        purchaseSearch.setCustomerUserId(customerUser.getId());
        purchaseSearch.setPoint(true);
        Page<PurchaseOrderVo> page = PageHelper.startPage(purchaseSearch.getPage(), purchaseSearch.getRows());
        List<PurchaseOrderVo> voList = purchaseOrderService.selectByCondition(purchaseSearch);
        return new GridPage<>(purchaseSearch.getPage(), purchaseSearch.getRows(), page.getTotal(), voList);
    }

    @Override
    public PurchaseOrderDetailsVo getDetailsById(Long id) {
        return purchaseOrderService.getDetailsById(id);
    }

    @Override
    public GridPage<PurchaseOrderLogisticsPageVo> queryOrderLogisticsByPage(
            PurchaseOrderLogisticsSearchDto orderLogisticsSearch) {
        CustomerUser customerUser = customerUserService.getCustomerUser();
        orderLogisticsSearch.setCustomerUserId(customerUser.getId());
        orderLogisticsSearch.setPoint(true);
        Page<PurchaseOrderVo> page = PageHelper.startPage(orderLogisticsSearch.getPage(),
                orderLogisticsSearch.getRows());
        return new GridPage<>(orderLogisticsSearch.getPage(), orderLogisticsSearch.getRows(), page.getTotal(),
                purchaseOrderLogisticsService.queryOrderLogisticsByPage(orderLogisticsSearch));
    }
}
