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

@Mapper
public interface CodeProjectConvert {

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

    default CodeProjectDO vo2Do(CodeProjectVO vo) {
        CodeProjectDO ret = new CodeProjectDO();
        ret.setExplanation(vo.getExplanation());
        vo.setExplanation(null);
        ret.setConsulting(JsonUtils.toJsonString(vo.getConsulting()));
        ret.setAssurance(JsonUtils.toJsonString(vo.getAssurance()));
        return ret;
    }

    default CodeProjectDTO do2Dto(CodeProjectDO doo) {
        CodeProjectDTO dto = new CodeProjectDTO();
        dto.setExplanation(doo.getExplanation());
        dto.setConsulting(JsonUtils.parseObject(doo.getConsulting(), CodeProjectDTO.Consulting.class));
        dto.setAssurance(JsonUtils.parseObject(doo.getAssurance(), CodeProjectDTO.Assurance.class));
        return dto;
    }

    @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(CodeProjectDO bean);
}
