package com.mosty.rzzx.service.impl;

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.mosty.common.base.util.StringUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.base.model.dto.rzzx.TbRzDyjkInsertDto;
import com.mosty.base.model.dto.rzzx.TbRzDyjkUpdateDto;
import com.mosty.base.model.entity.rzzx.TbRzDyjk;
import com.mosty.base.model.query.rzzx.TbRzDyjkQueryDto;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.rzzx.mapper.TbRzDyjkMapper;
import com.mosty.rzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.rzzx.service.TbRzDyjkService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author dw
 * @since 2022/7/18
 * 接口调用日志--服务--实现类
 **/
@AllArgsConstructor
@Service
public class TbRzDyjkServiceImpl extends ServiceImpl<TbRzDyjkMapper, TbRzDyjk>
        implements TbRzDyjkService {
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int insertEntity(TbRzDyjkInsertDto dto) {
        UserInfo user = UserInfoManager.get();
        TbRzDyjk item = new TbRzDyjk();
        BeanUtils.copyProperties(dto, item);
        item.setXtSjly("1");
        item.setCzrid(String.valueOf(user.getUserId()));
        item.setCzrsfzh(user.getIdEntityCard());
        item.setCzrxm(user.getUserName());
        item.setSsbm(user.getDeptName());
        item.setSsbmid(String.valueOf(user.getDeptId()));
        item.setSsbmdm(user.getDeptCode());
        item.setSsxgaj(user.getFxjDeptName());
        item.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        item.setSsxgajdm(user.getFxjDeptCode());
        item.setSssgaj(user.getDszDeptName());
        item.setSssgajid(String.valueOf(user.getDszDeptId()));
        item.setSssgajdm(user.getDszDeptCode());
        item.setDysj(new Date());
        return this.baseMapper.insert(item);
    }

    @Override
    public int updateEntity(TbRzDyjkUpdateDto dto) {
        TbRzDyjk item = this.baseMapper.selectById(dto.getId());
        if (item != null) {
            item.setJkfh(dto.getJkfh());
            item.setSfcg(dto.getSfcg());
            Date endDate = new Date();
            item.setFhsj(endDate);
            Integer time = (int) (endDate.getTime() - item.getDysj().getTime()) / 1000;
            item.setHs(time);
            return this.baseMapper.updateById(item);
        }
        return 0;
    }

    @Override
    public TbRzDyjk getInfo(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public IPage<TbRzDyjk> getList(TbRzDyjkQueryDto dto) {
        QueryWrapper<TbRzDyjk> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        return this.baseMapper.selectPage(
                new Page<>(dto.getPageNum(), dto.getPageSize()),
                qw.lambda()
                        .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbRzDyjk::getSsbmdm, dto.getSsbmdm())
                        .like(StringUtils.isNotBlank(dto.getCzrxm()), TbRzDyjk::getCzrxm, dto.getCzrxm())
                        .eq(StringUtils.isNotBlank(dto.getSfcg()), TbRzDyjk::getSfcg, dto.getSfcg())
                        .like(StringUtils.isNotBlank(dto.getJkxt()), TbRzDyjk::getJkxt, dto.getJkxt())
                        .ge(StringUtils.isNotBlank(dto.getStartTime()), TbRzDyjk::getDysj, dto.getStartTime())
                        .le(StringUtils.isNotBlank(dto.getEndTime()), TbRzDyjk::getDysj, dto.getEndTime())
        );
    }
}
