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.utils.PageUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.dto.jcgl.TbJcglShabllInsertAndEditDto;
import com.mosty.base.model.entity.jcgl.TbJcglShabll;
import com.mosty.base.model.query.jcgl.TbJcglShabllQuery;
import com.mosty.base.manager.mapper.TbJcglShabllMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglShabllService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author dw
 * @since 2022/8/1
 * 社会安保力量服务-实现类
 **/
@Service
public class TbJcglShabllServiceImpl extends ServiceImpl<TbJcglShabllMapper, TbJcglShabll>
        implements TbJcglShabllService {

    @Resource
    private TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int addEntity(TbJcglShabllInsertAndEditDto dto) {
        TbJcglShabll shabll = new TbJcglShabll();
        BeanUtils.copyProperties(dto, shabll);
        shabll.setId(UUIDGenerator.getUUID());
        shabll.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmdm());
        if (dept != null) {
            shabll.setSsbm(dept.getDeptname());
            shabll.setSsbmid(dept.getDeptid());
            shabll.setSsbmdm(dept.getDeptcode());
            shabll.setSsxgajdm(dept.getFxjcode());
            shabll.setSsxgajid(dept.getFxjid());
            shabll.setSsxgaj(dept.getFxjname());
            shabll.setSssgaj(dept.getDszname());
            shabll.setSssgajdm(dept.getDszcode());
            shabll.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.insert(shabll);
    }

    @Override
    public int editEntity(TbJcglShabllInsertAndEditDto dto) {
        TbJcglShabll shabll = new TbJcglShabll();
        BeanUtils.copyProperties(dto, shabll);
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(shabll.getSsbmdm());
        if (dept != null) {
            shabll.setSsbm(dept.getDeptname());
            shabll.setSsbmid(dept.getDeptid());
            shabll.setSsbmdm(dept.getDeptcode());
            shabll.setSsxgajdm(dept.getFxjcode());
            shabll.setSsxgajid(dept.getFxjid());
            shabll.setSsxgaj(dept.getFxjname());
            shabll.setSssgaj(dept.getDszname());
            shabll.setSssgajdm(dept.getDszcode());
            shabll.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.updateById(shabll);
    }

    @Override
    public IPage<TbJcglShabll> getPageList(TbJcglShabllQuery dto) {
        IPage<TbJcglShabll> page = PageUtils.buildPage(dto.getPageSize(), dto.getPageCurrent());
        QueryWrapper<TbJcglShabll> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        qw.lambda().like(StringUtils.isNotBlank(dto.getXm()), TbJcglShabll::getXm, dto.getXm())
                .like(StringUtils.isNotBlank(dto.getSfzh()), TbJcglShabll::getSfzh, dto.getSfzh())
                .eq(StringUtils.isNotBlank(dto.getAblllx()), TbJcglShabll::getAblllx, dto.getAblllx())
                .eq(StringUtils.isNotBlank(dto.getXbdm()), TbJcglShabll::getXbdm, dto.getXbdm())
                .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbJcglShabll::getSsbmdm, dto.getSsbmdm());
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    public Integer delEntity(List<String> ids) {
        return this.baseMapper.deleteBatchIds(ids);
    }

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