package com.ningdatech.carapi.safe.manage;

import cn.hutool.core.bean.BeanUtil;
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.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionWrapper;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.safe.entity.NdLawCertificateVerification;
import com.ningdatech.carapi.safe.entity.NdLawEquipmentInstall;
import com.ningdatech.carapi.safe.model.dto.LawEnforcementPageQuery;
import com.ningdatech.carapi.safe.model.vo.LawCertificateVerificationVO;
import com.ningdatech.carapi.safe.model.vo.LawEquipmentInstallVO;
import com.ningdatech.carapi.safe.service.INdLawCertificateVerificationService;
import com.ningdatech.carapi.safe.service.INdLawEquipmentInstallService;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author PoffyZhang
 * @date 2022/10/19 上午11:18
 */
@Component
@RequiredArgsConstructor
public class LawEnforcementManage {

    private final INdLawEquipmentInstallService equipmentInstallService;
    private final INdLawCertificateVerificationService certificateVerificationService;
    private final RegionsCacheHelper regionsCacheHelper;

    /**
     * 执行 设备安装 查询
     * @param lawEnforcementPageQuery
     * @return
     */
    public PageVo<LawEquipmentInstallVO> queryEquipmentInstall(LawEnforcementPageQuery lawEnforcementPageQuery) {
        Page<NdLawEquipmentInstall> page = lawEnforcementPageQuery.page();
        equipmentInstall(page, lawEnforcementPageQuery);
        List<LawEquipmentInstallVO> data = CollUtils.convert(page.getRecords(), e -> {
            LawEquipmentInstallVO vo = BeanUtil.copyProperties(e, LawEquipmentInstallVO.class);
            CodeUtil.searchCompanyNameAndRegionName(vo);
            return vo;
        });
        return PageVo.of(data, page.getTotal());
    }

    /**
     * 证件核查
     * @param lawEnforcementPageQuery
     * @return
     */
    public PageVo<LawCertificateVerificationVO> queryCertificateVerification(LawEnforcementPageQuery lawEnforcementPageQuery) {
        Page<NdLawCertificateVerification> page = lawEnforcementPageQuery.page();
        certificateVerification(page, lawEnforcementPageQuery);
        List<LawCertificateVerificationVO> data = CollUtils.convert(page.getRecords(), e -> {
            LawCertificateVerificationVO vo = BeanUtil.copyProperties(e, LawCertificateVerificationVO.class);
            CodeUtil.searchCompanyNameAndRegionName(vo);
            return vo;
        });
        return PageVo.of(data, page.getTotal());
    }

    private void certificateVerification(Page<NdLawCertificateVerification> page, LawEnforcementPageQuery lawEnforcementPageQuery) {
        LambdaQueryWrapper<NdLawCertificateVerification> wrapper = Wrappers.lambdaQuery(NdLawCertificateVerification.class);
        LawEnforcementManage context = (LawEnforcementManage)AopContext.currentProxy();
        context.searchCert(page,wrapper,lawEnforcementPageQuery);
    }

    @LambdaDataScope(wrapperIndex = 1)
    @BuildChildrenRegionWrapper(wrapperIndex = 1,entityIndex = 2)
    public void searchCert(Page<NdLawCertificateVerification> page, LambdaQueryWrapper<NdLawCertificateVerification> wrapper, LawEnforcementPageQuery lawEnforcementPageQuery) {
        wrapper.ge(Objects.nonNull(lawEnforcementPageQuery.getStartTime()),NdLawCertificateVerification::getCheckDate, lawEnforcementPageQuery.getStartTime())
                .le(Objects.nonNull(lawEnforcementPageQuery.getEndTime()),NdLawCertificateVerification::getCheckDate, lawEnforcementPageQuery.getEndTime())
                .like(Objects.nonNull(lawEnforcementPageQuery.getCarPlate()),NdLawCertificateVerification::getCarPlate, lawEnforcementPageQuery.getCarPlate())
                .like(StringUtils.isNotBlank(lawEnforcementPageQuery.getCompanyName()),NdLawCertificateVerification::getCompanyName, lawEnforcementPageQuery.getCompanyName())
                .like(StringUtils.isNotBlank(lawEnforcementPageQuery.getDriverName()),NdLawCertificateVerification::getDriverName, lawEnforcementPageQuery.getDriverName());
        wrapper.orderBy(Boolean.TRUE,Boolean.FALSE,NdLawCertificateVerification::getCheckDate);
        certificateVerificationService.page(page, wrapper);
    }


    private void equipmentInstall(Page<NdLawEquipmentInstall> page, LawEnforcementPageQuery lawEnforcementPageQuery) {
        LambdaQueryWrapper<NdLawEquipmentInstall> wrapper = Wrappers.lambdaQuery(NdLawEquipmentInstall.class);
        LawEnforcementManage context = (LawEnforcementManage)AopContext.currentProxy();
        context.searchInstall(page,wrapper,lawEnforcementPageQuery);
    }

    @LambdaDataScope(wrapperIndex = 1)
    @BuildChildrenRegionWrapper(entityIndex = 2,wrapperIndex = 1)
    public void searchInstall(Page<NdLawEquipmentInstall> page, LambdaQueryWrapper<NdLawEquipmentInstall> wrapper, LawEnforcementPageQuery lawEnforcementPageQuery) {
        wrapper.ge(Objects.nonNull(lawEnforcementPageQuery.getStartTime()),NdLawEquipmentInstall::getCheckDate, lawEnforcementPageQuery.getStartTime())
                .le(Objects.nonNull(lawEnforcementPageQuery.getEndTime()),NdLawEquipmentInstall::getCheckDate, lawEnforcementPageQuery.getEndTime())
                .eq(StringUtils.isNotBlank(lawEnforcementPageQuery.getEquipmentName()),NdLawEquipmentInstall::getEquipmentName, lawEnforcementPageQuery.getEquipmentName())
                .like(Objects.nonNull(lawEnforcementPageQuery.getCarPlate()),NdLawEquipmentInstall::getCarPlate, lawEnforcementPageQuery.getCarPlate())
                .like(StringUtils.isNotBlank(lawEnforcementPageQuery.getCompanyName()),NdLawEquipmentInstall::getCompanyName, lawEnforcementPageQuery.getCompanyName())
                .like(StringUtils.isNotBlank(lawEnforcementPageQuery.getDriverName()),NdLawEquipmentInstall::getDriverName, lawEnforcementPageQuery.getDriverName());
        wrapper.orderBy(Boolean.TRUE,Boolean.FALSE,NdLawEquipmentInstall::getCheckDate);
        equipmentInstallService.page(page, wrapper);
    }
}
