package cn.ey.biz.module.system.convert.abac;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.util.json.JsonUtils;
import cn.ey.biz.module.system.controller.admin.abac.vo.AbacVo;
import cn.ey.biz.module.system.dal.dataobject.abac.AbacDO;
import cn.ey.biz.module.system.framework.rule.RuleDefinition;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface AbacConvert {
    AbacConvert INSTANCE = Mappers.getMapper(AbacConvert.class);

    default AbacDO vo2Do(AbacVo abacVO) {
        AbacDO doo = new AbacDO();
        if (abacVO.getId() != null) {
            doo.setId(abacVO.getId());
        }
        doo.setApp(abacVO.getApp());
        doo.setName(abacVO.getName());
        doo.setMenus(JsonUtils.toJsonPrettyString(abacVO.getMenus()));
        doo.setRoleIds(JsonUtils.toJsonPrettyString(abacVO.getRoleIds()));
        doo.setCheckRoles(JsonUtils.toJsonPrettyString(abacVO.getCheckRoles()));
        doo.setRanks(JsonUtils.toJsonPrettyString(abacVO.getRanks()));
        doo.setRegions(JsonUtils.toJsonPrettyString(abacVO.getRegions()));
        doo.setMr(JsonUtils.toJsonPrettyString(abacVO.getMr()));
        doo.setSl(abacVO.getSl());
        doo.setSmu(abacVO.getSmu());
        doo.setSubSl(abacVO.getSsl());
        doo.setCompetencyCode(abacVO.getCompetencyCode());
        doo.setOu(JsonUtils.toJsonPrettyString(abacVO.getOu()));
        doo.setAadGroups(abacVO.getAadGroup());
        doo.setBu(JsonUtils.toJsonPrettyString(abacVO.getBu()));
        doo.setDyna(JsonUtils.toJsonPrettyString(abacVO.getDynamic()));
        doo.setRule(JsonUtils.toJsonPrettyString(abacVO.getRule()));
        doo.setState(abacVO.getStatus());
        doo.setScene(abacVO.getScene());
        doo.setServiceLine(JsonUtils.toJsonPrettyString(abacVO.getConfig()));
        return doo;
    }

    default AbacVo do2Vo(AbacDO doo) {
        AbacVo vo = new AbacVo();
        vo.setId(doo.getId());
        vo.setApp(doo.getApp());
        vo.setMenus(JsonUtils.parseArray(doo.getMenus(), Long.class));
        vo.setRoleIds(JsonUtils.parseArray(doo.getRoleIds(), Long.class));
        vo.setName(doo.getName());
        vo.setDynamic(JsonUtils.parseObject(doo.getDyna(), AbacVo.Pipe.class));
        vo.setRanks(JsonUtils.parseArray(doo.getRanks(), String.class));
        vo.setRegions(JsonUtils.parseArray(doo.getRegions(), String.class));
        vo.setCheckRoles(JsonUtils.parseArray(doo.getCheckRoles(), Long.class));
        vo.setRule(JsonUtils.parseObject(doo.getRule(), RuleDefinition.class));
        vo.setMr(JsonUtils.parseArray(doo.getMr(), String.class));
        vo.setOu(JsonUtils.parseArray(doo.getOu(), String.class));
        vo.setBu(JsonUtils.parseArray(doo.getBu(), String.class));
        vo.setAadGroup(doo.getAadGroups());
        vo.setSl(doo.getSl());
        vo.setSsl(doo.getSubSl());
        vo.setSmu(doo.getSmu());
        vo.setCompetencyCode(doo.getCompetencyCode());
        vo.setStatus(doo.getState());
        vo.setScene(doo.getScene());
        if (StringUtils.isNotBlank(doo.getServiceLine())) {
            vo.setConfig(JsonUtils.parseObject(doo.getServiceLine(), AbacVo.ServiceLineConfig.class));
        }
        return vo;
    }


    PageResult<AbacVo> convertPage(PageResult<AbacDO> page);

    List<AbacVo> dos2Vos(List<AbacDO> dos);

    @Mappings({
            @Mapping(expression = "java(ServiceLineAbacConverter.stringToServiceLineObject(vo.getConfig()))", target = "serviceLine"),
    })
    ServiceLineAbAcVoList abacVoConvert(AbacVo vo);
}

