package cn.ysk.cashier.cons.service.impl;

import cn.ysk.cashier.cons.domain.ViewConInfoFlow;
import cn.ysk.cashier.utils.FileUtil;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.cons.repository.ViewConInfoFlowRepository;
import cn.ysk.cashier.cons.service.ViewConInfoFlowService;
import cn.ysk.cashier.cons.service.dto.ViewConInfoFlowDto;
import cn.ysk.cashier.cons.service.dto.ViewConInfoFlowQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.ViewConInfoFlowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author admin
* @date 2024-07-17
**/
@Service
@RequiredArgsConstructor
public class ViewConInfoFlowServiceImpl implements ViewConInfoFlowService {

    private final ViewConInfoFlowRepository viewConInfoFlowRepository;
    private final ViewConInfoFlowMapper viewConInfoFlowMapper;

    @Override
    public Map<String,Object> queryAll(ViewConInfoFlowQueryCriteria criteria, Pageable pageable){
        Page<ViewConInfoFlow> page = viewConInfoFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(viewConInfoFlowMapper::toDto));
    }

    @Override
    public List<ViewConInfoFlowDto> queryAll(ViewConInfoFlowQueryCriteria criteria){
        return viewConInfoFlowMapper.toDto(viewConInfoFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ViewConInfoFlowDto findById(Integer consId) {
        ViewConInfoFlow viewConInfoFlow = viewConInfoFlowRepository.findById(consId).orElseGet(ViewConInfoFlow::new);
        ValidationUtil.isNull(viewConInfoFlow.getConsId(),"ViewConInfoFlow","consId",consId);
        return viewConInfoFlowMapper.toDto(viewConInfoFlow);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ViewConInfoFlowDto create(ViewConInfoFlow resources) {
        return viewConInfoFlowMapper.toDto(viewConInfoFlowRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ViewConInfoFlow resources) {
        ViewConInfoFlow viewConInfoFlow = viewConInfoFlowRepository.findById(resources.getConsId()).orElseGet(ViewConInfoFlow::new);
        ValidationUtil.isNull( viewConInfoFlow.getConsId(),"ViewConInfoFlow","id",resources.getConsId());
        viewConInfoFlow.copy(resources);
        viewConInfoFlowRepository.save(viewConInfoFlow);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer consId : ids) {
            viewConInfoFlowRepository.deleteById(consId);
        }
    }

    @Override
    public void download(List<ViewConInfoFlowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ViewConInfoFlowDto viewConInfoFlow : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("耗材名称", viewConInfoFlow.getConName());
            map.put(" conreturn",  viewConInfoFlow.getConReturn());
            map.put(" conin",  viewConInfoFlow.getConIn());
            map.put(" conconsume",  viewConInfoFlow.getConConsume());
            map.put(" conout",  viewConInfoFlow.getConOut());
            map.put(" balance",  viewConInfoFlow.getBalance());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}