package com.ximai.mes.wm.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ximai.common.constant.UserConstants;
import com.ximai.common.utils.data.DateUtils;
import com.ximai.common.utils.data.StringUtils;
import com.ximai.mes.md.domain.MdClient;
import com.ximai.mes.remote.ErpService;
import com.ximai.mes.remote.dto.MdClientErpDto;
import com.ximai.mes.remote.dto.MdClientErpQuery;
import com.ximai.mes.remote.dto.WmWarehouseErpDto;
import com.ximai.mes.remote.dto.WmWarehouseErpQuery;
import com.ximai.mes.wm.domain.WmStorageArea;
import com.ximai.mes.wm.domain.WmStorageLocation;
import com.ximai.mes.wm.mapper.WmStorageAreaMapper;
import com.ximai.mes.wm.mapper.WmStorageLocationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ximai.mes.wm.mapper.WmWarehouseMapper;
import com.ximai.mes.wm.domain.WmWarehouse;
import com.ximai.mes.wm.service.IWmWarehouseService;

/**
 * 仓库设置Service业务层处理
 *
 * @date 2022-05-07
 */
@Service
public class WmWarehouseServiceImpl implements IWmWarehouseService
{
    @Autowired
    private WmWarehouseMapper wmWarehouseMapper;

    @Autowired
    private WmStorageLocationMapper wmStorageLocationMapper;

    @Autowired
    private WmStorageAreaMapper wmStorageAreaMapper;
    @Autowired
    private ErpService erpService;

    /**
     * 查询仓库设置
     *
     * @param warehouseId 仓库设置主键
     * @return 仓库设置
     */
    @Override
    public WmWarehouse selectWmWarehouseByWarehouseId(Long warehouseId)
    {
        return wmWarehouseMapper.selectWmWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public WmWarehouse selectWmWarehouseByWarehouseCode(String warehouseCdoe) {
        return wmWarehouseMapper.selectWmWarehouseByWarehouseCode(warehouseCdoe);
    }

    /**
     * 查询仓库设置列表
     *
     * @param wmWarehouse 仓库设置
     * @return 仓库设置
     */
    @Override
    public List<WmWarehouse> selectWmWarehouseList(WmWarehouse wmWarehouse)
    {
        return wmWarehouseMapper.selectWmWarehouseList(wmWarehouse);
    }

    @Override
    public List<WmWarehouse> getTreeList() {
        return wmWarehouseMapper.getTreeList();
    }

    @Override
    public String checkWarehouseCodeUnique(WmWarehouse wmWarehouse) {
        WmWarehouse warehouse = wmWarehouseMapper.checkWarehouseCodeUnique(wmWarehouse);
        Long warehouseId = wmWarehouse.getWarehouseId()==null?-1L:wmWarehouse.getWarehouseId();
        if(StringUtils.isNotNull(warehouse) && warehouse.getWarehouseId().longValue() != warehouseId.longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkWarehouseNameUnique(WmWarehouse wmWarehouse) {
        WmWarehouse warehouse = wmWarehouseMapper.checkWarehouseNameUnique(wmWarehouse);
        Long warehouseId = wmWarehouse.getWarehouseId()==null?-1L:wmWarehouse.getWarehouseId();
        if(StringUtils.isNotNull(warehouse) && warehouse.getWarehouseId().longValue() != warehouseId.longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增仓库设置
     *
     * @param wmWarehouse 仓库设置
     * @return 结果
     */
    @Override
    public int insertWmWarehouse(WmWarehouse wmWarehouse)
    {
        wmWarehouse.setCreateTime(DateUtils.getNowDate());
        return wmWarehouseMapper.insertWmWarehouse(wmWarehouse);
    }

    /**
     * 修改仓库设置
     *
     * @param wmWarehouse 仓库设置
     * @return 结果
     */
    @Override
    public int updateWmWarehouse(WmWarehouse wmWarehouse)
    {
        wmWarehouse.setUpdateTime(DateUtils.getNowDate());
        return wmWarehouseMapper.updateWmWarehouse(wmWarehouse);
    }

    /**
     * 批量删除仓库设置
     *
     * @param warehouseIds 需要删除的仓库设置主键
     * @return 结果
     */
    @Override
    public int deleteWmWarehouseByWarehouseIds(Long[] warehouseIds)
    {
        return wmWarehouseMapper.deleteWmWarehouseByWarehouseIds(warehouseIds);
    }

    /**
     * 删除仓库设置信息
     *
     * @param warehouseId 仓库设置主键
     * @return 结果
     */
    @Override
    public int deleteWmWarehouseByWarehouseId(Long warehouseId)
    {
        return wmWarehouseMapper.deleteWmWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public WmWarehouse initVirtualWarehouse() {

        WmWarehouse warehouse = wmWarehouseMapper.selectWmWarehouseByWarehouseCode(UserConstants.VIRTUAL_WH);
        if(!StringUtils.isNotNull(warehouse)){
            warehouse = new WmWarehouse();
            warehouse.setWarehouseCode(UserConstants.VIRTUAL_WH);
            warehouse.setWarehouseName("线边库-虚拟");
            wmWarehouseMapper.insertWmWarehouse(warehouse);
        }


        WmStorageLocation location = wmStorageLocationMapper.selectWmStorageLocationByLocationCode(UserConstants.VIRTUAL_WS);
        if(!StringUtils.isNotNull(location)){
            location = new WmStorageLocation();
            location.setWarehouseId(warehouse.getWarehouseId());
            location.setLocationCode(UserConstants.VIRTUAL_WS);
            location.setLocationName("线边库库区-虚拟");
            location.setAreaFlag(UserConstants.YES);
            wmStorageLocationMapper.insertWmStorageLocation(location);
        }


        WmStorageArea area = wmStorageAreaMapper.selectWmStorageAreaByAreaCode(UserConstants.VIRTUAL_WA);
        if(!StringUtils.isNotNull(area)){
            area = new WmStorageArea();
            area.setLocationId(location.getLocationId());
            area.setAreaCode(UserConstants.VIRTUAL_WA);
            area.setAreaName("线边库库位-虚拟");
            wmStorageAreaMapper.insertWmStorageArea(area);
        }

        return warehouse;
    }

    @Override
    public void syncErpData() {
        WmWarehouseErpQuery warehouseErpQuery = new WmWarehouseErpQuery();
        List<WmWarehouseErpDto> list = erpService.getWarehouseList(warehouseErpQuery).getData();
        list.forEach(s->{
            WmWarehouse wmWarehouse = new WmWarehouse();
            wmWarehouse.setWarehouseCode(s.getWarehouseCode());
            List<WmWarehouse> dbList = wmWarehouseMapper.selectWmWarehouseList(wmWarehouse);
            if(dbList.size()==0){
                if("Y".equalsIgnoreCase(s.getVerifyStatus())){
                    WmWarehouse insertTemp = BeanUtil.toBean(s, WmWarehouse.class);
                    this.insertWmWarehouse(insertTemp);
                }
            }else{
                BeanUtil.copyProperties(s, dbList.get(0));
                this.updateWmWarehouse(dbList.get(0));
            }
        });
    }
}
