package com.bonus.sharedstation.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.sharedstation.web.domain.FirstAidInfo;
import com.bonus.sharedstation.web.domain.OperationStep;
import com.bonus.sharedstation.web.dto.FirstAidInfoDTO;
import com.bonus.sharedstation.web.mapper.FirstAidMapper;
import com.bonus.sharedstation.web.service.IFirstAidService;
import com.bonus.sharedstation.web.vo.FirstAidInfoVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.type.TypeReference;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 问卷调查答案 Service实现
 *
 * @author 云归
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class FirstAidServiceImpl extends ServiceImpl<FirstAidMapper, FirstAidInfo> implements IFirstAidService {


    @Resource
    private FirstAidMapper firstAidMapper;

    @Override
    public AjaxResult getFirstAidInfoList(FirstAidInfoDTO firstAidInfoDTO) {
        LambdaQueryWrapper<FirstAidInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<FirstAidInfo> page = new Page<>(firstAidInfoDTO.getCurrentPage(),firstAidInfoDTO.getLimit());
        queryWrapper
                .like(FirstAidInfo::getFirstAidName, firstAidInfoDTO.getFirstAidName())
                .eq(FirstAidInfo::getPublishStatus, firstAidInfoDTO.getPublishStatus())
                .eq(FirstAidInfo::getIsDeleted, firstAidInfoDTO.getIsDeleted())
                .orderByDesc(FirstAidInfo::getCreatedTime);

        Page<FirstAidInfo> firstAidInfoPage = firstAidMapper.selectPage(page, queryWrapper);
        List<FirstAidInfo> records = firstAidInfoPage.getRecords();
        if (StringUtils.isNotEmpty(records)) {
            for (FirstAidInfo firstAidInfo : records) {
                String instruction = firstAidInfo.getInstruction();
                if (StringUtils.isNotEmpty(instruction)) {
                    try {
                        ObjectMapper objectMapper = new ObjectMapper();
                        List<OperationStep> operationSteps = objectMapper.readValue(instruction, new TypeReference<List<OperationStep>>() {
                        });
                        // 现在operationSteps包含了从JSON字符串转换回的OperationStep对象列表
                        if (StringUtils.isNotEmpty(operationSteps)) {
                            firstAidInfo.setSteps(operationSteps);
                        }
                    } catch (IOException e) {
                        log.error("应急知识json 转换步骤失败 查询详情", e);
                        throw  new ServiceException("急救知识查询失败");
                    }
                }
            }
        }
        return AjaxResult.success(firstAidInfoPage);
    }
    @Override
    public AjaxResult selectAll(FirstAidInfoDTO firstAidInfoDTO) {
        LambdaQueryWrapper<FirstAidInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .like(FirstAidInfo::getFirstAidName, firstAidInfoDTO.getFirstAidName())
                .eq(FirstAidInfo::getPublishStatus, firstAidInfoDTO.getPublishStatus())
                .eq(FirstAidInfo::getIsDeleted, firstAidInfoDTO.getIsDeleted())
                .orderByDesc(FirstAidInfo::getCreatedTime);

        List<FirstAidInfo> firstAidInfos = firstAidMapper.selectList(queryWrapper);

        if (StringUtils.isNotEmpty(firstAidInfos)) {
            for (FirstAidInfo firstAidInfo : firstAidInfos) {
                String instruction = firstAidInfo.getInstruction();
                if (StringUtils.isNotEmpty(instruction)) {
                    try {
                        ObjectMapper objectMapper = new ObjectMapper();
                        List<OperationStep> operationSteps = objectMapper.readValue(instruction, new TypeReference<List<OperationStep>>() {
                        });
                        // 现在operationSteps包含了从JSON字符串转换回的OperationStep对象列表
                        if (StringUtils.isNotEmpty(operationSteps)) {
                            firstAidInfo.setSteps(operationSteps);
                        }
                    } catch (IOException e) {
                        log.error("应急知识json 转换步骤失败 查询详情",e);
                        throw new ServiceException("急救知识查询失败");
                    }
                }
            }
        }
        return AjaxResult.success(firstAidInfos);
    }



    @Override
    public AjaxResult selectInfoById(FirstAidInfoDTO firstAidInfoDTO) {
        FirstAidInfoVO firstAidInfoVO = new FirstAidInfoVO();
        FirstAidInfo firstAidInfo = firstAidMapper.selectById(firstAidInfoDTO.getId());
        if (StringUtils.isNull(firstAidInfo)) {
            throw new ServiceException("应急知识不存在");
        }
        BeanUtils.copyProperties(firstAidInfo, firstAidInfoVO);
        //回参数 改造
        String instruction = firstAidInfo.getInstruction();
        if (StringUtils.isNotEmpty(instruction)) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                List<OperationStep> operationSteps = objectMapper.readValue(instruction, new TypeReference<List<OperationStep>>() {
                });
                // 现在operationSteps包含了从JSON字符串转换回的OperationStep对象列表
                if (StringUtils.isNotEmpty(operationSteps)) {
                    firstAidInfoVO.setOperationSteps(operationSteps);
                }
            } catch (IOException e) {
                log.error("应急知识json 转换步骤失败 查询详情",e);
                throw new ServiceException("应急知识json 转换步骤失败 查询详情");
            }
        }
        return AjaxResult.success(firstAidInfoVO);
    }
}
