package com.chushang.inspection.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.web.Result;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.project.po.InspectionDetail;
import com.chushang.inspection.project.vo.DetailsVO;
import com.chushang.inspection.project.vo.InsConfigVO;
import com.chushang.inspection.project.service.InspectionConfigService;
import com.chushang.inspection.project.service.InspectionDetailService;
import com.chushang.oss.entity.dto.UploadFileDTO;
import com.chushang.oss.entity.vo.FileSourceVo;
import com.chushang.oss.feign.RemoteOssService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.project.po.InspectionConfig;
import com.chushang.inspection.project.mapper.InspectionConfigMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

/**
  * @auther: zhao
  * @date: 2024/6/18 15:39
  */
@Service
@CacheConfig(cacheNames = "inspection_config")
public class InspectionConfigServiceImpl extends ServiceImpl<InspectionConfigMapper, InspectionConfig> implements InspectionConfigService {

    @Resource
    InspectionDetailService detailService;

    @Override
    @CachePut(key = "#alias")
    public InsConfigVO getByAlias(String alias) {
        LambdaQueryWrapper<InspectionConfig> configSql = WrapperUtils.builder();
        configSql.eq(InspectionConfig::getAlias, alias).last(Operator.LIMIT_ONE.getCharacter());
        InspectionConfig insConfig = getOne(configSql);
        InsConfigVO insConfigVO = BeanUtil.copyProperties(insConfig, InsConfigVO.class);
        if (ObjectUtil.isNotEmpty(insConfigVO)){
            LambdaQueryWrapper<InspectionDetail> detailSql = WrapperUtils.builder();
            detailSql.eq(InspectionDetail::getConfigId, insConfigVO.getConfigId());
            List<InspectionDetail> list = detailService.list(detailSql);
            if (CollectionUtil.isNotEmpty(list)){
                List<DetailsVO> detailsList = BeanUtil.copyToList(list, DetailsVO.class);
                insConfigVO.setDetails(detailsList);
            }
        }
        return insConfigVO;
    }

    /**
     * CacheEvict 需要  allEntries = true 才能 删除成功, 同时必须 为void 方法, 等以后有机会在改吧
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional
    public void updateByEntityId(InspectionConfig config) {
        updateById(config);
    }

    /**
     * CacheEvict 需要  allEntries = true 才能 删除成功, 同时必须 为void 方法, 等以后有机会在改吧
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional
    public void saveEntity(InspectionConfig config) {
        save(config);
    }
}
