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

import cn.ysk.cashier.cons.domain.TbConsInfoFlow;
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.TbConsInfoFlowRepository;
import cn.ysk.cashier.cons.service.TbConsInfoFlowService;
import cn.ysk.cashier.cons.service.dto.TbConsInfoFlowDto;
import cn.ysk.cashier.cons.service.dto.TbConsInfoFlowQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.TbConsInfoFlowMapper;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
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.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author admin
* @date 2024-06-22
**/
@Service
@RequiredArgsConstructor
public class TbConsInfoFlowServiceImpl implements TbConsInfoFlowService {

    private final TbConsInfoFlowRepository tbConsInfoFlowRepository;
    private final TbConsInfoFlowMapper tbConsInfoFlowMapper;

    @Override
    public Map<String,Object> queryAll(TbConsInfoFlowQueryCriteria criteria, Pageable pageable){

        criteria.setAmount(BigDecimal.ZERO);
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<TbConsInfoFlow> page = tbConsInfoFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);

       page.get().forEach(it->{
          String name=  tbConsInfoFlowRepository.selectByPskId(it.getProSkuId());
          if(Objects.nonNull(name)){
              it.setProductName(name);
          }
       });

        return PageUtil.toPage(page.map(tbConsInfoFlowMapper::toDto));
    }

    @Override
    public List<TbConsInfoFlowDto> queryAll(TbConsInfoFlowQueryCriteria criteria){
        return tbConsInfoFlowMapper.toDto(tbConsInfoFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbConsInfoFlowDto findById(Integer id) {
        TbConsInfoFlow tbConsInfoFlow = tbConsInfoFlowRepository.findById(id).orElseGet(TbConsInfoFlow::new);
        ValidationUtil.isNull(tbConsInfoFlow.getId(),"TbConsInfoFlow","id",id);
        return tbConsInfoFlowMapper.toDto(tbConsInfoFlow);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbConsInfoFlowDto create(TbConsInfoFlow resources) {
        return tbConsInfoFlowMapper.toDto(tbConsInfoFlowRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbConsInfoFlowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbConsInfoFlowDto tbConsInfoFlow : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("耗材id", tbConsInfoFlow.getConsId());
            map.put("耗材名称", tbConsInfoFlow.getConName());
            map.put("变化值", tbConsInfoFlow.getAmount());
            map.put("变化后的值", tbConsInfoFlow.getBalance());
            map.put("操作代码", tbConsInfoFlow.getBizCode());
            map.put("操作说明", tbConsInfoFlow.getBizName());
            map.put("加减标识+/-", tbConsInfoFlow.getBizType());
            map.put(" createTime",  tbConsInfoFlow.getCreateTime());
            map.put(" updateTime",  tbConsInfoFlow.getUpdateTime());
            map.put("店铺id", tbConsInfoFlow.getShopId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}