package org.wcs.serve.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.business.data.intf.IDataLoader;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppBaseDbService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.siemensDb.AddSiemensDbReq;
import org.wcs.model.dto.serve.siemensDb.QuerySiemensDbReq;
import org.wcs.model.dto.serve.siemensDb.UpdateSiemensDbReq;
import org.wcs.model.po.app.AppBaseDb;
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.siemensDb.SiemensDbVo;
import org.wcs.serve.service.intf.ISiemensDbService;
import org.wcs.utils.AppBeanUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SiemensDbService implements ISiemensDbService {

    private final AppBaseDbService baseDbService;
    private final IDataLoader dataLoader;

    /**
     * 查询西门子DB数据
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @param request 查询参数
     * @return 查询结果
     */
    @Override
    public AppServeDataResponse<PageDataResponse<SiemensDbVo>> querySiemensDb(Integer pageIndex, Integer pageSize, QuerySiemensDbReq request) {
        Tuple2<Long, List<AppBaseDb>> queryResult = baseDbService.query(pageIndex, pageSize, request);
        if(queryResult == null) {
            return AppServeResponseFactory.fail("数据服务异常", null);
        }
        PageDataResponse<SiemensDbVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(queryResult.getItem1());
        pageDataResponse.setData(AppBeanUtils.copyListProperties(queryResult.getItem2(), SiemensDbVo::new));
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }

    /**
     * 新增西门子DB数据
     * @param request 新增参数
     * @return 新增结果
     */
    @Override
    public AppServeResponse addSiemensDb(AddSiemensDbReq request) {
        List<AppBaseDb> appBaseDbs = baseDbService.queryByName(request.getDbName());
        if(appBaseDbs == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(!appBaseDbs.isEmpty()) {
            return AppServeResponseFactory.fail("该名称已存在");
        }
        AppBaseDb db = new AppBaseDb();
        db.setDbName(request.getDbName());
        db.setDbAddress(request.getDbAddress());
        db.setPlcId(request.getPlcId());
        db.setRemark(request.getRemark());
        int insert = baseDbService.insert(db);
        return insert > 0 ? AppServeResponseFactory.success("添加成功") : AppServeResponseFactory.fail("添加失败，数据服务异常");
    }

    /**
     * 修改西门子DB数据
     * @param request 修改参数
     * @return 修改结果
     */
    @Override
    public AppServeResponse updateSiemensDb(UpdateSiemensDbReq request) {
        List<AppBaseDb> appBaseDbs = baseDbService.queryByName(request.getDbName());
        if(appBaseDbs == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(appBaseDbs.isEmpty()) {
            return AppServeResponseFactory.fail("该名称不存在");
        }
        AppBaseDb db = new AppBaseDb();
        db.setDbName(request.getDbName());
        db.setDbAddress(request.getDbAddress());
        db.setPlcId(request.getPlcId());
        db.setRemark(request.getRemark());
        int insert = baseDbService.update(db);
        return insert > 0 ? AppServeResponseFactory.success("更新成功") : AppServeResponseFactory.fail("更新失败，数据服务异常");
    }

    /**
     * 删除西门子DB数据
     * @param dbName DB名称
     * @return 删除结果
     */
    @Override
    public AppServeResponse deleteSiemensDb(String dbName) {
        List<AppBaseDb> appBaseDbs = baseDbService.queryByName(dbName);
        if(appBaseDbs == null) {
            return AppServeResponseFactory.fail("数据服务异常");
        }
        if(appBaseDbs.isEmpty()) {
            return AppServeResponseFactory.fail("该名称不存在");
        }
        int delete = baseDbService.deleteByName(dbName);
        return delete > 0 ? AppServeResponseFactory.success("删除成功") : AppServeResponseFactory.fail("删除失败，数据服务异常");
    }

    /**
     * 重新加载西门子DB数据
     * @return 重新加载结果
     */
    @Override
    public AppServeResponse reloadDbAddress() {
        dataLoader.loadDBAddress();
        return AppServeResponseFactory.success("已经尝试重新加载");
    }
}
