package org.wcs.serve.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppBasePlcService;
import org.wcs.model.dto.serve.plc.AddPlcReq;
import org.wcs.model.dto.serve.plc.QueryBasePlcReq;
import org.wcs.model.dto.serve.plc.UpdatePlcReq;
import org.wcs.model.po.app.AppBasePlc;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.model.vo.serve.plc.QueryBasePlcResp;
import org.wcs.serve.service.intf.IPlcService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PlcService implements IPlcService {

    private final AppBasePlcService appBasePlcService;


    /**
     * 根据条件模糊查询PLC 数据
     * @param queryPlcReq 查询条件
     * @return 查询结果
     */
    @Override
    public AppServeDataResponse<List<QueryBasePlcResp>> queryPlc(QueryBasePlcReq queryPlcReq) {
        List<AppBasePlc> basePlcList = appBasePlcService.queryIndistinct(queryPlcReq.getPlcName(), queryPlcReq.getPlcType());
        if(basePlcList == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        List<QueryBasePlcResp> queryBasePlcRespList = AppBeanUtils.copyListProperties(basePlcList, QueryBasePlcResp::new);
        return AppServeResponseFactory.success("查询成功", queryBasePlcRespList);
    }

    /**
     * 添加PLC
     * @param addPlcReq 查询条件
     * @return 查询结果
     */
    @Override
    public AppServeResponse addPlc(AddPlcReq addPlcReq) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录");
        }
        String operationUserId = userIdObj.toString();
        log.info("用户：{} 要添加 plc：{}",  operationUserId, AppStringUtils.objectToString(addPlcReq));
        List<AppBasePlc> queryByIdList = appBasePlcService.queryById(addPlcReq.getPlcId());
        if(queryByIdList ==  null) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        if(!queryByIdList.isEmpty()) {
            return AppServeResponseFactory.fail("PLC 已存在，请检查 PLCID 是否重复");
        }
        AppBasePlc insertPlc = new AppBasePlc();
        AppBeanUtils.copyProperties(addPlcReq, insertPlc);
        int insertCount = appBasePlcService.insert(insertPlc);
        if(insertCount == 0) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        return AppServeResponseFactory.success(String.format("添加成功，添加了 %d 条记录", insertCount));
    }

    /**
     * 修改PLC
     * @param updatePlcReq 更新PLC数据
     * @return 修改结果
     */
    @Override
    public AppServeResponse updatePlc(UpdatePlcReq updatePlcReq) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录");
        }
        String operationUserId = userIdObj.toString();
        log.info("用户：{} 要修改 plc：{}",  operationUserId, AppStringUtils.objectToString(updatePlcReq));
        List<AppBasePlc> queryByIdList = appBasePlcService.queryById(updatePlcReq.getPlcId());
        if(queryByIdList ==  null) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        if(queryByIdList.isEmpty()) {
            return AppServeResponseFactory.fail("PLC 不存在，请刷新后再试");
        }
        AppBasePlc updatePlc = new AppBasePlc();
        AppBeanUtils.copyProperties(updatePlcReq, updatePlc);
        int updateResult = appBasePlcService.update(updatePlc);
        if(updateResult == 0) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        return AppServeResponseFactory.success(String.format("更新成功，更新了 %d 条记录", updateResult));
    }

    /**
     * 删除 PLC
     * @param plcId PLC ID
     * @return AppServeResponse
     */
    @Override
    public AppServeResponse deletePlc(Integer plcId) {
        List<AppBasePlc> queryByIdList = appBasePlcService.queryById(plcId);
        if(queryByIdList ==  null) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        if(queryByIdList.isEmpty()) {
            return AppServeResponseFactory.fail("PLC 不存在，请刷新后再试");
        }
        AppBasePlc basePlc = queryByIdList.getFirst();
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录");
        }
        String operationUserId = userIdObj.toString();
        log.info("用户：{} 要删除 plc：{}",  operationUserId, AppStringUtils.objectToString(basePlc));
        int delete = appBasePlcService.deletePlc(plcId);
        if(delete > 0) {
            log.info("用户：{} 删除 plc：{} 成功", operationUserId, AppStringUtils.objectToString(basePlc));
            return AppServeResponseFactory.success("删除 plc 成功", null);
        }
        log.info("用户：{} 删除 plc：{} 失败", operationUserId, AppStringUtils.objectToString(basePlc));
        return AppServeResponseFactory.fail("删除 plc 失败");
    }
}
