package com.ruoyi.app.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.app.domain.AppPendingStorage;
import com.ruoyi.app.domain.AppPendingStorageRequest;
import com.ruoyi.app.domain.DTO.OtherStockInRequest;
import com.ruoyi.app.mapper.AppPendingStorageMapper;
import com.ruoyi.app.service.IAppPendingStorageService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 入库码盘Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-22
 */
@Service
public class AppPendingStorageServiceImpl implements IAppPendingStorageService
{
    @Autowired
    private AppPendingStorageMapper appPendingStorageMapper;

    /**
     * 查询入库码盘
     *
     * @param recordId 入库码盘主键
     * @return 入库码盘
     */
    @Override
    public AppPendingStorage selectAppPendingStorageByRecordId(String recordId)
    {
        return appPendingStorageMapper.selectAppPendingStorageByRecordId(recordId);
    }

    /**
     * 查询入库码盘列表
     *
     * @param appPendingStorage 入库码盘
     * @return 入库码盘
     */
    @Override
    public List<AppPendingStorage> selectAppPendingStorageList(AppPendingStorage appPendingStorage)
    {
        return appPendingStorageMapper.selectAppPendingStorageList(appPendingStorage);
    }

    /**
     * 新增入库码盘
     *
     * @param appPendingStorage 入库码盘
     * @return 结果
     */
    @Override
    public int insertAppPendingStorage(AppPendingStorage appPendingStorage)
    {
        appPendingStorage.setCreateTime(DateUtils.getNowDate());
        return appPendingStorageMapper.insertAppPendingStorage(appPendingStorage);
    }

    /**
     * 修改入库码盘
     *
     * @param appPendingStorage 入库码盘
     * @return 结果
     */
    @Override
    public int updateAppPendingStorage(AppPendingStorage appPendingStorage)
    {
        appPendingStorage.setUpdateTime(DateUtils.getNowDate());
        return appPendingStorageMapper.updateAppPendingStorage(appPendingStorage);
    }

    /**
     * 批量删除入库码盘
     *
     * @param recordIds 需要删除的入库码盘主键
     * @return 结果
     */
    @Override
    public int deleteAppPendingStorageByRecordIds(String[] recordIds)
    {
        return appPendingStorageMapper.deleteAppPendingStorageByRecordIds(recordIds);
    }

    /**
     * 删除入库码盘信息
     *
     * @param recordId 入库码盘主键
     * @return 结果
     */
    @Override
    public int deleteAppPendingStorageByRecordId(String recordId)
    {
        return appPendingStorageMapper.deleteAppPendingStorageByRecordId(recordId);
    }

    @Override
    public int addAppEmptyPmsOrderIn(String vehicleNo) {
        AppPendingStorage appPendingStorage = new AppPendingStorage(
                UUID.randomUUID().toString(),
                UUID.randomUUID().toString(),
                1,
                "-",
                "-",
                vehicleNo,
                "0",
                BigDecimal.valueOf(1),
                "0",
                "空载具",
                "个",
                "",
                "",
                2,
                1,
                "empty",
                "test"
        );
        if (appPendingStorage.getCreateTime() == null) {
            appPendingStorage.setCreateTime(DateUtils.getNowDate());
        }
        if (appPendingStorage.getUpdateTime() == null) {
            appPendingStorage.setUpdateTime(DateUtils.getNowDate());
        }
        return appPendingStorageMapper.insertAppPendingStorage(appPendingStorage);
    }

    public int addAppListPmsOrderIn(OtherStockInRequest otherStockInRequest) {
        List<AppPendingStorage> appPendingStorageList = new ArrayList<AppPendingStorage>();
        List<AppPendingStorageRequest> appPendingStorageRequestList = otherStockInRequest.getData();
        String vehicleNo = otherStockInRequest.getVehicleNo();

        for (AppPendingStorageRequest appPendingStorageRequest : appPendingStorageRequestList) {
            AppPendingStorage appPendingStorage = new AppPendingStorage();
            appPendingStorage.setRecordId(UUID.randomUUID().toString());
            appPendingStorage.setListId(appPendingStorageRequest.getListId());
            Integer orderType = Math.toIntExact(appPendingStorageRequest.getOrderType() == null ? 0 : appPendingStorageRequest.getOrderType());
            appPendingStorage.setOrderType(orderType);
            String customerId = appPendingStorageRequest.getCustomerName() == null ? "" : appPendingStorageRequest.getCustomerName();
            appPendingStorage.setCustomerId(customerId);
            appPendingStorage.setOrderId(appPendingStorageRequest.getListId());
            appPendingStorage.setGoodsId(appPendingStorageRequest.getGoodsId());
            appPendingStorage.setGoodsNum(appPendingStorageRequest.getGoodsNum());
            appPendingStorage.setVehicleNo(vehicleNo);
            appPendingStorage.setGoodsCode(appPendingStorageRequest.getGoodsId());
            appPendingStorage.setGoodsDesc(appPendingStorageRequest.getGoodsDesc());
            String unit = appPendingStorageRequest.getUnit() == null ? "无" : appPendingStorageRequest.getUnit();
            appPendingStorage.setUnit(unit);
            appPendingStorage.setSpare1(appPendingStorageRequest.getSpare1());
            appPendingStorage.setSpare2(appPendingStorageRequest.getSpare2());
            appPendingStorage.setStatus(0);
            appPendingStorage.setStorageType(1);
            appPendingStorage.setCreatePerson("PDA");

            appPendingStorage.setCreateTime(DateUtils.getNowDate());
            appPendingStorage.setUpdateTime(DateUtils.getNowDate());
            appPendingStorageList.add(appPendingStorage);
        }
        return appPendingStorageMapper.insertAppPendingStorageList(appPendingStorageList);
    }
}
