package org.wcs.serve.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppBaseScanMethodService;
import org.wcs.mapper.intf.AppStockScanService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.stockScan.AddStockScanReq;
import org.wcs.model.dto.serve.stockScan.QueryStockScanReq;
import org.wcs.model.dto.serve.stockScan.UpdateStockScanReq;
import org.wcs.model.po.app.AppBaseScanMethod;
import org.wcs.model.po.app.AppStockScan;
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.stockScan.AppBaseScanMethodVo;
import org.wcs.model.vo.serve.stockScan.AppStockScanVo;
import org.wcs.serve.service.intf.IStockScanService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class StockScanService implements IStockScanService {

    private final AppStockScanService appStockScanService;
    private final AppBaseScanMethodService appBaseScanMethodService;

    /**
     * 查询仓库扫码设置
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 仓库扫码设置列表
     */
    @Override
    public AppServeDataResponse<PageDataResponse<AppStockScanVo>> queryStockScan(QueryStockScanReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return AppServeResponseFactory.fail("参数错误", null);
        }
        Tuple2<Long, List<AppStockScan>> pageResult = appStockScanService.queryStockScan(request, pageSize, pageIndex);
        if(pageResult == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        PageDataResponse<AppStockScanVo> response = new PageDataResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount(pageResult.getItem1());
        response.setData(AppBeanUtils.copyListProperties(pageResult.getItem2(), AppStockScanVo::new));
        return AppServeResponseFactory.success("查询成功", response);
    }

    /**
     * 获取仓库扫码设置
     * @return 仓库扫码设置列表
     */
    @Override
    public AppServeDataResponse<List<AppStockScanVo>> queryStockScan() {
        List<AppStockScan> appStockScans = appStockScanService.queryAll();
        if(appStockScans == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success("查询成功", AppBeanUtils.copyListProperties(appStockScans, AppStockScanVo::new));
    }

    /**
     * 查询基础扫描方法
     * @return 查询结果
     */
    @Override
    public AppServeDataResponse<List<AppBaseScanMethodVo>> queryBaseScanMethod() {
        List<AppBaseScanMethod> appBaseScanMethods = appBaseScanMethodService.queryAll();
        if(appBaseScanMethods == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        return AppServeResponseFactory.success("查询成功", AppBeanUtils.copyListProperties(appBaseScanMethods, AppBaseScanMethodVo::new));
    }

    /**
     * 添加扫码信息
     * @param request 扫码信息
     * @return 添加结果
     */
    @Override
    public AppServeResponse addStockScan(AddStockScanReq request) {
        AppStockScan stockScanCheck = new AppStockScan();
        stockScanCheck.setScanId(request.getScanId());
        List<AppStockScan> stockScans = appStockScanService.query(stockScanCheck);
        if(stockScans == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(!stockScans.isEmpty()) {
            return AppServeResponseFactory.fail("扫码ID已存在");
        }
        AppStockScan insertStockScan = new AppStockScan();
        insertStockScan.setScanId(request.getScanId());
        insertStockScan.setScanName(request.getScanName());
        insertStockScan.setScanMethod(request.getScanMethod());
        insertStockScan.setScanStatus(request.getScanStatus());
        insertStockScan.setScanType(request.getScanType());
        insertStockScan.setParam1(request.getParam1());
        insertStockScan.setParam2(request.getParam2());
        insertStockScan.setParam3(request.getParam3());
        insertStockScan.setPlcId(request.getPlcId());
        insertStockScan.setReadStatusAddress(request.getReadStatusAddress());
        insertStockScan.setWriteTaskAddress(request.getWriteTaskAddress());
        insertStockScan.setLedNo(request.getLedNo());
        insertStockScan.setRemark(request.getRemark());
        int insert = appStockScanService.insert(insertStockScan);
        return insert > 0 ? AppServeResponseFactory.success() : AppServeResponseFactory.fail("数据服务异常");
    }

    /**
     * 修改仓库扫码
     *
     * @param request 修改数据
     * @return 修改结果
     */
    @Override
    public AppServeResponse updateStockScan(UpdateStockScanReq request) {
        AppStockScan stockScanCheck = new AppStockScan();
        stockScanCheck.setScanId(request.getScanId());
        List<AppStockScan> stockScans = appStockScanService.query(stockScanCheck);
        if(stockScans == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(stockScans.isEmpty()) {
            return AppServeResponseFactory.fail("扫码ID不存在");
        }
        AppStockScan insertStockScan = new AppStockScan();
        insertStockScan.setScanId(request.getScanId());
        insertStockScan.setScanName(request.getScanName());
        insertStockScan.setScanMethod(AppStringUtils.isEmptyOr(request.getScanMethod(), ""));
        insertStockScan.setScanStatus(request.getScanStatus());
        insertStockScan.setScanType(request.getScanType());
        insertStockScan.setParam1(AppStringUtils.isEmptyOr(request.getParam1(), ""));
        insertStockScan.setParam2(AppStringUtils.isEmptyOr(request.getParam2(), ""));
        insertStockScan.setParam3(AppStringUtils.isEmptyOr(request.getParam3(), ""));
        insertStockScan.setPlcId(request.getPlcId());
        insertStockScan.setReadStatusAddress(AppStringUtils.isEmptyOr(request.getReadStatusAddress(), ""));
        insertStockScan.setWriteTaskAddress(AppStringUtils.isEmptyOr(request.getWriteTaskAddress(), ""));
        insertStockScan.setLedNo(request.getLedNo());
        insertStockScan.setRemark(AppStringUtils.isEmptyOr(request.getRemark(), ""));
        int update = appStockScanService.update(insertStockScan);
        return update > 0 ? AppServeResponseFactory.success() : AppServeResponseFactory.fail("数据服务异常");
    }

    /**
     * 删除仓库扫码
     *
     * @param stockScanId 仓库扫码ID
     * @return 删除结果
     */
    @Override
    public AppServeResponse deleteStockScan(String stockScanId) {
        AppStockScan stockScanCheck = new AppStockScan();
        stockScanCheck.setScanId(stockScanId);
        List<AppStockScan> stockScans = appStockScanService.query(stockScanCheck);
        if(stockScans == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(stockScans.isEmpty()) {
            return AppServeResponseFactory.fail("扫码ID不存在");
        }
        int delete = appStockScanService.deleteById(stockScanId);
        return delete > 0 ? AppServeResponseFactory.success() : AppServeResponseFactory.fail("删除失败，数据异常");
    }
}
