package com.storlead.sales.service.impl.material;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.message.pojo.entity.UserMessageNoticeConfigEntity;
import com.storlead.sales.pojo.entity.customer.SysResourceEntity;
import com.storlead.sales.pojo.vo.customer.CustomerFollowUpDetailVO;
import com.storlead.sales.pojo.vo.customer.QuotationTimeListVO;
import com.storlead.sales.service.customer.SysResourceService;
import com.storlead.sales.pojo.entity.material.MaterialEntryRecordEntity;
import com.storlead.sales.pojo.vo.material.MaterialEntryVO;
import com.storlead.sales.mapper.material.MaterialEntryRecordMapper;
import com.storlead.sales.service.material.MaterialEntryRecordService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 物料进场记录 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2025-05-16
 */
@Service
public class MaterialEntryRecordServiceImpl extends MyBaseServiceImpl<MaterialEntryRecordMapper, MaterialEntryRecordEntity> implements MaterialEntryRecordService {

    @Resource
    private SysResourceService sysResourceService;

    @Override
    public MaterialEntryVO getVOById(Long id) {

        LambdaQueryWrapper<SysResourceEntity> resourceWrapper = new LambdaQueryWrapper<>();
        resourceWrapper.eq(SysResourceEntity::getResourceType, "1000");
        resourceWrapper.eq(SysResourceEntity::getResourceTypeId,id);
        resourceWrapper.eq(SysResourceEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<SysResourceEntity> sysResourceList = sysResourceService.list(resourceWrapper);
        MaterialEntryRecordEntity entity =  this.getById(id);
        MaterialEntryVO entryVO = new MaterialEntryVO();
        BeanUtils.copyProperties(entity,entryVO);
        if (!CollectionUtils.isEmpty(sysResourceList)) {
            entryVO.setResourceIdList(sysResourceList.stream().map(SysResourceEntity::getId).collect(Collectors.toList()));
            entryVO.setResourceList(sysResourceList);

        }

        return entryVO;
    }

    @Override
    public Map<Long,List<SysResourceEntity>> listResource(List<Long> ids) {

        LambdaQueryWrapper<SysResourceEntity> resourceWrapper = new LambdaQueryWrapper<>();
        resourceWrapper.eq(SysResourceEntity::getResourceType, "1000");
        resourceWrapper.in(SysResourceEntity::getResourceTypeId,ids);
        resourceWrapper.eq(SysResourceEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<SysResourceEntity> sysResourceList = sysResourceService.list(resourceWrapper);
        if (CollectionUtils.isEmpty(sysResourceList)) {
            return new HashMap<>();
        }
        Map<Long, List<SysResourceEntity>> groupMap = sysResourceList.stream().collect(Collectors.groupingBy(SysResourceEntity::getResourceTypeId));
        return groupMap;
    }
}
