/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package cn.ysk.cashier.service.impl.shopimpl;

import cn.ysk.cashier.pojo.shop.TbShopPurveyor;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbShopPurveyorRepository;
import cn.ysk.cashier.service.shop.TbShopPurveyorService;
import cn.ysk.cashier.dto.shop.TbShopPurveyorDto;
import cn.ysk.cashier.dto.shop.TbShopPurveyorQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbShopPurveyorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;

import java.time.Instant;
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 lyf
* @date 2024-01-22
**/
@Service
@RequiredArgsConstructor
public class TbShopPurveyorServiceImpl implements TbShopPurveyorService {

    private final TbShopPurveyorRepository tbShopPurveyorRepository;
    private final TbShopPurveyorMapper tbShopPurveyorMapper;

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

    @Override
    public List<TbShopPurveyorDto> queryAll(TbShopPurveyorQueryCriteria criteria){
        return tbShopPurveyorMapper.toDto(tbShopPurveyorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopPurveyorDto findById(Integer id) {
        TbShopPurveyor tbShopPurveyor = tbShopPurveyorRepository.findById(id).orElseGet(TbShopPurveyor::new);
        ValidationUtil.isNull(tbShopPurveyor.getId(),"TbShopPurveyor","id",id);
        return tbShopPurveyorMapper.toDto(tbShopPurveyor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopPurveyorDto create(TbShopPurveyor resources) {
        resources.setCreatedAt(Instant.now().toEpochMilli());
        resources.setUpdatedAt(Instant.now().toEpochMilli());
        return tbShopPurveyorMapper.toDto(tbShopPurveyorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbShopPurveyor resources) {
        TbShopPurveyor tbShopPurveyor = tbShopPurveyorRepository.findById(resources.getId()).orElseGet(TbShopPurveyor::new);
        tbShopPurveyor.setUpdatedAt(Instant.now().toEpochMilli());
        tbShopPurveyor.setSort(0);
        ValidationUtil.isNull( tbShopPurveyor.getId(),"TbShopPurveyor","id",resources.getId());
        tbShopPurveyor.copy(resources);
        tbShopPurveyorRepository.save(tbShopPurveyor);
    }

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

    @Override
    public void download(List<TbShopPurveyorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbShopPurveyorDto tbShopPurveyor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("店铺Id", tbShopPurveyor.getShopId());
            map.put("排序", tbShopPurveyor.getSort());
            map.put("名称", tbShopPurveyor.getName());
            map.put("联系人名字", tbShopPurveyor.getPurveyorName());
            map.put("联系人电话", tbShopPurveyor.getPurveyorTelephone());
            map.put("结算周期（日）", tbShopPurveyor.getPeriod());
            map.put("供应商地址", tbShopPurveyor.getAddress());
            map.put("标签", tbShopPurveyor.getTip());
            map.put("备注", tbShopPurveyor.getRemark());
            map.put(" createdAt",  tbShopPurveyor.getCreatedAt());
            map.put(" updatedAt",  tbShopPurveyor.getUpdatedAt());
            map.put("最后一次交易时间(主要做排序)", tbShopPurveyor.getLastTransactAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}