package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.CommonDao;
import com.rongwei.bscommon.sys.dao.EquOverhaulInventoryMapperDao;
import com.rongwei.bscommon.sys.service.EquOverhaulInventoryService;
import com.rongwei.bscommon.sys.service.SlaveSourceService;
import com.rongwei.bscommon.sys.utils.CommonUtils;
import com.rongwei.bscommon.sys.utils.StringUtil;
import com.rongwei.bsentity.domain.EquOverhaulInventoryDo;
import com.rongwei.bsentity.domain.VInventoryVo;
import com.rongwei.rwadmincommon.system.domain.SysOrganizationDo;
import com.rongwei.rwadmincommon.system.vo.SysOrganizationVo;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.utils.SecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 设备子系统——备品备件库存 服务实现类
 * </p>
 *
 * @author fpy
 * @since 2023-11-22
 */
@Service
public class EquOverhaulInventoryServiceImpl extends ServiceImpl<EquOverhaulInventoryMapperDao, EquOverhaulInventoryDo> implements EquOverhaulInventoryService {

    private static final Logger logger = LoggerFactory.getLogger(EquOverhaulInventoryServiceImpl.class);

    @Autowired
    private EquOverhaulInventoryMapperDao equOverhaulInventoryMapperDao;

    @Autowired
    private EquOverhaulInventoryService equOverhaulInventoryService;

    @Autowired
    private SlaveSourceService slaveSourceService;

    @Autowired
    private CommonDao commonDao;

    @Override
    public void updateInventoryReminderStatus() {
        equOverhaulInventoryMapperDao.updateInventoryReminderStatus();
    }

    @Override
    public void initializeInventory() {
        // 用于存储新增的库存数据
        List<EquOverhaulInventoryDo> equOverhaulInventoryDos = new ArrayList<>();
        // 获取创新所有库存
        List<VInventoryVo> allOracleInventory = slaveSourceService.getAllOracleInventory();
        // 查询所有工厂数据
        List<SysOrganizationVo> factoryList = commonDao.getAllFactoryNameAndId();
        // 查询库存表已有数据列表
        List<EquOverhaulInventoryDo> queryInventoryDos = equOverhaulInventoryService.selectAllInventory();
        for (VInventoryVo inventoryVo : allOracleInventory) {
            EquOverhaulInventoryDo equOverhaulInventoryDo = new EquOverhaulInventoryDo();
            List<EquOverhaulInventoryDo> resultInventoryList = queryInventoryDos.stream().filter(p -> p.getAccessorycode().equals(inventoryVo.getItem_num())).collect(Collectors.toList());
            if (resultInventoryList.size()==0){
                String uuid = SecurityUtil.getUUID();
                equOverhaulInventoryDo.setId(uuid);
                equOverhaulInventoryDo.setAccessory(inventoryVo.getProduct_name());
                equOverhaulInventoryDo.setBelongfactory(inventoryVo.getOrg_name());
                equOverhaulInventoryDo.setAccessorycode(inventoryVo.getItem_num());
                equOverhaulInventoryDo.setSpecifications(inventoryVo.getSpec());
                equOverhaulInventoryDo.setUnit(inventoryVo.getUnit());
                equOverhaulInventoryDo.setSource(inventoryVo.getCangku());
                equOverhaulInventoryDo.setBatch(inventoryVo.getBatch_num());
                List<SysOrganizationVo> resultList = factoryList.stream().filter(p -> p.getFullname().equals(inventoryVo.getOrg_name())).collect(Collectors.toList());
                equOverhaulInventoryDo.setTenantid(resultList.get(0).getId());
                if (inventoryVo.getQuantity()!= null){
                    equOverhaulInventoryDo.setInventorynumber(BigDecimal.valueOf(inventoryVo.getQuantity()));
                }
                equOverhaulInventoryDos.add(equOverhaulInventoryDo);
            }else {
                equOverhaulInventoryDo.setModifydate(new Date());
                equOverhaulInventoryDo.setId(resultInventoryList.get(0).getId());
                equOverhaulInventoryDo.setAccessory(inventoryVo.getProduct_name());
                equOverhaulInventoryDo.setBelongfactory(inventoryVo.getOrg_name());
                equOverhaulInventoryDo.setAccessorycode(inventoryVo.getItem_num());
                equOverhaulInventoryDo.setSpecifications(inventoryVo.getSpec());
                equOverhaulInventoryDo.setUnit(inventoryVo.getUnit());
                equOverhaulInventoryDo.setSource(inventoryVo.getCangku());
                equOverhaulInventoryDo.setBatch(inventoryVo.getBatch_num());
                List<SysOrganizationVo> resultList = factoryList.stream().filter(p -> p.getFullname().equals(inventoryVo.getOrg_name())).collect(Collectors.toList());
                equOverhaulInventoryDo.setTenantid(resultList.get(0).getId());
                if (inventoryVo.getQuantity()!= null){
                    equOverhaulInventoryDo.setInventorynumber(BigDecimal.valueOf(inventoryVo.getQuantity()));
                }
                equOverhaulInventoryDos.add(equOverhaulInventoryDo);
            }
        }
        // 数据保存到库存表中
        equOverhaulInventoryService.saveOrUpdateBatch(equOverhaulInventoryDos);
    }

    @Override
    public List<EquOverhaulInventoryDo> selectAllInventory() {
        return equOverhaulInventoryMapperDao.selectAllInventory();
    }
}
