/*
*  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 com.youchain.basicdata.service.impl;

import com.youchain.basicdata.domain.Zcjxb;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.ZcjxbRepository;
import com.youchain.basicdata.service.ZcjxbService;
import com.youchain.basicdata.service.dto.ZcjxbDto;
import com.youchain.basicdata.service.dto.ZcjxbQueryCriteria;
import com.youchain.basicdata.service.mapstruct.ZcjxbMapper;
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 dengjiangkun
* @date 2023-12-15
**/
@Service
@RequiredArgsConstructor
public class ZcjxbServiceImpl implements ZcjxbService {

    private final ZcjxbRepository zcjxbRepository;
    private final ZcjxbMapper zcjxbMapper;
    private final RedisUtils redisUtils;

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

    @Override
    public List<ZcjxbDto> queryAll(ZcjxbQueryCriteria criteria){
        return zcjxbMapper.toDto(zcjxbRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZcjxbDto findById(Long id) {
        Zcjxb zcjxb = zcjxbRepository.findById(id).orElseGet(Zcjxb::new);
        ValidationUtil.isNull(zcjxb.getId(),"Zcjxb","id",id);
        return zcjxbMapper.toDto(zcjxb);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZcjxbDto create(Zcjxb resources) {
        return zcjxbMapper.toDto(zcjxbRepository.save(resources));
    }

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

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            zcjxbRepository.deleteById(id);
        }
        redisUtils.del("ZCJXBData");
    }

    @Override
    public void download(List<ZcjxbDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZcjxbDto zcjxb : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("机械臂机台号", zcjxb.getCode());
            map.put("ip地址", zcjxb.getIp());
            map.put("端口号", zcjxb.getPort());
            map.put("通信类型", zcjxb.getCommunicationType());
            map.put("DB地址", zcjxb.getAddress());
            map.put("数据长度", zcjxb.getDataLength());
            map.put("偏移量-A返回空车", zcjxb.getOffsetA1());
            map.put("偏移量-B返回空车", zcjxb.getOffsetB1());
            map.put("偏移量-A叫料", zcjxb.getOffsetA2());
            map.put("偏移量-B叫料", zcjxb.getOffsetB2());
            map.put("偏移量-A车有料", zcjxb.getOffsetA3());
            map.put("偏移量-B车有料", zcjxb.getOffsetB3());
            map.put("A车的地标点", zcjxb.getPointA1());
            map.put("B车的地标点", zcjxb.getPointB1());
            map.put("A车的物料", zcjxb.getItemA1());
            map.put("B车的物料", zcjxb.getItemB1());
            map.put("是否启用", zcjxb.getEnabled());
            map.put("创建人", zcjxb.getCreateBy());
            map.put("更新人", zcjxb.getUpdateBy());
            map.put("创建时间", zcjxb.getCreateTime());
            map.put("更新时间", zcjxb.getUpdateTime());
            map.put("Modbus-tcp的任务是否结束", zcjxb.getModbusTcpTaskFinish());
            map.put("Modbus-tcp对应的地标点", zcjxb.getModbusTcpPoint());
            map.put("Modbus-tcp对应的物料", zcjxb.getModbusTcpItem());
            map.put("a车点位是否有任务", zcjxb.getTaskOffOnA());
            map.put("b车点位是否有任务", zcjxb.getTaskOffOnB());
            map.put("从机ID", zcjxb.getModbusTcpSlaveid());
            map.put("任务名称", zcjxb.getTaskName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}