package com.ssyf.module.robot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssyf.common.security.domain.ClientInfo;
import com.ssyf.common.security.helper.ClientContextHelper;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.error.ServiceRuntimeException;
import com.ssyf.module.robot.domain.entity.RRobotConsumptionRecord;
import com.ssyf.module.robot.domain.entity.RRobotRunRecord;
import com.ssyf.module.robot.domain.entity.RRobotRunningInfo;
import com.ssyf.module.robot.domain.query.RRobotRunRecordQuery;
import com.ssyf.module.robot.service.RRobotRunRecordService;
import com.ssyf.module.robot.mapper.RRobotRunRecordMapper;
import com.ssyf.plugs.mysql.helper.MybatisPlusHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ELSA
 * @description 针对表【r_robot_run_record(机器人运行记录表)】的数据库操作Service实现
 * @createDate 2025-12-18 10:53:31
 */
@Service
public class RRobotRunRecordServiceImpl extends ServiceImpl<RRobotRunRecordMapper, RRobotRunRecord>
        implements RRobotRunRecordService {

    @Override
    public RRobotRunRecord addOne(RRobotRunRecord query) {

        RRobotRunRecord entity = BeanUtil.toBean(query, RRobotRunRecord.class);
        entity.setUuid(IdUtil.simpleUUID());
        entity.setCreateTime(LocalDateTime.now());

        if (ClientContextHelper.isLogin()) {
            ClientInfo clientInfo = ClientContextHelper.getClientInfo();
            entity.setCreatorUuid(clientInfo.getId().toString()).setCreator(clientInfo.getNickname());
        }

        this.save(entity);
        return entity;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeByUid(String uuid) {
        checkedExistAndGet(uuid);
        this.remove(createBaseWrapper(null).eq(RRobotRunRecord::getUuid, uuid));
    }

    @Override
    public void removeBatchByUids(List<String> uuids) {
        this.remove(createBaseWrapper(null).in(RRobotRunRecord::getUuid, uuids));
    }

    @Override
    public void updateByUid(String uuid, RRobotRunRecordQuery query) {
        this.checkedExistAndGet(uuid);
        query.setUuid(null);
        RRobotRunRecord update = BeanUtil.toBean(query, RRobotRunRecord.class);
        LambdaQueryWrapper<RRobotRunRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RRobotRunRecord::getUuid, uuid);
        this.update(update, wrapper);
    }

    @Override
    public RRobotRunRecord getOneByUid(String uuid) {
        return this.getOne(createBaseWrapper(null).eq(RRobotRunRecord::getUuid, uuid));
    }

    @Override
    public PageInfo<RRobotRunRecord> getPage(RRobotRunRecordQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return PageInfo.of(this.list(createBaseWrapper(query)));
    }

    @Override
    public List<RRobotRunRecord> getList(RRobotRunRecordQuery query) {
        return this.list(createBaseWrapper(query));
    }

    @Override
    public RRobotRunRecord checkedExistAndGet(String uuid) {
        RRobotRunRecord old = this.getOne(createBaseWrapper(null).eq(RRobotRunRecord::getUuid, uuid));
        if (old == null) {
            ServiceRuntimeException.throw0(ApiCodeUser.PARAMETER_ERROR.getCode(), "数据不存在");
        }
        return old;
    }

    @Override
    public Integer countBy(RRobotRunRecordQuery query) {
        return this.count(createBaseWrapper(query));
    }

    private LambdaQueryWrapper<RRobotRunRecord> createBaseWrapper(RRobotRunRecordQuery query) {
        LambdaQueryWrapper<RRobotRunRecord> wrapper = new LambdaQueryWrapper<>();
        if (query == null) {
            return wrapper;
        }
        wrapper.eq(StrUtil.isNotBlank(query.getRobotGroupUuid()) , RRobotRunRecord::getRobotGroupUuid, query.getRobotGroupUuid());
        wrapper.eq(StrUtil.isNotBlank(query.getRobotGroupName()) , RRobotRunRecord::getRobotGroupName , query.getRobotGroupName());
        wrapper.like(StrUtil.isNotBlank(query.getRobotNickName()) , RRobotRunRecord::getRobotNickName, query.getRobotNickName());
        wrapper.like(StrUtil.isNotBlank(query.getLiveRoomId()), RRobotRunRecord::getLiveRoomId, query.getLiveRoomId());
        wrapper.like(StrUtil.isNotBlank(query.getAnchorId()), RRobotRunRecord::getAnchorId, query.getAnchorId());
        wrapper.like(StrUtil.isNotBlank(query.getAnchorName()), RRobotRunRecord::getAnchorName, query.getAnchorName());
        wrapper.like(StrUtil.isNotBlank(query.getLiveRoomName()), RRobotRunRecord::getLiveRoomName, query.getLiveRoomName());
        wrapper.like(StrUtil.isNotBlank(query.getAnchorAvatar()), RRobotRunRecord::getAnchorAvatar, query.getAnchorAvatar());


        wrapper.like(StrUtil.isNotBlank(query.getRobotUuid()), RRobotRunRecord::getRobotUuid, query.getRobotUuid());
        wrapper.like(StrUtil.isNotBlank(query.getRobotAccount()), RRobotRunRecord::getRobotAccount, query.getRobotAccount());
        wrapper.like(StrUtil.isNotBlank(query.getConfigName()), RRobotRunRecord::getConfigName, query.getConfigName());
        wrapper.like(StrUtil.isNotBlank(query.getConfigType()), RRobotRunRecord::getConfigType, query.getConfigType());
        wrapper.eq(query.getExecuteStatus() != null, RRobotRunRecord::getExecuteStatus, query.getExecuteStatus());
        wrapper.like(StrUtil.isNotBlank(query.getConfigUuid()), RRobotRunRecord::getConfigUuid, query.getConfigUuid());


        wrapper.like(StrUtil.isNotBlank(query.getUuid()), RRobotRunRecord::getUuid, query.getUuid());
        wrapper.ge(query.getCreateTimeStart() != null, RRobotRunRecord::getCreateTime, query.getCreateTimeStart());
        wrapper.le(query.getCreateTimeEnd() != null, RRobotRunRecord::getCreateTime, query.getCreateTimeEnd());
        wrapper.like(StrUtil.isNotBlank(query.getCreator()), RRobotRunRecord::getCreator, query.getCreator());
        wrapper.eq(StrUtil.isNotBlank(query.getCreatorUuid()), RRobotRunRecord::getCreatorUuid, query.getCreatorUuid());

        if (CollectionUtil.isNotEmpty(query.getSorts())) {
            MybatisPlusHelper.orderAndIgnoreError(RRobotRunRecord.class, wrapper, query.getSorts());
        } else {
            wrapper.orderByDesc(RRobotRunRecord::getCreateTime);
        }
        return wrapper;
    }
}




