package com.wms_main.service.controller.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms_main.dao.ITAppStandService;
import com.wms_main.model.dto.query.StandQuery;
import com.wms_main.model.dto.request.wms.UpdateStandRequest;
import com.wms_main.model.dto.response.wms.BaseWmsApiResponse;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.po.TAppStand;
import com.wms_main.model.vo.wms.PageVo;
import com.wms_main.model.vo.wms.StandVo;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.service.controller.IStandControllerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 站台控制器服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class StandControllerServiceImpl implements IStandControllerService {

    private final ITAppStandService standService;

    /**
     * 获取所有站台信息
     *
     * @param standQuery 查询参数
     * @return 站台信息列表
     */
    @Override
    public WmsApiResponse<List<StandVo>> getAllStands(StandQuery standQuery) {
        log.info("开始获取所有站台信息，参数：{}", standQuery);
        try {
            // 构建查询条件
            LambdaQueryWrapper<TAppStand> queryWrapper = new LambdaQueryWrapper<>();
            
            // 添加站台ID条件
            if (StringUtils.isNotEmpty(standQuery.getStandId())) {
                queryWrapper.like(TAppStand::getStandId, standQuery.getStandId());
            }
            
            // 添加站台类型条件
            if (standQuery.getStandType() != null) {
                queryWrapper.eq(TAppStand::getStandType, standQuery.getStandType());
            }
            
            // 执行查询
            List<TAppStand> standList = standService.list(queryWrapper);
            
            // 转换为VO对象
            List<StandVo> standVoList = standList.stream()
                    .map(StandVo::of)
                    .collect(Collectors.toList());
            
            return WmsApiResponse.success("获取站台信息成功", standVoList);
        } catch (Exception e) {
            log.error("获取站台信息异常", e);
            return WmsApiResponse.error("获取站台信息异常：" + e.getMessage(), null);
        }
    }

    /**
     * A更新站台信息
     *
     * @param request 更新请求
     * @return 更新结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseWmsApiResponse updateStandInfo(UpdateStandRequest request) {
        log.info("开始更新站台信息，参数：{}", request);
        try {
            // 参数校验
            if (request == null) {
                return BaseWmsApiResponse.error("更新参数不能为空");
            }
            
            if (StringUtils.isEmpty(request.getStandId())) {
                return BaseWmsApiResponse.error("站台ID不能为空");
            }
            
            // 查询站台是否存在
            TAppStand stand = standService.getById(request.getStandId());
            if (stand == null) {
                return BaseWmsApiResponse.error("站台不存在");
            }
            
            // 设置需要更新的字段
            stand.setStandType(request.getStandType());
            stand.setStandStatus(request.getStandStatus());
            stand.setStandName(request.getStandName());
            stand.setStandIp(request.getStandIp());
            stand.setStandDesc(request.getStandDesc());
            
            // 设置更新时间和更新人
            stand.setLastUpdateTime(LocalDateTime.now());
            stand.setLastUpdateUser(request.getUserName());
            
            // 更新站台信息
            boolean result = standService.updateById(stand);
            
            if (result) {
                log.info("站台信息更新成功，ID：{}", request.getStandId());
                return BaseWmsApiResponse.success("更新站台信息成功");
            } else {
                log.warn("站台信息更新失败，ID：{}", request.getStandId());
                return BaseWmsApiResponse.error("更新站台信息失败");
            }
        } catch (Exception e) {
            log.error("更新站台信息异常", e);
            return BaseWmsApiResponse.error("更新站台信息异常：" + e.getMessage());
        }
    }

    /**
     * 关闭站台
     *
     * @param request 关闭请求
     * @return 关闭结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseWmsApiResponse closeStandInfo(UpdateStandRequest request) {
        log.info("开始关闭站台，参数：{}", request);
        try {
            // 参数校验
            if (request == null) {
                return BaseWmsApiResponse.error("关闭参数不能为空");
            }
            
            if (StringUtils.isEmpty(request.getStandId())) {
                return BaseWmsApiResponse.error("站台ID不能为空");
            }
            
            // 查询站台是否存在
            TAppStand stand = standService.getById(request.getStandId());
            if (stand == null) {
                return BaseWmsApiResponse.error("站台不存在");
            }
            
            // 设置站台状态为不可用
            stand.setStandStatus(1);
            stand.setLastUpdateTime(LocalDateTime.now());
            stand.setLastUpdateUser(request.getUserName());
            
            // 更新站台信息
            boolean result = standService.updateById(stand);
            
            if (result) {
                log.info("站台关闭成功，ID：{}", request.getStandId());
                return BaseWmsApiResponse.success("关闭站台成功");
            } else {
                log.warn("站台关闭失败，ID：{}", request.getStandId());
                return BaseWmsApiResponse.error("关闭站台失败");
            }
        } catch (Exception e) {
            log.error("关闭站台异常", e);
            return BaseWmsApiResponse.error("关闭站台异常：" + e.getMessage());
        }
    }
}
