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.CodeOtherDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeOtherVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.dal.dataobject.code.CodeOtherDO;
import cn.ey.biz.module.system.dal.dataobject.code.CodeOtherVerDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface CodeOtherConvert {

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

    default CodeOtherDO rule2Do(CodeOtherVO.Rule rule) {
        CodeOtherDO otherDO = new CodeOtherDO();
        otherDO.setCountry(rule.getCountry());
        otherDO.setSl(rule.getSl());
        otherDO.setRegion(rule.getRegion());
        otherDO.setConditions(JsonUtils.toJsonString(rule.getCondition()));
        otherDO.setRequests(JsonUtils.toJsonString(rule.getRequests()));

        return otherDO;
    }

    default CodeOtherDTO.Rule do2Rule(CodeOtherDO doo) {
        CodeOtherDTO.Rule rule = new CodeOtherDTO.Rule();
        rule.setRequests(JsonUtils.parseArray(doo.getRequests(), CodeOtherDTO.Condition.class));
        rule.setCondition(JsonUtils.parseObject(doo.getConditions(), CodeOtherDTO.Condition.class));
        rule.setSl(doo.getSl());
        rule.setRegion(doo.getRegion());
        rule.setCountry(doo.getCountry());
        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(CodeOtherVerDO bean);
}
