package com.fjhx.mes.service.material.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.mes.entity.material.dto.MaterialPreparationSelectDto;
import com.fjhx.mes.entity.material.po.MaterialPreparation;
import com.fjhx.mes.entity.material.vo.MaterialPreparationVo;
import com.fjhx.mes.mapper.material.MaterialPreparationMapper;
import com.fjhx.mes.service.material.MaterialPreparationService;
import com.fjhx.tenant.utils.DeptUstil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 生产备料 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-11
 */
@Service
public class MaterialPreparationServiceImpl extends ServiceImpl<MaterialPreparationMapper, MaterialPreparation> implements MaterialPreparationService {

    @Autowired
    private ProductInfoService productInfoService;

    private IWrapper<MaterialPreparation> getPageWrapper(MaterialPreparationSelectDto dto) {
        IWrapper<MaterialPreparation> wrapper = getWrapper();

        wrapper.eq(MaterialPreparation::getStatus, dto.getStatus());

        //时间范围过滤
        wrapper.ge("mp", MaterialPreparation::getCreateTime, dto.getBeginTime());
        wrapper.le("mp", MaterialPreparation::getCreateTime, dto.getEndTime());

        //关键字过滤
        wrapper.keyword(dto.getKeyword(), new SqlField("pi.code"));

        //权限过滤：生产备料-子公司看自己的,总公司看全部
        Long companyId = SecurityUtils.getCompanyId();
        if (!Objects.equals(companyId, 100L)) {
            wrapper.eq("mp", MaterialPreparation::getCompanyId, companyId);
        } else {
            wrapper.eq("mp", MaterialPreparation::getCompanyId, dto.getCompanyId());
        }

        wrapper.orderByDesc("mp", MaterialPreparation::getCreateTime);
        wrapper.orderByDesc("mp", MaterialPreparation::getId);
        return wrapper;
    }

    @Override
    public Page<MaterialPreparationVo> getPage(MaterialPreparationSelectDto dto) {
        IWrapper<MaterialPreparation> wrapper = getPageWrapper(dto);

        Page<MaterialPreparationVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        List<MaterialPreparationVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }

        //获取生产公司信息
        List<Long> companyIds = records.stream().map(MaterialPreparation::getCompanyId).collect(Collectors.toList());
        Map<Long, String> companyNameMap = DeptUstil.getDeptNameMap(companyIds);

        for (MaterialPreparationVo record : records) {
            //赋值生产公司名称
            record.setCompanyName(companyNameMap.get(record.getCompanyId()));
        }

        return page;
    }

    @Override
    public List<MaterialPreparationVo> getList(MaterialPreparationSelectDto dto) {
        IWrapper<MaterialPreparation> wrapper = getPageWrapper(dto);

        wrapper.in("mp", MaterialPreparation::getId, dto.getPreparationIdList());

        return this.baseMapper.getList(wrapper);

    }

}
