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

import cn.hutool.core.collection.CollectionUtil;
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.TbJcglXfRwMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglXfRwService;
import com.mosty.base.model.entity.jcgl.TbJcglXf;
import com.mosty.base.model.entity.jcgl.TbJcglXfRw;
import com.mosty.base.model.query.jcgl.TbJcglXfRwQuery;
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 java.util.List;

@AllArgsConstructor
@Service
public class TbJcglXfRwServiceImpl extends ServiceImpl<TbJcglXfRwMapper, TbJcglXfRw>
        implements TbJcglXfRwService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

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

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

    @Override
    public Integer insert(TbJcglXfRw xf) {
        if (StringUtils.isEmpty(xf.getXfids())) {
            throw new BusinessException("巡访ids 不能为空！");
        }
        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());
        return this.baseMapper.insert(xf);
    }

    @Override
    public Integer update(TbJcglXfRw xf) {
        if (StringUtils.isEmpty(xf.getXfids())) {
            throw new BusinessException("巡访ids 不能为空！");
        }
        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 i = this.baseMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public Boolean deleteByIds(List<String> id) {
        if (CollectionUtil.isNotEmpty(id)) {
            int i = this.baseMapper.deleteBatchIds(id);
            return i > 0;
        }
        return false;
    }
}
