package cn.superdesk.uniorder.service.sys.logger.impl;

import cn.superdesk.uniorder.common.exception.utils.AssertUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.sys.logger.SysUserOperationLog;
import cn.superdesk.uniorder.dao.entity.sys.logger.SysUserOperationLogExample;
import cn.superdesk.uniorder.dao.mapper.sys.logger.SysUserOperationLogMapper;
import cn.superdesk.uniorder.dto.param.system.logger.SysUserOperationLogQueryCriteria;
import cn.superdesk.uniorder.service.sys.logger.SysUserOperationLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 操作日志
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-12-10 17:17:34
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-12-10 17:17:34
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class SysUserOperationLogServiceImpl implements SysUserOperationLogService {

    @Autowired
    private SysUserOperationLogMapper sysUserOperationLogMapper;


    /****************************************** extends BaseService 部分 start ******************************************/


    @Override
    public int insertSelective(SysUserOperationLog entity) {
        return sysUserOperationLogMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysUserOperationLog entity) {
        return sysUserOperationLogMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysUserOperationLog findById(Long id) {
        return sysUserOperationLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysUserOperationLog> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<SysUserOperationLog> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public void addSysUserOperationLog(SysUserOperationLog entity) {
        sysUserOperationLogMapper.insertSelective(entity);
    }

    public void updateSysUserOperationLog(SysUserOperationLog entity) {
        SysUserOperationLog originEntity = sysUserOperationLogMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        sysUserOperationLogMapper.updateByPrimaryKeySelective(originEntity);
    }

    public void deleteSysUserOperationLog(Long id) {
        sysUserOperationLogMapper.deleteByPrimaryKey(id);
    }

    public SysUserOperationLog findSysUserOperationLogById(Long id) {
        SysUserOperationLog entity = sysUserOperationLogMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

    public R<List<SysUserOperationLog>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        // 样例 保留
//		SysUserOperationLog sysUserOperationLogConditions = BeanUtils.mapToBean(conditions,SysUserOperationLog.class);

        SysUserOperationLogExample          example  = new SysUserOperationLogExample();
        SysUserOperationLogExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause("operation_time desc");

        PageHelper.startPage(pageNun, pageSize);
        List<SysUserOperationLog>     resultList = sysUserOperationLogMapper.selectByExample(example);
        PageInfo<SysUserOperationLog> page       = new PageInfo<SysUserOperationLog>(resultList);
        PageHelper.clearPage();

        return new R<List<SysUserOperationLog>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    public R<List<SysUserOperationLog>> pageQueryGenericParam(SysUserOperationLogQueryCriteria queryCriteria) {

        SysUserOperationLogExample          example  = new SysUserOperationLogExample();
        SysUserOperationLogExample.Criteria criteria = example.createCriteria();


        example.setOrderByClause("operation_time desc");

        // TODO 添加条件... 后期添加 queryCriteria 转 SysUserOperationLogExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<SysUserOperationLog>     resultList = sysUserOperationLogMapper.selectByExample(example);
        PageInfo<SysUserOperationLog> page       = new PageInfo<SysUserOperationLog>(resultList);
        PageHelper.clearPage();

        return new R<List<SysUserOperationLog>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

}