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.PurchaseRawMaterial;
import com.ningdatech.carapi.industry.model.req.PurchaseRawMaterialListReq;
import com.ningdatech.carapi.industry.model.vo.PurchaseRawMaterialListVo;
import com.ningdatech.carapi.industry.service.IPurchaseRawMaterialService;
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>
 * PurchaseRawMaterialManage
 * </p>
 *
 * @author WendyYang
 * @since 10:51 2022/11/9
 */
@Component
@AllArgsConstructor
public class PurchaseRawMaterialManage {

    private final IPurchaseRawMaterialService purchaseRawMaterialService;
    private final RegionsCacheHelper regionsCacheHelper;

    public PageVo<PurchaseRawMaterialListVo> list(PurchaseRawMaterialListReq req) {
        LambdaQueryWrapper<PurchaseRawMaterial> query = Wrappers.lambdaQuery(PurchaseRawMaterial.class);
        PurchaseRawMaterialManage context = (PurchaseRawMaterialManage) AopContext.currentProxy();
        context.buildQuery(query,req);
        Page<PurchaseRawMaterial> page = purchaseRawMaterialService.page(req.page(), query);
        if (0L == page.getTotal()) {
            return PageVo.empty();
        }
        List<PurchaseRawMaterialListVo> data = page.getRecords().stream().map(w -> {
            PurchaseRawMaterialListVo vo = BeanUtil.copyProperties(w, PurchaseRawMaterialListVo.class);
            vo.setRegions(regionsCacheHelper.listParents(w.getRegionId()));
            return vo;
        }).collect(Collectors.toList());
        return PageVo.of(data, page.getTotal());
    }

    @LambdaDataScope
    public void buildQuery(LambdaQueryWrapper<PurchaseRawMaterial> query,PurchaseRawMaterialListReq req) {
        if (null != req.getRegionId()) {
            query.in(PurchaseRawMaterial::getRegionId, regionsCacheHelper.listChildRegionId(req.getRegionId()));
        }
        if (StrUtil.isNotBlank(req.getRawMaterialName())) {
            query.like(PurchaseRawMaterial::getRawMaterialName, req.getRawMaterialName());
        }
        if (StrUtil.isNotBlank(req.getPurchaseOrg())) {
            query.like(PurchaseRawMaterial::getPurchaseOrg, req.getPurchaseOrg());
        }
        if (StrUtil.isNotBlank(req.getModel())) {
            query.like(PurchaseRawMaterial::getModel, req.getModel());
        }
        if (req.getStartDate() != null) {
            query.ge(PurchaseRawMaterial::getPurchaseDate, req.getStartDate());
        }
        if (req.getEndDate() != null) {
            query.le(PurchaseRawMaterial::getPurchaseDate, req.getEndDate());
        }
        query.orderByDesc(PurchaseRawMaterial::getUpdateOn);
    }

    public void saveOrUpdate(PurchaseRawMaterialListVo req) {
        PurchaseRawMaterial entity = BeanUtil.copyProperties(req, PurchaseRawMaterial.class);
        entity.setUpdateBy(LoginUserUtil.getUserId());
        if (entity.getId() == null) {
            entity.setCreateBy(entity.getUpdateBy());
            entity.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
        }
        purchaseRawMaterialService.saveOrUpdate(entity);
    }

}
