package org.wcs.serve.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppEtagControllerInfoService;
import org.wcs.mapper.intf.AppEtagTagInfoService;
import org.wcs.mapper.intf.AppEtagTagLocationService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagBase.*;
import org.wcs.model.po.app.AppEtagControllerInfo;
import org.wcs.model.po.app.AppEtagTagInfo;
import org.wcs.model.po.app.AppEtagTagLocation;
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.etagBase.EtagControllerVo;
import org.wcs.model.vo.serve.etagBase.EtagLocationVo;
import org.wcs.model.vo.serve.etagBase.EtagTagInfoVo;
import org.wcs.serve.service.intf.IEtagBaseService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppUUIDUtils;

import java.util.List;

/**
 * 电子标签控制器数据服务
 */
@Service
@RequiredArgsConstructor
public class EtagBaseService implements IEtagBaseService {

    private final AppEtagControllerInfoService etagControllerInfoService; // 电子标签控制器信息服务
    private final AppEtagTagInfoService etagTagInfoService; // 电子标签小标签信息服务
    private final AppEtagTagLocationService etagLocationService; // 电子标签小标签位置信息服务

    /**
     * 查询电子标签控制器基础数据
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public AppServeDataResponse<PageDataResponse<EtagControllerVo>> queryEtagController(EtagControllerQueryReq request, Integer pageIndex, Integer pageSize) {
        if(request == null || pageIndex == null || pageSize == null || pageIndex < 1 || pageSize < 1) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        Tuple2<Long, List<AppEtagControllerInfo>> controllerInfo = etagControllerInfoService.queryEtagControllerInfo(request, pageSize, pageIndex);
        if(controllerInfo == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        List<EtagControllerVo> etagControllerVos = AppBeanUtils.copyListProperties(controllerInfo.getItem2(), EtagControllerVo::new);
        PageDataResponse<EtagControllerVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(controllerInfo.getItem1());
        pageDataResponse.setData(etagControllerVos);
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }

    /**
     * 添加电子标签控制器基础数据
     * @param request 添加参数
     * @return 响应
     */
    @Override
    public AppServeResponse addEtagController(AddEtagControllerReq request) {
        List<AppEtagControllerInfo> appEtagControllerInfos = etagControllerInfoService.queryEtagControllerInfoById(request.getControllerId());
        if(appEtagControllerInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(!appEtagControllerInfos.isEmpty()) {
            return AppServeResponseFactory.fail("控制器编号已存在", null);
        }
        AppEtagControllerInfo appEtagControllerInfo = new AppEtagControllerInfo();
        AppBeanUtils.copyProperties(request, appEtagControllerInfo);
        int result = etagControllerInfoService.insertEtagControllerInfo(appEtagControllerInfo);
        if(result <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 修改电子标签控制器基础数据
     * @param request 修改参数
     * @return 响应
     */
    @Override
    public AppServeResponse updateEtagController(UpdateEtagControllerReq request) {
        List<AppEtagControllerInfo> appEtagControllerInfos = etagControllerInfoService.queryEtagControllerInfoById(request.getControllerId());
        if(appEtagControllerInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(appEtagControllerInfos.isEmpty()) {
            return AppServeResponseFactory.fail("控制器编号不存在", null);
        }
        AppEtagControllerInfo appEtagControllerInfo = new AppEtagControllerInfo();
        AppBeanUtils.copyProperties(request, appEtagControllerInfo);
        int updated = etagControllerInfoService.updateEtagControllerInfo(appEtagControllerInfo);
        if(updated <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 删除电子标签控制器基础数据
     * @param etagControllerId 控制器编号
     * @return 响应
     */
    @Override
    public AppServeResponse deleteEtagController(Integer etagControllerId) {
        List<AppEtagControllerInfo> appEtagControllerInfos = etagControllerInfoService.queryEtagControllerInfoById(etagControllerId);
        if(appEtagControllerInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(appEtagControllerInfos.isEmpty()) {
            return AppServeResponseFactory.fail("控制器编号不存在", null);
        }
        int deleted = etagControllerInfoService.deleteEtagControllerInfoById(etagControllerId);
        if(deleted <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 查询电子标签小标签基础信息
     * @param request 添加参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public AppServeDataResponse<PageDataResponse<EtagTagInfoVo>> queryEtagTagInfo(EtagTagInfoQueryReq request, Integer pageIndex, Integer pageSize) {
        if(request == null || pageIndex == null || pageSize == null || pageIndex < 1 || pageSize < 1) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        Tuple2<Long, List<AppEtagTagInfo>> tagInfo = etagTagInfoService.queryEtagTagInfo(request, pageSize, pageIndex);
        if(tagInfo == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        List<EtagTagInfoVo> etagTagInfoVos = AppBeanUtils.copyListProperties(tagInfo.getItem2(), EtagTagInfoVo::new);
        PageDataResponse<EtagTagInfoVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(tagInfo.getItem1());
        pageDataResponse.setData(etagTagInfoVos);
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }

    /**
     * 添加电子标签小标签基础信息
     * @param request 添加参数
     * @return 响应
     */
    @Override
    public AppServeResponse addEtagTagInfo(AddEtagTagInfoReq request) {
        if(request == null) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        List<AppEtagTagInfo> appEtagTagInfos = etagTagInfoService.queryEtagTagInfoByTagName(request.getTagName());
        if(appEtagTagInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(!appEtagTagInfos.isEmpty()) {
            return AppServeResponseFactory.fail("标签名称已存在", null);
        }
        AppEtagTagInfo appEtagTagInfo = new AppEtagTagInfo();
        AppBeanUtils.copyProperties(request, appEtagTagInfo);
        Integer value = etagTagInfoService.insert(appEtagTagInfo).result();
        if(value == null || value <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 修改电子标签小标签基础信息
     * @param request 修改参数
     * @return 响应
     */
    @Override
    public AppServeResponse updateEtagTagInfo(UpdateEtagTagInfoReq request) {
        if(request == null) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        List<AppEtagTagInfo> appEtagTagInfos = etagTagInfoService.queryEtagTagInfoByTagName(request.getTagName());
        if(appEtagTagInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(appEtagTagInfos.isEmpty()) {
            return AppServeResponseFactory.fail("标签名称不存在", null);
        }
        AppEtagTagInfo appEtagTagInfo = new AppEtagTagInfo();
        AppBeanUtils.copyProperties(request, appEtagTagInfo);
        Integer result = etagTagInfoService.update(appEtagTagInfo).result();
        if(result == null || result <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 删除电子标签小标签基础信息
     * @param tagName 标签名称
     * @return 响应
     */
    @Override
    public AppServeResponse deleteEtagTagInfo(String tagName) {
        if(tagName == null) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        List<AppEtagTagInfo> appEtagTagInfos = etagTagInfoService.queryEtagTagInfoByTagName(tagName);
        if(appEtagTagInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(appEtagTagInfos.isEmpty()) {
            return AppServeResponseFactory.fail("标签名称不存在", null);
        }
        Integer result = etagTagInfoService.deleteByTagName(tagName).result();
        if(result == null || result <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 查询电子标签小标签位置信息
     * @param request 添加参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public AppServeDataResponse<PageDataResponse<EtagLocationVo>> queryEtagLocation(EtagLocationQueryReq request, Integer pageIndex, Integer pageSize) {
        Tuple2<Long, List<AppEtagTagLocation>> etagTagLocationTuple = etagLocationService.queryEtagTagLocation(request, pageIndex, pageSize);
        if(etagTagLocationTuple == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        List<EtagLocationVo> etagLocationVos = AppBeanUtils.copyListProperties(etagTagLocationTuple.getItem2(), EtagLocationVo::new);
        PageDataResponse<EtagLocationVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(etagTagLocationTuple.getItem1());
        pageDataResponse.setData(etagLocationVos);
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }

    /**
     * 添加电子标签小标签位置信息
     * @param request 添加参数
     * @return 响应
     */
    @Override
    public AppServeResponse addEtagLocation(AddEtagLocationReq request) {
        List<AppEtagTagInfo> etagTagInfos = etagTagInfoService.queryEtagTagInfoByTagName(request.getTagName());
        if(etagTagInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(etagTagInfos.isEmpty()) {
            return AppServeResponseFactory.fail("标签名称不存在，请先添加对应的标签", null);
        }
        List<AppEtagTagLocation> etagTagLocations = etagLocationService.queryWithLocationAndType(request.getLocation(), request.getLocationType());
        if(etagTagLocations == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(!etagTagLocations.isEmpty()) {
            return AppServeResponseFactory.fail("货位已存在", null);
        }
        AppEtagTagLocation appEtagTagLocation = new AppEtagTagLocation();
        AppBeanUtils.copyProperties(request, appEtagTagLocation);
        appEtagTagLocation.setRecordId(AppUUIDUtils.getNewUUID());
        Integer insertResult = etagLocationService.insert(appEtagTagLocation).result();
        if(insertResult == null || insertResult <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 修改电子标签小标签位置信息
     * @param request 修改参数
     * @return 响应
     */
    @Override
    public AppServeResponse updateEtagLocation(UpdateEtagLocationReq request) {
        List<AppEtagTagInfo> etagTagInfos = etagTagInfoService.queryEtagTagInfoByTagName(request.getTagName());
        if(etagTagInfos == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(etagTagInfos.isEmpty()) {
            return AppServeResponseFactory.fail("标签名称不存在，请先添加对应的标签", null);
        }
        List<AppEtagTagLocation> etagTagLocations = etagLocationService.queryWithRecordId(request.getRecordId());
        if(etagTagLocations == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(etagTagLocations.isEmpty()) {
            return AppServeResponseFactory.fail("记录不存在", null);
        }
        AppEtagTagLocation appEtagTagLocation = new AppEtagTagLocation();
        AppBeanUtils.copyProperties(request, appEtagTagLocation);
        Integer updateResult = etagLocationService.updateAll(appEtagTagLocation).result();
        if(updateResult == null || updateResult <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }

    /**
     * 删除电子标签小标签位置信息
     * @param recordId 记录ID
     * @return 响应
     */
    @Override
    public AppServeResponse deleteEtagLocation(String recordId) {
        List<AppEtagTagLocation> etagTagLocations = etagLocationService.queryWithRecordId(recordId);
        if(etagTagLocations == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        if(etagTagLocations.isEmpty()) {
            return AppServeResponseFactory.fail("记录不存在", null);
        }
        Integer deleteResult = etagLocationService.deleteWithRecordId(recordId).result();
        if(deleteResult == null || deleteResult <= 0) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success();
    }
}
