package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DataJinganAnalysis;
import com.usky.fire.domain.DataJinganPerson;
import com.usky.fire.mapper.DataJinganPersonMapper;
import com.usky.fire.service.DataJinganPersonService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2024-03-07
 */
@Service
public class DataJinganPersonServiceImpl extends AbstractCrudService<DataJinganPersonMapper, DataJinganPerson> implements DataJinganPersonService {
    @Override
    public CommonPage<DataJinganPerson> personList(String startTime, String endTime,String artificialCode,Integer id, Integer handleStatus, Integer pageNum, Integer pageSize) {
        IPage<DataJinganPerson> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DataJinganPerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DataJinganPerson::getTenantId, SecurityUtils.getTenantId())
                .eq(null != handleStatus,DataJinganPerson::getHandleStatus, handleStatus)
                .eq(null != id,DataJinganPerson::getId, id)
                .like(StringUtils.isNotBlank(artificialCode),DataJinganPerson::getArtificialCode,artificialCode)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime),
                        DataJinganPerson::getTimestamp, startTime, endTime)
                .orderByDesc(DataJinganPerson::getId);
        page = this.page(page,queryWrapper);
        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getCurrent(),page.getSize());
    }

    public void update(DataJinganPerson dataJinganPerson){
        dataJinganPerson.setHandleStatus(1);
        dataJinganPerson.setHandleBy(SecurityUtils.getUsername());
        dataJinganPerson.setHandleTime(LocalDateTime.now());
        dataJinganPerson.setHandlePhone(SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
        this.updateById(dataJinganPerson);
    }
}
