package com.mosty.qwzx.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.model.entity.jcgl.TbJcglXf;
import com.mosty.base.model.entity.qwzx.TbQwFa;
import com.mosty.base.model.query.jcgl.TbJcglXfQuery;
import com.mosty.base.model.query.qwzx.TbQwFaQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.qwzx.mapper.TbQwFaMapper;
import com.mosty.qwzx.remote.TbBaseAdaptRemoteService;
import com.mosty.qwzx.service.TbQwFaService;
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 TbQwFaServiceImpl extends ServiceImpl<TbQwFaMapper, TbQwFa>
        implements TbQwFaService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

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

    @Override
    public IPage<TbQwFa> paginQuery(TbQwFaQuery query) {
        query.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(query.getSsbmdm(), null));
        Page<TbQwFa> tbJcglXfPage = this.baseMapper.selectPage(new Page<>(query.getPageCurrent(), query.getPageSize()), new LambdaQueryWrapper<TbQwFa>()
                .likeRight(StringUtils.isNotBlank(query.getSsbmdm()), TbQwFa::getSsbmdm, query.getSsbmdm())
                .like(StringUtils.isNotBlank(query.getFamc()), TbQwFa::getFamc, query.getFamc())
        );
        return tbJcglXfPage;
    }

    @Override
    public Integer insert(TbQwFa fa) {

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

    @Override
    public Integer update(TbQwFa fa) {

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

    @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;
    }
}
