package com.shkj.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.core.domain.Result;
import com.shkj.common.enums.IsDelEnum;
import com.shkj.common.utils.DateUtils;
import com.shkj.common.utils.SecurityUtils;
import com.shkj.wms.bo.BaseLocationPlcQueryPageBo;
import com.shkj.wms.bo.BaseLocationPlcSaveOrUpdateBo;
import com.shkj.wms.bo.BaseLocationSaveOrUpdateBo;
import com.shkj.wms.config.ValidList;
import com.shkj.wms.domain.BaseArea;
import com.shkj.wms.domain.BaseLocationPlc;
import com.shkj.wms.mapper.BaseLocationPlcMapper;
import com.shkj.wms.service.IBaseLocationPlcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统货位与PLC货位关系Service业务层处理
 *
 * @author zjx
 * @date 2023-04-20
 */
@Service
public class BaseLocationPlcServiceImpl extends ServiceImpl<BaseLocationPlcMapper, BaseLocationPlc> implements IBaseLocationPlcService {
    @Autowired
    private BaseLocationPlcMapper baseLocationPlcMapper;

    /**
     * 查询系统货位与PLC货位关系
     *
     * @param id 系统货位与PLC货位关系主键
     * @return 系统货位与PLC货位关系
     */
    @Override
    public BaseLocationPlc selectBaseLocationPlcById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询系统货位与PLC货位关系列表
     *
     * @param bo 系统货位与PLC货位关系
     * @return 系统货位与PLC货位关系
     */
    @Override
    public List<BaseLocationPlc> selectBaseLocationPlcList(BaseLocationPlcQueryPageBo bo) {
        return baseLocationPlcMapper.selectBaseLocationPlcList(bo);
    }

    /**
     * 新增系统货位与PLC货位关系
     *
     * @param bo 系统货位与PLC货位关系
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBaseLocationPlc(BaseLocationPlc bo) {
        LambdaQueryWrapper<BaseLocationPlc> wrapper = new LambdaQueryWrapper<BaseLocationPlc>().eq(BaseLocationPlc::getLocationCode, bo.getLocationCode())
                .eq(BaseLocationPlc::getLocationPlc, bo.getLocationPlc()).last("limit 1");
        BaseLocationPlc one = getOne(wrapper);
        if (one != null) {
            this.baseMapper.updateById(bo);
        }
        this.baseMapper.insert(bo);
        return Result.ok();
    }

    /**
     * 修改系统货位与PLC货位关系
     *
     * @param bo 系统货位与PLC货位关系
     * @return 结果
     */
    @Override
    public Result<Boolean> updateBaseLocationPlc(BaseLocationPlc bo) {
        bo.setUpdateTime(DateUtils.getNowDate());
        this.baseMapper.updateById(bo);
        return Result.ok();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> saveOrUpdateBaseLocationPlcs(ValidList<BaseLocationPlcSaveOrUpdateBo> list) {
        List<BaseLocationPlc> addList = new ArrayList<>();
        List<BaseLocationPlc> updList = new ArrayList<>();

        for (BaseLocationPlcSaveOrUpdateBo bo : list) {
            if (bo.getId() != null) {
                BaseLocationPlc one = getById(bo.getId());
                BaseLocationPlc update = BeanUtil.toBean(bo, BaseLocationPlc.class);
                updList.add(update);
            } else {
                BaseLocationPlc add = BeanUtil.toBean(bo, BaseLocationPlc.class);
                addList.add(add);
            }
        }
        this.updateBatchById(updList);
        this.saveBatch(addList);
        return Result.ok();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> deleteWithValidById(List<Long> ids) {
        this.baseMapper.deleteBatchIds(ids);
        return Result.ok();
    }

}
