package com.chinaweal.aiccs.aiccs.seriousillegalPerson.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.chinaweal.aiccs.aiccs.seriousillegalPerson.entity.XrSeriousIllegalPersonList;
import com.chinaweal.aiccs.aiccs.seriousillegalPerson.entity.dto.SeriousIllegalListQueryDto;
import com.chinaweal.aiccs.aiccs.seriousillegalPerson.mapper.XrSeriousIllegalPersonListMapper;
import com.chinaweal.aiccs.aiccs.seriousillegalPerson.service.IXrSeriousIllegalPersonListService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.entity.OrgUnits;
import com.chinaweal.aiccs.org.service.AicorgService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 严重违法失信自然人名单表 服务实现类
 *
 * @author zhouxy
 * @since 2025-07-25
 */
@Service
public class XrSeriousIllegalPersonListServiceImpl extends ServiceImpl<XrSeriousIllegalPersonListMapper, XrSeriousIllegalPersonList> implements IXrSeriousIllegalPersonListService {

    @Resource
    private XrSeriousIllegalPersonListMapper xrSeriousIllegalPersonListMapper;

    @Autowired
    private AicorgService aicorgService;

    @Override
    public XrSeriousIllegalPersonList checkDecOrg(XrSeriousIllegalPersonList xrSeriousIllegalPersonList, String orgID, int illegalType){
        //查询当前机构的层级
        OrgUnits org = aicorgService.getOne(new LambdaQueryWrapper<OrgUnits>().eq(OrgUnits::getOrgUnitId, orgID)
                .eq(OrgUnits::getDeleted, "0"));
        String regionID = org.getOrgNumber();
        String orgName = org.getOrgUnitName();
        if(org.getOrgLevel() == 5){
            //若是所一级则查上级的区县一级
            OrgUnits parentOrg = aicorgService.getOne(new LambdaQueryWrapper<OrgUnits>().eq(OrgUnits::getOrgUnitId, org.getParentId())
                    .eq(OrgUnits::getDeleted, "0"));
            regionID = parentOrg.getOrgNumber();
            orgName = parentOrg.getOrgUnitName();
        }

        if(illegalType == 0){   //列入
            xrSeriousIllegalPersonList.setDecOrg(regionID);
            xrSeriousIllegalPersonList.setDecOrgCn(orgName);
        } else if(illegalType == 1){   //移出
            xrSeriousIllegalPersonList.setRemDecOrg(regionID);
            xrSeriousIllegalPersonList.setRemDecOrgCn(orgName);
        }
        return xrSeriousIllegalPersonList;
    }

    @Override
    public IPage<XrSeriousIllegalPersonList> listXrSeriousIllegalPersonList(BaseQuery<SeriousIllegalListQueryDto> query, AICUser user) {
        SeriousIllegalListQueryDto entity = query.getEntity(SeriousIllegalListQueryDto.class);
        Page<XrSeriousIllegalPersonList> page = new Page<>(query.getCurrent(), query.getSize());

        entity.setSuperViseDeptId(org.apache.commons.lang3.StringUtils.left(user.getRegionID(), 6));
        LambdaQueryWrapper<XrSeriousIllegalPersonList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XrSeriousIllegalPersonList::getIsHandling,"0")
                .eq(XrSeriousIllegalPersonList::getOperationStatus,"0")
                .likeRight(XrSeriousIllegalPersonList::getAreaCode, entity.getSuperViseDeptId())
                .orderByDesc(XrSeriousIllegalPersonList::getAbnTime);
        if(StringUtils.isNotEmpty(entity.getInv())){
            queryWrapper.like(XrSeriousIllegalPersonList::getInv, entity.getInv());
        }
        if(StringUtils.isNotEmpty(entity.getCerType())){
            queryWrapper.eq(XrSeriousIllegalPersonList::getCerType, entity.getCerType());
        }
        if(StringUtils.isNotEmpty(entity.getCerNo())){
            queryWrapper.eq(XrSeriousIllegalPersonList::getCerNo, entity.getCerNo());
        }
        if(StringUtils.isNotEmpty(entity.getSerIllRea())){
            queryWrapper.eq(XrSeriousIllegalPersonList::getSerIllRea, entity.getSerIllRea());
        }
        if(StringUtils.isNotEmpty(entity.getAbnDateStart()) && StringUtils.isNotEmpty(entity.getAbnDateEnd())){
            queryWrapper.between(XrSeriousIllegalPersonList::getAbnTime, entity.getAbnDateStart(), entity.getAbnDateEnd());
        }
        return page(page, queryWrapper);
    }
}
