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.TbRzHjInsertDto;
import com.mosty.base.model.dto.rzzx.TbRzHjUpdateDto;
import com.mosty.base.model.entity.rzzx.TbRzHj;
import com.mosty.base.model.query.rzzx.TbRzHjQueryDto;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.rzzx.mapper.TbRzHjMapper;
import com.mosty.rzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.rzzx.service.TbRzHjService;
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 TbRzHjServiceImpl extends ServiceImpl<TbRzHjMapper, TbRzHj> implements TbRzHjService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    @Override
    public int insertEntity(TbRzHjInsertDto dto) {
        UserInfo user = UserInfoManager.get();
        TbRzHj hj = new TbRzHj();
        BeanUtils.copyProperties(dto, hj);
        hj.setFjrId(String.valueOf(user.getUserId()));
        hj.setFjrSfzh(user.getIdEntityCard());
        hj.setFjrXm(user.getUserName());
        hj.setKssj(new Date());
        hj.setSsbm(user.getDeptName());
        hj.setSsbmid(String.valueOf(user.getDeptId()));
        hj.setSsbmdm(user.getDeptCode());
        hj.setSsxgaj(user.getFxjDeptName());
        hj.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        hj.setSsxgajdm(user.getFxjDeptCode());
        hj.setSssgaj(user.getDszDeptName());
        hj.setSssgajid(String.valueOf(user.getDszDeptId()));
        hj.setSssgajdm(user.getDszDeptCode());
        return this.baseMapper.insert(hj);
    }

    @Override
    public int updateEntity(TbRzHjUpdateDto dto) {
        TbRzHj hj = this.baseMapper.selectById(dto.getId());
        if (hj != null) {
            hj.setSfcg(dto.getSfcg());
            hj.setJssj(new Date());
            return this.baseMapper.updateById(hj);
        }
        return 0;
    }

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

    @Override
    public IPage<TbRzHj> getList(TbRzHjQueryDto dto) {
        QueryWrapper<TbRzHj> 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()
                        .like(StringUtils.isNotBlank(dto.getFjrXm()), TbRzHj::getFjrXm, dto.getFjrXm())
                        .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbRzHj::getSsbmdm, dto.getSsbmdm())
                        .eq(StringUtils.isNotBlank(dto.getSfcg()), TbRzHj::getSfcg, dto.getSfcg())
                        .like(StringUtils.isNotBlank(dto.getHjhm()), TbRzHj::getHjhm, dto.getHjhm())
                        .ge(StringUtils.isNotBlank(dto.getStartTime()), TbRzHj::getKssj, dto.getStartTime())
                        .le(StringUtils.isNotBlank(dto.getEndTime()), TbRzHj::getKssj, dto.getEndTime())
        );
    }
}
