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

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.TbJcglXfMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglXfService;
import com.mosty.base.model.entity.jcgl.TbJcglXf;
import com.mosty.base.model.query.jcgl.TbJcglXfQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@AllArgsConstructor
@Service
public class TbJcglXfServiceImpl extends ServiceImpl<TbJcglXfMapper, TbJcglXf>
        implements TbJcglXfService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

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

    @Override
    public IPage<TbJcglXf> paginQuery(TbJcglXfQuery query) {
        query.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(query.getSsbmdm(), null));
        Page<TbJcglXf> tbJcglXfPage = this.baseMapper.selectPage(new Page<>(query.getPageCurrent(), query.getPageSize()), new LambdaQueryWrapper<TbJcglXf>()
                .likeRight(StringUtils.isNotBlank(query.getSsbmdm()), TbJcglXf::getSsbmdm, query.getSsbmdm())
                .eq(StringUtils.isNotBlank(query.getXflx()), TbJcglXf::getXflx, query.getXflx())
                .like(StringUtils.isNotBlank(query.getXfdw()), TbJcglXf::getXfdw, query.getXfdw())
                .like(StringUtils.isNotBlank(query.getXfmj()), TbJcglXf::getXfzzXm, query.getXfmj())
                .eq(StringUtils.isNotBlank(query.getRwzt()), TbJcglXf::getRwzt, query.getRwzt())
                .ge(StringUtils.isNotBlank(query.getKssj()), TbJcglXf::getXfsj, query.getKssj())
                .le(StringUtils.isNotBlank(query.getJssj()), TbJcglXf::getXfsj, query.getJssj())
                .orderByDesc(TbJcglXf::getXtCjsj)
        );
        return tbJcglXfPage;
    }

    @Override
    public Integer insert(TbJcglXf xf) {

        if (StringUtils.isEmpty(xf.getSsbmdm())) {
            UserInfo userInfo = UserInfoManager.get();
            xf.setSsbmdm(userInfo.getDeptCode());
        }
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xf.getSsbmdm());
        if (ObjectUtils.isNotEmpty(dept)) {
            xf.setSsbm(dept.getDeptname());
            xf.setSsbmid(dept.getDeptid());
            xf.setSsbmdm(dept.getDeptcode());
            xf.setSsxgajdm(dept.getFxjcode());
            xf.setSsxgajid(dept.getFxjid());
            xf.setSsxgaj(dept.getFxjname());
            xf.setSssgaj(dept.getDszname());
            xf.setSssgajdm(dept.getDszcode());
            xf.setSssgajid(dept.getDszid());
        }
        xf.setId(UUIDGenerator.getUUID());
        xf.setRwzt("1");
        return this.baseMapper.insert(xf);
    }

    @Override
    public Integer update(TbJcglXf xf) {

        if (StringUtils.isEmpty(xf.getSsbmdm())) {
            UserInfo userInfo = UserInfoManager.get();
            xf.setSsbmdm(userInfo.getDeptCode());
        }
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xf.getSsbmdm());
        if (ObjectUtils.isNotEmpty(dept)) {
            xf.setSsbm(dept.getDeptname());
            xf.setSsbmid(dept.getDeptid());
            xf.setSsbmdm(dept.getDeptcode());
            xf.setSsxgajdm(dept.getFxjcode());
            xf.setSsxgajid(dept.getFxjid());
            xf.setSsxgaj(dept.getFxjname());
            xf.setSssgaj(dept.getDszname());
            xf.setSssgajdm(dept.getDszcode());
            xf.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.updateById(xf);
    }

    @Override
    public Boolean deleteById(String id) {
        int total = this.baseMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public Boolean deleteByIds(List<String> id) {
        if (!CollectionUtils.isEmpty(id)) {
            int total = this.baseMapper.deleteBatchIds(id);
            return total > 0;
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> xfbmtj(TbJcglXfQuery query) {
        query.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(query.getSsbmdm(), null));
        return this.baseMapper.xfbmtj(query);
    }


    @Override
    public int bacthSb(List<String> id) {
        if (!CollectionUtils.isEmpty(id)) {
            return this.baseMapper.bacthSb(id);
        }
        return 0;
    }

}
