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.TbJcglXfDwglMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglXfDwglService;
import com.mosty.base.model.entity.jcgl.TbJcglXfDwgl;
import com.mosty.base.model.entity.jcgl.TbJcglXfZg;
import com.mosty.base.model.query.jcgl.TbJcglXfDwglQuery;
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 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;

@AllArgsConstructor
@Service
public class TbJcglXfDwglServiceImpl extends ServiceImpl<TbJcglXfDwglMapper, TbJcglXfDwgl>
        implements TbJcglXfDwglService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

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

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

    @Override
    public Integer insert(TbJcglXfDwgl 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());
        return this.baseMapper.insert(xf);
    }

    @Override
    public Integer update(TbJcglXfDwgl 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;
    }
}
