package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppEtagTagLocationMapper;
import org.wcs.mapper.intf.AppEtagTagLocationService;
import org.wcs.model.bo.db.DataBaseActionResult;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagBase.EtagLocationQueryReq;
import org.wcs.model.po.app.AppEtagTagLocation;

import java.util.List;

/**
 * 电子标签库位表持久层实现类
 */
@Service
public class AppEtagTagLocationDao extends ServiceImpl<AppEtagTagLocationMapper, AppEtagTagLocation> implements AppEtagTagLocationService {

    /**
     * 查询电子标签库位信息
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public Tuple2<Long, List<AppEtagTagLocation>> queryEtagTagLocation(EtagLocationQueryReq request, Integer pageIndex, Integer pageSize) {
        if(request == null || pageIndex == null || pageSize == null || pageIndex <= 0 || pageSize <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTagLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getLocation() != null, AppEtagTagLocation::getLocation, request.getLocation())
                .like(request.getTagName() != null, AppEtagTagLocation::getTagName, request.getTagName())
                .like(request.getLocationType() != null, AppEtagTagLocation::getLocationType, request.getLocationType())
                .like(request.getStand() != null, AppEtagTagLocation::getStand, request.getStand());
        queryWrapper.orderByAsc(AppEtagTagLocation::getLocation);
        Page<AppEtagTagLocation> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppEtagTagLocation> etagTagLocationPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(etagTagLocationPage.getTotal(), etagTagLocationPage.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询电子标签库位信息
     * @param location 位置
     * @param locationType 位置类型
     * @return 响应
     */
    @Override
    public List<AppEtagTagLocation> queryWithLocationAndType(String location, Integer locationType) {
        if(location == null || locationType == null) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTagLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagTagLocation::getLocation, location)
                .eq(AppEtagTagLocation::getLocationType, locationType);
        queryWrapper.orderByAsc(AppEtagTagLocation::getLocation);
        try {
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加电子标签库位信息
     * @param appEtagTagLocation 添加参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> insert(AppEtagTagLocation appEtagTagLocation) {
        try {
            return new DataBaseActionResult<>(baseMapper.insert(appEtagTagLocation));
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 查询指定记录ID的电子标签库位信息
     * @param recordId 记录ID
     * @return 响应
     */
    @Override
    public List<AppEtagTagLocation> queryWithRecordId(String recordId) {
        if(recordId == null) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTagLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagTagLocation::getRecordId, recordId);
        queryWrapper.orderByAsc(AppEtagTagLocation::getLocation);
        try {
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询所有电子标签库位信息
     * @return 响应
     */
    @Override
    public List<AppEtagTagLocation> queryAll() {
        try {
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 修改电子标签库位信息
     * @param appEtagTagLocation 修改参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> updateAll(AppEtagTagLocation appEtagTagLocation) {
        if(appEtagTagLocation == null || appEtagTagLocation.getRecordId() == null) {
            return new DataBaseActionResult<>(0, new Exception("传入的数据不正确"));
        }
        LambdaUpdateWrapper<AppEtagTagLocation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppEtagTagLocation::getRecordId, appEtagTagLocation.getRecordId());
        updateWrapper.set(AppEtagTagLocation::getLocation, appEtagTagLocation.getLocation())
                .set(AppEtagTagLocation::getTagName, appEtagTagLocation.getTagName())
                .set(AppEtagTagLocation::getLocationType, appEtagTagLocation.getLocationType())
                .set(AppEtagTagLocation::getStand, appEtagTagLocation.getStand())
                .set(AppEtagTagLocation::getRemark, appEtagTagLocation.getRemark());
        try {
            return new DataBaseActionResult<>(baseMapper.update(appEtagTagLocation, updateWrapper));
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 删除指定记录ID的电子标签库位信息
     * @param recordId 记录ID
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> deleteWithRecordId(String recordId) {
        if(recordId == null) {
            return new DataBaseActionResult<>(0, new Exception("传入的数据不正确"));
        }
        LambdaQueryWrapper<AppEtagTagLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagTagLocation::getRecordId, recordId);
        try {
            return new DataBaseActionResult<>(baseMapper.delete(queryWrapper));
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }


}
