package com.njcn.cssystem.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.dto.DeviceLogDTO;
import com.njcn.cssystem.mapper.CsLogsPOMapper;
import com.njcn.cssystem.pojo.po.CsLogsPO;
import com.njcn.cssystem.service.CsLogsPOService;
import com.njcn.web.pojo.param.BaseParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 
 * Description:
 * Date: 2023/8/7  14:02【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class CsLogsPOServiceImpl extends ServiceImpl<CsLogsPOMapper, CsLogsPO> implements CsLogsPOService {

    @Override
    public void addLog(DeviceLogDTO deviceLogDTO) {
        CsLogsPO csPO = new CsLogsPO();
        BeanUtils.copyProperties(deviceLogDTO,csPO);
        csPO.setUserName(deviceLogDTO.getUserIndex());
        csPO.setCreateBy(deviceLogDTO.getUserIndex());
        csPO.setUpdateBy(deviceLogDTO.getUserIndex());
        this.save(csPO);
    }

    @Override
    public IPage<CsLogsPO> queryPage(BaseParam baseParam) {
        String beginDay = LocalDateTimeUtil.format(
                LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse(baseParam.getSearchBeginTime(), "yyyy-MM-dd")),
                "yyyy-MM-dd HH:mm:ss"
        );
        String endDay = LocalDateTimeUtil.format(
                LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse(baseParam.getSearchEndTime(), "yyyy-MM-dd")),
                "yyyy-MM-dd HH:mm:ss"
        );
        Page<CsLogsPO> returnpage = new Page<> (baseParam.getPageNum ( ), baseParam.getPageSize ( ));
        QueryWrapper<CsLogsPO> csLogsPOQueryWrapper = new QueryWrapper<>();
        csLogsPOQueryWrapper.lambda()
                .ge(StringUtils.isNotBlank(beginDay),CsLogsPO::getCreateTime,beginDay).
                le(StringUtils.isNotBlank(beginDay),CsLogsPO::getCreateTime,endDay).orderByDesc(CsLogsPO::getCreateTime);
        IPage<CsLogsPO> list = this.getBaseMapper().selectPage(returnpage,csLogsPOQueryWrapper);
        return list;
    }
}
