package cn.lihongjie.coal.coalWashingDailyAnalysisParam.service;

import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.coalWashingDailyAnalysisParam.dto.CoalWashingDailyAnalysisParamDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisParam.dto.CreateCoalWashingDailyAnalysisParamDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisParam.dto.UpdateCoalWashingDailyAnalysisParamDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisParam.entity.CoalWashingDailyAnalysisParamEntity;
import cn.lihongjie.coal.coalWashingDailyAnalysisParam.mapper.CoalWashingDailyAnalysisParamMapper;
import cn.lihongjie.coal.coalWashingDailyAnalysisParam.repository.CoalWashingDailyAnalysisParamRepository;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.LinkedList;

@Service
@Slf4j
@Transactional
public
class CoalWashingDailyAnalysisParamService extends BaseService<
                CoalWashingDailyAnalysisParamEntity, CoalWashingDailyAnalysisParamRepository> {
    @Autowired private CoalWashingDailyAnalysisParamRepository repository;

    @Autowired private CoalWashingDailyAnalysisParamMapper mapper;

    @Autowired private ConversionService conversionService;

    public CoalWashingDailyAnalysisParamDto create(CreateCoalWashingDailyAnalysisParamDto request) {
        CoalWashingDailyAnalysisParamEntity entity = mapper.toEntity(request);

        this.repository.save(entity);
        return getById(entity.getId());
    }

    public CoalWashingDailyAnalysisParamDto update(UpdateCoalWashingDailyAnalysisParamDto request) {
        CoalWashingDailyAnalysisParamEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {
        this.repository.deleteAllById(request.getIds());
    }

    public CoalWashingDailyAnalysisParamDto getById(String id) {
        CoalWashingDailyAnalysisParamEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<CoalWashingDailyAnalysisParamDto> list(CommonQuery query) {
        Page<CoalWashingDailyAnalysisParamEntity> page =
                repository.findAll(
                        query.specification(conversionService),
                        PageRequest.of(
                                query.getPageNo(),
                                query.getPageSize(),
                                Sort.by(query.getOrders())));

        LinkedList<String> names =
                new LinkedList<>(Arrays.asList("重介精煤", "螺旋精煤", "跳汰精煤", "筛精", "浮精"));

        if (page.isEmpty()) {

            for (int i = 0; i < 10; i++) {

                CoalWashingDailyAnalysisParamEntity param =
                        new CoalWashingDailyAnalysisParamEntity();

                param.setName(names.isEmpty() ? "" : names.removeFirst());
                param.setCode("c" + i);
                param.setSortKey(i);
                param.setStatus(StringUtils.isNotEmpty(param.getName()) ? 1 : 0);
                this.save(param);
            }

            return list(query);
        }

        return page.map(this.mapper::toDto);
    }
}
