
package com.tmerclub.cloud.product.feign;

import com.tmerclub.cloud.api.product.dto.WarehouseSearchDTO;
import com.tmerclub.cloud.api.product.feign.WarehouseFeignClient;
import com.tmerclub.cloud.common.order.vo.WarehouseVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.api.product.dto.WarehouseDTO;
import com.tmerclub.cloud.product.service.WarehouseService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @author tmerclub
 * @date 2020/12/14
 */
@DubboService
public class WarehouseFeignController implements WarehouseFeignClient {
    private static final Logger logger = LoggerFactory.getLogger(WarehouseFeignController.class);

    @Autowired
    private WarehouseService warehouseService;


    @Override
    public ServerResponseEntity<Void> createWarehouseByShopId(Long shopId, Integer sysType, String phone, String name) {
        warehouseService.createWarehouseByShopId(shopId, sysType, phone, name);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Long> getDefaultIdByShopId(Long shopId,Integer sysType) {
        WarehouseVO warehouseVO = warehouseService.getDefaultWarehouseByShopId(shopId,sysType);
        return ServerResponseEntity.success(warehouseVO.getWarehouseId());
    }

    @Override
    public ServerResponseEntity<Boolean> isDefaultWarehouse(Long warehouseId) {
        WarehouseVO warehouseVO = warehouseService.getByWarehouseId(warehouseId);
        return ServerResponseEntity.success(Objects.equals(warehouseVO.getType(), 0));
    }

    @Override
    public WarehouseVO getWarehouseById(Long warehouseId) {
        return warehouseService.getByWarehouseId(warehouseId);
    }

    @Override
    public List<WarehouseVO> listWarehouse(WarehouseSearchDTO warehouseSearchDTO) {
        WarehouseDTO warehouseDTO = new WarehouseDTO();
        BeanUtils.copyProperties(warehouseSearchDTO, warehouseDTO);
        return warehouseService.listWarehouse(warehouseDTO);
    }

    @Override
    public List<WarehouseVO> listCanDeliveryWarehouse(WarehouseSearchDTO warehouseSearchDTO) {
        WarehouseDTO warehouseDTO = new WarehouseDTO();
        BeanUtils.copyProperties(warehouseSearchDTO, warehouseDTO);
        return warehouseService.listCanDeliveryWarehouse(warehouseDTO);
    }

    @Override
    public List<WarehouseVO> listWarehouseByShopId(Long shopId, Integer systype) {
        List<WarehouseVO> warehouseList = warehouseService.listWarehouseByShopId(shopId, systype);
        return warehouseList;
    }

    @Override
    public ServerResponseEntity<Void> insertWarehouseAndArea(WarehouseDTO warehouseDTO) {
        warehouseService.insertWarehouseAndArea(warehouseDTO);
        // 清除缓存
        warehouseService.removeWarehouseCache(warehouseDTO.getWarehouseId(), warehouseDTO.getShopId(), warehouseDTO.getSysType());
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateWarehouseAndArea(WarehouseDTO warehouseDTO) {
        warehouseService.updateWarehouseAndArea(warehouseDTO);
        // 清除缓存
        warehouseService.removeWarehouseCache(warehouseDTO.getWarehouseId(), warehouseDTO.getShopId(), warehouseDTO.getSysType());
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> deleteWarehouseAndArea(Long warehouseId) {
        WarehouseVO warehouseVO = warehouseService.deleteWarehouseAndArea(warehouseId);
        warehouseService.removeWarehouseCache(warehouseVO.getWarehouseId(), warehouseVO.getShopId(), warehouseVO.getSysType());
        return ServerResponseEntity.success();
    }

}
