package com.rf.psychological.scale.service.impl;

import com.rf.psychological.scale.dao.model.ScaleEntity;
import com.rf.psychological.scale.dao.repository.ScaleRepository;
import com.rf.psychological.scale.service.ScaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzf
 * @description:
 * @date 2021/5/14 10:51
 */
@Service
public class ScaleServiceImpl implements ScaleService {

    @Autowired
    private ScaleRepository scaleRepository;


    /**
     * 保存
     *
     * @param scaleEntity
     */
    @Override
    public void saveScale(ScaleEntity scaleEntity) {
        //scaleEntity.encrypt();
        this.scaleRepository.save(scaleEntity);
    }

    /**
     * 全量查询
     *
     * @return
     */
    @Override
    public List<ScaleEntity> findAll() {
        //List<ScaleEntity> all = this.scaleRepository.findAll();
        //for (ScaleEntity scaleEntity : all) {
        //    scaleEntity.decrypt();
        //}
        return  this.scaleRepository.findAll();
    }


    @Override
    public void saveBatch(List<ScaleEntity> scaleEntities) {

        this.scaleRepository.saveAll(scaleEntities);
    }

    /**
     * 根据flag查询检测列表
     *
     * @param flag
     * @return
     */
    @Override
    @Cacheable(value = "scale", key = "#flag")
    public List<ScaleEntity> getScaleByFlag(String flag) {
        return this.scaleRepository.getScaleByFlag(flag);
    }

    @Override
    public void updateScale(ScaleEntity scaleEntity) {
        this.scaleRepository.updateScale(scaleEntity.getAnswer(),scaleEntity.getCheckItems(),scaleEntity.getId());
    }

    @Override
    public void deleteScaleByFlag(String flag) {
        this.scaleRepository.deleteScaleByFlag(flag);
    }

    @Override
    public List<ScaleEntity> getScaleByFlagAndInstitutionNo(String institutionNo, String flag) {
        return this.scaleRepository.getScaleByFlagAndInstitutionNo(institutionNo,flag);
    }
}
