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.PurchaseOil;
import com.ningdatech.carapi.industry.model.req.PurchaseOilListReq;
import com.ningdatech.carapi.industry.model.vo.PurchaseOilListVo;
import com.ningdatech.carapi.industry.service.IPurchaseOilService;
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.Objects;
import java.util.stream.Collectors;

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

    private final RegionsCacheHelper regionsCacheHelper;
    private final IPurchaseOilService purchaseOilService;

    public PageVo<PurchaseOilListVo> purchaseOilList(PurchaseOilListReq req) {
        LambdaQueryWrapper<PurchaseOil> wrapper = Wrappers.lambdaQuery(PurchaseOil.class);
        PurchaseOilManage context = (PurchaseOilManage) AopContext.currentProxy();
        context.buildQuery(wrapper,req);
        Page<PurchaseOil> page = purchaseOilService.page(req.page(), wrapper);
        if (0L == page.getTotal()) {
            return PageVo.empty();
        }
        List<PurchaseOilListVo> data = page.getRecords().stream().map(w -> {
            PurchaseOilListVo vo = BeanUtil.copyProperties(w, PurchaseOilListVo.class);
            vo.setRegions(regionsCacheHelper.listParents(w.getRegionId()));
            return vo;
        }).collect(Collectors.toList());
        return PageVo.of(data, page.getTotal());
    }

    @LambdaDataScope
    public void buildQuery(LambdaQueryWrapper<PurchaseOil> wrapper,PurchaseOilListReq req) {
        wrapper.in(Objects.nonNull(req.getRegionId()),PurchaseOil::getRegionId, regionsCacheHelper.listChildRegionId(req.getRegionId()))
                .like(StrUtil.isNotBlank(req.getOilName()),PurchaseOil::getOilName, req.getOilName())
                .like(StrUtil.isNotBlank(req.getPurchaseOrg()),PurchaseOil::getPurchaseOrg, req.getPurchaseOrg())
                .ge(Objects.nonNull(req.getStartDate()),PurchaseOil::getPurchaseDate, req.getStartDate())
                .le(Objects.nonNull(req.getEndDate()),PurchaseOil::getPurchaseDate, req.getEndDate())
                .orderByDesc(PurchaseOil::getUpdateOn);
    }

    public void saveOrUpdate(PurchaseOilListVo req) {
        PurchaseOil oil = BeanUtil.copyProperties(req, PurchaseOil.class);
        oil.setUpdateBy(LoginUserUtil.getUserId());
        if (Objects.isNull(oil.getId())) {
            oil.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
            oil.setCreateBy(oil.getUpdateBy());
        }
        purchaseOilService.saveOrUpdate(oil);
    }

}
