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

import cn.ysk.cashier.cons.domain.ViewHandover;
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.ViewHandoverRepository;
import cn.ysk.cashier.cons.service.ViewHandoverService;
import cn.ysk.cashier.cons.service.dto.ViewHandoverDto;
import cn.ysk.cashier.cons.service.dto.ViewHandoverQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.ViewHandoverMapper;
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-08-05
**/
@Service
@RequiredArgsConstructor
public class ViewHandoverServiceImpl implements ViewHandoverService {

    private final ViewHandoverRepository viewHandoverRepository;
    private final ViewHandoverMapper viewHandoverMapper;

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

    @Override
    public List<ViewHandoverDto> queryAll(ViewHandoverQueryCriteria criteria){
        return viewHandoverMapper.toDto(viewHandoverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ViewHandoverDto findById(Integer id) {
        ViewHandover viewHandover = viewHandoverRepository.findById(id).orElseGet(ViewHandover::new);
        ValidationUtil.isNull(viewHandover.getId(),"ViewHandover","id",id);
        return viewHandoverMapper.toDto(viewHandover);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ViewHandoverDto create(ViewHandover resources) {
        return viewHandoverMapper.toDto(viewHandoverRepository.save(resources));
    }

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

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

    @Override
    public void download(List<ViewHandoverDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ViewHandoverDto viewHandover : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("交班日期", viewHandover.getTradeDay());
            map.put("打印机编号", viewHandover.getPrintNo());
            map.put(" dutyId",  viewHandover.getDutyId());
            map.put(" shopId",  viewHandover.getShopId());
            map.put(" merchantName",  viewHandover.getMerchantName());
            map.put(" startTime",  viewHandover.getStartTime());
            map.put(" endTime",  viewHandover.getEndTime());
            map.put(" staffId",  viewHandover.getStaffId());
            map.put(" staffName",  viewHandover.getStaffName());
            map.put(" payInfos",  viewHandover.getPayInfos());
            map.put(" memberData",  viewHandover.getMemberData());
            map.put(" productCategories",  viewHandover.getProductCategories());
            map.put(" totalAmount",  viewHandover.getTotalAmount());
            map.put(" imprest",  viewHandover.getImprest());
            map.put(" payable",  viewHandover.getPayable());
            map.put(" handIn",  viewHandover.getHandIn());
            map.put(" returnAmount",  viewHandover.getReturnAmount());
            map.put(" orderNum",  viewHandover.getOrderNum());
            map.put(" quickAmount",  viewHandover.getQuickAmount());
            map.put(" productInfoPos",  viewHandover.getProductInfoPos());
            map.put(" productInfos",  viewHandover.getProductInfos());
            map.put(" createTime",  viewHandover.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}