package cn.iocoder.yudao.module.configure.api.uniTemplate;


import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.configure.api.uniTemplate.dto.UniTemplateCreateDTO;
import cn.iocoder.yudao.module.configure.api.uniTemplate.dto.UniTemplateDTO;
import cn.iocoder.yudao.module.configure.controller.admin.unitemplate.vo.UniTemplateCreateReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.unitemplate.vo.UniTemplateRespVO;
import cn.iocoder.yudao.module.configure.dal.dataobject.unitemplate.UniTemplateDO;
import cn.iocoder.yudao.module.configure.service.unitemplate.UniTemplateService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
public class UniTemplateApiImpl implements UniTemplateApi {

    @Resource
    private UniTemplateService uniTemplateService;

    @Override
    @Transactional
    public CommonResult<Long> createUniTemplate(UniTemplateCreateDTO createReqVO) {
        UniTemplateCreateReqVO uniTemplateCreateReqVO = new UniTemplateCreateReqVO();
        BeanUtil.copyProperties(createReqVO, uniTemplateCreateReqVO);
        return CommonResult.success(uniTemplateService.createUniTemplate(uniTemplateCreateReqVO));
    }

    @Override
    public CommonResult<Boolean> delUniTemplate(Long uniTemplateId) {
        uniTemplateService.deleteUniTemplate(uniTemplateId);
        return success(true);
    }

    @Override
    public CommonResult<UniTemplateDTO> getTemplateById(Long id) {
        UniTemplateRespVO uniTemplate = uniTemplateService.getUniTemplate(id);
        UniTemplateDTO result = new UniTemplateDTO();
        BeanUtil.copyProperties(uniTemplate, result);
        return CommonResult.success(result);
    }

    @Override
    public CommonResult<UniTemplateDTO> getDefaultTemplate() {
        UniTemplateDO templateDO = uniTemplateService.getDefaultUniTemplate();
        UniTemplateDTO result = new UniTemplateDTO();
        BeanUtil.copyProperties(templateDO, result);
        return CommonResult.success(result);
    }
}
