package com.jwipc.order.logic.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.aggregation.ModelAggregationService;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.api.dto.OaFileParamDTO;
import com.jwipc.exception.DaoException;
import com.jwipc.order.constant.PcnMange;
import com.jwipc.order.constant.PcnOaClient;
import com.jwipc.order.convert.PcnManagerConvert;
import com.jwipc.order.convert.PurchaseOrderDetailUnlockLogConvert;
import com.jwipc.order.dto.*;
import com.jwipc.order.entity.*;
import com.jwipc.order.logic.PcnManagerLogic;
import com.jwipc.order.logic.PurchaseOrderDetailUnlockLogLogic;
import com.jwipc.order.model.po.DeliveryOrderDetailPO;
import com.jwipc.order.model.query.PcnChangeApproveCondQuery;
import com.jwipc.order.model.query.PcnManagerQuery;
import com.jwipc.order.model.query.PurchaseOrderUnlockLogQuery;
import com.jwipc.order.service.*;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.prop.SysProp;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.clent.dto.SupplierInvitaionDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.FileClient;
import com.jwipc.sys.client.SequenceClient;
import com.jwipc.sys.client.UserClient;
import com.jwipc.sys.client.dto.DictDTO;
import com.jwipc.sys.client.dto.FileResposePreviewCDTO;
import com.jwipc.sys.client.dto.UserDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/19 0019 14:36
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PurchaseOrderDetailUnlockLogLogicImpl implements PurchaseOrderDetailUnlockLogLogic {

    @Autowired
    IPurchaseOrderDetailUnlockLogService purchaseOrderDetailUnlockLogService;
    @Autowired
    PurchaseOrderDetailUnlockLogConvert purchaseOrderDetailUnlockLogConvert;

    private final SysProp sysProp;

    @Override
    public PageView queryUnlockLogByPage(PurchaseOrderUnlockLogQuery query) {
        final Page<PurchaseOrderDetailUnlockLog> page = PageUtil.toPage(query);
        final List<PurchaseOrderDetailUnlockLog> list = purchaseOrderDetailUnlockLogService.findList(page, query);
        return PageUtil.getPV(purchaseOrderDetailUnlockLogConvert.ListToDTOs(list), page.getTotal());
    }

    @Override
    public List<PurchaseOrderDetailUnlockLogListDTO> getListAll(PurchaseOrderUnlockLogQuery purchaseOrderUnlockLogQuery) {
        //需要单独查询分页总数
        purchaseOrderUnlockLogQuery.setRows(1);
        purchaseOrderUnlockLogQuery.setPage(1);
        Page<PurchaseOrderDetailUnlockLog> page = PageUtil.toPage(purchaseOrderUnlockLogQuery);
        final List<PurchaseOrderDetailUnlockLog> list = purchaseOrderDetailUnlockLogService.findList(page, purchaseOrderUnlockLogQuery);

        if (page.getTotal() > sysProp.getExportMaxCount()) {
            throw new DaoException(StrUtil.format("导出数据超过{}条", sysProp.getExportMaxCount()));
        }
        List<PurchaseOrderDetailUnlockLog> purchaseOrderDetailUnlockLogs = purchaseOrderDetailUnlockLogService.listByQuery(purchaseOrderUnlockLogQuery);
        return purchaseOrderDetailUnlockLogConvert.ListToDTOs(purchaseOrderDetailUnlockLogs);
    }
}
