package com.lutao.cloud.oil.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.core.util.StringUtil;
import com.lutao.cloud.oil.mapper.DivisionInspectionDetailMapper;
import com.lutao.cloud.oil.pojo.param.BaseParam;
import com.lutao.cloud.oil.pojo.param.DivisionInspectionDetailParam;
import com.lutao.cloud.oil.pojo.vo.DivisionInspectionDetailVO;
import com.lutao.cloud.oil.pojo.vo.ProblemRectifyProportionVO;
import com.lutao.cloud.oil.service.DivisionInspectionDetailService;
import com.lutao.entity.DivisionInspectionDetail;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Year;

@Service
public class DivisionInspectionDetailServiceImpl extends ServiceImpl<DivisionInspectionDetailMapper, DivisionInspectionDetail> implements DivisionInspectionDetailService {
    @Override
    public MessageInfo<IPage<DivisionInspectionDetailVO>> getDivisionInspectionDetailPage(Page page, DivisionInspectionDetailParam param) {
        return MessageInfo.ok(this.baseMapper.getDivisionInspectionDetailPage(page,param));
    }

    @Override
    public MessageInfo<ProblemRectifyProportionVO> getDivisionInspectionDetailChart(BaseParam param) {
        //问题隐患数量
        int problemNumber = count(
                Wrappers.lambdaQuery(DivisionInspectionDetail.class)
                        .eq(StringUtil.isNotBlank(param.getAppId()),DivisionInspectionDetail::getAppId, param.getAppId())
                        .eq(StringUtil.isNotBlank(param.getPlatformId()),DivisionInspectionDetail::getPlatformId, param.getPlatformId())
                        .between(DivisionInspectionDetail::getInputTime, LocalDate.of(Year.now().getValue(), 1, 1), LocalDate.of(Year.now().getValue(), 12, 31))
        );

        //问题隐患整改完成数量
        int rectifyNumber = count(
                Wrappers.lambdaQuery(DivisionInspectionDetail.class)
                        .eq(StringUtil.isNotBlank(param.getAppId()),DivisionInspectionDetail::getAppId, param.getAppId())
                        .eq(StringUtil.isNotBlank(param.getPlatformId()),DivisionInspectionDetail::getPlatformId, param.getPlatformId())
                        .eq(DivisionInspectionDetail::getHiddenProblemsState, "002")
                        .between(DivisionInspectionDetail::getInputTime, LocalDate.of(Year.now().getValue(), 1, 1), LocalDate.of(Year.now().getValue(), 12, 31))
        );
        ProblemRectifyProportionVO problemRectifyProportionVO = new ProblemRectifyProportionVO().setProblemNumber(problemNumber).setRectifyNumber(rectifyNumber);

        return MessageInfo.ok(problemRectifyProportionVO);
    }
}
