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.IpUtil;
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.TbRzSpbfInsertDto;
import com.mosty.base.model.entity.rzzx.TbRzSpbf;
import com.mosty.base.model.query.rzzx.TbRzSpbfQueryDto;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.rzzx.mapper.TbRzSpbfMapper;
import com.mosty.rzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.rzzx.service.TbRzSpbfService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author dw
 * @since 2022/7/12
 * 视频播放日志服务-实现类
 **/
@AllArgsConstructor
@Service
public class TbRzSpbfServiceImpl extends ServiceImpl<TbRzSpbfMapper, TbRzSpbf>
        implements TbRzSpbfService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

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

    @Override
    public int updateEntity(String id) {
        TbRzSpbf item = this.baseMapper.selectById(id);
        item.setJssj(new Date());
        return this.baseMapper.updateById(item);
    }

    @Override
    public IPage<TbRzSpbf> getPageList(TbRzSpbfQueryDto dto) {
        QueryWrapper<TbRzSpbf> 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.ge(StringUtils.isNotBlank(dto.getKssj()), "date_format(kssj,'%Y-%m-%d %H:%i:%s')", dto.getKssj())
                        .le(StringUtils.isNotBlank(dto.getJssj()), "date_format(kssj,'%Y-%m-%d %H:%i:%s')", dto.getJssj())
                        .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), "ssbmdm", dto.getSsbmdm())
                        .and(StringUtils.isNotBlank(dto.getKeyword()),
                                wq -> wq.like("sxtid", dto.getKeyword()).or().like("sxtmc", dto.getKeyword())
                        )
                        .orderByDesc("xt_cjsj")
        );
    }

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

    @Override
    public Boolean updateLyId(String lyId) {
        List<TbRzSpbf> ly_id = baseMapper.selectList(new QueryWrapper<TbRzSpbf>().eq(org.apache.commons.lang3.StringUtils.isNotBlank(lyId), "lyid", lyId)
        .isNotNull("jssj")
        );
        if(!(ly_id !=null && ly_id.size()>0)){
            TbRzSpbf tbRzSpbf = new TbRzSpbf();
            tbRzSpbf.setJssj(new Date());
            tbRzSpbf.setLyid(lyId);
            baseMapper.update(tbRzSpbf, new QueryWrapper<TbRzSpbf>().eq(org.apache.commons.lang3.StringUtils.isNotBlank(lyId), "lyid", lyId));
        }
        return true;
    }
}
