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.AppStockScanMapper;
import org.wcs.mapper.intf.AppStockScanService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.stockScan.QueryStockScanReq;
import org.wcs.model.po.app.AppStockScan;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppStockScanDao extends ServiceImpl<AppStockScanMapper, AppStockScan> implements AppStockScanService {

    /**
     * 分页查询仓库扫码列表
     * @param request 查询条件
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppStockScan>> queryStockScan(QueryStockScanReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppStockScan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getScanId() != null, AppStockScan::getScanId, request.getScanId())
                .like(request.getScanName() != null, AppStockScan::getScanName, request.getScanName())
                .eq(request.getScanStatus() != null, AppStockScan::getScanStatus, request.getScanStatus())
                .eq(request.getScanType() != null, AppStockScan::getScanType, request.getScanType())
                .like(request.getScanMethod() != null, AppStockScan::getScanMethod, request.getScanMethod());
        queryWrapper.orderByDesc(AppStockScan::getScanId);
        Page<AppStockScan> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppStockScan> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据ID查询
     *
     * @param scanId 扫描ID
     * @return 查询结果
     */
    @Override
    public AppStockScan queryById(String scanId) {
        try {
            return super.baseMapper.selectById(scanId);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 条件查询
     *
     * @param appStockScan 查询参数
     * @return 查询结果
     */
    @Override
    public List<AppStockScan> query(AppStockScan appStockScan) {
        LambdaQueryWrapper<AppStockScan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(appStockScan.getScanId() != null, AppStockScan::getScanId, appStockScan.getScanId())
                .eq(appStockScan.getScanName() != null, AppStockScan::getScanName, appStockScan.getScanName())
                .eq(appStockScan.getScanMethod() != null, AppStockScan::getScanMethod, appStockScan.getScanMethod())
                .eq(appStockScan.getScanStatus() != null, AppStockScan::getScanStatus, appStockScan.getScanStatus())
                .eq(appStockScan.getScanType() != null, AppStockScan::getScanType, appStockScan.getScanType())
                .eq(appStockScan.getParam1() != null, AppStockScan::getParam1, appStockScan.getParam1())
                .eq(appStockScan.getParam2() != null, AppStockScan::getParam2, appStockScan.getParam2())
                .eq(appStockScan.getParam3() != null, AppStockScan::getParam3, appStockScan.getParam3())
                .eq(appStockScan.getPlcId() != null, AppStockScan::getPlcId, appStockScan.getPlcId())
                .eq(appStockScan.getReadStatusAddress() != null, AppStockScan::getReadStatusAddress, appStockScan.getReadStatusAddress())
                .eq(appStockScan.getWriteTaskAddress() != null, AppStockScan::getWriteTaskAddress, appStockScan.getWriteTaskAddress())
                .eq(appStockScan.getLedNo() != null, AppStockScan::getLedNo, appStockScan.getLedNo());
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 查询所有数据
     * @return 所有数据
     */
    @Override
    public List<AppStockScan> queryAll() {
        try {
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 插入数据
     * @param appStockScan 要插入的数据
     * @return 插入结果
     */
    @Override
    public int insert(AppStockScan appStockScan) {
        appStockScan.setCreateTime(LocalDateTime.now());
        appStockScan.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appStockScan) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 更新数据
     * @param appStockScan 要更新的数据
     * @return 更新结果
     */
    @Override
    public int update(AppStockScan appStockScan) {
        LambdaUpdateWrapper<AppStockScan> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockScan::getScanId, appStockScan.getScanId());
        updateWrapper.set(appStockScan.getScanName() != null, AppStockScan::getScanName, appStockScan.getScanName())
                .set(appStockScan.getScanMethod() != null, AppStockScan::getScanMethod, appStockScan.getScanMethod())
                .set(appStockScan.getScanStatus() != null, AppStockScan::getScanStatus, appStockScan.getScanStatus())
                .set(appStockScan.getScanType() != null, AppStockScan::getScanType, appStockScan.getScanType())
                .set(appStockScan.getParam1() != null, AppStockScan::getParam1, appStockScan.getParam1())
                .set(appStockScan.getParam2() != null, AppStockScan::getParam2, appStockScan.getParam2())
                .set(appStockScan.getParam3() != null, AppStockScan::getParam3, appStockScan.getParam3())
                .set(appStockScan.getPlcId() != null, AppStockScan::getPlcId, appStockScan.getPlcId())
                .set(appStockScan.getReadStatusAddress() != null, AppStockScan::getReadStatusAddress, appStockScan.getReadStatusAddress())
                .set(appStockScan.getWriteTaskAddress() != null, AppStockScan::getWriteTaskAddress, appStockScan.getWriteTaskAddress())
                .set(appStockScan.getLedNo() != null, AppStockScan::getLedNo, appStockScan.getLedNo())
                .set(appStockScan.getRemark() != null, AppStockScan::getRemark, appStockScan.getRemark())
                .set(AppStockScan::getUpdateTime, LocalDateTime.now());
        try {
            return super.update(updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除仓库扫码信息
     * @param stockScanId 仓库扫码ID
     * @return 删除结果
     */
    @Override
    public int deleteById(String stockScanId) {
        try {
            return super.removeById(stockScanId) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }
}
