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.AppConveyCheckStandMapper;
import org.wcs.mapper.intf.AppConveyCheckStandService;
import org.wcs.model.bo.db.DataBaseActionResult;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.conveyStand.QueryCheckStandReq;
import org.wcs.model.po.app.AppConveyCheckStand;

import java.util.List;

/**
 * 输送线复核台数据库操作类
 */
@Slf4j
@Service
public class AppConveyCheckStandDao extends ServiceImpl<AppConveyCheckStandMapper, AppConveyCheckStand> implements AppConveyCheckStandService {

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

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

    /**
     * 添加一条数据
     * @param appConveyCheckStand 添加参数
     * @return 添加结果
     */
    @Override
    public DataBaseActionResult<Integer> insert(AppConveyCheckStand appConveyCheckStand) {
        if(appConveyCheckStand == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        try {
            int insert = baseMapper.insert(appConveyCheckStand);
            return new DataBaseActionResult<>(insert);
        } catch (Exception e) {
            log.error("复核台基础数据添加数据失败", e);
            return new DataBaseActionResult<>(0, e);
        }
    }

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

    /**
     * 修改一条数据
     * @param appConveyCheckStand 修改参数
     * @return 修改结果
     */
    @Override
    public DataBaseActionResult<Integer> update(AppConveyCheckStand appConveyCheckStand) {
        if(appConveyCheckStand == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        LambdaUpdateWrapper<AppConveyCheckStand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppConveyCheckStand::getStandId, appConveyCheckStand.getStandId());
        updateWrapper.set(AppConveyCheckStand::getAreaId, appConveyCheckStand.getAreaId());
        updateWrapper.set(AppConveyCheckStand::getStandStatus, appConveyCheckStand.getStandStatus());
        updateWrapper.set(AppConveyCheckStand::getPlcId, appConveyCheckStand.getPlcId());
        updateWrapper.set(AppConveyCheckStand::getRemark, appConveyCheckStand.getRemark());
        updateWrapper.set(AppConveyCheckStand::getStandName, appConveyCheckStand.getStandName());
        updateWrapper.set(AppConveyCheckStand::getWriteTaskAddress, appConveyCheckStand.getWriteTaskAddress());
        updateWrapper.set(AppConveyCheckStand::getReadArriveAddress, appConveyCheckStand.getReadArriveAddress());
        updateWrapper.set(AppConveyCheckStand::getRouter, appConveyCheckStand.getRouter());
        try {
            int update = baseMapper.update(appConveyCheckStand, updateWrapper);
            return new DataBaseActionResult<>(update);
        } catch (Exception e) {
            log.error("复核台基础数据修改数据失败", e);
            return new DataBaseActionResult<>(0, e);
        }
    }
}
