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

import cn.ysk.cashier.dto.shop.TbShopStorageRecordDto;
import cn.ysk.cashier.dto.shop.TbShopStorageRecordQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbShopStorageRecordMapper;
import cn.ysk.cashier.pojo.shop.TbShopStorageRecord;
import cn.ysk.cashier.repository.shop.TbShopStorageRecordRepository;
import cn.ysk.cashier.service.shop.TbShopStorageRecordService;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
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 ww
 * @date 2024-05-21
 **/
@Service
@RequiredArgsConstructor
public class TbShopStorageRecordServiceImpl implements TbShopStorageRecordService {

    private final TbShopStorageRecordRepository tbShopStorageRecordRepository;
    private final TbShopStorageRecordMapper tbShopStorageRecordMapper;

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

    @Override
    public List<TbShopStorageRecordDto> queryAll(TbShopStorageRecordQueryCriteria criteria){
        return tbShopStorageRecordMapper.toDto(tbShopStorageRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopStorageRecordDto findById(Integer id) {
        TbShopStorageRecord tbShopStorageRecord = tbShopStorageRecordRepository.findById(id).orElseGet(TbShopStorageRecord::new);
        ValidationUtil.isNull(tbShopStorageRecord.getId(),"TbShopStorageRecord","id",id);
        return tbShopStorageRecordMapper.toDto(tbShopStorageRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopStorageRecordDto create(TbShopStorageRecord resources) {
        resources.setTime(new Timestamp(System.currentTimeMillis()));
        return tbShopStorageRecordMapper.toDto(tbShopStorageRecordRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbShopStorageRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbShopStorageRecordDto tbShopStorageRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("存酒信息id", tbShopStorageRecord.getStorageId());
            map.put("记录", tbShopStorageRecord.getContent());
            map.put("操作时间", tbShopStorageRecord.getTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}