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

import cn.ey.biz.common.util.json.JsonUtils;
import cn.ey.biz.module.system.api.code.dto.CodeEpDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeEpVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.dal.dataobject.code.CodeEpDO;
import cn.ey.biz.module.system.dal.dataobject.code.CodeEpVerDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface CodeEpConvert {

    CodeEpConvert INSTANCE = Mappers.getMapper(CodeEpConvert.class);

    default CodeEpDO rule2Do(CodeEpVO.Rule rule) {

        CodeEpDO codeEpDO = new CodeEpDO();
        codeEpDO.setCountry(rule.getCountry());
        codeEpDO.setSl(rule.getSl());
        codeEpDO.setRegion(rule.getRegion());
        codeEpDO.setRankType(rule.getCondition().getRankCode());
//        codeEpDO.setRankTypeName(rule.getCondition().getRankTypeName());
        codeEpDO.setRankLevel(JsonUtils.toJsonString(rule.getCondition().getRankValue()));
        codeEpDO.setGpnType(rule.getCondition().getGpnCode());
//        codeEpDO.setGpnTypeName(rule.getCondition().getGpnTypeName());
        codeEpDO.setExceptionEp(JsonUtils.toJsonString(rule.getCondition().getExceptionEp()));
        codeEpDO.setGpnCountry(JsonUtils.toJsonString(rule.getCondition().getGpnValues()));

        return codeEpDO;
    }

    default CodeEpDTO.Rule do2Rule(CodeEpDO doo) {
        CodeEpDTO.Condition condition = new CodeEpDTO.Condition();
        condition.setExceptionEp(JsonUtils.parseArray(doo.getExceptionEp(), CodeEpDTO.User.class));
        condition.setGpnValues(JsonUtils.parseArray(doo.getGpnCountry(), String.class));
        condition.setGpnCode(doo.getGpnType());
        condition.setRankValue(JsonUtils.parseArray(doo.getRankLevel(), String.class));
        condition.setRankCode(doo.getRankType());

        CodeEpDTO.Rule rule = new CodeEpDTO.Rule();
        rule.setCondition(condition);
        rule.setCountry(doo.getCountry());
        rule.setRegion(doo.getRegion());
        rule.setSl(doo.getSl());

        return rule;
    }

    default CodeEpVO.Rule do2RuleVO(CodeEpDO doo) {
        CodeEpVO.Condition condition = new CodeEpVO.Condition();
        condition.setExceptionEp(JsonUtils.parseArray(doo.getExceptionEp(), CodeEpVO.Ep.class));
        condition.setGpnValues(JsonUtils.parseArray(doo.getGpnCountry(), String.class));
        condition.setGpnCode(doo.getGpnType());
        condition.setRankValue(JsonUtils.parseArray(doo.getRankLevel(), String.class));
        condition.setRankCode(doo.getRankType());

        CodeEpVO.Rule rule = new CodeEpVO.Rule();
        rule.setCondition(condition);
        rule.setCountry(doo.getCountry());
        rule.setRegion(doo.getRegion());
        rule.setSl(doo.getSl());

        return rule;
    }

    @Mapping(source = "id", target = "ver")
    @Mapping(source = "updater", target = "updateUser")
    @Mapping(source = "updateTime", target = "updateTime", dateFormat = "yyyy.MM.dd HH:mm:ss")
    @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy.MM.dd HH:mm:ss")
    HistoryVO do2History(CodeEpVerDO bean);
}
