package com.fjhx.service.material.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.Condition;
import com.fjhx.constants.ConfigConstant;
import com.fjhx.entity.material.Material;
import com.fjhx.mapper.material.MaterialMapper;
import com.fjhx.service.common.CommonConfigService;
import com.fjhx.service.material.MaterialService;
import com.fjhx.utils.WrapperUtil;
import com.fjhx.utils.wrapperUtil.IWrapper;
import com.fjhx.utils.wrapperUtil.KeywordData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-08-05
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private CommonConfigService commonConfigService;

    @Override
    public Page<Material> getPage(Map<String, String> condition) {

        QueryWrapper<Material> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .eq("material_classify_id", "materialClassifyId")
                .keyword("code", "name");

        wrapper.orderByAsc("code");

        return page(condition, wrapper);
    }

    @Override
    public String getDelayPeriod() {
        return commonConfigService.getValue(ConfigConstant.DELAY_PERIOD);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setDelayPeriod(Map<String, Integer> condition) {
        Integer value = condition.get("value");
        commonConfigService.updateValue(ConfigConstant.DELAY_PERIOD, value + "");
        update(Wrappers.<Material>lambdaUpdate().set(Material::getDelayPeriod, value));
    }

    @Override
    public List<Map<String, Object>> getMaterialBySupplierId(Long supplierId) {
        return baseMapper.getMaterialBySupplierId(supplierId);
    }

    @Override
    public Page<Map<String, Object>> getStockPage(Condition condition) {
        String keyword = condition.getKeyword();

        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.and(ObjectUtil.isNotEmpty(keyword), q ->
                q.like("m.name", keyword).or().like("m.code", keyword));

        return baseMapper.getStockPage(condition.getPage(), wrapper);
    }

    @Override
    public Page<Map<String, Object>> getMaterialAndStock(Condition condition) {
        IWrapper<Object> wrapper = IWrapper.getWrapper(condition)
                .keyword(new KeywordData("m", Material::getName), new KeywordData("m", Material::getCode))
                .eq("m", Material::getTechnologyType)
                .eq("m", Material::getPurpose)
                .gt("s.quantity", 0);

        return baseMapper.getMaterialAndStock(createPage(condition), wrapper);
    }

}
