package org.wcs.serve.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppConveyCheckStandService;
import org.wcs.mapper.intf.AppConveyDeliverStandService;
import org.wcs.mapper.intf.AppConveyPickStandService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.conveyStand.*;
import org.wcs.model.po.app.AppConveyCheckStand;
import org.wcs.model.po.app.AppConveyDeliverStand;
import org.wcs.model.po.app.AppConveyPickStand;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.model.vo.serve.common.PageDataResponse;
import org.wcs.model.vo.serve.conveyStand.ConveyCheckStandVo;
import org.wcs.model.vo.serve.conveyStand.ConveyDeliverStandVo;
import org.wcs.model.vo.serve.conveyStand.ConveyPickStandVo;
import org.wcs.serve.service.intf.IConveyStandService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.List;

/**
 * 输送线站台服务
 */
@Service
@RequiredArgsConstructor
public class ConveyStandService implements IConveyStandService {

    private final AppConveyPickStandService conveyPickStandService; // 捡选站台操作类
    private final AppConveyCheckStandService conveyCheckStandService; // 复核站台操作类
    private final AppConveyDeliverStandService conveyDeliverStandService; // 发货站台操作类

    /**
     * 查询捡货站台
     * @param request 获取参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 站台列表
     */
    @Override
    public AppServeDataResponse<PageDataResponse<ConveyPickStandVo>> queryPickStand(QueryPickStandReq request, Integer pageSize, Integer pageIndex) {
        Tuple2<Long, List<AppConveyPickStand>> queryPickStand = conveyPickStandService.queryPickStand(request, pageSize, pageIndex);
        if(queryPickStand == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        List<ConveyPickStandVo> conveyPickStandVos = AppBeanUtils.copyListProperties(queryPickStand.getItem2(), ConveyPickStandVo::new);
        PageDataResponse<ConveyPickStandVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(queryPickStand.getItem1());
        response.setData(conveyPickStandVos);
        return AppServeResponseFactory.success(response);
    }

    /**
     * 添加捡选站台
     * @param request 添加参数
     * @return 添加结果
     */
    @Override
    public AppServeResponse addConveyPickStand(AddConveyPickStandReq request) {
        List<AppConveyPickStand> conveyPickStands = conveyPickStandService.queryPickStandByStandId(request.getStandId());
        if(conveyPickStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(!conveyPickStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台已存在");
        }
        AppConveyPickStand newPickStand = new AppConveyPickStand();
        AppBeanUtils.copyProperties(request, newPickStand);
        int result = conveyPickStandService.insertPickStand(newPickStand);
        if(result <= 0) {
            return AppServeResponseFactory.fail("添加失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 删除捡选站台
     * @param standId 站台ID
     * @return 删除结果
     */
    @Override
    public AppServeResponse deleteConveyPickStand(String standId) {
        if(AppStringUtils.isEmpty(standId)) {
            return AppServeResponseFactory.fail("站台号为空");
        }
        List<AppConveyPickStand> conveyPickStands = conveyPickStandService.queryPickStandByStandId(standId);
        if(conveyPickStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(conveyPickStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台不存在");
        }
        int deleteRowCount = conveyPickStandService.deletePickStand(standId);
        if(deleteRowCount <= 0) {
            return AppServeResponseFactory.fail("删除失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 修改捡选站台
     * @param request 修改参数
     * @return 修改结果
     */
    @Override
    public AppServeResponse updateConveyPickStand(UpdateConveyPickStandReq request) {
        List<AppConveyPickStand> conveyPickStands = conveyPickStandService.queryPickStandByStandId(request.getStandId());
        if(conveyPickStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(conveyPickStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台不存在");
        }
        AppConveyPickStand updatePickStand = new AppConveyPickStand();
        AppBeanUtils.copyProperties(request, updatePickStand);
        int updateRowCount = conveyPickStandService.updatePickStand(updatePickStand);
        if(updateRowCount <= 0) {
            return AppServeResponseFactory.fail("修改失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 查询复核站台
     * @param request 获取参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 站台列表
     */
    @Override
    public AppServeDataResponse<PageDataResponse<ConveyCheckStandVo>> queryCheckStand(QueryCheckStandReq request, Integer pageSize, Integer pageIndex) {
        Tuple2<Long, List<AppConveyCheckStand>> queryResult = conveyCheckStandService.query(request, pageSize, pageIndex);
        if(queryResult == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        List<ConveyCheckStandVo> conveyCheckStandVos = AppBeanUtils.copyListProperties(queryResult.getItem2(), ConveyCheckStandVo::new);
        PageDataResponse<ConveyCheckStandVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(queryResult.getItem1());
        response.setData(conveyCheckStandVos);
        return AppServeResponseFactory.success(response);
    }

    /**
     * 添加复核站台
     * @param request 添加参数
     * @return 添加结果
     */
    @Override
    public AppServeResponse addConveyCheckStand(AddConveyCheckStandReq request) {
        List<AppConveyCheckStand> checkStands = conveyCheckStandService.queryByStandId(request.getStandId());
        if(checkStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(!checkStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台已存在");
        }
        AppConveyCheckStand newCheckStand = new AppConveyCheckStand();
        AppBeanUtils.copyProperties(request, newCheckStand);
        Integer result = conveyCheckStandService.insert(newCheckStand).result();
        if(result <= 0) {
            return AppServeResponseFactory.fail("添加失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 删除复核站台
     * @param standId 站台ID
     * @return 删除结果
     */
    @Override
    public AppServeResponse deleteConveyCheckStand(String standId) {
        List<AppConveyCheckStand> checkStands = conveyCheckStandService.queryByStandId(standId);
        if(checkStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(checkStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台不存在");
        }
        Integer result = conveyCheckStandService.deleteByStandId(standId).result();
        if(result <= 0) {
            return AppServeResponseFactory.fail("删除失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 修改复核站台
     * @param request 修改参数
     * @return 修改结果
     */
    @Override
    public AppServeResponse updateConveyCheckStand(UpdateConveyCheckStandReq request) {
        List<AppConveyCheckStand> checkStands = conveyCheckStandService.queryByStandId(request.getStandId());
        if(checkStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(checkStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台不存在");
        }
        AppConveyCheckStand updateCheckStand = new AppConveyCheckStand();
        AppBeanUtils.copyProperties(request, updateCheckStand);
        Integer result = conveyCheckStandService.update(updateCheckStand).result();
        if(result <= 0) {
            return AppServeResponseFactory.fail("修改失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 获取发货站台
     * @param request 获取参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 站台列表
     */
    @Override
    public AppServeDataResponse<PageDataResponse<ConveyDeliverStandVo>> queryDeliverStand(QueryDeliverStandReq request, Integer pageSize, Integer pageIndex) {
        Tuple2<Long, List<AppConveyDeliverStand>> queryResult = conveyDeliverStandService.query(request, pageSize, pageIndex);
        if(queryResult == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        List<ConveyDeliverStandVo> conveyDeliverStandVos = AppBeanUtils.copyListProperties(queryResult.getItem2(), ConveyDeliverStandVo::new);
        PageDataResponse<ConveyDeliverStandVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(queryResult.getItem1());
        response.setData(conveyDeliverStandVos);
        return AppServeResponseFactory.success(response);
    }

    /**
     * 添加发货站台
     * @param request 添加参数
     * @return 添加结果
     */
    @Override
    public AppServeResponse addConveyDeliverStand(AddConveyDeliverStandReq request) {
        List<AppConveyDeliverStand> conveyDeliverStands = conveyDeliverStandService.queryByStandId(request.getStandId());
        if(conveyDeliverStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(!conveyDeliverStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台已存在");
        }
        AppConveyDeliverStand newDeliverStand = new AppConveyDeliverStand();
        AppBeanUtils.copyProperties(request, newDeliverStand);
        Integer result = conveyDeliverStandService.insert(newDeliverStand).result();
        if(result <= 0) {
            return AppServeResponseFactory.fail("添加失败，数据服务异常");
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 删除发货站台
     * @param standId 站台号
     * @return 操作结果
     */
    @Override
    public AppServeResponse deleteConveyDeliverStand(String standId) {
        List<AppConveyDeliverStand> conveyDeliverStands = conveyDeliverStandService.queryByStandId(standId);
        if(conveyDeliverStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(conveyDeliverStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台不存在");
        }
        Integer result = conveyDeliverStandService.delete(standId).result();
        if(result <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 更新发货站台信息
     * @param request 要更新的数据
     * @return 操作结果
     */
    @Override
    public AppServeResponse updateConveyDeliverStand(UpdateConveyDeliverStandReq request) {
        List<AppConveyDeliverStand> conveyDeliverStands = conveyDeliverStandService.queryByStandId(request.getStandId());
        if(conveyDeliverStands == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(conveyDeliverStands.isEmpty()) {
            return AppServeResponseFactory.fail("站台不存在");
        }
        AppConveyDeliverStand conveyDeliverStand = new AppConveyDeliverStand();
        conveyDeliverStand.setStandId(request.getStandId());
        conveyDeliverStand.setStandName(request.getStandName());
        conveyDeliverStand.setPlcId(request.getPlcId());
        conveyDeliverStand.setAreaId(request.getAreaId());
        conveyDeliverStand.setStandStatus(request.getStandStatus());
        conveyDeliverStand.setRouter(request.getRouter());
        conveyDeliverStand.setReadArriveAddress(request.getReadArriveAddress());
        conveyDeliverStand.setWriteTaskAddress(request.getWriteTaskAddress());
        conveyDeliverStand.setRemark(request.getRemark());
        Integer result = conveyDeliverStandService.update(conveyDeliverStand).result();
        return result > 0 ? AppServeResponseFactory.success() : AppServeResponseFactory.fail("数据服务异常");
    }
}
