package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.data.manager.IDIccidRecordData;
import cc.iotkit.data.manager.IFeedBackEvaluationData;
import cc.iotkit.manager.config.BeanUtils;
import cc.iotkit.manager.dto.vo.FeedBackEvaluation.FeedBackEvaluationPageReqVO;
import cc.iotkit.manager.dto.vo.FeedBackEvaluation.FeedBackEvaluationSaveReqVO;
import cc.iotkit.manager.dto.vo.FeedBackEvaluation.FeedBackEvaluationVO;
import cc.iotkit.manager.dto.vo.category.vo.DIccidRecordPageReqVO;
import cc.iotkit.manager.dto.vo.category.vo.DIccidRecordRespVO;
import cc.iotkit.manager.service.FeedBackEvaluationService;
import cc.iotkit.model.FeedBackEvaluation.FeedBackEvaluationDO;
import cc.iotkit.model.iccid.DIccidRecordDO;
import cn.hutool.core.util.ObjectUtil;

import cn.hutool.db.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static io.micrometer.core.instrument.binder.http.HttpRequestTags.exception;


/**
 * 意见反馈评价 Service 实现类
 *
 * @author zhp
 */
@Service
@Slf4j
public class FeedBackEvaluationServiceImpl implements FeedBackEvaluationService {

    @Autowired
    private IFeedBackEvaluationData iFeedBackEvaluationData;

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public Long createFeedBackEvaluation(FeedBackEvaluationSaveReqVO createReqVO) {
       // String id = TimestampIdentifierGenerator.getIdCreateFeedBackEvaluation();

       // createReqVO.setId(id);
        // 插入
        FeedBackEvaluationDO feedBackEvaluation = BeanUtils.toBean(createReqVO, FeedBackEvaluationDO.class);
        //查询工单号
     /*   if(ObjectUtil.isNotEmpty(feedBackEvaluation.getWorderId())){
            String worderId = feedBackEvaluation.getWorderId();
            TbWorderDO worderDO = worderMapper.selectById(worderId);

            feedBackEvaluation.setCustomerPhone(worderDO.getCustomerPhone());
            feedBackEvaluation.setCustomerName(worderDO.getCustomerName());
            feedBackEvaluation.setDeviceId(worderDO.getProductId());
        }*/


        iFeedBackEvaluationData.save(feedBackEvaluation);
        // 返回
        return null;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, ServiceException.class})
    public void updateFeedBackEvaluation(FeedBackEvaluationSaveReqVO updateReqVO) {
        // 校验存在
        validateFeedBackEvaluationExists(updateReqVO.getId());
        // 更新
        FeedBackEvaluationDO updateObj = BeanUtils.toBean(updateReqVO, FeedBackEvaluationDO.class);
        iFeedBackEvaluationData.save(updateObj);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, ServiceException.class})
    public void deleteFeedBackEvaluation(Long id) {
        // 校验存在
        validateFeedBackEvaluationExists(id);
        // 删除
        iFeedBackEvaluationData.deleteById(id);
    }

    private void validateFeedBackEvaluationExists(Long id) {
        if (iFeedBackEvaluationData.findById(id) == null) {
            throw new RuntimeException("不存在");
        }
    }

    @Override
    public FeedBackEvaluationDO getFeedBackEvaluation(Long id) {
        return iFeedBackEvaluationData.findById(id);
    }

    @Override
    public Paging<FeedBackEvaluationVO> getFeedBackEvaluationPage(PageRequest<FeedBackEvaluationPageReqVO> pageReqVO) {


        // 参数校验
        if(pageReqVO == null) {
            pageReqVO = new PageRequest<>();
        }
        PageRequest<FeedBackEvaluationDO> feedBackEvaluationDO = pageReqVO.to(FeedBackEvaluationDO.class);
        if (feedBackEvaluationDO == null) {
            throw new IllegalArgumentException("Conversion to TbFeedBackEvaluation resulted in null");
        }
     //   Paging<FeedBackEvaluationDO> a=  iFeedBackEvaluationData.findAll(feedBackEvaluationDO);

        return iFeedBackEvaluationData.findAll(feedBackEvaluationDO).to(FeedBackEvaluationVO.class);
    }

}