package com.pm.control.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import com.pm.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pm.control.mapper.IbmsDeviceControlLogMapper;
import com.pm.control.service.IIbmsDeviceControlLogService;
import com.pm.common.config.PageData;

/**
 * 设备控制记录Service业务层处理
 *
 * @author LiXiangFei
 * @date 2025-04-16
 */
@Service
public class IbmsDeviceControlLogServiceImpl implements IIbmsDeviceControlLogService
{
    @Autowired
    private IbmsDeviceControlLogMapper ibmsDeviceControlLogMapper;

    /**
     * 查询设备控制记录
     *
     * @param id 设备控制记录主键
     * @return 设备控制记录
     */
    @Override
    public PageData selectIbmsDeviceControlLogById(Long id)
    {
        return ibmsDeviceControlLogMapper.selectIbmsDeviceControlLogById(id);
    }

    /**
     * 查询设备控制记录列表
     *
     * @param pd
     * @return 设备控制记录
     */
    @Override
    public List<PageData> selectIbmsDeviceControlLogList(PageData pd)
    {
        List<PageData> list = ibmsDeviceControlLogMapper.selectIbmsDeviceControlLogList(pd);
        if (pd.containsKey("export")) // 如果是导出 则处理数据
        {
            list.forEach(pageData -> {
                if(pageData.containsKey("controlResult")){
                    pageData.put("controlResult", "0".equals(pageData.getString("controlResult")) ? "失败" : "成功");
                }
                if (pageData.containsKey("operateTime")){
                    String timeStr = pageData.getString("operateTime");
                    LocalDateTime dateTime;
                    if (timeStr.contains("T")) {
                        dateTime = LocalDateTime.parse(timeStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
                    } else {
                        dateTime = LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    }
                    pageData.put("operateTime", dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                }
            });
        }
        return list;
    }

    /**
     * 新增设备控制记录
     *
     * @param pd
     * @return 结果
     */
    @Override
    public int insertIbmsDeviceControlLog(PageData pd)
    {
        pd.put("createTime", DateUtils.getTime());
        return ibmsDeviceControlLogMapper.insertIbmsDeviceControlLog(pd);
    }

    /**
     * 修改设备控制记录
     *
     * @param pd
     * @return 结果
     */
    @Override
    public int updateIbmsDeviceControlLog(PageData pd)
    {
        return ibmsDeviceControlLogMapper.updateIbmsDeviceControlLog(pd);
    }

    /**
     * 批量删除设备控制记录
     *
     * @param ids 需要删除的设备控制记录主键
     * @return 结果
     */
    @Override
    public int deleteIbmsDeviceControlLogByIds(Long[] ids)
    {
        return ibmsDeviceControlLogMapper.deleteIbmsDeviceControlLogByIds(ids);
    }

    /**
     * 删除设备控制记录信息
     *
     * @param id 设备控制记录主键
     * @return 结果
     */
    @Override
    public int deleteIbmsDeviceControlLogById(Long id)
    {
        return ibmsDeviceControlLogMapper.deleteIbmsDeviceControlLogById(id);
    }
}
