package com.hz.pm.api.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.model.PageVo;
import com.hz.pm.api.sys.mapper.EarlyWarningRecordsMapper;
import com.hz.pm.api.sys.model.entity.WflowEarlyWarningRecords;
import com.hz.pm.api.sys.model.req.WarningListReq;
import com.hz.pm.api.sys.model.vo.WflowEarlyWarningRecordsVO;
import com.hz.pm.api.sys.service.IEarlyWarningRecordsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 业务实现类
 * 菜单
 * </p>
 *
 * @author ZPF
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EarlyWarningRecordsServiceImpl extends ServiceImpl<EarlyWarningRecordsMapper, WflowEarlyWarningRecords>
        implements IEarlyWarningRecordsService {

    @Override
    public PageVo<WflowEarlyWarningRecordsVO> records(Integer ruleType, WarningListReq req) {
        Page<WflowEarlyWarningRecords> page = req.page();
        LambdaQueryWrapper<WflowEarlyWarningRecords> wrapper = Wrappers.lambdaQuery(WflowEarlyWarningRecords.class)
                .eq(WflowEarlyWarningRecords::getRuleType, ruleType)
                .eq(StringUtils.isNotBlank(req.getAreaCode()),WflowEarlyWarningRecords::getAreaCode,req.getAreaCode())
                .like(StringUtils.isNotBlank(req.getProjectName()), WflowEarlyWarningRecords::getProjectName, req.getProjectName())
                .like(StringUtils.isNotBlank(req.getBuildOrgName()), WflowEarlyWarningRecords::getBuildOrgName, req.getBuildOrgName())
                .eq(StringUtils.isNotBlank(req.getBuildOrgCode()), WflowEarlyWarningRecords::getBuildOrgCode, req.getBuildOrgCode())
                .ge(Objects.nonNull(req.getStartTime()),WflowEarlyWarningRecords::getWarningTime,req.getStartTime())
                .le(Objects.nonNull(req.getEndTime()),WflowEarlyWarningRecords::getWarningTime,req.getEndTime())
                .orderByDesc(WflowEarlyWarningRecords::getCreateOn);

        this.page(page,wrapper);

        if(CollUtil.isEmpty(page.getRecords())){
            return PageVo.empty();
        }

        List<WflowEarlyWarningRecordsVO> res = page.getRecords().stream()
                .map(p -> BeanUtil.copyProperties(p,WflowEarlyWarningRecordsVO.class))
                .collect(Collectors.toList());

        return PageVo.of(res,page.getTotal());
    }
}
