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

import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.market.ai.AreaSliceClaimAI;
import com.chinaweal.aiccs.aiccs.market.entity.AreaSlice;
import com.chinaweal.aiccs.aiccs.market.entity.BizOwnerAssign;
import com.chinaweal.aiccs.aiccs.market.entity.dto.SliceAssignLogDto;
import com.chinaweal.aiccs.aiccs.market.service.AreaSliceService;
import com.chinaweal.aiccs.aiccs.market.service.BizOwnerAssignService;
import com.chinaweal.aiccs.aiccs.market.service.InitService;
import com.chinaweal.aiccs.common.enums.SliceTypeEnums;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author ymc
 * @date 2022年04月25日 14:56
 */
@Service
@Slf4j(topic = "claim")
public class InitServiceImpl implements InitService {


    @Resource
    private AreaSliceService areaSliceService;
    
    @Resource
    private BizOwnerAssignService bizOwnerAssignService;
    
    @Override
    public void repairAssignOwner(String sliceId, String pripid) {
        // 数据校验
        AreaSlice areaSlice = areaSliceService.getById(sliceId);
        AssertUtils.isNotNull(areaSlice);
        SliceAssignLogDto assignLog = null;
        List<SliceAssignLogDto> claimLogList = bizOwnerAssignService.listBySliceInfo(null, pripid);
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(claimLogList)) {
            for (SliceAssignLogDto each : claimLogList) {
                if (each.getRemDate() == null) {
                    if (assignLog == null || Integer.parseInt(assignLog.getSliceType()) < Integer.parseInt(each.getSliceType())) {
                        assignLog = each;
                    }
                }
            }
        }
        SliceTypeEnums typeEnums = null;
        if (assignLog != null) {
            typeEnums = SliceTypeEnums.getEnumsByCode(assignLog.getSliceType());
        }
        // 片区是否在范围校验
        if (typeEnums != null && Integer.parseInt(typeEnums.getCode()) >= Integer.parseInt(areaSlice.getSliceType())) {
            log.error("主体id：{}，当前认领等级低于已认领等级", pripid);
        }

        String operator = AreaSliceClaimAI.OPERATOR;
        BizOwnerAssign bizOwnerAssign = null;
        if (SliceTypeEnums.REGION_PUBLIC.getCode().equals(areaSlice.getSliceType())) { // 区公共片区认领

            bizOwnerAssign = bizOwnerAssignService.areaClaim(areaSlice.getSliceId(), pripid, operator);
        } else if (SliceTypeEnums.STATION_PUBLIC.getCode().equals(areaSlice.getSliceType())) {  // 所公共片区认领

            // 如果认领的父片区id不等于该已认领的区公共片区id，则为错误数据
            if (areaSlice.getPSliceId() == null || assignLog != null
                    && !areaSlice.getPSliceId().equals(assignLog.getSliceId())) {
                log.error("主体id：{}，sliceId：{}，当前认领片区和已认领片区不存在关联关系", pripid, sliceId);
            }
            // 区补领
            if (assignLog == null) {
                bizOwnerAssignService.areaClaim(areaSlice.getPSliceId(), pripid, operator);
            }
            bizOwnerAssign = bizOwnerAssignService.areaClaim(areaSlice.getSliceId(), pripid, operator);
        } else { // 指定片区认领
            log.error("主体id：{}，sliceId：{}，认领逻辑出错，初始化补认领不存在子片区认领", pripid, sliceId);
        }

    }
 
}
