package com.zt.plat.module.base.api.businessdictionarytype;

import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.api.businessdictionarytype.dto.BusinessDictionaryDataDTO;
import com.zt.plat.module.base.api.businessdictionarytype.dto.BusinessDictionaryTypePageReqDTO;
import com.zt.plat.module.base.api.businessdictionarytype.dto.BusinessDictionaryTypeRespDTO;
import com.zt.plat.module.base.api.businessdictionarytype.dto.BusinessDictionaryTypeSaveReqDTO;
import com.zt.plat.module.base.controller.admin.businessdictionarytype.vo.BusinessDictionaryTypePageReqVO;
import com.zt.plat.module.base.controller.admin.businessdictionarytype.vo.BusinessDictionaryTypeRespVO;
import com.zt.plat.module.base.controller.admin.businessdictionarytype.vo.BusinessDictionaryTypeSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.businessdictionarytype.BusinessDictionaryDataDO;
import com.zt.plat.module.base.dal.dataobject.businessdictionarytype.BusinessDictionaryTypeDO;
import com.zt.plat.module.base.service.businessdictionarytype.BusinessDictionaryTypeService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

@RestController
@Validated
public class BusinessDictionaryTypeApiImpl implements BusinessDictionaryTypeApi {

    @Resource
    private BusinessDictionaryTypeService businessDictionaryTypeService;

    @Override
    public CommonResult<BusinessDictionaryTypeRespDTO> createBusinessDictionaryType(BusinessDictionaryTypeSaveReqDTO createReqDTO) {
        BusinessDictionaryTypeRespVO respVO = businessDictionaryTypeService.createBusinessDictionaryType(convertSaveReq(createReqDTO));
        return success(BeanUtils.toBean(respVO, BusinessDictionaryTypeRespDTO.class));
    }

    @Override
    public CommonResult<Boolean> updateBusinessDictionaryType(BusinessDictionaryTypeSaveReqDTO updateReqDTO) {
        businessDictionaryTypeService.updateBusinessDictionaryType(convertSaveReq(updateReqDTO));
        return success(true);
    }

    @Override
    public CommonResult<Boolean> deleteBusinessDictionaryType(Long id) {
        businessDictionaryTypeService.deleteBusinessDictionaryType(id);
        return success(true);
    }

    @Override
    public CommonResult<Boolean> deleteBusinessDictionaryTypeList(List<Long> ids) {
        businessDictionaryTypeService.deleteBusinessDictionaryTypeListByIds(ids);
        return success(true);
    }

    @Override
    public CommonResult<BusinessDictionaryTypeRespDTO> getBusinessDictionaryType(Long id) {
        BusinessDictionaryTypeDO typeDO = businessDictionaryTypeService.getBusinessDictionaryType(id);
        return success(BeanUtils.toBean(typeDO, BusinessDictionaryTypeRespDTO.class));
    }

    @Override
    public CommonResult<PageResult<BusinessDictionaryTypeRespDTO>> getBusinessDictionaryTypePage(BusinessDictionaryTypePageReqDTO pageReqDTO) {
        BusinessDictionaryTypePageReqVO pageReqVO = BeanUtils.toBean(pageReqDTO, BusinessDictionaryTypePageReqVO.class);
        PageResult<BusinessDictionaryTypeDO> pageResult = businessDictionaryTypeService.getBusinessDictionaryTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessDictionaryTypeRespDTO.class));
    }

    @Override
    public CommonResult<List<BusinessDictionaryDataDTO>> getBusinessDictionaryDataListByDictionaryTypeId(Long dictionaryTypeId) {
        List<BusinessDictionaryDataDO> list = businessDictionaryTypeService.getBusinessDictionaryDataListByDictionaryTypeId(dictionaryTypeId);
        return success(BeanUtils.toBean(list, BusinessDictionaryDataDTO.class));
    }

    @Override
    public CommonResult<List<BusinessDictionaryDataDTO>> getBusinessDictionaryDataListByType(String type) {
        List<BusinessDictionaryDataDO> list = businessDictionaryTypeService.getBusinessDictionaryDataListByType(type);
        return success(BeanUtils.toBean(list, BusinessDictionaryDataDTO.class));
    }

    private BusinessDictionaryTypeSaveReqVO convertSaveReq(BusinessDictionaryTypeSaveReqDTO dto) {
        BusinessDictionaryTypeSaveReqVO reqVO = BeanUtils.toBean(dto, BusinessDictionaryTypeSaveReqVO.class);
        if (dto.getBusinessDictionaryDatas() != null && !dto.getBusinessDictionaryDatas().isEmpty()) {
            reqVO.setBusinessDictionaryDatas(BeanUtils.toBean(dto.getBusinessDictionaryDatas(), BusinessDictionaryDataDO.class));
        }
        return reqVO;
    }
}
