package com.ningdatech.pmapi.expert.manage;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.expert.model.dto.ReviewTemplateSettingsDTO;
import com.ningdatech.pmapi.expert.model.entity.ReviewTemplateSettings;
import com.ningdatech.pmapi.expert.model.enumeration.ReviewTemplateTypeEnum;
import com.ningdatech.pmapi.expert.model.req.ReviewTemplateReq;
import com.ningdatech.pmapi.expert.model.vo.ReviewTemplateVO;
import com.ningdatech.pmapi.expert.service.IReviewTemplateSettingsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * ReviewTemplateSettingsManage
 * </p>
 *
 * @author WendyYang
 * @since 09:41 2023/2/15
 */
@Component
@AllArgsConstructor
public class ReviewTemplateSettingsManage {

    private final IReviewTemplateSettingsService reviewTemplateSettingsService;

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(ReviewTemplateReq req) {
        ReviewTemplateTypeEnum type = ReviewTemplateTypeEnum.getByCode(req.getTemplateType());
        LambdaUpdateWrapper<ReviewTemplateSettings> update = Wrappers.lambdaUpdate(ReviewTemplateSettings.class)
                .eq(ReviewTemplateSettings::getTemplateType, type.getCode())
                .eq(ReviewTemplateSettings::getRegionCode, req.getRegionCode())
                .eq(ReviewTemplateSettings::getIsLast, Boolean.TRUE)
                .set(ReviewTemplateSettings::getIsLast, Boolean.FALSE);
        reviewTemplateSettingsService.update(update);
        ReviewTemplateSettings settings = new ReviewTemplateSettings();
        settings.setTemplateType(type.getCode());
        settings.setRegionCode(req.getRegionCode());
        settings.setIsLast(Boolean.TRUE);
        settings.setContent(JSONUtil.toJsonStr(req.getTemplates()));
        reviewTemplateSettingsService.save(settings);
    }

    public ReviewTemplateVO getReviewTemplateSettings(Integer templateType, String regionCode) {
        LambdaQueryWrapper<ReviewTemplateSettings> query = Wrappers.lambdaQuery(ReviewTemplateSettings.class)
                .eq(ReviewTemplateSettings::getIsLast, Boolean.TRUE)
                .eq(ReviewTemplateSettings::getRegionCode, regionCode)
                .eq(ReviewTemplateSettings::getTemplateType, templateType)
                .last(BizConst.LIMIT_1);
        ReviewTemplateSettings settings = reviewTemplateSettingsService.getOne(query);
        return buildTemplateDetail(settings);
    }

    public ReviewTemplateVO getReviewTemplateSettings(Long templateId) {
        ReviewTemplateSettings settings = reviewTemplateSettingsService.getById(templateId);
        return buildTemplateDetail(settings);
    }

    public List<ReviewTemplateVO> listReviewTemplateSettings(List<Long> templateIds) {
        List<ReviewTemplateSettings> settings = reviewTemplateSettingsService.listByIds(templateIds);
        return CollUtils.convert(settings, this::buildTemplateDetail);
    }

    private ReviewTemplateVO buildTemplateDetail(ReviewTemplateSettings settings) {
        if (settings == null) {
            throw BizException.wrap("模版不存在");
        }
        return ReviewTemplateVO.builder()
                .templateId(settings.getId())
                .templateType(settings.getTemplateType())
                .templates(JSONUtil.toList(settings.getContent(), ReviewTemplateSettingsDTO.class))
                .build();
    }

}
