package com.jwipc.tender.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.prop.SysProp;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.tender.convert.BidQuoteLogConvert;
import com.jwipc.tender.dto.BidQuoteLogDTO;
import com.jwipc.tender.logic.BidQuoteLogLogic;
import com.jwipc.tender.model.po.BidQuoteLogPO;
import com.jwipc.tender.model.query.BidQuoteLogQuery;
import com.jwipc.tender.service.ITenderBidQuoteLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class BidQuoteLogLogicImpl implements BidQuoteLogLogic {
    private final ITenderBidQuoteLogService bidQuoteLogService;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final BidQuoteLogConvert bidQuoteLogConvert;
    private final SysProp sysProp;

    @Override
    public PageView getReport(BidQuoteLogQuery query) {
        final Set<String> userPurchaseGroupCode = userPurchaseGroupClient.getUserPurchaseGroupCode(
                LoginUtil.getUserId());
        query.setUserPurchaseGroupCodes(userPurchaseGroupCode);
        Page<BidQuoteLogPO> page = PageUtil.toPage(query);
        List<BidQuoteLogPO> list = bidQuoteLogService.findForPage(page, query);
        List<BidQuoteLogDTO> resul = bidQuoteLogConvert.convert1(list);
        return PageUtil.getPV(resul, page.getTotal());
    }

    @Override
    public List<BidQuoteLogDTO> getAll(BidQuoteLogQuery tenderQuery) {
        final Set<String> userPurchaseGroupCode = userPurchaseGroupClient.getUserPurchaseGroupCode(
                LoginUtil.getUserId());
        tenderQuery.setUserPurchaseGroupCodes(userPurchaseGroupCode);
        Page<BidQuoteLogPO> page = new Page<>(1, 1);
        bidQuoteLogService.findForPage(page, tenderQuery);
        if (page.getTotal() > sysProp.getExportMaxCount()) {
            throw new DaoException(StrUtil.format("导出数据超过{}条", sysProp.getExportMaxCount()));
        }
        final List<BidQuoteLogPO> forAll = bidQuoteLogService.findForAll(tenderQuery);
        return bidQuoteLogConvert.convert1(forAll);
    }
}