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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.model.entity.jcgl.TbJcglZjk;
import com.mosty.base.model.query.jcgl.TbJcglZjkQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.manager.mapper.TbJcglZjkMapper;
import com.mosty.base.manager.service.TbJcglZjkService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 基础管理-专家库 服务实现类
 * </p>
 *
 * @author zengbo
 * @since 2022-07-28
 */
@AllArgsConstructor
@Service
public class TbJcglZjkServiceImpl extends ServiceImpl<TbJcglZjkMapper, TbJcglZjk>
        implements TbJcglZjkService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int insert(TbJcglZjk zjk) {
        zjk.setId(UUIDGenerator.getUUID());
        if (StringUtils.isNotBlank(zjk.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(zjk.getSsbmdm());
            zjk.setSsbm(dept.getDeptname());
            zjk.setSsbmid(dept.getDeptid());
            zjk.setSsbmdm(dept.getDeptcode());
            zjk.setSsxgajdm(dept.getFxjcode());
            zjk.setSsxgajid(dept.getFxjid());
            zjk.setSsxgaj(dept.getFxjname());
            zjk.setSssgaj(dept.getDszname());
            zjk.setSssgajdm(dept.getDszcode());
            zjk.setSssgajid(dept.getDszid());
        }
        zjk.setXtSjly("1");
        return this.baseMapper.insert(zjk);
    }

    @Override
    public IPage<TbJcglZjk> queryPage(TbJcglZjkQuery item) {
        IPage<TbJcglZjk> page = PageUtils.buildPage(item.getPageSize(), item.getPageCurrent());
        QueryWrapper<TbJcglZjk> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        item.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(item.getSsbmdm(), null));
        qw.lambda().eq(TbJcglZjk::getXtScbz, 0)
                .like(StringUtils.isNotBlank(item.getZjly()), TbJcglZjk::getZjly, item.getZjly())
                .eq(StringUtils.isNotBlank(item.getGlryId()), TbJcglZjk::getGlryId, item.getGlryId())
                .like(StringUtils.isNotBlank(item.getZjxm()), TbJcglZjk::getZjxm, item.getZjxm())
                .eq(StringUtils.isNotBlank(item.getZjsfzh()), TbJcglZjk::getZjsfzh, item.getZjsfzh())
                .eq(StringUtils.isNotBlank(item.getGlbmdm()), TbJcglZjk::getGlbmdm, item.getGlbmdm())
                .likeRight(StringUtils.isNotBlank(item.getSsbmdm()), TbJcglZjk::getSsbmdm, item.getSsbmdm());
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    public int update(TbJcglZjk zjk) {
        if (StringUtils.isNotBlank(zjk.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(zjk.getSsbmdm());
            zjk.setSsbm(dept.getDeptname());
            zjk.setSsbmid(dept.getDeptid());
            zjk.setSsbmdm(dept.getDeptcode());
            zjk.setSsxgajdm(dept.getFxjcode());
            zjk.setSsxgajid(dept.getFxjid());
            zjk.setSsxgaj(dept.getFxjname());
            zjk.setSssgaj(dept.getDszname());
            zjk.setSssgajdm(dept.getDszcode());
            zjk.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.updateById(zjk);
    }

    @Override
    public int deleteById(List<String> list) {
        return this.baseMapper.deleteBatch(list);
    }

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