package com.chinaweal.aiccs.aiccs.market.service.impl;

import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.market.ai.AreaSliceClaimAI;
import com.chinaweal.aiccs.aiccs.market.entity.BizOwnerAssign;
import com.chinaweal.aiccs.aiccs.market.entity.EntAddressChangeLog;
import com.chinaweal.aiccs.aiccs.market.entity.dto.AssignEntity;
import com.chinaweal.aiccs.aiccs.market.mapper.EntAddressChangeLogMapper;
import com.chinaweal.aiccs.aiccs.market.service.BizOwnerAssignService;
import com.chinaweal.aiccs.aiccs.market.service.IEntAddressChangeLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 企业主体地址更新审计表 服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2022-07-07
 */
@Service
@Slf4j(topic = "claim")
public class EntAddressChangeLogServiceImpl extends ServiceImpl<EntAddressChangeLogMapper, EntAddressChangeLog> implements IEntAddressChangeLogService {

    @Resource
    private AreaSliceClaimAI areaSliceClaimAI;
    @Resource
    private EBaseinfoService eBaseinfoService;
    @Resource
    private BizOwnerAssignService bizOwnerAssignService;

    @Override
    public EntAddressChangeLog saveEntAddressChangeLog(EntAddressChangeLog entAddressChangeLog) {
        save(entAddressChangeLog);
        return entAddressChangeLog;
    }

    @Override
    public void updateEntAddressChangeLog(EntAddressChangeLog entAddressChangeLog) {
        updateById(entAddressChangeLog);
    }

    @Override
    public IPage<EntAddressChangeLog> listEntAddressChangeLog(BaseQuery<EntAddressChangeLog> query) {
        EntAddressChangeLog entity = query.getEntity(EntAddressChangeLog.class);
        Page<EntAddressChangeLog> page = query.getPage();

        LambdaQueryWrapper<EntAddressChangeLog> lambdaQueryWrapper = Wrappers.lambdaQuery();

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public void autoClaimAIForAddressChange(EntAddressChangeLog entAddressChangeLog) {

        String pripid = entAddressChangeLog.getPripid();
        EBaseinfo each = eBaseinfoService.getOneByPripid(pripid);

        if (each == null) throw new BusinessException("查不出改企业");

        if (!"1".equals(each.getRegstate())) return;

        // 清空认领记录
        eBaseinfoService.clearRegulatory(pripid);
        bizOwnerAssignService.lambdaUpdate().eq(BizOwnerAssign::getPripid, pripid).isNull(BizOwnerAssign::getRemType)
                .update(new BizOwnerAssign()
                        .setRemDate(LocalDateTime.now())
                        .setRemReason("地址变更调整")
                        .setRemType("1")
                        .setRemOperatorName("system")
                        .setUpdateTime(LocalDateTime.now()));


        AssignEntity entity = new AssignEntity()
                .setAddress(StringUtils.isNotBlank(each.getDom()) ? each.getDom() : each.getAddress())
                .setPripid(each.getPripid())
                .setEntName(each.getEntname())
                .setEntType(each.getEnttype())
                .setLerep(each.getLerep())
                .setRegNo(each.getRegno())
                .setRegOrg(each.getRegorg())
                .setRegulatory(each.getSuporg())
                .setUniscid(each.getUniscid());

        // 重新认领
        areaSliceClaimAI.claimInScope(entity, null);

    }

}
