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

import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.cons.domain.TbConUnit;
import cn.ysk.cashier.cons.service.dto.ConUnitPO;
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.TbConUnitRepository;
import cn.ysk.cashier.cons.service.TbConUnitService;
import cn.ysk.cashier.cons.service.dto.TbConUnitDto;
import cn.ysk.cashier.cons.service.dto.TbConUnitQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.TbConUnitMapper;
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.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

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

    private final TbConUnitRepository tbConUnitRepository;
    private final TbConUnitMapper tbConUnitMapper;

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

    @Override
    public List<TbConUnitDto> queryAll(TbConUnitQueryCriteria criteria){
        return tbConUnitMapper.toDto(tbConUnitRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbConUnitDto findById(Integer id) {
        TbConUnit tbConUnit = tbConUnitRepository.findById(id).orElseGet(TbConUnit::new);
        ValidationUtil.isNull(tbConUnit.getId(),"TbConUnit","id",id);
        return tbConUnitMapper.toDto(tbConUnit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbConUnitDto create(TbConUnit resources) {
        return tbConUnitMapper.toDto(tbConUnitRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbConUnit resources) {
        TbConUnit tbConUnit = tbConUnitRepository.findById(resources.getId()).orElseGet(TbConUnit::new);
        ValidationUtil.isNull( tbConUnit.getId(),"TbConUnit","id",resources.getId());
        tbConUnit.copy(resources);
        tbConUnitRepository.save(tbConUnit);
    }

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

    @Override
    public void download(List<TbConUnitDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbConUnitDto tbConUnit : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("主单位名称", tbConUnit.getName());
            map.put("主单位值", tbConUnit.getValue());
            map.put("状态 1 启用 0 禁用", tbConUnit.getStatus());
            map.put(" createTime",  tbConUnit.getCreateTime());
            map.put(" updateTime",  tbConUnit.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> queryAllInfo(TbConUnitQueryCriteria criteria, Pageable pageable) {
        List<ConUnitPO> list=new ArrayList<>();
        if(ObjectUtil.isEmpty(criteria.getParentId())){
            criteria.setParentId(0);
        }
        Page<TbConUnit> page = tbConUnitRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        if(Objects.nonNull(page)&&Objects.nonNull(page.getContent())){
            page.getContent().parallelStream().forEach(it->{
                ConUnitPO po=new ConUnitPO();
                po.setId(it.getId());
                po.setName(it.getName());
                po.setValue(it.getValue());
                po.setIsConsume(it.getIsConsume());
                po.setCreateTime(it.getCreateTime());
                po.setUpdateTime(it.getUpdateTime());

                criteria.setParentId(it.getId());
                criteria.setName(null);
                criteria.setStatus(null);
                Page<TbConUnit>   childPage = tbConUnitRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
                if(Objects.nonNull(childPage)&&Objects.nonNull(childPage.getContent())){
                    List<ConUnitPO> list1=new ArrayList<>();
                    for (TbConUnit tbConUnit : childPage.getContent()) {
                        ConUnitPO child=new ConUnitPO();
                        child.setId(tbConUnit.getId());
                        child.setName(tbConUnit.getName());
                        child.setValue(tbConUnit.getValue());
                        child.setIsConsume(tbConUnit.getIsConsume());
                        child.setCreateTime(tbConUnit.getCreateTime());
                        child.setUpdateTime(tbConUnit.getUpdateTime());
                        list1.add(child);
                    }
                    po.setChildConUnit(list1);
                }
                list.add(po);
            });
        }
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",list);
        map.put("totalElements",page.getTotalElements());
        return map;

    }
}