package com.friday.tooldriver.service.rpa.taieoc;

import com.friday.base.database.annotation.ListPage;
import com.friday.base.common.constants.SystemCode;
import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.keepalive.constant.taieoc.TableName;
import com.friday.operation.api.keepalive.dto.taieoc.BadgeFilterDto;
import com.friday.operation.api.keepalive.dto.taieoc.BadgeInfoDto;
import com.friday.operation.api.keepalive.dto.taieoc.BadgePageDto;
import com.friday.operation.api.keepalive.dto.taieoc.BadgeReqDto;
import com.friday.operation.api.keepalive.service.taieoc.IBadgeManage;
import com.friday.tooldriver.constants.TaieOcManageCode;
import com.friday.tooldriver.mapper.taieoc.BadgeMapper;
import com.friday.tooldriver.mapper.taieoc.OcMapper;
import com.friday.tooldriver.mapper.taieoc.OcTenantMapper;
import com.friday.tooldriver.po.taieoc.BadgePo;
import com.friday.tooldriver.po.taieoc.OcInfoPo;
import com.friday.tooldriver.po.taieoc.OcTenantPo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BadgeManageImpl implements IBadgeManage {

    @Autowired
    private BadgeMapper badgeMapper;

    @Autowired
    private OcTenantMapper tenantMapper;

    @Autowired
    private OcMapper ocMapper;

    /**
     * 新增工牌信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DubboResult createBadgeInfo(BadgeReqDto reqDto) {
        try{
            if(StringUtils.isEmpty(reqDto.getBadgeType()) || ObjectUtils.isEmpty(reqDto.getOcTenantId()) || ObjectUtils.isEmpty(reqDto.getBadgeNum())
                || StringUtils.isAnyEmpty(reqDto.getBadgeSecretKey(), reqDto.getBadgeVersion())){
                return DubboResult.newFailure(TaieOcManageCode.PARAM_VERIFY_ERROR.getCode(), TaieOcManageCode.PARAM_VERIFY_ERROR.getMessage());
            }
            badgeMapper.insert(BadgePo.createPO(reqDto));
            return DubboResult.newSuccess(TaieOcManageCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error(TaieOcManageCode.FAIL.toString(e.getMessage()),e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return DubboResult.newFailure(TaieOcManageCode.FAIL.getCode(),TaieOcManageCode.FAIL.getMessage());
        }
    }

    @Override
    @ListPage(tableName = TableName.OC_BADGE_PAGE_NAME)
    public DubboResult<ArrayList<BadgePageDto>> queryBadgePage(BadgeFilterDto filterDto) {
        List<BadgePo> badgePos = badgeMapper.queryBadge(filterDto);
        Map<Long, List<BadgeInfoDto>> collect = badgePos.stream().map(BadgePo::createInfo).collect(Collectors.groupingBy(BadgeInfoDto::getOcTenantId));
        ArrayList<BadgePageDto> badgePageDto = collect.keySet().stream().map(e -> {
            OcTenantPo ocTenantPo = tenantMapper.selectByPrimaryKey(e);
            OcInfoPo ocInfoPo = ocMapper.selectByPrimaryKey(ocTenantPo.getOcInfoId());
            return BadgePageDto.builder()
                    .badgeInfoDtoList(collect.get(e))
                    .tenant(ocTenantPo.getOcTenant())
                    .supplier(ocInfoPo.getSupplier())
                    .ocInfo(ocInfoPo.getOcInfo())
                    .build();
        }).filter(f->StringUtils.isEmpty(filterDto.getOcInfo()) || f.getOcInfo().contains(filterDto.getOcInfo())).collect(Collectors.toCollection(ArrayList::new));
        return DubboResult.newSuccess(badgePageDto);
    }

    @Override
    public DubboResult updateBadgeInfo(BadgeReqDto reqDto) {
        try{
            badgeMapper.update(BadgePo.createPO(reqDto));
            return DubboResult.newSuccess(TaieOcManageCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error(TaieOcManageCode.FAIL.toString(e.getMessage()),e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return DubboResult.newFailure(TaieOcManageCode.FAIL.getCode(),TaieOcManageCode.FAIL.getMessage());
        }
    }
}
