package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.data.entity.PvModuleModel;
import com.jiayue.ipfcst.common.data.entity.WindTurbineInfo;
import com.jiayue.ipfcst.common.data.repository.PvModuleModelRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 场站信息业务层
 *
 * @author zzy
 * @version 1.0
 * @since 2019/8/7 9:53
 */
@Service
@Slf4j
public class PvModuleModelService extends BaseService {

    private final PvModuleModelRepository pvModuleModelRepository;

    @Autowired
    public PvModuleModelService(PvModuleModelRepository pvModuleModelRepository) {
        this.pvModuleModelRepository = pvModuleModelRepository;
    }

    /**
     * 获取所有光伏组件信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<PvModuleModel> getAll() {
        return this.pvModuleModelRepository.findAll();
    }

    /**
     * 保存光伏组件信息
     *
     * @param pvModuleModel 光伏组件信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void save(PvModuleModel pvModuleModel) {
        this.pvModuleModelRepository.save(pvModuleModel);
    }

    /**
     * 删除场站信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public boolean delete(String ids) {
        boolean flag = false;
        if (!StringUtils.isEmpty(ids)) {
            String[] idArray = ids.split(",");
            if (idArray != null && idArray.length > 0) {
                for (String id : idArray) {
                    this.pvModuleModelRepository.deleteById(Integer.valueOf(id));
                    log.info("删除光伏组件ID为[" + id + "]成功！");

                }
                flag = true;
            } else {
                this.pvModuleModelRepository.deleteById(Integer.valueOf(ids));
                flag = true;
                log.info("删除光伏组件ID为[" + ids + "]成功！");
            }
        }

        return flag;

    }

    /**
     * 查询场站信息
     *
     * @return 场站信息
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Map<String, Object> get(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>();
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page - 1, size, sort); //页码：前端从1开始，jpa从0开始，做个转换
        Page pagepvModuleModel = pvModuleModelRepository.findAll(pageable);
        map.put("content", pagepvModuleModel.getContent());// 结果集
        map.put("count", pagepvModuleModel.getTotalElements());// 总记录数
        return map;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(PvModuleModel bean) {
//        PvModuleModel beanOld = pvModuleModelRepository.findByModel(bean.getModel());
//        if (null != beanOld) {
//            bean.setId(beanOld.getId());
//        }
        pvModuleModelRepository.save(bean);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(List<PvModuleModel> beans) {
        if(beans != null && beans.size() > 0){
            pvModuleModelRepository.deleteAll();
            pvModuleModelRepository.saveAll(beans);
        }
    }
}
