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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppConveyPickStandMapper;
import org.wcs.mapper.intf.AppConveyPickStandService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.conveyStand.QueryPickStandReq;
import org.wcs.model.po.app.AppConveyPickStand;

import java.util.List;

@Slf4j
@Service
public class AppConveyPickStandDao extends ServiceImpl<AppConveyPickStandMapper, AppConveyPickStand> implements AppConveyPickStandService {

    /**
     * 查询捡货站台
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 站台列表
     */
    @Override
    public Tuple2<Long, List<AppConveyPickStand>> queryPickStand(QueryPickStandReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppConveyPickStand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getAreaId() != null, AppConveyPickStand::getAreaId, request.getAreaId());
        queryWrapper.like(request.getStandId() != null, AppConveyPickStand::getStandId, request.getStandId());
        queryWrapper.eq(request.getStandStatus() != null, AppConveyPickStand::getStatus, request.getStandStatus());
        queryWrapper.eq(request.getStandType() != null, AppConveyPickStand::getStandType, request.getStandType());
        try {
            Page<AppConveyPickStand> page = new Page<>(pageIndex, pageSize);
            Page<AppConveyPickStand> conveyPickStandPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(conveyPickStandPage.getTotal(), conveyPickStandPage.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据站台ID查询站台
     * @param standId 站台ID
     * @return 站台列表
     */
    @Override
    public List<AppConveyPickStand> queryPickStandByStandId(String standId) {
        LambdaQueryWrapper<AppConveyPickStand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppConveyPickStand::getStandId, standId);
        queryWrapper.orderByAsc(AppConveyPickStand::getStandId);
        try {
            return super.list(queryWrapper);
        } catch ( Exception e) {
            return null;
        }
    }

    /**
     * 查询所有站台
     * @return 站台列表
     */
    @Override
    public List<AppConveyPickStand> queryAll() {
        try{
            return super.list();
        } catch ( Exception e) {
            return null;
        }
    }

    /**
     * 添加捡选站台
     * @param appConveyPickStand 添加参数
     * @return 添加结果
     */
    @Override
    public int insertPickStand(AppConveyPickStand appConveyPickStand) {
        try {
            return super.baseMapper.insert(appConveyPickStand);
        } catch (Exception e) {
            log.error("数据库插入捡选站台数据异常", e);
            return 0;
        }
    }

    /**
     * 删除捡选站台
     * @param standId 站台ID
     * @return 删除结果
     */
    @Override
    public int deletePickStand(String standId) {
        try {
            return super.removeById(standId) ? 1 : 0;
        } catch (Exception e) {
            log.error("数据库删除站台数据异常", e);
            return 0;
        }
    }

    /**
     * 修改捡选站台
     * @param appConveyPickStand 修改参数
     * @return 修改结果
     */
    @Override
    public int updatePickStand(AppConveyPickStand appConveyPickStand) {
        LambdaUpdateWrapper<AppConveyPickStand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppConveyPickStand::getStandId, appConveyPickStand.getStandId());
        updateWrapper.set(appConveyPickStand.getStandName() != null, AppConveyPickStand::getStandName, appConveyPickStand.getStandName());
        updateWrapper.set(appConveyPickStand.getPlcId() != null, AppConveyPickStand::getPlcId, appConveyPickStand.getPlcId());
        updateWrapper.set(appConveyPickStand.getAreaId() != null, AppConveyPickStand::getAreaId, appConveyPickStand.getAreaId());
        updateWrapper.set(appConveyPickStand.getStatus() != null, AppConveyPickStand::getStatus, appConveyPickStand.getStatus());
        updateWrapper.set(appConveyPickStand.getStandType() != null, AppConveyPickStand::getStandType, appConveyPickStand.getStandType());
        updateWrapper.set(appConveyPickStand.getRouter() != null, AppConveyPickStand::getRouter, appConveyPickStand.getRouter());
        updateWrapper.set(appConveyPickStand.getReadArriveAddress() != null, AppConveyPickStand::getReadArriveAddress, appConveyPickStand.getReadArriveAddress());
        updateWrapper.set(appConveyPickStand.getWriteTaskAddress() != null, AppConveyPickStand::getWriteTaskAddress, appConveyPickStand.getWriteTaskAddress());
        updateWrapper.set(appConveyPickStand.getRemark() != null, AppConveyPickStand::getRemark, appConveyPickStand.getRemark());
        try {
            return super.update(appConveyPickStand, updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            log.error("数据库修改站台数据异常", e);
            return 0;
        }
    }
}
