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.PurchaseWheel;
import com.ningdatech.carapi.industry.model.req.PurchaseWheelListReq;
import com.ningdatech.carapi.industry.model.vo.PurchaseWheelListVo;
import com.ningdatech.carapi.industry.service.IPurchaseWheelService;
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>
 * PurchaseOilManage
 * </p>
 *
 * @author WendyYang
 * @since 14:09 2022/11/9
 */
@Component
@AllArgsConstructor
public class PurchaseWheelManage {

    private final RegionsCacheHelper regionsCacheHelper;
    private final IPurchaseWheelService purchaseWheelService;

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

    public void saveOrUpdate(PurchaseWheelListVo req) {
        PurchaseWheel wheel = BeanUtil.copyProperties(req, PurchaseWheel.class);
        wheel.setUpdateBy(LoginUserUtil.getUserId());
        if (wheel.getId() == null) {
            wheel.setCreateBy(wheel.getUpdateBy());
            wheel.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
        }
        purchaseWheelService.saveOrUpdate(wheel);
    }

}
