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

import cn.ysk.cashier.cons.domain.ViewConsSuppFlow;
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.ViewConsSuppFlowRepository;
import cn.ysk.cashier.cons.service.ViewConsSuppFlowService;
import cn.ysk.cashier.cons.service.dto.ViewConsSuppFlowDto;
import cn.ysk.cashier.cons.service.dto.ViewConsSuppFlowQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.ViewConsSuppFlowMapper;
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-03
**/
@Service
@RequiredArgsConstructor
public class ViewConsSuppFlowServiceImpl implements ViewConsSuppFlowService {

    private final ViewConsSuppFlowRepository viewConsSuppFlowRepository;
    private final ViewConsSuppFlowMapper viewConsSuppFlowMapper;

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

    @Override
    public List<ViewConsSuppFlowDto> queryAll(ViewConsSuppFlowQueryCriteria criteria){
        return viewConsSuppFlowMapper.toDto(viewConsSuppFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ViewConsSuppFlowDto findById(Integer id) {
        ViewConsSuppFlow viewConsSuppFlow = viewConsSuppFlowRepository.findById(id).orElseGet(ViewConsSuppFlow::new);
        ValidationUtil.isNull(viewConsSuppFlow.getId(),"ViewConsSuppFlow","id",id);
        return viewConsSuppFlowMapper.toDto(viewConsSuppFlow);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ViewConsSuppFlowDto create(ViewConsSuppFlow resources) {
        return viewConsSuppFlowMapper.toDto(viewConsSuppFlowRepository.save(resources));
    }

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

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

    @Override
    public void download(List<ViewConsSuppFlowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ViewConsSuppFlowDto viewConsSuppFlow : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" conInfoId",  viewConsSuppFlow.getConInfoId());
            map.put("店铺id", viewConsSuppFlow.getShopId());
            map.put(" skuId",  viewConsSuppFlow.getSkuId());
            map.put(" productId",  viewConsSuppFlow.getProductId());
            map.put(" supplierId",  viewConsSuppFlow.getSupplierId());
            map.put("出入库类型: in 入库 out 出库", viewConsSuppFlow.getType());
            map.put("数量", viewConsSuppFlow.getStockNumber());
            map.put("进价", viewConsSuppFlow.getPrice());
            map.put("小计", viewConsSuppFlow.getAmount());
            map.put("剩余库存", viewConsSuppFlow.getBalance());
            map.put("应付款", viewConsSuppFlow.getAccountsPayable());
            map.put("实付款", viewConsSuppFlow.getActualPayment());
            map.put(" paymentTime",  viewConsSuppFlow.getPaymentTime());
            map.put(" createTime",  viewConsSuppFlow.getCreateTime());
            map.put(" updateTime",  viewConsSuppFlow.getUpdateTime());
            map.put("耗材名称", viewConsSuppFlow.getConName());
            map.put("耗材代码", viewConsSuppFlow.getConCode());
            map.put("耗材类型名称", viewConsSuppFlow.getConTypeName());
            map.put("联系人名字", viewConsSuppFlow.getPurveyorName());
            map.put("供应商地址", viewConsSuppFlow.getAddress());
            map.put("联系人电话", viewConsSuppFlow.getPurveyorTelephone());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}