package com.hzs.system.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.domain.system.base.BdStorehouse;
import com.hzs.common.domain.system.base.BdStorehouseAreaMiddle;
import com.hzs.system.base.mapper.BdStorehouseAreaMiddleMapper;
import com.hzs.system.base.service.IBdStorehouseAreaMiddleService;
import com.hzs.system.base.vo.BdStorehouseAreaMiddleVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库地区中间表 服务实现类
 */
@Service
public class BdStorehouseAreaMiddleServiceImpl extends ServiceImpl<BdStorehouseAreaMiddleMapper, BdStorehouseAreaMiddle> implements IBdStorehouseAreaMiddleService {

    @Override
    public List<BdStorehouseAreaMiddleVO> getBdStorehouseAreaMiddleList(BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        return baseMapper.getBdStorehouseAreaMiddleList(storehouseAreaMiddleVO);
    }

    @Override
    public boolean intStoArMiddle(BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        BdStorehouseAreaMiddle sto = BeanUtil.copyProperties(storehouseAreaMiddleVO, BdStorehouseAreaMiddle.class);
        for (Integer i : storehouseAreaMiddleVO.getProvinceList()) {
            sto.setProvince(i);
            baseMapper.insert(sto);
        }
        return true;
    }

    @Override
    public boolean upStoArMiddle(BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        BdStorehouseAreaMiddle sto = BeanUtil.copyProperties(storehouseAreaMiddleVO, BdStorehouseAreaMiddle.class);
        sto.setPkStorehouse(storehouseAreaMiddleVO.getPkStorehouse());
        for (Integer i : storehouseAreaMiddleVO.getProvinceList()) {
            sto.setProvince(i);
            baseMapper.updateById(sto);
        }
        return true;
    }

    @Override
    public BdStorehouse queryBdStorehouseByProvince(Integer province, Integer type) {
        List<BdStorehouse> bdStorehouseList = baseMapper.queryBdStorehouseByProvince(province, type);
        if (CollectionUtil.isNotEmpty(bdStorehouseList)) {
            return bdStorehouseList.get(0);
        }
        return null;
    }

}
