package com.mosty.base.manager.service.impl;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.manager.mapper.TbJcglDsfMapper;
import com.mosty.base.manager.service.TbJcglDsfService;
import com.mosty.base.model.dto.jcgl.TbJcglDsfDto;
import com.mosty.base.model.dto.jcgl.TbJcglDsfJudgeDto;
import com.mosty.base.model.entity.jcgl.TbJcglAbfc;
import com.mosty.base.model.entity.jcgl.TbJcglDsf;
import com.mosty.base.model.query.jcgl.TbJcglDsfQuery;
import com.mosty.base.model.vo.jcgl.TbJcglDsfVo;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.symmetric.Noekeon;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
public class TbJcglDsfServiceImpl extends ServiceImpl<TbJcglDsfMapper, TbJcglDsf>
        implements TbJcglDsfService {

    @Override
    public int addEntity(TbJcglDsfDto dto) {
        TbJcglDsf item = new TbJcglDsf();
        BeanUtils.copyProperties(dto, item);
        item.setCode(UuidUtils.generateUuid());
        item.setZt("01");
        item.setXtSjly("1");
        return this.baseMapper.insert(item);
    }

    @Override
    public int editEntity(TbJcglDsfDto dto) {
        TbJcglDsf item = new TbJcglDsf();
        BeanUtils.copyProperties(dto, item);
        return this.baseMapper.updateById(item);
    }

    @Override
    public void deleteEntity(List<String> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }

    @Override
    public IPage<TbJcglDsf> getPageList(TbJcglDsfQuery dto) {
        return this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbJcglDsf>()
                        .like(StringUtils.isNotBlank(dto.getXtmc()), TbJcglDsf::getXtmc, dto.getXtmc())
                        .like(StringUtils.isNotBlank(dto.getZrrxm()), TbJcglDsf::getZrrxm, dto.getZrrxm())
                        .eq(StringUtils.isNotBlank(dto.getZt()), TbJcglDsf::getZt, dto.getZt())
                        .eq(StringUtils.isNotBlank(dto.getFwlx()), TbJcglDsf::getFwlx, dto.getFwlx())
        );
    }

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

    @Override
    public int judge(TbJcglDsfJudgeDto dto) {
        TbJcglDsf dsf = this.baseMapper.selectById(dto.getId());
        if (dsf == null) {
            throw new BusinessException("第三方数据不存在");
        }
        dsf.setZt(dto.getZt());
        return this.baseMapper.updateById(dsf);
    }

    @Override
    public void checkDsf() {
        List<TbJcglDsf> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<TbJcglDsf>()
                        .eq(TbJcglDsf::getXtSjzt, "1")
                        .eq(TbJcglDsf::getZt, "2")
        );
        list.forEach(item -> {
            Date time = item.getGqsj();
            Date now = new Date();
            if (now.getTime() >= time.getTime()) {
                item.setZt("05");
                this.baseMapper.updateById(item);
            }
        });
    }

    @Override
    public TbJcglDsfVo getDsfInfo(String code) {
        TbJcglDsf tbJcglDsf = this.baseMapper.selectOne(new LambdaQueryWrapper<TbJcglDsf>()
                .eq(StringUtils.isNotBlank(code),TbJcglDsf::getCode,code)
                .eq(TbJcglDsf::getZt,"02"));
        TbJcglDsfVo vo = new TbJcglDsfVo();
        if (null==tbJcglDsf){
            return null;
        }
        BeanUtils.copyProperties(tbJcglDsf, vo);
        return vo;
    }

}
