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.AppConveyDeliverStandMapper;
import org.wcs.mapper.intf.AppConveyDeliverStandService;
import org.wcs.model.bo.db.DataBaseActionResult;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.conveyStand.QueryDeliverStandReq;
import org.wcs.model.po.app.AppConveyDeliverStand;

import java.util.List;

/**
 * 输送线站台数据访问实现类
 */
@Service
public class AppConveyDeliverStandStandDao extends ServiceImpl<AppConveyDeliverStandMapper, AppConveyDeliverStand> implements AppConveyDeliverStandService {

    /**
     * 查询输送线站台
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 站台列表
     */
    @Override
    public Tuple2<Long, List<AppConveyDeliverStand>> query(QueryDeliverStandReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppConveyDeliverStand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(request.getAreaId() != null, AppConveyDeliverStand::getAreaId, request.getAreaId());
        queryWrapper.like(request.getStandId() != null, AppConveyDeliverStand::getStandId, request.getStandId());
        queryWrapper.eq(request.getStandStatus() != null, AppConveyDeliverStand::getStandStatus, request.getStandStatus());
        queryWrapper.orderByAsc(AppConveyDeliverStand::getStandId);
        try {
            Page<AppConveyDeliverStand> page = new Page<>(pageIndex, pageSize);
            Page<AppConveyDeliverStand> conveyDeliverStandPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(conveyDeliverStandPage.getTotal(), conveyDeliverStandPage.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

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

    /**
     * 添加数据
     * @param appConveyDeliverStand 添加参数
     * @return 添加结果
     */
    @Override
    public DataBaseActionResult<Integer> insert(AppConveyDeliverStand appConveyDeliverStand) {
        if(appConveyDeliverStand == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        try {
            int insert = baseMapper.insert(appConveyDeliverStand);
            return new DataBaseActionResult<>(insert);
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 修改数据
     * @param appConveyDeliverStand 修改参数
     * @return 修改结果
     */
    @Override
    public DataBaseActionResult<Integer> update(AppConveyDeliverStand appConveyDeliverStand) {
        if(appConveyDeliverStand == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        LambdaUpdateWrapper<AppConveyDeliverStand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppConveyDeliverStand::getStandId, appConveyDeliverStand.getStandId());
        updateWrapper.set(AppConveyDeliverStand::getAreaId, appConveyDeliverStand.getAreaId())
                .set(AppConveyDeliverStand::getStandName, appConveyDeliverStand.getStandName())
                .set(AppConveyDeliverStand::getPlcId, appConveyDeliverStand.getPlcId())
                .set(AppConveyDeliverStand::getStandStatus, appConveyDeliverStand.getStandStatus())
                .set(AppConveyDeliverStand::getRouter, appConveyDeliverStand.getRouter())
                .set(AppConveyDeliverStand::getReadArriveAddress, appConveyDeliverStand.getReadArriveAddress())
                .set(AppConveyDeliverStand::getWriteTaskAddress, appConveyDeliverStand.getWriteTaskAddress())
                .set(AppConveyDeliverStand::getRemark, appConveyDeliverStand.getRemark());
        try {
            int update = baseMapper.update(appConveyDeliverStand, updateWrapper);
            return new DataBaseActionResult<>(update);
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 删除数据
     * @param standId 站台ID
     * @return 删除结果
     */
    @Override
    public DataBaseActionResult<Integer> delete(String standId) {
        if(standId == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        try {
            LambdaQueryWrapper<AppConveyDeliverStand> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppConveyDeliverStand::getStandId, standId);
            int delete = baseMapper.delete(queryWrapper);
            return new DataBaseActionResult<>(delete);
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }
}
