package com.mosty.yjzl.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.mosty.base.utils.UUIDGenerator;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.base.model.entity.yjzl.TbYjDyyjxx;
import com.mosty.base.model.query.yjzl.TbYjDyyjxxQuery;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.yjzl.mapper.TbYjDyyjxxMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjDyyjxxService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@AllArgsConstructor
@Service
public class TbYjDyyjxxServiceImpl implements TbYjDyyjxxService {

    private TbYjDyyjxxMapper tbYjDyyjxxMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbYjDyyjxx> queryByPage(TbYjDyyjxxQuery dto) {
        IPage<TbYjDyyjxx> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        QueryWrapper<TbYjDyyjxx> qw = new QueryWrapper<>();
//        UserInfo user = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, user);

        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        qw.lambda().eq(StringUtils.isNotBlank(dto.getDyryId()), TbYjDyyjxx::getDyryId, dto.getDyryId())
                .like(StringUtils.isNotBlank(dto.getDyryXm()), TbYjDyyjxx::getDyryXm, dto.getDyryXm())
                .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbYjDyyjxx::getSsbmdm, dto.getSsbmdm())
                .like(StringUtils.isNotBlank(dto.getDyrySfzh()), TbYjDyyjxx::getDyrySfzh, dto.getDyrySfzh())
                .eq(StringUtils.isNotBlank(dto.getYjId()), TbYjDyyjxx::getYjId, dto.getYjId())
                .eq(StringUtils.isNotBlank(dto.getDxjssjh()), TbYjDyyjxx::getDxjssjh, dto.getDxjssjh())
                .ge(dto.getYjkssj() != null, TbYjDyyjxx::getYjsj, dto.getYjkssj())
                .le(dto.getYjjssj() != null, TbYjDyyjxx::getYjsj, dto.getYjkssj());
        return tbYjDyyjxxMapper.selectPage(page, qw);
    }

    @Override
    public int insert(TbYjDyyjxx dyyjxx) {
        dyyjxx.setId(UUIDGenerator.getUUID());
        UserInfo user = UserInfoManager.get();
        dyyjxx.setSsbm(user.getDeptName());
        dyyjxx.setSsbmid(String.valueOf(user.getDeptId()));
        dyyjxx.setSsbmdm(user.getDeptCode());
        dyyjxx.setSsxgaj(user.getFxjDeptName());
        dyyjxx.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        dyyjxx.setSsxgajdm(user.getFxjDeptCode());
        dyyjxx.setSssgaj(user.getDszDeptName());
        dyyjxx.setSssgajid(String.valueOf(user.getDszDeptId()));
        dyyjxx.setSssgajdm(user.getDszDeptCode());
        return tbYjDyyjxxMapper.insert(dyyjxx);
    }

    @Override
    public int deleteById(List<String> list) {
        return tbYjDyyjxxMapper.deleteByIds(list);
    }
}
