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

import cn.ysk.cashier.dto.shop.ShopStorageGoodDto;
import cn.ysk.cashier.dto.shop.TbShopStorageGoodDto;
import cn.ysk.cashier.dto.shop.TbShopStorageGoodQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbShopStorageGoodMapper;
import cn.ysk.cashier.pojo.shop.TbShopStorageGood;
import cn.ysk.cashier.repository.shop.TbShopStorageGoodRepository;
import cn.ysk.cashier.service.shop.TbShopStorageGoodService;
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.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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;

/**
 * @author ww
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-05-21
 **/
@Service
@RequiredArgsConstructor
public class TbShopStorageGoodServiceImpl implements TbShopStorageGoodService {

    private final TbShopStorageGoodRepository tbShopStorageGoodRepository;
    private final TbShopStorageGoodMapper tbShopStorageGoodMapper;

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

    @Override
    public List<TbShopStorageGoodDto> queryAll(TbShopStorageGoodQueryCriteria criteria) {
        return tbShopStorageGoodMapper.toDto(tbShopStorageGoodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopStorageGoodDto findById(Integer id) {
        TbShopStorageGood tbShopStorageGood = tbShopStorageGoodRepository.findById(id).orElseGet(TbShopStorageGood::new);
        ValidationUtil.isNull(tbShopStorageGood.getId(), "TbShopStorageGood", "id", id);
        return tbShopStorageGoodMapper.toDto(tbShopStorageGood);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopStorageGoodDto create(TbShopStorageGood resources) {
        resources.setIsDel(0);
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return tbShopStorageGoodMapper.toDto(tbShopStorageGoodRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ShopStorageGoodDto resources) {
        List<TbShopStorageGood> storageGoodList = new ArrayList<>();
        for (ShopStorageGoodDto.DetailInfo good : resources.getGoods()) {
            TbShopStorageGood storageGood = new TbShopStorageGood();
            storageGood.setIsDel(0);
            storageGood.setCreateTime(new Timestamp(System.currentTimeMillis()));
            storageGood.setName(good.getName());
            storageGood.setImgUrl(good.getImgUrl());
            storageGood.setPeriod(resources.getPeriod());
            storageGood.setSource(resources.getSource());
            storageGood.setUnit(resources.getUnit());
            storageGood.setShopId(resources.getShopId());
            storageGoodList.add(storageGood);
        }
        tbShopStorageGoodRepository.saveAll(storageGoodList);
    }

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

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

    @Override
    public void download(List<TbShopStorageGoodDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbShopStorageGoodDto tbShopStorageGood : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("酒品名", tbShopStorageGood.getName());
            map.put("图片地址", tbShopStorageGood.getImgUrl());
            map.put("单位", tbShopStorageGood.getUnit());
            map.put("有效期(天)", tbShopStorageGood.getPeriod());
            map.put("0:未删除；1:已删除", tbShopStorageGood.getIsDel());
            map.put(" createTime", tbShopStorageGood.getCreateTime());
            map.put("0:手动；1:商品；", tbShopStorageGood.getSource());
            map.put("商户Id", tbShopStorageGood.getShopId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}