package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.rds.mybatis.PageHelper;
import com.cusc.nirvana.user.rnr.fp.converter.FpUserOperationLogConverter;
import com.cusc.nirvana.user.rnr.fp.dao.FpUserOperationLogDao;
import com.cusc.nirvana.user.rnr.fp.dao.entity.FpUserOperationLogPO;
import com.cusc.nirvana.user.rnr.fp.dto.FpUserOperationLogDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFpUserOperationLogService;
import com.cusc.nirvana.user.rnr.mg.constants.CommonDeleteEnum;
import com.cusc.nirvana.user.util.CuscStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户操作日志(FpUserOperationLog)表服务实现类
 *
 * @author yuy336
 * @since 2022-06-20 09:38:35
 */
@Service
public class FpUserOperationLogServiceImpl extends ServiceImpl<FpUserOperationLogDao, FpUserOperationLogPO> implements IFpUserOperationLogService {

    /**
     * 通过UUID查询单条数据
     *
     * @param bean
     * @return 实例对象
     */
    @Override
    public FpUserOperationLogDTO getByUuid(FpUserOperationLogDTO bean) {
        FpUserOperationLogPO record = this.getPoByUuid(bean.getUuid());;
        return FpUserOperationLogConverter.INSTANCE.poToDto(record);
    }
    
     /**
     * 通过查询条件查询集合数据
     *
     * @param bean
     * @return 集合对象
     */
    @Override
    public List<FpUserOperationLogDTO> queryByList(FpUserOperationLogDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        queryWrapper.orderByDesc("create_time");
        List<FpUserOperationLogPO> record = this.list(queryWrapper);
        return FpUserOperationLogConverter.INSTANCE.poListToDtoList(record);
    }

    /**
     * 分页查询
     *
     * @param bean 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<FpUserOperationLogDTO> queryByPage(FpUserOperationLogDTO bean) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.orderByDesc("create_time");
		queryWrapper.eq("org_id",bean.getOrgId());
		queryWrapper.isNotNull("creator");
		queryWrapper.eq(CuscStringUtils.isNotEmpty(bean.getCreator()),"creator",bean.getCreator());
		queryWrapper.eq(CuscStringUtils.isNotEmpty(bean.getOptModule()),"opt_module",bean.getOptModule());
		queryWrapper.eq(CuscStringUtils.isNotEmpty(bean.getOptAction()),"opt_action",bean.getOptAction());
		queryWrapper.gt(null != bean.getCreateTimeQueryStart(),"create_time",bean.getCreateTimeQueryStart());
		queryWrapper.lt(null != bean.getCreateTimeQueryStartEnd(),"create_time",bean.getCreateTimeQueryStartEnd());


		Page<FpUserOperationLogPO> page =
                this.page(new Page<>(bean.getCurrPage(), bean.getPageSize()), queryWrapper);
        return PageHelper.convert(page, FpUserOperationLogDTO.class);
    }

    /**
     * 新增数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpUserOperationLogDTO insert(FpUserOperationLogDTO bean) {
       FpUserOperationLogPO fpUserOperationLogPO = FpUserOperationLogConverter.INSTANCE.dtoToPo(bean);
	   fpUserOperationLogPO.setUuid(CuscStringUtils.generateUuid());
        this.save(fpUserOperationLogPO);
		bean.setUuid(fpUserOperationLogPO.getUuid());
        return bean;
    }

    /**
     * 修改数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpUserOperationLogDTO update(FpUserOperationLogDTO bean) {
        FpUserOperationLogPO fpUserOperationLogPO = this.getPoByUuid(bean.getUuid());
		 if(fpUserOperationLogPO == null){
            return null;
        }
		FpUserOperationLogPO tmpBean = FpUserOperationLogConverter.INSTANCE.dtoToPo(bean);
		tmpBean.setId(fpUserOperationLogPO.getId());
        this.updateById(tmpBean);
        return bean;
    }

    /**
     * 通过主键删除数据
     * @param bean 实例对象
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(FpUserOperationLogDTO bean) {
        FpUserOperationLogPO fpUserOperationLogPO = this.getPoByUuid(bean.getUuid());
		 if (fpUserOperationLogPO == null) {
            return false;
        }
        FpUserOperationLogPO tmpBean = new FpUserOperationLogPO();
        tmpBean.setId(fpUserOperationLogPO.getId());
        return this.updateById(tmpBean);
    }
    
    /**
     * 通过UUID查询单条数据
     *
     * @param uuid
     * @return 实例对象
     */
    private FpUserOperationLogPO getPoByUuid(String uuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        return this.getOne(queryWrapper);
    }
}
