package com.ningdatech.carapi.industry.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
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.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.industry.entity.PurchaseVehicle;
import com.ningdatech.carapi.industry.model.req.PurchaseVehicleListReq;
import com.ningdatech.carapi.industry.model.vo.PurchaseVehicleListVo;
import com.ningdatech.carapi.industry.service.IPurchaseVehicleService;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import lombok.AllArgsConstructor;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

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

/**
 * <p>
 * PurchaseVehicleManage
 * </p>
 *
 * @author WendyYang
 * @since 14:09 2022/11/9
 */
@Component
@AllArgsConstructor
public class PurchaseVehicleManage {

    private final RegionsCacheHelper regionsCacheHelper;
    private final IPurchaseVehicleService purchaseVehicleService;

    public PageVo<PurchaseVehicleListVo> purchaseWheelList(PurchaseVehicleListReq req) {
        LambdaQueryWrapper<PurchaseVehicle> query = Wrappers.lambdaQuery(PurchaseVehicle.class);
        PurchaseVehicleManage context = (PurchaseVehicleManage) AopContext.currentProxy();
        context.buildQuery(query,req);
        Page<PurchaseVehicle> page = purchaseVehicleService.page(req.page(), query);
        if (0L == page.getTotal()) {
            return PageVo.empty();
        }
        List<PurchaseVehicleListVo> data = page.getRecords().stream().map(w -> {
            PurchaseVehicleListVo vo = BeanUtil.copyProperties(w, PurchaseVehicleListVo.class);
            vo.setRegions(regionsCacheHelper.listParents(w.getRegionId()));
            return vo;
        }).collect(Collectors.toList());
        return PageVo.of(data, page.getTotal());
    }
    @LambdaDataScope
    public void buildQuery(LambdaQueryWrapper<PurchaseVehicle> query,PurchaseVehicleListReq req) {
        if (null != req.getRegionId()) {
            query.in(PurchaseVehicle::getRegionId, regionsCacheHelper.listChildRegionId(req.getRegionId()));
        }
        if (StrUtil.isNotBlank(req.getVehicleType())) {
            query.like(PurchaseVehicle::getVehicleType, req.getVehicleType());
        }
        if (StrUtil.isNotBlank(req.getPurchaseOrg())) {
            query.like(PurchaseVehicle::getPurchaseOrg, req.getPurchaseOrg());
        }
        if (req.getStartDate() != null) {
            query.ge(PurchaseVehicle::getPurchaseDate, req.getStartDate());
        }
        if (req.getEndDate() != null) {
            query.le(PurchaseVehicle::getPurchaseDate, req.getEndDate());
        }
        query.orderByDesc(PurchaseVehicle::getUpdateOn);
    }

    public void saveOrUpdate(PurchaseVehicleListVo req) {
        PurchaseVehicle vehicle = BeanUtil.copyProperties(req, PurchaseVehicle.class);
        vehicle.setUpdateBy(LoginUserUtil.getUserId());
        if (vehicle.getId() == null) {
            vehicle.setCreateBy(vehicle.getUpdateBy());
            vehicle.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
        }
        purchaseVehicleService.saveOrUpdate(vehicle);
    }

}
