package com.foreverwin.mesnac.meapi.service.impl;

import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.InventoryAssyData;
import com.foreverwin.mesnac.meapi.mapper.InventoryAssyDataMapper;
import com.foreverwin.mesnac.meapi.service.InventoryAssyDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Leon.L
 * @since 2021-07-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InventoryAssyDataServiceImpl extends ServiceImpl<InventoryAssyDataMapper, InventoryAssyData> implements InventoryAssyDataService {


    @Autowired
    private InventoryAssyDataMapper inventoryAssyDataMapper;

    @Override
    public IPage<InventoryAssyData> selectPage(FrontPage<InventoryAssyData> frontPage, InventoryAssyData inventoryAssyData) {
        QueryWrapper<InventoryAssyData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(inventoryAssyData);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<InventoryAssyData> selectList(InventoryAssyData inventoryAssyData) {
        QueryWrapper<InventoryAssyData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(inventoryAssyData);
        return super.list(queryWrapper);
    }

    @Override
    public void saveOrUpdateInventoryData(String inventoryBo, String dataField, String dataAttr) {
        Map<String, Object> map = new HashMap<>();
        map.put(InventoryAssyData.INVENTORY_BO, inventoryBo);
        List<InventoryAssyData> list = inventoryAssyDataMapper.selectByMap(map);
        if (list == null || list.size() <= 0) {
            Long sequence = new Long(10);

            InventoryAssyData inventoryData = new InventoryAssyData();
            inventoryData.setHandle("InventoryAssyDataBO:" + inventoryBo + "," + dataField);
            inventoryData.setInventoryBo(inventoryBo);
            inventoryData.setSequence(sequence);
            inventoryData.setDataField(dataField);
            inventoryData.setDataAttr(dataAttr);
            this.save(inventoryData);
        } else {
            Boolean isExist = false;

            for (int i = 0; i < list.size(); i++) {
                InventoryAssyData inventoryAssyData = list.get(i);
                if (dataField.equals(inventoryAssyData.getDataField())) {
                    inventoryAssyData.setDataAttr(dataAttr);
                    this.updateById(inventoryAssyData);

                    isExist = true; break;
                }
            }

            if (!isExist) {
                Long sequence = new Long(list.size()+1);

                InventoryAssyData inventoryData = new InventoryAssyData();
                inventoryData.setHandle("InventoryAssyDataBO:" + inventoryBo + "," + dataField);
                inventoryData.setInventoryBo(inventoryBo);
                inventoryData.setSequence(sequence);
                inventoryData.setDataField(dataField);
                inventoryData.setDataAttr(dataAttr);
                this.save(inventoryData);
            }
        }
    }
}