package com.njcn.csdevice.service.impl;

import com.alibaba.fastjson.JSONArray;
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.csdevice.mapper.CsLogsPOMapper;
import com.njcn.csdevice.pojo.po.CsLogsPO;
import com.njcn.csdevice.service.CsLogsPOService;
import com.njcn.user.enums.AppRoleEnum;
import com.njcn.web.pojo.param.BaseParam;
import com.njcn.web.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

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

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

    @Override
    public IPage<CsLogsPO> queryPage(BaseParam baseParam) {
        Page<CsLogsPO> returnpage = new Page<> (baseParam.getPageNum ( ), baseParam.getPageSize ( ));
        String username = RequestUtil.getUsername();
        String userRole = RequestUtil.getUserRole();
        List<String> strings = JSONArray.parseArray(userRole, String.class);
        QueryWrapper<CsLogsPO> csLogsPOQueryWrapper = new QueryWrapper<>();
        csLogsPOQueryWrapper.lambda()
//                .eq(!strings.contains(AppRoleEnum.OPERATION_MANAGER.getCode()),CsLogsPO::getUserName, username)
                .ge(StringUtils.isNotBlank(baseParam.getSearchBeginTime()),CsLogsPO::getCreateTime,baseParam.getSearchBeginTime()).
                le(StringUtils.isNotBlank(baseParam.getSearchEndTime()),CsLogsPO::getCreateTime,baseParam.getSearchEndTime()).orderByDesc(CsLogsPO::getCreateTime);
        IPage<CsLogsPO> list = this.getBaseMapper().selectPage(returnpage,csLogsPOQueryWrapper);
        return list;
    }
}
