package com.yeejoin.amos.boot.module.ys.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.module.ys.api.dto.YsEmergencyCaseDto;
import com.yeejoin.amos.boot.module.ys.api.entity.YsEmergencyCase;
import com.yeejoin.amos.boot.module.ys.api.mapper.YsEmergencyCaseMapper;
import com.yeejoin.amos.boot.module.ys.api.service.IYsEmergencyCaseService;
import com.yeejoin.amos.feign.privilege.model.AgencyUserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService;

import java.util.Date;
import java.util.List;

/**
 * 救援案例管理服务实现类
 *
 * @author system_generator
 * @date 2024-09-24
 */
@Service
public class YsEmergencyCaseServiceImpl extends BaseService<YsEmergencyCaseDto,YsEmergencyCase,YsEmergencyCaseMapper> implements IYsEmergencyCaseService {
    @Autowired
    private YsEmergencyCaseMapper ysEmergencyCaseMapper;

    /**
     * 分页查询
     */
    public Page<YsEmergencyCaseDto> queryForYsEmergencyCasePage(Page<YsEmergencyCaseDto> page)   {
        return this.queryForPage(page, null, false);
    }

    /**
     * 列表查询 示例
     */
    public List<YsEmergencyCaseDto> queryForYsEmergencyCaseList()   {
        return this.queryForList("" , false);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdate(YsEmergencyCaseDto dto, AgencyUserModel userModel) {
        YsEmergencyCase emergencyCase = new YsEmergencyCase();
        if (ValidationUtil.isEmpty(dto.getSequenceNbr())) {//新增
            dto.setCreateDate(new Date());
            dto.setCreateUserId(userModel.getUserId());
            dto.setCreateUserName(userModel.getUserName());
        }
        emergencyCase.setVideoInfo(JSON.toJSONString(dto.getVideoInfo()));
        emergencyCase.setPicInfo(JSON.toJSONString(dto.getPicInfo()));
        emergencyCase.setFileInfo(JSON.toJSONString(dto.getFileInfo()));
        emergencyCase.setAudioInfo(JSON.toJSONString(dto.getAudioInfo()));
        BeanUtils.copyProperties(dto, emergencyCase);
        return this.saveOrUpdate(emergencyCase);
    }


    @Override
    public Page<YsEmergencyCaseDto> queryForYsEmergencyCasePage(Page<YsEmergencyCaseDto> page, YsEmergencyCaseDto dto) {
        return ysEmergencyCaseMapper.queryForYsEmergencyCasePage(page, dto);
    }


    @Override
    public Boolean deleteById(String sequenceNbr) {
        LambdaUpdateWrapper<YsEmergencyCase> lambda = new UpdateWrapper<YsEmergencyCase>().lambda();
        lambda.eq(YsEmergencyCase::getSequenceNbr, sequenceNbr);
        lambda.set(YsEmergencyCase::getIsDelete, Boolean.TRUE);
        return this.update(lambda);
    }


    @Override
    public YsEmergencyCaseDto queryBySequenceNbr(String sequenceNbr) {
        YsEmergencyCaseDto caseDto = new YsEmergencyCaseDto();
        LambdaQueryWrapper<YsEmergencyCase> lambda = new QueryWrapper<YsEmergencyCase>().lambda();
        lambda.eq(YsEmergencyCase::getSequenceNbr, sequenceNbr);
        YsEmergencyCase emergencyCase = this.baseMapper.selectOne(lambda);
        if (emergencyCase != null) {
            BeanUtils.copyProperties(emergencyCase, caseDto);
            // 如果案例有相关的图片或者其他特殊处理字段，可以在此进行处理
            if (!ValidationUtil.isEmpty(emergencyCase.getPicInfo())) {
                caseDto.setPicInfo(JSON.parseArray(emergencyCase.getPicInfo()));
            }
            if (!ValidationUtil.isEmpty(emergencyCase.getVideoInfo())) {
                caseDto.setVideoInfo(JSON.parseArray(emergencyCase.getVideoInfo()));
            }
            if (!ValidationUtil.isEmpty(emergencyCase.getFileInfo())) {
                caseDto.setFileInfo(JSON.parseArray(emergencyCase.getFileInfo()));
            }
            if (!ValidationUtil.isEmpty(emergencyCase.getAudioInfo())) {
                caseDto.setAudioInfo(JSON.parseArray(emergencyCase.getAudioInfo()));
            }
        }
        return caseDto;
    }


}