package com.jwipc.inquiry.logic.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.inquiry.convert.PriceInfoConvert;
import com.jwipc.inquiry.convert.PriceWarningConvert;
import com.jwipc.inquiry.dto.PriceWarningDTO;
import com.jwipc.inquiry.entity.PriceInfo;
import com.jwipc.inquiry.logic.PriceInfoLogic;
import com.jwipc.inquiry.logic.PriceWarningLogic;
import com.jwipc.inquiry.model.po.PriceWarningPO;
import com.jwipc.inquiry.model.query.PriceWarningQuery;
import com.jwipc.inquiry.service.IPriceInfoService;
import com.jwipc.inquiry.service.IPriceWarningService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PriceWarningLogicImpl implements PriceWarningLogic {
    private final IPriceWarningService priceWarningService;
    private final PriceWarningConvert priceWarningConvert;

    @Override
    public PageView findByPage(PriceWarningQuery query) {
        final Page<PriceInfo> dbPage = PageUtil.toPage(query);
        query.setExpireDate(StringUtils.isEmpty(query.getExpireDate())?DateUtil.format(new Date(),"yyyy-MM-dd"):query.getExpireDate());
        query.setEndTime(DateUtil.format(DateUtil.offsetDay(DateUtil.parse(query.getExpireDate()),(query.getExpireNum()==null?30:query.getExpireNum())),"yyyy-MM-dd"));
        List<PriceWarningPO> priceWarningPOS = priceWarningService.findPageByQuery(dbPage, query);
        return PageUtil.getPV(priceWarningConvert.po2PageListDTO(priceWarningPOS), dbPage.getTotal());
    }
//列表主数据
@Override
public List<PriceWarningDTO> listAll(PriceWarningQuery query) {
    query.setExpireDate(StringUtils.isEmpty(query.getExpireDate())?DateUtil.format(new Date(),"yyyy-MM-dd"):query.getExpireDate());
    query.setEndTime(DateUtil.format(DateUtil.offsetDay(DateUtil.parse(query.getExpireDate()),(query.getExpireNum()==null?30:query.getExpireNum())),"yyyy-MM-dd"));
    List<PriceWarningPO> priceWarningPOS = priceWarningService.findPageByQuery(null, query);
    return priceWarningConvert.po2PageListDTO(priceWarningPOS);
}
}
