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

import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.repository.ScaleMarksRepository;
import com.rf.psychological.scale.service.ScaleMarksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author zzf
 * @description:
 * @date 2021/5/14 10:51
 */
@Service
@Slf4j
public class ScaleMarksServiceImpl implements ScaleMarksService {
    @Autowired
    private ScaleMarksRepository scaleMarksRepository;

    /**
     * 根据flag获取全部评分规则
     *
     * @param flag
     */
    @Override
    public List<ScaleMarksEntity> getScaleMarksByFlag(String flag) {
        //List<ScaleMarksEntity> scaleMarksByFlag = this.scaleMarksRepository.getScaleMarksByFlag(flag);
        //for (ScaleMarksEntity scaleMarksEntity : scaleMarksByFlag) {
        //    scaleMarksEntity.decrypt();
        //}
        return this.scaleMarksRepository.getScaleMarksByFlag(flag);
    }

    /**
     * 保存
     *
     * @param scaleMarksEntity
     */
    @Override
    public void saveScaleMarks(ScaleMarksEntity scaleMarksEntity) {
        //scaleMarksEntity.encrypt();
        this.scaleMarksRepository.save(scaleMarksEntity);
    }


    @Override
    public List<ScaleMarksEntity> getScaleMarkByName(String name) {
        ScaleMarksEntity scaleMarksEntity = new ScaleMarksEntity();
        scaleMarksEntity.setName(name);
        return scaleMarksRepository.findAll(Example.of(scaleMarksEntity));
    }

    @Override
    public List<ScaleMarksEntity> findAll() {
        //List<ScaleMarksEntity> all = this.scaleMarksRepository.findAll();
        //for (ScaleMarksEntity scaleMarksEntity : all) {
        //    scaleMarksEntity.decrypt();
        //}
        return this.scaleMarksRepository.findAll();
    }


    @Override
    public void updateById(String id, String symptom, String improvementSuggestions) {
        //this.scaleMarksRepository.updateById(id, DESede.encryptString(symptom), DESede.encryptString(improvementSuggestions));
        this.scaleMarksRepository.updateById(id, symptom, improvementSuggestions);
    }

    @Override
    public void updateMark(ScaleMarksEntity scaleMarksEntity) {
        this.scaleMarksRepository.updateMark(scaleMarksEntity.getName(), scaleMarksEntity.getScoreStart(), scaleMarksEntity.getScoreEnd()
                , scaleMarksEntity.getSymptom(), scaleMarksEntity.getImprovementSuggestions(), scaleMarksEntity.getNameExplain(),scaleMarksEntity.getReference()
                , scaleMarksEntity.getSuggestion(),scaleMarksEntity.getStandardDeviation(),scaleMarksEntity.getId());
    }

    @Override
    public void deleteScaleMarkByFlag(String flag) {
        this.scaleMarksRepository.deleteScaleMarkByFlag(flag);
    }

    @Override
    public List<ScaleMarksEntity> getScaleMarksByFlagAndName(String flag, String name) {
        return this.scaleMarksRepository.findScaleMarksByFlagAndName(flag,name);
    }


    @Override
    public void saveBatch(List<ScaleMarksEntity> scaleMarksEntities) {
        //String sql="insert into t_scale_marks (id, flag, improvement_suggestions, score_end, score_start, symptom, name,name_explain, reference, suggestion, is_total_score_explain) values " +
        //        "(?,?,?,?,?,?,?,?,?,?,?)";
        //
        //jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        //    @Override
        //    public void setValues(PreparedStatement ps, int i) throws SQLException {
        //        ScaleMarksEntity scaleMarksEntity = scaleMarksEntities.get(i);
        //        //scaleMarksEntity.encrypt();
        //        ps.setObject(1,scaleMarksEntity.getId());
        //        ps.setObject(2,scaleMarksEntity.getFlag());
        //        ps.setObject(3,scaleMarksEntity.getImprovementSuggestions());
        //        ps.setObject(4,scaleMarksEntity.getScoreEnd());
        //        ps.setObject(5,scaleMarksEntity.getScoreStart());
        //        ps.setObject(6,scaleMarksEntity.getSymptom());
        //        ps.setObject(7,scaleMarksEntity.getName());
        //        ps.setObject(10,scaleMarksEntity.getNameExplain());
        //        ps.setObject(11,scaleMarksEntity.getReference());
        //        ps.setObject(12,scaleMarksEntity.getSuggestion());
        //        ps.setObject(13,scaleMarksEntity.getIsTotalScoreExplain());
        //    }
        //
        //    @Override
        //    public int getBatchSize() {
        //        return scaleMarksEntities.size();
        //    }
        //});
        log.info("批量加入注释，添加数据使用，一般不会使用，暂时注释");
    }
}
