package com.tuoheng.admin.service.warehouse;

import com.tuoheng.admin.entity.domain.Warehouse;
import com.tuoheng.admin.entity.request.warehouse.QueryWarehouseListRequest;
import com.tuoheng.admin.entity.request.warehouse.QueryWarehousePageListRequest;
import com.tuoheng.admin.mapper.WarehouseMapper;
import com.tuoheng.admin.service.warehouse.query.QueryWarehouseListService;
import com.tuoheng.admin.service.warehouse.query.QueryWarehousePageListService;
import com.tuoheng.admin.utils.GaodeUtil;
import com.tuoheng.common.common.BaseServiceImpl;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.system.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 仓库Service业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-02-06
 */
@Service
public class WarehouseServiceImpl extends BaseServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    @Autowired
    private QueryWarehouseListService queryWarehouseListService;

    @Autowired
    private QueryWarehousePageListService queryWarehousePageListService;

    /**
     * 查询仓库列表
     *
     * @param request 仓库
     * @return 仓库
     */
    @Override
    public JsonResult getList(QueryWarehouseListRequest request) {
        return queryWarehouseListService.getList(request);
    }

    /**
     * 查询仓库分页列表
     *
     * @param request 仓库
     * @return 仓库
     */
    @Override
    public JsonResult getPageList(QueryWarehousePageListRequest request) {
        return queryWarehousePageListService.getPageList(request);
    }

    /**
     * 查询仓库
     *
     * @param id 仓库主键
     * @return 仓库
     */
    @Override
    public JsonResult getOneById(Integer id) {
        return null;
    }

    @Override
    public JsonResult editInfo(Warehouse entity) {
        //新增
        if (StringUtils.isNull(entity.getId())) {
            if (StringUtils.isEmpty(entity.getWarehouseName())) {
                throw new ServiceException("仓库名称不能为空");
            }
            Integer tenantId = ShiroUtils.getTenantId();
            entity.setTenantId(tenantId);
            entity.setCreateUser(ShiroUtils.getUserId());
        }
        if (StringUtils.isNotEmpty(entity.getLongitude()) && StringUtils.isNotEmpty(entity.getLatitude())) {
            String gaodeAddress = GaodeUtil.getGaodeAddress(entity.getLongitude(), entity.getLatitude());
            entity.setLocation(gaodeAddress);
        }
        entity.setUpdateUser(ShiroUtils.getUserId());
        super.edit(entity);
        return JsonResult.success();
    }

}
