package com.fs.oms.service.impl;

import java.util.Date;
import java.util.List;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.OrderUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.oms.domain.OmsWarehouse;
import com.fs.oms.mapper.OmsWarehouseMapper;
import com.fs.oms.param.OmsStoreAddParam;
import com.fs.oms.vo.OmsStoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.oms.mapper.OmsStoreMapper;
import com.fs.oms.domain.OmsStore;
import com.fs.oms.service.IOmsStoreService;

/**
 * 网店Service业务层处理
 *
 * @author fs
 * @date 2023-05-18
 */
@Service
public class OmsStoreServiceImpl implements IOmsStoreService
{
    @Autowired
    private OmsStoreMapper omsStoreMapper;

    @Autowired
    private OmsWarehouseMapper omsWarehouseMapper;


    /**
     * 查询网店
     *
     * @param storeId 网店ID
     * @return 网店
     */
    @Override
    public OmsStore selectOmsStoreById(Long storeId)
    {
        return omsStoreMapper.selectOmsStoreById(storeId);
    }

    /**
     * 查询网店列表
     *
     * @param omsStore 网店
     * @return 网店
     */
    @Override
    public List<OmsStore> selectOmsStoreList(OmsStore omsStore)
    {
        return omsStoreMapper.selectOmsStoreList(omsStore);
    }

    @Override
    public List<OmsStoreVO> selectOmsStoreListVO(OmsStore omsStore) {
        return omsStoreMapper.selectOmsStoreListVO(omsStore);
    }

    /**
     * 新增网店
     *
     * @param omsStore 网店
     * @return 结果
     */
    @Override
    public int insertOmsStore(OmsStoreAddParam omsStore,Long userId)
    {
        omsStore.setCreateTime(DateUtils.getNowDate());
        omsStore.setStoreCode(OrderUtils.getOrderNo());//网点编码
        OmsWarehouse warehouse=new OmsWarehouse();
        warehouse.setCreateTime(new Date());
        warehouse.setWarehouseName(omsStore.getChannelWarehouseName());
        warehouse.setWarehouseType(1);//渠道仓
        warehouse.setLinkUsername(omsStore.getLinkUsername());
        warehouse.setLinkPhone(omsStore.getLinkPhone());
        warehouse.setCityIds(omsStore.getCityIds());
        warehouse.setAddress(omsStore.getAddress());



        warehouse.setCreateUserId(userId);
        omsWarehouseMapper.insertOmsWarehouse(warehouse);
        omsStore.setWarehouseId(warehouse.getWarehouseId());

        return omsStoreMapper.insertOmsStore(omsStore);
    }

    /**
     * 修改网店
     *
     * @param omsStore 网店
     * @return 结果
     */
    @Override
    public int updateOmsStore(OmsStoreAddParam omsStore)
    {
        omsStore.setUpdateTime(DateUtils.getNowDate());
        OmsWarehouse warehouse=omsWarehouseMapper.selectOmsWarehouseById(omsStore.getWarehouseId());
        warehouse.setWarehouseName(omsStore.getChannelWarehouseName());
        omsWarehouseMapper.updateOmsWarehouse(warehouse);
        return omsStoreMapper.updateOmsStore(omsStore);
    }

    /**
     * 批量删除网店
     *
     * @param storeIds 需要删除的网店ID
     * @return 结果
     */
    @Override
    public int deleteOmsStoreByIds(Long[] storeIds)
    {
        return omsStoreMapper.deleteOmsStoreByIds(storeIds);
    }

    /**
     * 删除网店信息
     *
     * @param storeId 网店ID
     * @return 结果
     */
    @Override
    public int deleteOmsStoreById(Long storeId)
    {
        return omsStoreMapper.deleteOmsStoreById(storeId);
    }
}
