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

@Mapper
public interface CodeCustomerConvert {

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

    default CodeCustomerVO do2Vo(CodeCustomerDO doo) {
        CodeCustomerVO vo = new CodeCustomerVO();
        vo.setClientID(JsonUtils.parseObject(doo.getCustomerId(), CodeCustomerVO.ClientID.class));
        vo.setClientName(JsonUtils.parseObject(doo.getCustomerName(), CodeCustomerVO.ClientName.class));
        vo.setExplanation(doo.getExplanation());
        return vo;
    }

    default CodeCustomerDO vo2Do(CodeCustomerVO vo) {
        CodeCustomerDO doo = new CodeCustomerDO();
        doo.setActive(1);
        doo.setCustomerId(JsonUtils.toJsonString(vo.getClientID()));
        doo.setCustomerName(JsonUtils.toJsonString(vo.getClientName()));
        doo.setExplanation(vo.getExplanation());
        return doo;
    }

    default CodeCustomerDTO do2Dto(CodeCustomerDO doo) {
        CodeCustomerDTO dto = new CodeCustomerDTO();
        dto.setClientID(JsonUtils.parseObject(doo.getCustomerId(), CodeCustomerDTO.ClientID.class));
        dto.setClientName(JsonUtils.parseObject(doo.getCustomerName(), CodeCustomerDTO.ClientName.class));
        dto.setExplanation(doo.getExplanation());
        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(CodeCustomerDO bean);
}
