package com.cool.store.service.store.impl;

import com.cool.store.dao.RegionDao;
import com.cool.store.dao.StoreDao;
import com.cool.store.dao.store.StoreMaterialBlacklistDAO;
import com.cool.store.entity.RegionDO;
import com.cool.store.entity.StoreDO;
import com.cool.store.entity.store.StoreMaterialBlacklistDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.store.StoreMaterialBlacklistQueryRequest;
import com.cool.store.service.dict.impl.DictService;
import com.cool.store.service.store.StoreMaterialBlacklistService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.vo.store.StoreMaterialBlacklistVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 门店物料黑名单服务实现
 * @author wangff
 * @date 2026-03-30
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class StoreMaterialBlacklistServiceImpl implements StoreMaterialBlacklistService {

    private final StoreMaterialBlacklistDAO storeMaterialBlacklistDAO;
    private final StoreDao storeDao;
    private final RegionDao regionDao;
    private final DictService dictService;


    @Override
    public PageInfo<StoreMaterialBlacklistVO> pageByStoreId(StoreMaterialBlacklistQueryRequest request) {
        StoreDO storeDO = storeDao.getByStoreId(request.getStoreId());
        if (Objects.isNull(storeDO)) {
            throw new ServiceException(ErrorCodeEnum.STORE_NOT_FIND);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<StoreMaterialBlacklistDO> list = storeMaterialBlacklistDAO.getByStoreId(request.getStoreId());
        PageInfo<StoreMaterialBlacklistDO> page = new PageInfo<>(list);

        PageInfo<StoreMaterialBlacklistVO> result = BeanUtil.toPage(page, StoreMaterialBlacklistVO.class);
        RegionDO branch = regionDao.getRegionById(storeDO.getBranch());
        for (StoreMaterialBlacklistVO vo : result.getList()) {
            vo.setStoreNum(storeDO.getStoreNum());
            vo.setStoreName(storeDO.getStoreName());
            if (Objects.nonNull(branch)) {
                vo.setBranchName(branch.getName());
            }
        }
        dictService.fillDictField(result.getList());
        return result;
    }
}