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

import cn.ysk.cashier.cons.domain.TbHandover;
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.TbHandoverRepository;
import cn.ysk.cashier.cons.service.TbHandoverService;
import cn.ysk.cashier.cons.service.dto.TbHandoverDto;
import cn.ysk.cashier.cons.service.dto.TbHandoverQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.TbHandoverMapper;
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-25
**/
@Service
@RequiredArgsConstructor
public class TbHandoverServiceImpl implements TbHandoverService {

    private final TbHandoverRepository tbHandoverRepository;
    private final TbHandoverMapper tbHandoverMapper;

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

        Page<TbHandover> page = tbHandoverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tbHandoverMapper::toDto));
    }

    @Override
    public List<TbHandoverDto> queryAll(TbHandoverQueryCriteria criteria){
        return tbHandoverMapper.toDto(tbHandoverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbHandoverDto findById(Integer id) {
        TbHandover tbHandover = tbHandoverRepository.findById(id).orElseGet(TbHandover::new);
        ValidationUtil.isNull(tbHandover.getId(),"TbHandover","id",id);
        return tbHandoverMapper.toDto(tbHandover);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbHandoverDto create(TbHandover resources) {
        return tbHandoverMapper.toDto(tbHandoverRepository.save(resources));
    }

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

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

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