package cn.iocoder.yudao.module.system.service.evaluation;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.evaluation.EvaluationServiceQualityDO;
import cn.iocoder.yudao.module.system.dal.dataobject.evaluation.EvaluationServiceQualityItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.evaluation.EvaluationServiceQualityItemMapper;
import cn.iocoder.yudao.module.system.dal.mysql.evaluation.EvaluationServiceQualityMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

@Service
@Validated
public class EvaluationServiceQualityServiceImpl implements EvaluationServiceQualityService {

    @Resource
    private EvaluationServiceQualityMapper qualityMapper;
    @Resource
    private EvaluationServiceQualityItemMapper qualityItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer create(EvaluationServiceQualitySaveReqVO createReqVO) {
        EvaluationServiceQualityDO quality = BeanUtils.toBean(createReqVO, EvaluationServiceQualityDO.class);
        if (quality.getTenantId() == null) {
            quality.setTenantId(TenantContextHolder.getTenantId());
        }
        qualityMapper.insert(quality);
        saveItems(Long.valueOf(quality.getId()), quality.getTenantId(), createReqVO.getItems());
        return quality.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EvaluationServiceQualitySaveReqVO updateReqVO) {
        EvaluationServiceQualityDO quality = BeanUtils.toBean(updateReqVO, EvaluationServiceQualityDO.class);
        if (quality.getTenantId() == null) {
            quality.setTenantId(TenantContextHolder.getTenantId());
        }
        qualityMapper.updateById(quality);

        Long evaluationId = Long.valueOf(quality.getId());
        qualityItemMapper.deleteByEvaluationId(evaluationId);
        saveItems(evaluationId, quality.getTenantId(), updateReqVO.getItems());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        Long evaluationId = Long.valueOf(id);
        qualityItemMapper.deleteByEvaluationId(evaluationId);
        qualityMapper.deleteById(id);
    }

    @Override
    public EvaluationServiceQualityRespVO get(Integer id) {
        EvaluationServiceQualityDO quality = qualityMapper.selectById(id);
        if (quality == null) {
            return null;
        }
        EvaluationServiceQualityRespVO respVO = BeanUtils.toBean(quality, EvaluationServiceQualityRespVO.class);
        List<EvaluationServiceQualityItemDO> items = qualityItemMapper.selectListByEvaluationId(Long.valueOf(id));
        respVO.setItems(BeanUtils.toBean(items, EvaluationServiceQualityItemRespVO.class));
        return respVO;
    }

    @Override
    public PageResult<EvaluationServiceQualityRespVO> getPage(EvaluationServiceQualityPageReqVO pageReqVO) {
        if (pageReqVO.getTenantIds() == null || pageReqVO.getTenantIds().length == 0) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        PageResult<EvaluationServiceQualityDO> pageResult = qualityMapper.selectPage(pageReqVO);
        return BeanUtils.toBean(pageResult, EvaluationServiceQualityRespVO.class);
    }

    private void saveItems(Long evaluationId, Long tenantId, List<EvaluationServiceQualityItemSaveReqVO> itemReqList) {
        if (CollectionUtil.isEmpty(itemReqList)) {
            return;
        }
        for (EvaluationServiceQualityItemSaveReqVO itemReq : itemReqList) {
            EvaluationServiceQualityItemDO item = BeanUtils.toBean(itemReq, EvaluationServiceQualityItemDO.class);
            item.setEvaluationId(evaluationId);
            item.setTenantId(tenantId);
            qualityItemMapper.insert(item);
        }
    }
}

