package com.shkj.wms.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.shkj.common.core.domain.Result;
import com.shkj.common.utils.SecurityUtils;
import com.shkj.wms.bo.BaseLogicareaVmQueryListBo;
import com.shkj.wms.bo.BaseLogicareaVmSaveOrUpdateBo;
import com.shkj.wms.config.ValidList;
import com.shkj.wms.domain.BaseDerive;
import com.shkj.wms.domain.BaseLogicAreaDerive;
import com.shkj.wms.mapper.BaseLogicareaVmMapper;
import com.shkj.wms.service.IBaseLogicareaVmService;
import com.shkj.wms.vo.BaseLogicareaDeriveVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 逻辑区域与车型关系Service业务层处理
 *
 * @author zjx
 * @date 2023-04-19
 */
@Slf4j
@Service
public class BaseLogicAreaVmServiceImpl extends ServiceImpl<BaseLogicareaVmMapper, BaseLogicAreaDerive> implements IBaseLogicareaVmService
{
    @Autowired
    private BaseLogicareaVmMapper baseLogicareaVmMapper;


    @Override
    public Result<Boolean> saveOrUpdateBaseLogicAreaVm(ValidList<BaseLogicareaVmSaveOrUpdateBo> list) {
        List<BaseLogicAreaDerive> addList = new ArrayList<>();
        List<BaseLogicAreaDerive> updList = new ArrayList<>();

        for (BaseLogicareaVmSaveOrUpdateBo bo : list) {
            if (bo.getId() !=null){
                BaseLogicAreaDerive update =BeanUtil.toBean(bo, BaseLogicAreaDerive.class);
                updList.add(update);
            }else {
                LambdaQueryWrapper<BaseLogicAreaDerive> wrapper = new LambdaQueryWrapper<BaseLogicAreaDerive>()
                        .eq(BaseLogicAreaDerive::getLogicareaId, bo.getLogicareaId())
                        .eq(BaseLogicAreaDerive::getDeriveId,bo.getDeriveId()).last("limit 1");
                BaseLogicAreaDerive one = getOne(wrapper);
                if(one != null){
                    return  Result.err().msg("逻辑区域与派生关系已存在");
                }
                BaseLogicAreaDerive add =BeanUtil.toBean(bo, BaseLogicAreaDerive.class);
                add.setBranchId(SecurityUtils.getCurrentBranchId());
                addList.add(add);
            }
        }
        this.updateBatchById(updList);
        this.saveBatch(addList);
        return  Result.ok();

    }

    @Override
    public Result<BaseLogicAreaDerive> queryById(Long id) {
        BaseLogicAreaDerive blvm=this.baseMapper.selectById(id);
        BaseLogicareaDeriveVo vo = BeanUtil.toBean(blvm, BaseLogicareaDeriveVo.class);
        return Result.ok().data(vo);
    }



    @Override
    public List<BaseLogicareaDeriveVo> queryBaseLogicAreaVmList(BaseLogicareaVmQueryListBo bo) {
        List<BaseLogicareaDeriveVo> baseLogicAreaVms = this.baseLogicareaVmMapper.queryBaseLogicAreaVmList(bo);
        return baseLogicAreaVms;
    }


    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<BaseLogicareaDeriveVo> entity2Vo(Collection<BaseLogicAreaDerive> collection) {
        List<BaseLogicareaDeriveVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, BaseLogicareaDeriveVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<BaseLogicAreaDerive> page = (Page<BaseLogicAreaDerive>) collection;
            Page<BaseLogicareaDeriveVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }
}
