package com.mosty.rzzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.base.model.dto.rzzx.TbRzXtfkInsertDto;
import com.mosty.base.model.entity.rzzx.TbRzXtfk;
import com.mosty.base.model.query.rzzx.TbRzXtfkQueryDto;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.rzzx.mapper.TbRzXtfkMapper;
import com.mosty.rzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.rzzx.service.TbRzXtfkService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @author dw
 * @since 2022/8/4
 **/
@AllArgsConstructor
@Service
public class TbRzXtfkServiceImpl extends ServiceImpl<TbRzXtfkMapper, TbRzXtfk>
        implements TbRzXtfkService {
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int insertEntity(TbRzXtfkInsertDto dto) {
        TbRzXtfk fk = new TbRzXtfk();
        BeanUtils.copyProperties(dto, fk);
        fk.setFksj(new Date());
        fk.setId(UUIDGenerator.getUUID());
        UserInfo user = UserInfoManager.get();
        if (user != null) {
            fk.setFkrid(String.valueOf(user.getUserId()));
            fk.setFkrsfzh(user.getIdEntityCard());
            fk.setFkrxm(user.getUserName());
            fk.setSsbm(user.getDeptName());
            fk.setSsbmid(String.valueOf(user.getDeptId()));
            fk.setSsbmdm(user.getDeptCode());
            fk.setSsxgaj(user.getFxjDeptName());
            fk.setSsxgajid(String.valueOf(user.getFxjDeptId()));
            fk.setSsxgajdm(user.getFxjDeptCode());
            fk.setSssgaj(user.getDszDeptName());
            fk.setSssgajid(String.valueOf(user.getDszDeptId()));
            fk.setSssgajdm(user.getDszDeptCode());
            fk.setXtSjly("2");
        }
        return this.baseMapper.insert(fk);
    }

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

    @Override
    public IPage<TbRzXtfk> getList(TbRzXtfkQueryDto dto) {
//        UserInfo user = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, user);

        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        QueryWrapper<TbRzXtfk> qw = new QueryWrapper<>();
        return this.baseMapper.selectPage(
                new Page<>(dto.getPageNum(), dto.getPageSize()),
                qw.lambda()
                        .eq(StringUtils.isNotBlank(dto.getFklx()), TbRzXtfk::getFklx, dto.getFklx())
                        .like(StringUtils.isNotBlank(dto.getFkxt()), TbRzXtfk::getFkxt, dto.getFkxt())
                        .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbRzXtfk::getSsbmdm, dto.getSsbmdm())
                        .like(StringUtils.isNotBlank(dto.getFknr()), TbRzXtfk::getFknr, dto.getFknr())
                        .like(StringUtils.isNotBlank(dto.getFkrxm()), TbRzXtfk::getFkrxm, dto.getFkrxm())
                        .eq(StringUtils.isNotBlank(dto.getSsbmdm()), TbRzXtfk::getSsbmdm, dto.getSsbmdm())
                        .ge(StringUtils.isNotBlank(dto.getStartTime()), TbRzXtfk::getFksj, dto.getStartTime())
                        .le(StringUtils.isNotBlank(dto.getEndTime()), TbRzXtfk::getFksj, dto.getEndTime())
                        .eq(TbRzXtfk::getXtScbz, "0").orderByDesc(TbRzXtfk::getXtCjsj)
        );
    }

    @Override
    public Boolean updateEntity(TbRzXtfkInsertDto dto) {
        TbRzXtfk fk = new TbRzXtfk();
        BeanUtils.copyProperties(dto, fk);
        UserInfo user = UserInfoManager.get();
        System.err.println(user);
        fk.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        this.baseMapper.updateById(fk);
        return true;
    }

    @Override
    public void deleteEntity(String id) {
        this.baseMapper.updateEntity(id);
    }
}
