package com.ruoyi.app.service.impl;

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

import com.ruoyi.app.domain.AppPendingStorage;
import com.ruoyi.app.domain.AppPmsOrderIn;
import com.ruoyi.app.domain.DTO.PmsInData;
import com.ruoyi.app.domain.DTO.PmsStockInCompleteReq;
import com.ruoyi.app.mapper.AppPendingStorageMapper;
import com.ruoyi.app.mapper.AppPmsOrderInMapper;
import com.ruoyi.app.service.IAppPmsOrderInService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@Service
public class AppPmsOrderInServiceImpl implements IAppPmsOrderInService
{
    @Autowired
    private AppPmsOrderInMapper appPmsOrderInMapper;
    @Autowired
    private AppPendingStorageMapper appPendingStorageMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param listId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AppPmsOrderIn selectAppPmsOrderInByListId(String listId)
    {
        return appPmsOrderInMapper.selectAppPmsOrderInByListId(listId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param appPmsOrderIn 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AppPmsOrderIn> selectAppPmsOrderInList(AppPmsOrderIn appPmsOrderIn)
    {
        return appPmsOrderInMapper.selectAppPmsOrderInList(appPmsOrderIn);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param appPmsOrderIn 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAppPmsOrderIn(AppPmsOrderIn appPmsOrderIn)
    {
        return appPmsOrderInMapper.insertAppPmsOrderIn(appPmsOrderIn);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param appPmsOrderIn 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAppPmsOrderIn(AppPmsOrderIn appPmsOrderIn)
    {
        return appPmsOrderInMapper.updateAppPmsOrderIn(appPmsOrderIn);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param listIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAppPmsOrderInByListIds(String[] listIds)
    {
        return appPmsOrderInMapper.deleteAppPmsOrderInByListIds(listIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param listId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAppPmsOrderInByListId(String listId)
    {
        return appPmsOrderInMapper.deleteAppPmsOrderInByListId(listId);
    }

    @Override
    public List<AppPmsOrderIn> getInfoWithString(String param) {
        if(StringUtils.isEmpty(param)) {
            return appPmsOrderInMapper.selectAppPmsOrderInListWithParam("");
        }
        return appPmsOrderInMapper.selectAppPmsOrderInListWithParam(param);
    }

    @Override
    @Transactional
    public int pmsStockInComplete(PmsStockInCompleteReq request) {
        List<PmsInData> pmsInData = request.getPmsInData();
        if(StringUtils.isEmpty(request.getVehicleNo()) || pmsInData.isEmpty()) {
            return 0;
        }
        int insertNum = 0;
        for (PmsInData pmsInDatum : pmsInData) {
            // 查询订单数据
            AppPmsOrderIn appPmsOrderIn = new AppPmsOrderIn();
            appPmsOrderIn.setListId(pmsInDatum.getListId());
            List<AppPmsOrderIn> appPmsOrderIns = appPmsOrderInMapper.selectAppPmsOrderInList(appPmsOrderIn);
            if(appPmsOrderIns == null || appPmsOrderIns.isEmpty()) {
                continue;
            }
            // 将订单数据插入码盘表
            AppPmsOrderIn appPmsOrderInInsert = appPmsOrderIns.get(0);
            AppPendingStorage appPendingStorage = new AppPendingStorage();
            BeanUtils.copyProperties(appPmsOrderInInsert, appPendingStorage);
            appPendingStorage.setRecordId(IdUtils.randomUUID());
            appPendingStorage.setVehicleNo(request.getVehicleNo());
            appPendingStorage.setGoodsNum(pmsInDatum.getGoodsNum());
            appPendingStorage.setStatus(1);
            appPendingStorage.setStorageType(2);
            appPendingStorage.setCreateTime(DateUtils.getNowDate());
            appPendingStorage.setUpdateTime(DateUtils.getNowDate());
            appPendingStorage.setCreatePerson("PDA");
            appPendingStorageMapper.insertAppPendingStorage(appPendingStorage);
            // 变更订单剩余数量
            AppPmsOrderIn update = new AppPmsOrderIn();
            update.setListId(pmsInDatum.getListId());
            update.setRemainingNum(appPmsOrderInInsert.getRemainingNum().subtract(pmsInDatum.getGoodsNum()));
            appPmsOrderInMapper.updateAppPmsOrderIn(update);
            insertNum ++;
        }
        return insertNum;
    }
}
