package com.ningdatech.carapi.driver.manage;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.driver.constant.ArticleTypeEnum;
import com.ningdatech.carapi.driver.entity.ArticleInfo;
import com.ningdatech.carapi.driver.model.po.ReqArticleInfoListPO;
import com.ningdatech.carapi.driver.model.vo.ResArticleInfoListVO;
import com.ningdatech.carapi.driver.service.IArticleInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/10/9 下午4:34
 */

@Component
@RequiredArgsConstructor
public class ArticleInfoManage {

    private final IArticleInfoService iArticleInfoService;

    public PageVo<ResArticleInfoListVO> articleInfoList(ReqArticleInfoListPO reqArticleInfoListPo) {
        ArticleTypeEnum articleType = reqArticleInfoListPo.getArticleType();
        String title = reqArticleInfoListPo.getTitle();
        LocalDateTime startTime = reqArticleInfoListPo.getStartTime();
        LocalDateTime endTime = reqArticleInfoListPo.getEndTime();
        Integer pageNumber = reqArticleInfoListPo.getPageNumber();
        Integer pageSize = reqArticleInfoListPo.getPageSize();

        Page<ArticleInfo> page = iArticleInfoService.page(new Page<>(pageNumber, pageSize)
                , Wrappers.lambdaQuery(ArticleInfo.class)
                        .eq(Objects.nonNull(articleType), ArticleInfo::getArticleType, articleType.name())
                        .like(StringUtils.isNotBlank(title), ArticleInfo::getTitle, title)
                        .gt(Objects.nonNull(startTime), ArticleInfo::getFormulationTime, startTime)
                        .lt(Objects.nonNull(endTime), ArticleInfo::getFormulationTime, endTime));

        PageVo<ResArticleInfoListVO> pageVo = new PageVo<>();
        long total = page.getTotal();
        pageVo.setTotal(total);
        if (total == 0) {
            return pageVo;
        }

        List<ArticleInfo> records = page.getRecords();
        List<ResArticleInfoListVO> resArticleInfoListVOList = records.stream().map(r -> {
            ResArticleInfoListVO resArticleInfoListVO = new ResArticleInfoListVO();
            resArticleInfoListVO.setArticleId(r.getId());
            resArticleInfoListVO.setArticleStatus(r.getArticleStatus());
            String currentArticleType = r.getArticleType();
            if (StringUtils.isNotBlank(currentArticleType)) {
                resArticleInfoListVO.setArticleType(ArticleTypeEnum.valueOf(currentArticleType));
            }
            resArticleInfoListVO.setFormulationTime(r.getFormulationTime());
            resArticleInfoListVO.setTitle(r.getTitle());
            return resArticleInfoListVO;
        }).collect(Collectors.toList());

        pageVo.setRecords(resArticleInfoListVOList);
        return pageVo;
    }
}
